blob: 31c5ea2da9120ba92221581789a74cbba6a8ac35 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar071d4272004-06-13 20:20:40 +000019#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
Bram Moolenaar314f11d2010-08-09 22:07:08 +020023#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#ifdef MACOS
28# include <time.h> /* for time_t */
29#endif
30
Bram Moolenaar33570922005-01-25 22:26:29 +000031#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000032
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000033#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
34 be freed. */
35
Bram Moolenaar071d4272004-06-13 20:20:40 +000036/*
Bram Moolenaar33570922005-01-25 22:26:29 +000037 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
38 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000039 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
40 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
41 * HI2DI() converts a hashitem pointer to a dictitem pointer.
42 */
Bram Moolenaar33570922005-01-25 22:26:29 +000043static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000044#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000045#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000046#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000047
48/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000049 * Structure returned by get_lval() and used by set_var_lval().
50 * For a plain name:
51 * "name" points to the variable name.
52 * "exp_name" is NULL.
53 * "tv" is NULL
54 * For a magic braces name:
55 * "name" points to the expanded variable name.
56 * "exp_name" is non-NULL, to be freed later.
57 * "tv" is NULL
58 * For an index in a list:
59 * "name" points to the (expanded) variable name.
60 * "exp_name" NULL or non-NULL, to be freed later.
61 * "tv" points to the (first) list item value
62 * "li" points to the (first) list item
63 * "range", "n1", "n2" and "empty2" indicate what items are used.
64 * For an existing Dict item:
65 * "name" points to the (expanded) variable name.
66 * "exp_name" NULL or non-NULL, to be freed later.
67 * "tv" points to the dict item value
68 * "newkey" is NULL
69 * For a non-existing Dict item:
70 * "name" points to the (expanded) variable name.
71 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000072 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000073 * "newkey" is the key for the new item.
74 */
75typedef struct lval_S
76{
77 char_u *ll_name; /* start of variable name (can be NULL) */
78 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000079 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000080 isn't NULL it's the Dict to which to add
81 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 listitem_T *ll_li; /* The list item or NULL. */
83 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000084 int ll_range; /* TRUE when a [i:j] range was used */
85 long ll_n1; /* First index for list */
86 long ll_n2; /* Second index for list range */
87 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000088 dict_T *ll_dict; /* The Dictionary or NULL */
89 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000090 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000091} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000099static char *e_listreq = N_("E714: List required");
100static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000101static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000102static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
103static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
104static char *e_funcdict = N_("E717: Dictionary entry already exists");
105static char *e_funcref = N_("E718: Funcref required");
106static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
107static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000108static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000109static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200110#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200111static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200112#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000113
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +0100114#define NAMESPACE_CHAR (char_u *)"abglstvw"
115
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200116static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000117#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000120 * Old Vim variables such as "v:version" are also available without the "v:".
121 * Also in functions. We need a special hashtable for them.
122 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000123static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124
125/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000126 * When recursively copying lists and dicts we need to remember which ones we
127 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000128 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 */
130static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131#define COPYID_INC 2
132#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000133
Bram Moolenaar8502c702014-06-17 12:51:16 +0200134/* Abort conversion to string after a recursion error. */
135static int did_echo_string_emsg = FALSE;
136
Bram Moolenaard9fba312005-06-26 22:34:35 +0000137/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000138 * Array to hold the hashtab with variables local to each sourced script.
139 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000141typedef struct
142{
143 dictitem_T sv_var;
144 dict_T sv_dict;
145} scriptvar_T;
146
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200147static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
148#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
149#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150
151static int echo_attr = 0; /* attributes used for ":echo" */
152
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000153/* Values for trans_function_name() argument: */
154#define TFN_INT 1 /* internal function name OK */
155#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100156#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
157
158/* Values for get_lval() flags argument: */
159#define GLV_QUIET TFN_QUIET /* no error messages */
160#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000161
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162/*
163 * Structure to hold info for a user function.
164 */
165typedef struct ufunc ufunc_T;
166
167struct ufunc
168{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000169 int uf_varargs; /* variable nr of arguments */
170 int uf_flags;
171 int uf_calls; /* nr of active calls */
172 garray_T uf_args; /* arguments */
173 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000174#ifdef FEAT_PROFILE
175 int uf_profiling; /* TRUE when func is being profiled */
176 /* profiling the function as a whole */
177 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000178 proftime_T uf_tm_total; /* time spent in function + children */
179 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 proftime_T uf_tm_children; /* time spent in children this call */
181 /* profiling the function per line */
182 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000183 proftime_T *uf_tml_total; /* time spent in a line + children */
184 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000185 proftime_T uf_tml_start; /* start time for current line */
186 proftime_T uf_tml_children; /* time spent in children for this line */
187 proftime_T uf_tml_wait; /* start wait time for current line */
188 int uf_tml_idx; /* index of line being timed; -1 if none */
189 int uf_tml_execed; /* line being timed was executed */
190#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000191 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 int uf_refcount; /* for numbered function: reference count */
194 char_u uf_name[1]; /* name of function (actually longer); can
195 start with <SNR>123_ (<SNR> is K_SPECIAL
196 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197};
198
199/* function flags */
200#define FC_ABORT 1 /* abort function on error */
201#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000202#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
204/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000205 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000207static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000209/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000210static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
211
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000212/* list heads for garbage collection */
213static dict_T *first_dict = NULL; /* list of all dicts */
214static list_T *first_list = NULL; /* list of all lists */
215
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000216/* From user function to hashitem and back. */
217static ufunc_T dumuf;
218#define UF2HIKEY(fp) ((fp)->uf_name)
219#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
220#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
221
222#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
223#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224
Bram Moolenaar33570922005-01-25 22:26:29 +0000225#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
226#define VAR_SHORT_LEN 20 /* short variable name length */
227#define FIXVAR_CNT 12 /* number of fixed variables */
228
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000230typedef struct funccall_S funccall_T;
231
232struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233{
234 ufunc_T *func; /* function being called */
235 int linenr; /* next line to be executed */
236 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000237 struct /* fixed variables for arguments */
238 {
239 dictitem_T var; /* variable (without room for name) */
240 char_u room[VAR_SHORT_LEN]; /* room for the name */
241 } fixvar[FIXVAR_CNT];
242 dict_T l_vars; /* l: local function variables */
243 dictitem_T l_vars_var; /* variable for l: scope */
244 dict_T l_avars; /* a: argument variables */
245 dictitem_T l_avars_var; /* variable for a: scope */
246 list_T l_varlist; /* list for a:000 */
247 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
248 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 linenr_T breakpoint; /* next line with breakpoint or zero */
250 int dbg_tick; /* debug_tick when breakpoint was set */
251 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000252#ifdef FEAT_PROFILE
253 proftime_T prof_child; /* time spent in a child */
254#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000255 funccall_T *caller; /* calling function or NULL */
256};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257
258/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000259 * Info used by a ":for" loop.
260 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000261typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262{
263 int fi_semicolon; /* TRUE if ending in '; var]' */
264 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 listwatch_T fi_lw; /* keep an eye on the item used. */
266 list_T *fi_list; /* list being used */
267} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000268
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000269/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000270 * Struct used by trans_function_name()
271 */
272typedef struct
273{
Bram Moolenaar33570922005-01-25 22:26:29 +0000274 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000275 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dictitem_T *fd_di; /* Dictionary item used */
277} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000278
Bram Moolenaara7043832005-01-21 11:56:39 +0000279
280/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000281 * Array to hold the value of v: variables.
282 * The value is in a dictitem, so that it can also be used in the v: scope.
283 * The reason to use this table anyway is for very quick access to the
284 * variables with the VV_ defines.
285 */
286#include "version.h"
287
288/* values for vv_flags: */
289#define VV_COMPAT 1 /* compatible, also used without "v:" */
290#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000291#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000292
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000293#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
295static struct vimvar
296{
297 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000298 dictitem_T vv_di; /* value and name for key */
299 char vv_filler[16]; /* space for LONGEST name below!!! */
300 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
301} vimvars[VV_LEN] =
302{
303 /*
304 * The order here must match the VV_ defines in vim.h!
305 * Initializing a union does not work, leave tv.vval empty to get zero's.
306 */
307 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
308 {VV_NAME("count1", VAR_NUMBER), VV_RO},
309 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
310 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
311 {VV_NAME("warningmsg", VAR_STRING), 0},
312 {VV_NAME("statusmsg", VAR_STRING), 0},
313 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
314 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
315 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
316 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
317 {VV_NAME("termresponse", VAR_STRING), VV_RO},
318 {VV_NAME("fname", VAR_STRING), VV_RO},
319 {VV_NAME("lang", VAR_STRING), VV_RO},
320 {VV_NAME("lc_time", VAR_STRING), VV_RO},
321 {VV_NAME("ctype", VAR_STRING), VV_RO},
322 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
323 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
324 {VV_NAME("fname_in", VAR_STRING), VV_RO},
325 {VV_NAME("fname_out", VAR_STRING), VV_RO},
326 {VV_NAME("fname_new", VAR_STRING), VV_RO},
327 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
328 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
329 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
330 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
331 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
332 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("progname", VAR_STRING), VV_RO},
334 {VV_NAME("servername", VAR_STRING), VV_RO},
335 {VV_NAME("dying", VAR_NUMBER), VV_RO},
336 {VV_NAME("exception", VAR_STRING), VV_RO},
337 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
338 {VV_NAME("register", VAR_STRING), VV_RO},
339 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
340 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000341 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
342 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000343 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000344 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
345 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000346 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
347 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000351 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000352 {VV_NAME("swapname", VAR_STRING), VV_RO},
353 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000354 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200355 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000356 {VV_NAME("mouse_win", VAR_NUMBER), 0},
357 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
358 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000359 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000360 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100361 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000362 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200363 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200364 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200365 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200366 {VV_NAME("option_new", VAR_STRING), VV_RO},
367 {VV_NAME("option_old", VAR_STRING), VV_RO},
368 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100369 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100370 {VV_NAME("false", VAR_SPECIAL), VV_RO},
371 {VV_NAME("true", VAR_SPECIAL), VV_RO},
372 {VV_NAME("null", VAR_SPECIAL), VV_RO},
373 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000374};
375
376/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000377#define vv_type vv_di.di_tv.v_type
378#define vv_nr vv_di.di_tv.vval.v_number
379#define vv_float vv_di.di_tv.vval.v_float
380#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000381#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200382#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000383#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000384
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200385static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000386#define vimvarht vimvardict.dv_hashtab
387
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100388static void prepare_vimvar(int idx, typval_T *save_tv);
389static void restore_vimvar(int idx, typval_T *save_tv);
390static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
391static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
392static char_u *skip_var_one(char_u *arg);
393static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
394static void list_glob_vars(int *first);
395static void list_buf_vars(int *first);
396static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000397#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100398static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000399#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100400static void list_vim_vars(int *first);
401static void list_script_vars(int *first);
402static void list_func_vars(int *first);
403static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
404static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
405static int check_changedtick(char_u *arg);
406static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
407static void clear_lval(lval_T *lp);
408static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
409static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
410static void list_fix_watch(list_T *l, listitem_T *item);
411static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
412static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
413static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
414static void item_lock(typval_T *tv, int deep, int lock);
415static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000416
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100417static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
418static int eval1(char_u **arg, typval_T *rettv, int evaluate);
419static int eval2(char_u **arg, typval_T *rettv, int evaluate);
420static int eval3(char_u **arg, typval_T *rettv, int evaluate);
421static int eval4(char_u **arg, typval_T *rettv, int evaluate);
422static int eval5(char_u **arg, typval_T *rettv, int evaluate);
423static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
424static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000425
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100426static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
427static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
428static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
429static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
430static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
431static long list_len(list_T *l);
432static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
433static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
434static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
435static long list_find_nr(list_T *l, long idx, int *errorp);
436static long list_idx_of_item(list_T *l, listitem_T *item);
437static int list_append_number(list_T *l, varnumber_T n);
438static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
439static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
440static list_T *list_copy(list_T *orig, int deep, int copyID);
441static char_u *list2string(typval_T *tv, int copyID);
442static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap);
443static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo, int copyID);
444static int free_unref_items(int copyID);
445static dictitem_T *dictitem_copy(dictitem_T *org);
446static void dictitem_remove(dict_T *dict, dictitem_T *item);
447static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
448static long dict_len(dict_T *d);
449static char_u *dict2string(typval_T *tv, int copyID);
450static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
451static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
452static char_u *tv2string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
453static char_u *string_quote(char_u *str, int function);
454static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
455static int find_internal_func(char_u *name);
456static char_u *deref_func_name(char_u *name, int *lenp, int no_autoload);
457static int get_func_tv(char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100458static void emsg_funcname(char *ermsg, char_u *name);
459static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000460
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000461#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100462static void f_abs(typval_T *argvars, typval_T *rettv);
463static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000464#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100465static void f_add(typval_T *argvars, typval_T *rettv);
466static void f_alloc_fail(typval_T *argvars, typval_T *rettv);
467static void f_and(typval_T *argvars, typval_T *rettv);
468static void f_append(typval_T *argvars, typval_T *rettv);
469static void f_argc(typval_T *argvars, typval_T *rettv);
470static void f_argidx(typval_T *argvars, typval_T *rettv);
471static void f_arglistid(typval_T *argvars, typval_T *rettv);
472static void f_argv(typval_T *argvars, typval_T *rettv);
473static void f_assert_equal(typval_T *argvars, typval_T *rettv);
474static void f_assert_exception(typval_T *argvars, typval_T *rettv);
475static void f_assert_fails(typval_T *argvars, typval_T *rettv);
476static void f_assert_false(typval_T *argvars, typval_T *rettv);
477static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000478#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100479static void f_asin(typval_T *argvars, typval_T *rettv);
480static void f_atan(typval_T *argvars, typval_T *rettv);
481static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000482#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100483static void f_browse(typval_T *argvars, typval_T *rettv);
484static void f_browsedir(typval_T *argvars, typval_T *rettv);
485static void f_bufexists(typval_T *argvars, typval_T *rettv);
486static void f_buflisted(typval_T *argvars, typval_T *rettv);
487static void f_bufloaded(typval_T *argvars, typval_T *rettv);
488static void f_bufname(typval_T *argvars, typval_T *rettv);
489static void f_bufnr(typval_T *argvars, typval_T *rettv);
490static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
491static void f_byte2line(typval_T *argvars, typval_T *rettv);
492static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
493static void f_byteidx(typval_T *argvars, typval_T *rettv);
494static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
495static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000496#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100497static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000498#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100499#ifdef FEAT_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100500static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100501static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
502static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100503static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100504# ifdef FEAT_JOB
505static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
506# endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100507static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100508static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
509static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100510static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100511static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100512static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
513static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100514static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100515static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100516#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100517static void f_changenr(typval_T *argvars, typval_T *rettv);
518static void f_char2nr(typval_T *argvars, typval_T *rettv);
519static void f_cindent(typval_T *argvars, typval_T *rettv);
520static void f_clearmatches(typval_T *argvars, typval_T *rettv);
521static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000522#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100523static void f_complete(typval_T *argvars, typval_T *rettv);
524static void f_complete_add(typval_T *argvars, typval_T *rettv);
525static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000526#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100527static void f_confirm(typval_T *argvars, typval_T *rettv);
528static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000529#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100530static void f_cos(typval_T *argvars, typval_T *rettv);
531static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000532#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100533static void f_count(typval_T *argvars, typval_T *rettv);
534static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
535static void f_cursor(typval_T *argsvars, typval_T *rettv);
536static void f_deepcopy(typval_T *argvars, typval_T *rettv);
537static void f_delete(typval_T *argvars, typval_T *rettv);
538static void f_did_filetype(typval_T *argvars, typval_T *rettv);
539static void f_diff_filler(typval_T *argvars, typval_T *rettv);
540static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +0100541static void f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100542static void f_empty(typval_T *argvars, typval_T *rettv);
543static void f_escape(typval_T *argvars, typval_T *rettv);
544static void f_eval(typval_T *argvars, typval_T *rettv);
545static void f_eventhandler(typval_T *argvars, typval_T *rettv);
546static void f_executable(typval_T *argvars, typval_T *rettv);
547static void f_exepath(typval_T *argvars, typval_T *rettv);
548static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200549#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100550static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200551#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100552static void f_expand(typval_T *argvars, typval_T *rettv);
553static void f_extend(typval_T *argvars, typval_T *rettv);
554static void f_feedkeys(typval_T *argvars, typval_T *rettv);
555static void f_filereadable(typval_T *argvars, typval_T *rettv);
556static void f_filewritable(typval_T *argvars, typval_T *rettv);
557static void f_filter(typval_T *argvars, typval_T *rettv);
558static void f_finddir(typval_T *argvars, typval_T *rettv);
559static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000560#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100561static void f_float2nr(typval_T *argvars, typval_T *rettv);
562static void f_floor(typval_T *argvars, typval_T *rettv);
563static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000564#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100565static void f_fnameescape(typval_T *argvars, typval_T *rettv);
566static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
567static void f_foldclosed(typval_T *argvars, typval_T *rettv);
568static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
569static void f_foldlevel(typval_T *argvars, typval_T *rettv);
570static void f_foldtext(typval_T *argvars, typval_T *rettv);
571static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
572static void f_foreground(typval_T *argvars, typval_T *rettv);
573static void f_function(typval_T *argvars, typval_T *rettv);
574static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
575static void f_get(typval_T *argvars, typval_T *rettv);
576static void f_getbufline(typval_T *argvars, typval_T *rettv);
577static void f_getbufvar(typval_T *argvars, typval_T *rettv);
578static void f_getchar(typval_T *argvars, typval_T *rettv);
579static void f_getcharmod(typval_T *argvars, typval_T *rettv);
580static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
581static void f_getcmdline(typval_T *argvars, typval_T *rettv);
582static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
583static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
584static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
585static void f_getcwd(typval_T *argvars, typval_T *rettv);
586static void f_getfontname(typval_T *argvars, typval_T *rettv);
587static void f_getfperm(typval_T *argvars, typval_T *rettv);
588static void f_getfsize(typval_T *argvars, typval_T *rettv);
589static void f_getftime(typval_T *argvars, typval_T *rettv);
590static void f_getftype(typval_T *argvars, typval_T *rettv);
591static void f_getline(typval_T *argvars, typval_T *rettv);
592static void f_getmatches(typval_T *argvars, typval_T *rettv);
593static void f_getpid(typval_T *argvars, typval_T *rettv);
594static void f_getcurpos(typval_T *argvars, typval_T *rettv);
595static void f_getpos(typval_T *argvars, typval_T *rettv);
596static void f_getqflist(typval_T *argvars, typval_T *rettv);
597static void f_getreg(typval_T *argvars, typval_T *rettv);
598static void f_getregtype(typval_T *argvars, typval_T *rettv);
599static void f_gettabvar(typval_T *argvars, typval_T *rettv);
600static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
601static void f_getwinposx(typval_T *argvars, typval_T *rettv);
602static void f_getwinposy(typval_T *argvars, typval_T *rettv);
603static void f_getwinvar(typval_T *argvars, typval_T *rettv);
604static void f_glob(typval_T *argvars, typval_T *rettv);
605static void f_globpath(typval_T *argvars, typval_T *rettv);
606static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
607static void f_has(typval_T *argvars, typval_T *rettv);
608static void f_has_key(typval_T *argvars, typval_T *rettv);
609static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
610static void f_hasmapto(typval_T *argvars, typval_T *rettv);
611static void f_histadd(typval_T *argvars, typval_T *rettv);
612static void f_histdel(typval_T *argvars, typval_T *rettv);
613static void f_histget(typval_T *argvars, typval_T *rettv);
614static void f_histnr(typval_T *argvars, typval_T *rettv);
615static void f_hlID(typval_T *argvars, typval_T *rettv);
616static void f_hlexists(typval_T *argvars, typval_T *rettv);
617static void f_hostname(typval_T *argvars, typval_T *rettv);
618static void f_iconv(typval_T *argvars, typval_T *rettv);
619static void f_indent(typval_T *argvars, typval_T *rettv);
620static void f_index(typval_T *argvars, typval_T *rettv);
621static void f_input(typval_T *argvars, typval_T *rettv);
622static void f_inputdialog(typval_T *argvars, typval_T *rettv);
623static void f_inputlist(typval_T *argvars, typval_T *rettv);
624static void f_inputrestore(typval_T *argvars, typval_T *rettv);
625static void f_inputsave(typval_T *argvars, typval_T *rettv);
626static void f_inputsecret(typval_T *argvars, typval_T *rettv);
627static void f_insert(typval_T *argvars, typval_T *rettv);
628static void f_invert(typval_T *argvars, typval_T *rettv);
629static void f_isdirectory(typval_T *argvars, typval_T *rettv);
630static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100631#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
632static void f_isnan(typval_T *argvars, typval_T *rettv);
633#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100634static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100635#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +0100636# ifdef FEAT_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100637static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfa4bce72016-02-13 23:50:08 +0100638# endif
Bram Moolenaar65edff82016-02-21 16:40:11 +0100639static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100640static void f_job_start(typval_T *argvars, typval_T *rettv);
641static void f_job_stop(typval_T *argvars, typval_T *rettv);
642static void f_job_status(typval_T *argvars, typval_T *rettv);
643#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100644static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100645static void f_js_decode(typval_T *argvars, typval_T *rettv);
646static void f_js_encode(typval_T *argvars, typval_T *rettv);
647static void f_json_decode(typval_T *argvars, typval_T *rettv);
648static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100649static void f_keys(typval_T *argvars, typval_T *rettv);
650static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
651static void f_len(typval_T *argvars, typval_T *rettv);
652static void f_libcall(typval_T *argvars, typval_T *rettv);
653static void f_libcallnr(typval_T *argvars, typval_T *rettv);
654static void f_line(typval_T *argvars, typval_T *rettv);
655static void f_line2byte(typval_T *argvars, typval_T *rettv);
656static void f_lispindent(typval_T *argvars, typval_T *rettv);
657static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000658#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100659static void f_log(typval_T *argvars, typval_T *rettv);
660static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000661#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200662#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100663static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200664#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100665static void f_map(typval_T *argvars, typval_T *rettv);
666static void f_maparg(typval_T *argvars, typval_T *rettv);
667static void f_mapcheck(typval_T *argvars, typval_T *rettv);
668static void f_match(typval_T *argvars, typval_T *rettv);
669static void f_matchadd(typval_T *argvars, typval_T *rettv);
670static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
671static void f_matcharg(typval_T *argvars, typval_T *rettv);
672static void f_matchdelete(typval_T *argvars, typval_T *rettv);
673static void f_matchend(typval_T *argvars, typval_T *rettv);
674static void f_matchlist(typval_T *argvars, typval_T *rettv);
675static void f_matchstr(typval_T *argvars, typval_T *rettv);
676static void f_max(typval_T *argvars, typval_T *rettv);
677static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000678#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100679static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000680#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100681static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100682#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100683static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100684#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100685static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
686static void f_nr2char(typval_T *argvars, typval_T *rettv);
687static void f_or(typval_T *argvars, typval_T *rettv);
688static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100689#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100690static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100691#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000692#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100693static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000694#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100695static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
696static void f_printf(typval_T *argvars, typval_T *rettv);
697static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200698#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100699static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200700#endif
701#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100702static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200703#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100704static void f_range(typval_T *argvars, typval_T *rettv);
705static void f_readfile(typval_T *argvars, typval_T *rettv);
706static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100707#ifdef FEAT_FLOAT
708static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
709#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100710static void f_reltimestr(typval_T *argvars, typval_T *rettv);
711static void f_remote_expr(typval_T *argvars, typval_T *rettv);
712static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
713static void f_remote_peek(typval_T *argvars, typval_T *rettv);
714static void f_remote_read(typval_T *argvars, typval_T *rettv);
715static void f_remote_send(typval_T *argvars, typval_T *rettv);
716static void f_remove(typval_T *argvars, typval_T *rettv);
717static void f_rename(typval_T *argvars, typval_T *rettv);
718static void f_repeat(typval_T *argvars, typval_T *rettv);
719static void f_resolve(typval_T *argvars, typval_T *rettv);
720static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000721#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100722static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000723#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100724static void f_screenattr(typval_T *argvars, typval_T *rettv);
725static void f_screenchar(typval_T *argvars, typval_T *rettv);
726static void f_screencol(typval_T *argvars, typval_T *rettv);
727static void f_screenrow(typval_T *argvars, typval_T *rettv);
728static void f_search(typval_T *argvars, typval_T *rettv);
729static void f_searchdecl(typval_T *argvars, typval_T *rettv);
730static void f_searchpair(typval_T *argvars, typval_T *rettv);
731static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
732static void f_searchpos(typval_T *argvars, typval_T *rettv);
733static void f_server2client(typval_T *argvars, typval_T *rettv);
734static void f_serverlist(typval_T *argvars, typval_T *rettv);
735static void f_setbufvar(typval_T *argvars, typval_T *rettv);
736static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
737static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100738static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100739static void f_setline(typval_T *argvars, typval_T *rettv);
740static void f_setloclist(typval_T *argvars, typval_T *rettv);
741static void f_setmatches(typval_T *argvars, typval_T *rettv);
742static void f_setpos(typval_T *argvars, typval_T *rettv);
743static void f_setqflist(typval_T *argvars, typval_T *rettv);
744static void f_setreg(typval_T *argvars, typval_T *rettv);
745static void f_settabvar(typval_T *argvars, typval_T *rettv);
746static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
747static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100748#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100749static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100750#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100751static void f_shellescape(typval_T *argvars, typval_T *rettv);
752static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
753static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000754#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100755static void f_sin(typval_T *argvars, typval_T *rettv);
756static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000757#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100758static void f_sort(typval_T *argvars, typval_T *rettv);
759static void f_soundfold(typval_T *argvars, typval_T *rettv);
760static void f_spellbadword(typval_T *argvars, typval_T *rettv);
761static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
762static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000763#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100764static void f_sqrt(typval_T *argvars, typval_T *rettv);
765static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000766#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100767static void f_str2nr(typval_T *argvars, typval_T *rettv);
768static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000769#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100770static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000771#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100772static void f_stridx(typval_T *argvars, typval_T *rettv);
773static void f_string(typval_T *argvars, typval_T *rettv);
774static void f_strlen(typval_T *argvars, typval_T *rettv);
775static void f_strpart(typval_T *argvars, typval_T *rettv);
776static void f_strridx(typval_T *argvars, typval_T *rettv);
777static void f_strtrans(typval_T *argvars, typval_T *rettv);
778static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
779static void f_strwidth(typval_T *argvars, typval_T *rettv);
780static void f_submatch(typval_T *argvars, typval_T *rettv);
781static void f_substitute(typval_T *argvars, typval_T *rettv);
782static void f_synID(typval_T *argvars, typval_T *rettv);
783static void f_synIDattr(typval_T *argvars, typval_T *rettv);
784static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
785static void f_synstack(typval_T *argvars, typval_T *rettv);
786static void f_synconcealed(typval_T *argvars, typval_T *rettv);
787static void f_system(typval_T *argvars, typval_T *rettv);
788static void f_systemlist(typval_T *argvars, typval_T *rettv);
789static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
790static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
791static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
792static void f_taglist(typval_T *argvars, typval_T *rettv);
793static void f_tagfiles(typval_T *argvars, typval_T *rettv);
794static void f_tempname(typval_T *argvars, typval_T *rettv);
795static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200796#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100797static void f_tan(typval_T *argvars, typval_T *rettv);
798static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200799#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100800static void f_tolower(typval_T *argvars, typval_T *rettv);
801static void f_toupper(typval_T *argvars, typval_T *rettv);
802static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000803#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100804static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000805#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100806static void f_type(typval_T *argvars, typval_T *rettv);
807static void f_undofile(typval_T *argvars, typval_T *rettv);
808static void f_undotree(typval_T *argvars, typval_T *rettv);
809static void f_uniq(typval_T *argvars, typval_T *rettv);
810static void f_values(typval_T *argvars, typval_T *rettv);
811static void f_virtcol(typval_T *argvars, typval_T *rettv);
812static void f_visualmode(typval_T *argvars, typval_T *rettv);
813static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
814static void f_winbufnr(typval_T *argvars, typval_T *rettv);
815static void f_wincol(typval_T *argvars, typval_T *rettv);
816static void f_winheight(typval_T *argvars, typval_T *rettv);
817static void f_winline(typval_T *argvars, typval_T *rettv);
818static void f_winnr(typval_T *argvars, typval_T *rettv);
819static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
820static void f_winrestview(typval_T *argvars, typval_T *rettv);
821static void f_winsaveview(typval_T *argvars, typval_T *rettv);
822static void f_winwidth(typval_T *argvars, typval_T *rettv);
823static void f_writefile(typval_T *argvars, typval_T *rettv);
824static void f_wordcount(typval_T *argvars, typval_T *rettv);
825static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000826
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100827static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
828static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
829static int get_env_len(char_u **arg);
830static int get_id_len(char_u **arg);
831static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
832static char_u *find_name_end(char_u *arg, char_u **expr_start, char_u **expr_end, int flags);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000833#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
834#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
835 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100836static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
837static int eval_isnamec(int c);
838static int eval_isnamec1(int c);
839static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
840static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100841static typval_T *alloc_string_tv(char_u *string);
842static void init_tv(typval_T *varp);
843static long get_tv_number(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100844#ifdef FEAT_FLOAT
845static float_T get_tv_float(typval_T *varp);
846#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100847static linenr_T get_tv_lnum(typval_T *argvars);
848static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
849static char_u *get_tv_string(typval_T *varp);
850static char_u *get_tv_string_buf(typval_T *varp, char_u *buf);
851static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
852static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
853static hashtab_T *find_var_ht(char_u *name, char_u **varname);
854static funccall_T *get_funccal(void);
855static void vars_clear_ext(hashtab_T *ht, int free_val);
856static void delete_var(hashtab_T *ht, hashitem_T *hi);
857static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
858static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
859static void set_var(char_u *name, typval_T *varp, int copy);
860static int var_check_ro(int flags, char_u *name, int use_gettext);
861static int var_check_fixed(int flags, char_u *name, int use_gettext);
862static int var_check_func_name(char_u *name, int new_var);
863static int valid_varname(char_u *varname);
864static int tv_check_lock(int lock, char_u *name, int use_gettext);
865static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
866static char_u *find_option_end(char_u **arg, int *opt_flags);
867static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd);
868static int eval_fname_script(char_u *p);
869static int eval_fname_sid(char_u *p);
870static void list_func_head(ufunc_T *fp, int indent);
871static ufunc_T *find_func(char_u *name);
872static int function_exists(char_u *name);
873static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000874#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100875static void func_do_profile(ufunc_T *fp);
876static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
877static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000878static int
879# ifdef __BORLANDC__
880 _RTLENTRYF
881# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100882 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000883static int
884# ifdef __BORLANDC__
885 _RTLENTRYF
886# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100887 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000888#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100889static int script_autoload(char_u *name, int reload);
890static char_u *autoload_name(char_u *name);
891static void cat_func_name(char_u *buf, ufunc_T *fp);
892static void func_free(ufunc_T *fp);
893static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
894static int can_free_funccal(funccall_T *fc, int copyID) ;
895static void free_funccal(funccall_T *fc, int free_val);
896static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
897static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
898static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
899static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
900static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
901static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
902static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
903static int write_list(FILE *fd, list_T *list, int binary);
904static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000905
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200906
907#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100908static int compare_func_name(const void *s1, const void *s2);
909static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200910#endif
911
Bram Moolenaar33570922005-01-25 22:26:29 +0000912/*
913 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000914 */
915 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100916eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000917{
Bram Moolenaar33570922005-01-25 22:26:29 +0000918 int i;
919 struct vimvar *p;
920
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200921 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
922 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200923 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000924 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000925 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000926
927 for (i = 0; i < VV_LEN; ++i)
928 {
929 p = &vimvars[i];
930 STRCPY(p->vv_di.di_key, p->vv_name);
931 if (p->vv_flags & VV_RO)
932 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
933 else if (p->vv_flags & VV_RO_SBX)
934 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
935 else
936 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000937
938 /* add to v: scope dict, unless the value is not always available */
939 if (p->vv_type != VAR_UNKNOWN)
940 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000941 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000942 /* add to compat scope dict */
943 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000944 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100945 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
946
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000947 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100948 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200949 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100950 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100951
952 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
953 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
954 set_vim_var_nr(VV_NONE, VVAL_NONE);
955 set_vim_var_nr(VV_NULL, VVAL_NULL);
956
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200957 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200958
959#ifdef EBCDIC
960 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100961 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200962 */
963 sortFunctions();
964#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000965}
966
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000967#if defined(EXITFREE) || defined(PROTO)
968 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100969eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000970{
971 int i;
972 struct vimvar *p;
973
974 for (i = 0; i < VV_LEN; ++i)
975 {
976 p = &vimvars[i];
977 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000978 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000979 vim_free(p->vv_str);
980 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000981 }
982 else if (p->vv_di.di_tv.v_type == VAR_LIST)
983 {
984 list_unref(p->vv_list);
985 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000986 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000987 }
988 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000989 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000990 hash_clear(&compat_hashtab);
991
Bram Moolenaard9fba312005-06-26 22:34:35 +0000992 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100993# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200994 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100995# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000996
997 /* global variables */
998 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000999
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001000 /* autoloaded script names */
1001 ga_clear_strings(&ga_loaded);
1002
Bram Moolenaarcca74132013-09-25 21:00:28 +02001003 /* Script-local variables. First clear all the variables and in a second
1004 * loop free the scriptvar_T, because a variable in one script might hold
1005 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001006 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001007 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001008 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001009 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001010 ga_clear(&ga_scripts);
1011
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001012 /* unreferenced lists and dicts */
1013 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001014
1015 /* functions */
1016 free_all_functions();
1017 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001018}
1019#endif
1020
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001021/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 * Return the name of the executed function.
1023 */
1024 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001025func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001027 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028}
1029
1030/*
1031 * Return the address holding the next breakpoint line for a funccall cookie.
1032 */
1033 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001034func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035{
Bram Moolenaar33570922005-01-25 22:26:29 +00001036 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037}
1038
1039/*
1040 * Return the address holding the debug tick for a funccall cookie.
1041 */
1042 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001043func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044{
Bram Moolenaar33570922005-01-25 22:26:29 +00001045 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046}
1047
1048/*
1049 * Return the nesting level for a funccall cookie.
1050 */
1051 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001052func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053{
Bram Moolenaar33570922005-01-25 22:26:29 +00001054 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055}
1056
1057/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001058funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001060/* pointer to list of previously used funccal, still around because some
1061 * item in it is still being used. */
1062funccall_T *previous_funccal = NULL;
1063
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064/*
1065 * Return TRUE when a function was ended by a ":return" command.
1066 */
1067 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001068current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069{
1070 return current_funccal->returned;
1071}
1072
1073
1074/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 * Set an internal variable to a string value. Creates the variable if it does
1076 * not already exist.
1077 */
1078 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001079set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001081 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001082 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083
1084 val = vim_strsave(value);
1085 if (val != NULL)
1086 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001087 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001088 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001090 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001091 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 }
1093 }
1094}
1095
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001096static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001097static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001098static char_u *redir_endp = NULL;
1099static char_u *redir_varname = NULL;
1100
1101/*
1102 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001103 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001104 * Returns OK if successfully completed the setup. FAIL otherwise.
1105 */
1106 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001107var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001108{
1109 int save_emsg;
1110 int err;
1111 typval_T tv;
1112
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001113 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001114 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001115 {
1116 EMSG(_(e_invarg));
1117 return FAIL;
1118 }
1119
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001120 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001121 redir_varname = vim_strsave(name);
1122 if (redir_varname == NULL)
1123 return FAIL;
1124
1125 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1126 if (redir_lval == NULL)
1127 {
1128 var_redir_stop();
1129 return FAIL;
1130 }
1131
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001132 /* The output is stored in growarray "redir_ga" until redirection ends. */
1133 ga_init2(&redir_ga, (int)sizeof(char), 500);
1134
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001135 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001136 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001137 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1139 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001140 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001141 if (redir_endp != NULL && *redir_endp != NUL)
1142 /* Trailing characters are present after the variable name */
1143 EMSG(_(e_trailing));
1144 else
1145 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001146 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001147 var_redir_stop();
1148 return FAIL;
1149 }
1150
1151 /* check if we can write to the variable: set it to or append an empty
1152 * string */
1153 save_emsg = did_emsg;
1154 did_emsg = FALSE;
1155 tv.v_type = VAR_STRING;
1156 tv.vval.v_string = (char_u *)"";
1157 if (append)
1158 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1159 else
1160 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001161 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001162 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001163 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001164 if (err)
1165 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001166 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001167 var_redir_stop();
1168 return FAIL;
1169 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001170
1171 return OK;
1172}
1173
1174/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001175 * Append "value[value_len]" to the variable set by var_redir_start().
1176 * The actual appending is postponed until redirection ends, because the value
1177 * appended may in fact be the string we write to, changing it may cause freed
1178 * memory to be used:
1179 * :redir => foo
1180 * :let foo
1181 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001182 */
1183 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001184var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001185{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001186 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001187
1188 if (redir_lval == NULL)
1189 return;
1190
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001191 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001192 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001193 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001194 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001195
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001196 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001197 {
1198 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001199 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001200 }
1201 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001202 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001203}
1204
1205/*
1206 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001207 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001208 */
1209 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001210var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001211{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001212 typval_T tv;
1213
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001214 if (redir_lval != NULL)
1215 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001216 /* If there was no error: assign the text to the variable. */
1217 if (redir_endp != NULL)
1218 {
1219 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1220 tv.v_type = VAR_STRING;
1221 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001222 /* Call get_lval() again, if it's inside a Dict or List it may
1223 * have changed. */
1224 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001225 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001226 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1227 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1228 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001229 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001230
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001231 /* free the collected output */
1232 vim_free(redir_ga.ga_data);
1233 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001234
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001235 vim_free(redir_lval);
1236 redir_lval = NULL;
1237 }
1238 vim_free(redir_varname);
1239 redir_varname = NULL;
1240}
1241
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242# if defined(FEAT_MBYTE) || defined(PROTO)
1243 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001244eval_charconvert(
1245 char_u *enc_from,
1246 char_u *enc_to,
1247 char_u *fname_from,
1248 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249{
1250 int err = FALSE;
1251
1252 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1253 set_vim_var_string(VV_CC_TO, enc_to, -1);
1254 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1255 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1256 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1257 err = TRUE;
1258 set_vim_var_string(VV_CC_FROM, NULL, -1);
1259 set_vim_var_string(VV_CC_TO, NULL, -1);
1260 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1261 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1262
1263 if (err)
1264 return FAIL;
1265 return OK;
1266}
1267# endif
1268
1269# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1270 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001271eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272{
1273 int err = FALSE;
1274
1275 set_vim_var_string(VV_FNAME_IN, fname, -1);
1276 set_vim_var_string(VV_CMDARG, args, -1);
1277 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1278 err = TRUE;
1279 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1280 set_vim_var_string(VV_CMDARG, NULL, -1);
1281
1282 if (err)
1283 {
1284 mch_remove(fname);
1285 return FAIL;
1286 }
1287 return OK;
1288}
1289# endif
1290
1291# if defined(FEAT_DIFF) || defined(PROTO)
1292 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001293eval_diff(
1294 char_u *origfile,
1295 char_u *newfile,
1296 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297{
1298 int err = FALSE;
1299
1300 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1301 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1302 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1303 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1304 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1305 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1306 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1307}
1308
1309 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001310eval_patch(
1311 char_u *origfile,
1312 char_u *difffile,
1313 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314{
1315 int err;
1316
1317 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1318 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1319 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1320 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1321 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1322 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1323 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1324}
1325# endif
1326
1327/*
1328 * Top level evaluation function, returning a boolean.
1329 * Sets "error" to TRUE if there was an error.
1330 * Return TRUE or FALSE.
1331 */
1332 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001333eval_to_bool(
1334 char_u *arg,
1335 int *error,
1336 char_u **nextcmd,
1337 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338{
Bram Moolenaar33570922005-01-25 22:26:29 +00001339 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 int retval = FALSE;
1341
1342 if (skip)
1343 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001344 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 else
1347 {
1348 *error = FALSE;
1349 if (!skip)
1350 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001351 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001352 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 }
1354 }
1355 if (skip)
1356 --emsg_skip;
1357
1358 return retval;
1359}
1360
1361/*
1362 * Top level evaluation function, returning a string. If "skip" is TRUE,
1363 * only parsing to "nextcmd" is done, without reporting errors. Return
1364 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1365 */
1366 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001367eval_to_string_skip(
1368 char_u *arg,
1369 char_u **nextcmd,
1370 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371{
Bram Moolenaar33570922005-01-25 22:26:29 +00001372 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 char_u *retval;
1374
1375 if (skip)
1376 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001377 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 retval = NULL;
1379 else
1380 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001381 retval = vim_strsave(get_tv_string(&tv));
1382 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 }
1384 if (skip)
1385 --emsg_skip;
1386
1387 return retval;
1388}
1389
1390/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001391 * Skip over an expression at "*pp".
1392 * Return FAIL for an error, OK otherwise.
1393 */
1394 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001395skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001396{
Bram Moolenaar33570922005-01-25 22:26:29 +00001397 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001398
1399 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001400 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001401}
1402
1403/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001405 * When "convert" is TRUE convert a List into a sequence of lines and convert
1406 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 * Return pointer to allocated memory, or NULL for failure.
1408 */
1409 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001410eval_to_string(
1411 char_u *arg,
1412 char_u **nextcmd,
1413 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414{
Bram Moolenaar33570922005-01-25 22:26:29 +00001415 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001417 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001418#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001419 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001422 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 retval = NULL;
1424 else
1425 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001426 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001427 {
1428 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001429 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001430 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001431 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001432 if (tv.vval.v_list->lv_len > 0)
1433 ga_append(&ga, NL);
1434 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001435 ga_append(&ga, NUL);
1436 retval = (char_u *)ga.ga_data;
1437 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001438#ifdef FEAT_FLOAT
1439 else if (convert && tv.v_type == VAR_FLOAT)
1440 {
1441 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1442 retval = vim_strsave(numbuf);
1443 }
1444#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001445 else
1446 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001447 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 }
1449
1450 return retval;
1451}
1452
1453/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001454 * Call eval_to_string() without using current local variables and using
1455 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 */
1457 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001458eval_to_string_safe(
1459 char_u *arg,
1460 char_u **nextcmd,
1461 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462{
1463 char_u *retval;
1464 void *save_funccalp;
1465
1466 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001467 if (use_sandbox)
1468 ++sandbox;
1469 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001470 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001471 if (use_sandbox)
1472 --sandbox;
1473 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 restore_funccal(save_funccalp);
1475 return retval;
1476}
1477
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478/*
1479 * Top level evaluation function, returning a number.
1480 * Evaluates "expr" silently.
1481 * Returns -1 for an error.
1482 */
1483 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001484eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485{
Bram Moolenaar33570922005-01-25 22:26:29 +00001486 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001488 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489
1490 ++emsg_off;
1491
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001492 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 retval = -1;
1494 else
1495 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001496 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001497 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 }
1499 --emsg_off;
1500
1501 return retval;
1502}
1503
Bram Moolenaara40058a2005-07-11 22:42:07 +00001504/*
1505 * Prepare v: variable "idx" to be used.
1506 * Save the current typeval in "save_tv".
1507 * When not used yet add the variable to the v: hashtable.
1508 */
1509 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001510prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001511{
1512 *save_tv = vimvars[idx].vv_tv;
1513 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1514 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1515}
1516
1517/*
1518 * Restore v: variable "idx" to typeval "save_tv".
1519 * When no longer defined, remove the variable from the v: hashtable.
1520 */
1521 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001522restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001523{
1524 hashitem_T *hi;
1525
Bram Moolenaara40058a2005-07-11 22:42:07 +00001526 vimvars[idx].vv_tv = *save_tv;
1527 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1528 {
1529 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1530 if (HASHITEM_EMPTY(hi))
1531 EMSG2(_(e_intern2), "restore_vimvar()");
1532 else
1533 hash_remove(&vimvarht, hi);
1534 }
1535}
1536
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001537#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001538/*
1539 * Evaluate an expression to a list with suggestions.
1540 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001541 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001542 */
1543 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001544eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001545{
1546 typval_T save_val;
1547 typval_T rettv;
1548 list_T *list = NULL;
1549 char_u *p = skipwhite(expr);
1550
1551 /* Set "v:val" to the bad word. */
1552 prepare_vimvar(VV_VAL, &save_val);
1553 vimvars[VV_VAL].vv_type = VAR_STRING;
1554 vimvars[VV_VAL].vv_str = badword;
1555 if (p_verbose == 0)
1556 ++emsg_off;
1557
1558 if (eval1(&p, &rettv, TRUE) == OK)
1559 {
1560 if (rettv.v_type != VAR_LIST)
1561 clear_tv(&rettv);
1562 else
1563 list = rettv.vval.v_list;
1564 }
1565
1566 if (p_verbose == 0)
1567 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001568 restore_vimvar(VV_VAL, &save_val);
1569
1570 return list;
1571}
1572
1573/*
1574 * "list" is supposed to contain two items: a word and a number. Return the
1575 * word in "pp" and the number as the return value.
1576 * Return -1 if anything isn't right.
1577 * Used to get the good word and score from the eval_spell_expr() result.
1578 */
1579 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001580get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001581{
1582 listitem_T *li;
1583
1584 li = list->lv_first;
1585 if (li == NULL)
1586 return -1;
1587 *pp = get_tv_string(&li->li_tv);
1588
1589 li = li->li_next;
1590 if (li == NULL)
1591 return -1;
1592 return get_tv_number(&li->li_tv);
1593}
1594#endif
1595
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001596/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001597 * Top level evaluation function.
1598 * Returns an allocated typval_T with the result.
1599 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001600 */
1601 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001602eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001603{
1604 typval_T *tv;
1605
1606 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001607 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001608 {
1609 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001610 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001611 }
1612
1613 return tv;
1614}
1615
1616
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001618 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001619 * Uses argv[argc] for the function arguments. Only Number and String
1620 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001621 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001623 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001624call_vim_function(
1625 char_u *func,
1626 int argc,
1627 char_u **argv,
1628 int safe, /* use the sandbox */
1629 int str_arg_only, /* all arguments are strings */
1630 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631{
Bram Moolenaar33570922005-01-25 22:26:29 +00001632 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 long n;
1634 int len;
1635 int i;
1636 int doesrange;
1637 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001638 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001640 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001642 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643
1644 for (i = 0; i < argc; i++)
1645 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001646 /* Pass a NULL or empty argument as an empty string */
1647 if (argv[i] == NULL || *argv[i] == NUL)
1648 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001649 argvars[i].v_type = VAR_STRING;
1650 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001651 continue;
1652 }
1653
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001654 if (str_arg_only)
1655 len = 0;
1656 else
1657 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001658 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 if (len != 0 && len == (int)STRLEN(argv[i]))
1660 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001661 argvars[i].v_type = VAR_NUMBER;
1662 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 }
1664 else
1665 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001666 argvars[i].v_type = VAR_STRING;
1667 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 }
1669 }
1670
1671 if (safe)
1672 {
1673 save_funccalp = save_funccal();
1674 ++sandbox;
1675 }
1676
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001677 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1678 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001680 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 if (safe)
1682 {
1683 --sandbox;
1684 restore_funccal(save_funccalp);
1685 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001686 vim_free(argvars);
1687
1688 if (ret == FAIL)
1689 clear_tv(rettv);
1690
1691 return ret;
1692}
1693
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001694/*
1695 * Call vimL function "func" and return the result as a number.
1696 * Returns -1 when calling the function fails.
1697 * Uses argv[argc] for the function arguments.
1698 */
1699 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001700call_func_retnr(
1701 char_u *func,
1702 int argc,
1703 char_u **argv,
1704 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001705{
1706 typval_T rettv;
1707 long retval;
1708
1709 /* All arguments are passed as strings, no conversion to number. */
1710 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1711 return -1;
1712
1713 retval = get_tv_number_chk(&rettv, NULL);
1714 clear_tv(&rettv);
1715 return retval;
1716}
1717
1718#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1719 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1720
Bram Moolenaar4f688582007-07-24 12:34:30 +00001721# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001722/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001723 * Call vimL function "func" and return the result as a string.
1724 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001725 * Uses argv[argc] for the function arguments.
1726 */
1727 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001728call_func_retstr(
1729 char_u *func,
1730 int argc,
1731 char_u **argv,
1732 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001733{
1734 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001735 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001736
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001737 /* All arguments are passed as strings, no conversion to number. */
1738 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001739 return NULL;
1740
1741 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001742 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 return retval;
1744}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001745# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001746
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001747/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001748 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001749 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001750 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001751 */
1752 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001753call_func_retlist(
1754 char_u *func,
1755 int argc,
1756 char_u **argv,
1757 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001758{
1759 typval_T rettv;
1760
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001761 /* All arguments are passed as strings, no conversion to number. */
1762 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001763 return NULL;
1764
1765 if (rettv.v_type != VAR_LIST)
1766 {
1767 clear_tv(&rettv);
1768 return NULL;
1769 }
1770
1771 return rettv.vval.v_list;
1772}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773#endif
1774
1775/*
1776 * Save the current function call pointer, and set it to NULL.
1777 * Used when executing autocommands and for ":source".
1778 */
1779 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001780save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001782 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 current_funccal = NULL;
1785 return (void *)fc;
1786}
1787
1788 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001789restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001791 funccall_T *fc = (funccall_T *)vfc;
1792
1793 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794}
1795
Bram Moolenaar05159a02005-02-26 23:04:13 +00001796#if defined(FEAT_PROFILE) || defined(PROTO)
1797/*
1798 * Prepare profiling for entering a child or something else that is not
1799 * counted for the script/function itself.
1800 * Should always be called in pair with prof_child_exit().
1801 */
1802 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001803prof_child_enter(
1804 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001805{
1806 funccall_T *fc = current_funccal;
1807
1808 if (fc != NULL && fc->func->uf_profiling)
1809 profile_start(&fc->prof_child);
1810 script_prof_save(tm);
1811}
1812
1813/*
1814 * Take care of time spent in a child.
1815 * Should always be called after prof_child_enter().
1816 */
1817 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001818prof_child_exit(
1819 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001820{
1821 funccall_T *fc = current_funccal;
1822
1823 if (fc != NULL && fc->func->uf_profiling)
1824 {
1825 profile_end(&fc->prof_child);
1826 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1827 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1828 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1829 }
1830 script_prof_restore(tm);
1831}
1832#endif
1833
1834
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835#ifdef FEAT_FOLDING
1836/*
1837 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1838 * it in "*cp". Doesn't give error messages.
1839 */
1840 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001841eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842{
Bram Moolenaar33570922005-01-25 22:26:29 +00001843 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 int retval;
1845 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001846 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1847 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848
1849 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001850 if (use_sandbox)
1851 ++sandbox;
1852 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001854 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 retval = 0;
1856 else
1857 {
1858 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001859 if (tv.v_type == VAR_NUMBER)
1860 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001861 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 retval = 0;
1863 else
1864 {
1865 /* If the result is a string, check if there is a non-digit before
1866 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001867 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 if (!VIM_ISDIGIT(*s) && *s != '-')
1869 *cp = *s++;
1870 retval = atol((char *)s);
1871 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001872 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 }
1874 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001875 if (use_sandbox)
1876 --sandbox;
1877 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878
1879 return retval;
1880}
1881#endif
1882
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001884 * ":let" list all variable values
1885 * ":let var1 var2" list variable values
1886 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001887 * ":let var += expr" assignment command.
1888 * ":let var -= expr" assignment command.
1889 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001890 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 */
1892 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001893ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894{
1895 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001896 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001897 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001899 int var_count = 0;
1900 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001901 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001902 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001903 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904
Bram Moolenaardb552d602006-03-23 22:59:57 +00001905 argend = skip_var_list(arg, &var_count, &semicolon);
1906 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001907 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001908 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1909 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001910 expr = skipwhite(argend);
1911 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1912 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001914 /*
1915 * ":let" without "=": list variables
1916 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001917 if (*arg == '[')
1918 EMSG(_(e_invarg));
1919 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001920 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001921 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001922 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001923 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001924 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001925 list_glob_vars(&first);
1926 list_buf_vars(&first);
1927 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001928#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001929 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001930#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001931 list_script_vars(&first);
1932 list_func_vars(&first);
1933 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 eap->nextcmd = check_nextcmd(arg);
1936 }
1937 else
1938 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001939 op[0] = '=';
1940 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001941 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001942 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001943 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1944 op[0] = *expr; /* +=, -= or .= */
1945 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001946 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001947 else
1948 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001949
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 if (eap->skip)
1951 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001952 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 if (eap->skip)
1954 {
1955 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001956 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 --emsg_skip;
1958 }
1959 else if (i != FAIL)
1960 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001961 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001962 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001963 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 }
1965 }
1966}
1967
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001968/*
1969 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1970 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001971 * When "nextchars" is not NULL it points to a string with characters that
1972 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1973 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001974 * Returns OK or FAIL;
1975 */
1976 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001977ex_let_vars(
1978 char_u *arg_start,
1979 typval_T *tv,
1980 int copy, /* copy values from "tv", don't move */
1981 int semicolon, /* from skip_var_list() */
1982 int var_count, /* from skip_var_list() */
1983 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001984{
1985 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001986 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001987 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001988 listitem_T *item;
1989 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001990
1991 if (*arg != '[')
1992 {
1993 /*
1994 * ":let var = expr" or ":for var in list"
1995 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001996 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001997 return FAIL;
1998 return OK;
1999 }
2000
2001 /*
2002 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2003 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002004 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002005 {
2006 EMSG(_(e_listreq));
2007 return FAIL;
2008 }
2009
2010 i = list_len(l);
2011 if (semicolon == 0 && var_count < i)
2012 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002013 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002014 return FAIL;
2015 }
2016 if (var_count - semicolon > i)
2017 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002018 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002019 return FAIL;
2020 }
2021
2022 item = l->lv_first;
2023 while (*arg != ']')
2024 {
2025 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002026 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002027 item = item->li_next;
2028 if (arg == NULL)
2029 return FAIL;
2030
2031 arg = skipwhite(arg);
2032 if (*arg == ';')
2033 {
2034 /* Put the rest of the list (may be empty) in the var after ';'.
2035 * Create a new list for this. */
2036 l = list_alloc();
2037 if (l == NULL)
2038 return FAIL;
2039 while (item != NULL)
2040 {
2041 list_append_tv(l, &item->li_tv);
2042 item = item->li_next;
2043 }
2044
2045 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002046 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002047 ltv.vval.v_list = l;
2048 l->lv_refcount = 1;
2049
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002050 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2051 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002052 clear_tv(&ltv);
2053 if (arg == NULL)
2054 return FAIL;
2055 break;
2056 }
2057 else if (*arg != ',' && *arg != ']')
2058 {
2059 EMSG2(_(e_intern2), "ex_let_vars()");
2060 return FAIL;
2061 }
2062 }
2063
2064 return OK;
2065}
2066
2067/*
2068 * Skip over assignable variable "var" or list of variables "[var, var]".
2069 * Used for ":let varvar = expr" and ":for varvar in expr".
2070 * For "[var, var]" increment "*var_count" for each variable.
2071 * for "[var, var; var]" set "semicolon".
2072 * Return NULL for an error.
2073 */
2074 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002075skip_var_list(
2076 char_u *arg,
2077 int *var_count,
2078 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002079{
2080 char_u *p, *s;
2081
2082 if (*arg == '[')
2083 {
2084 /* "[var, var]": find the matching ']'. */
2085 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002086 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002087 {
2088 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2089 s = skip_var_one(p);
2090 if (s == p)
2091 {
2092 EMSG2(_(e_invarg2), p);
2093 return NULL;
2094 }
2095 ++*var_count;
2096
2097 p = skipwhite(s);
2098 if (*p == ']')
2099 break;
2100 else if (*p == ';')
2101 {
2102 if (*semicolon == 1)
2103 {
2104 EMSG(_("Double ; in list of variables"));
2105 return NULL;
2106 }
2107 *semicolon = 1;
2108 }
2109 else if (*p != ',')
2110 {
2111 EMSG2(_(e_invarg2), p);
2112 return NULL;
2113 }
2114 }
2115 return p + 1;
2116 }
2117 else
2118 return skip_var_one(arg);
2119}
2120
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002121/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002122 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002123 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002124 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002125 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002126skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002127{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002128 if (*arg == '@' && arg[1] != NUL)
2129 return arg + 2;
2130 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2131 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002132}
2133
Bram Moolenaara7043832005-01-21 11:56:39 +00002134/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002135 * List variables for hashtab "ht" with prefix "prefix".
2136 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002137 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002138 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002139list_hashtable_vars(
2140 hashtab_T *ht,
2141 char_u *prefix,
2142 int empty,
2143 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002144{
Bram Moolenaar33570922005-01-25 22:26:29 +00002145 hashitem_T *hi;
2146 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002147 int todo;
2148
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002149 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002150 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2151 {
2152 if (!HASHITEM_EMPTY(hi))
2153 {
2154 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002155 di = HI2DI(hi);
2156 if (empty || di->di_tv.v_type != VAR_STRING
2157 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002158 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002159 }
2160 }
2161}
2162
2163/*
2164 * List global variables.
2165 */
2166 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002167list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002168{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002169 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002170}
2171
2172/*
2173 * List buffer variables.
2174 */
2175 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002176list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002177{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002178 char_u numbuf[NUMBUFLEN];
2179
Bram Moolenaar429fa852013-04-15 12:27:36 +02002180 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002181 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002182
2183 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002184 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2185 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002186}
2187
2188/*
2189 * List window variables.
2190 */
2191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002192list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002193{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002194 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002195 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002196}
2197
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002198#ifdef FEAT_WINDOWS
2199/*
2200 * List tab page variables.
2201 */
2202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002203list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002204{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002205 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002206 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002207}
2208#endif
2209
Bram Moolenaara7043832005-01-21 11:56:39 +00002210/*
2211 * List Vim variables.
2212 */
2213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002214list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002215{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002216 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002217}
2218
2219/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002220 * List script-local variables, if there is a script.
2221 */
2222 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002223list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002224{
2225 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002226 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2227 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002228}
2229
2230/*
2231 * List function variables, if there is a function.
2232 */
2233 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002234list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002235{
2236 if (current_funccal != NULL)
2237 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002238 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002239}
2240
2241/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002242 * List variables in "arg".
2243 */
2244 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002245list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002246{
2247 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002248 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002250 char_u *name_start;
2251 char_u *arg_subsc;
2252 char_u *tofree;
2253 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254
2255 while (!ends_excmd(*arg) && !got_int)
2256 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002257 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002258 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002259 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002260 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2261 {
2262 emsg_severe = TRUE;
2263 EMSG(_(e_trailing));
2264 break;
2265 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002266 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002267 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002268 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002269 /* get_name_len() takes care of expanding curly braces */
2270 name_start = name = arg;
2271 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2272 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002273 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002274 /* This is mainly to keep test 49 working: when expanding
2275 * curly braces fails overrule the exception error message. */
2276 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002277 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002278 emsg_severe = TRUE;
2279 EMSG2(_(e_invarg2), arg);
2280 break;
2281 }
2282 error = TRUE;
2283 }
2284 else
2285 {
2286 if (tofree != NULL)
2287 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002288 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002289 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002290 else
2291 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002292 /* handle d.key, l[idx], f(expr) */
2293 arg_subsc = arg;
2294 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002295 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002296 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002297 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002298 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002299 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002300 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002301 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002302 case 'g': list_glob_vars(first); break;
2303 case 'b': list_buf_vars(first); break;
2304 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002305#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002306 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002307#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002308 case 'v': list_vim_vars(first); break;
2309 case 's': list_script_vars(first); break;
2310 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002311 default:
2312 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002313 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002314 }
2315 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002316 {
2317 char_u numbuf[NUMBUFLEN];
2318 char_u *tf;
2319 int c;
2320 char_u *s;
2321
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002322 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002323 c = *arg;
2324 *arg = NUL;
2325 list_one_var_a((char_u *)"",
2326 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002327 tv.v_type,
2328 s == NULL ? (char_u *)"" : s,
2329 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002330 *arg = c;
2331 vim_free(tf);
2332 }
2333 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002334 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002335 }
2336 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002337
2338 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002339 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002340
2341 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002342 }
2343
2344 return arg;
2345}
2346
2347/*
2348 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2349 * Returns a pointer to the char just after the var name.
2350 * Returns NULL if there is an error.
2351 */
2352 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002353ex_let_one(
2354 char_u *arg, /* points to variable name */
2355 typval_T *tv, /* value to assign to variable */
2356 int copy, /* copy value from "tv" */
2357 char_u *endchars, /* valid chars after variable name or NULL */
2358 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002359{
2360 int c1;
2361 char_u *name;
2362 char_u *p;
2363 char_u *arg_end = NULL;
2364 int len;
2365 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002366 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002367
2368 /*
2369 * ":let $VAR = expr": Set environment variable.
2370 */
2371 if (*arg == '$')
2372 {
2373 /* Find the end of the name. */
2374 ++arg;
2375 name = arg;
2376 len = get_env_len(&arg);
2377 if (len == 0)
2378 EMSG2(_(e_invarg2), name - 1);
2379 else
2380 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002381 if (op != NULL && (*op == '+' || *op == '-'))
2382 EMSG2(_(e_letwrong), op);
2383 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002384 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002385 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002386 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002387 {
2388 c1 = name[len];
2389 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002390 p = get_tv_string_chk(tv);
2391 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002392 {
2393 int mustfree = FALSE;
2394 char_u *s = vim_getenv(name, &mustfree);
2395
2396 if (s != NULL)
2397 {
2398 p = tofree = concat_str(s, p);
2399 if (mustfree)
2400 vim_free(s);
2401 }
2402 }
2403 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002404 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002405 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002406 if (STRICMP(name, "HOME") == 0)
2407 init_homedir();
2408 else if (didset_vim && STRICMP(name, "VIM") == 0)
2409 didset_vim = FALSE;
2410 else if (didset_vimruntime
2411 && STRICMP(name, "VIMRUNTIME") == 0)
2412 didset_vimruntime = FALSE;
2413 arg_end = arg;
2414 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002415 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002416 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002417 }
2418 }
2419 }
2420
2421 /*
2422 * ":let &option = expr": Set option value.
2423 * ":let &l:option = expr": Set local option value.
2424 * ":let &g:option = expr": Set global option value.
2425 */
2426 else if (*arg == '&')
2427 {
2428 /* Find the end of the name. */
2429 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002430 if (p == NULL || (endchars != NULL
2431 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002432 EMSG(_(e_letunexp));
2433 else
2434 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002435 long n;
2436 int opt_type;
2437 long numval;
2438 char_u *stringval = NULL;
2439 char_u *s;
2440
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002441 c1 = *p;
2442 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002443
2444 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002445 s = get_tv_string_chk(tv); /* != NULL if number or string */
2446 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002447 {
2448 opt_type = get_option_value(arg, &numval,
2449 &stringval, opt_flags);
2450 if ((opt_type == 1 && *op == '.')
2451 || (opt_type == 0 && *op != '.'))
2452 EMSG2(_(e_letwrong), op);
2453 else
2454 {
2455 if (opt_type == 1) /* number */
2456 {
2457 if (*op == '+')
2458 n = numval + n;
2459 else
2460 n = numval - n;
2461 }
2462 else if (opt_type == 0 && stringval != NULL) /* string */
2463 {
2464 s = concat_str(stringval, s);
2465 vim_free(stringval);
2466 stringval = s;
2467 }
2468 }
2469 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002470 if (s != NULL)
2471 {
2472 set_option_value(arg, n, s, opt_flags);
2473 arg_end = p;
2474 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002475 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002476 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002477 }
2478 }
2479
2480 /*
2481 * ":let @r = expr": Set register contents.
2482 */
2483 else if (*arg == '@')
2484 {
2485 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002486 if (op != NULL && (*op == '+' || *op == '-'))
2487 EMSG2(_(e_letwrong), op);
2488 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002489 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002490 EMSG(_(e_letunexp));
2491 else
2492 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002493 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002494 char_u *s;
2495
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002496 p = get_tv_string_chk(tv);
2497 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002498 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002499 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002500 if (s != NULL)
2501 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002502 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002503 vim_free(s);
2504 }
2505 }
2506 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002507 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002508 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002509 arg_end = arg + 1;
2510 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002511 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002512 }
2513 }
2514
2515 /*
2516 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002517 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002518 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002519 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002520 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002521 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002522
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002523 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002524 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002525 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002526 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2527 EMSG(_(e_letunexp));
2528 else
2529 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002530 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 arg_end = p;
2532 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002533 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002534 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002535 }
2536
2537 else
2538 EMSG2(_(e_invarg2), arg);
2539
2540 return arg_end;
2541}
2542
2543/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002544 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2545 */
2546 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002547check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002548{
2549 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2550 {
2551 EMSG2(_(e_readonlyvar), arg);
2552 return TRUE;
2553 }
2554 return FALSE;
2555}
2556
2557/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558 * Get an lval: variable, Dict item or List item that can be assigned a value
2559 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2560 * "name.key", "name.key[expr]" etc.
2561 * Indexing only works if "name" is an existing List or Dictionary.
2562 * "name" points to the start of the name.
2563 * If "rettv" is not NULL it points to the value to be assigned.
2564 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2565 * wrong; must end in space or cmd separator.
2566 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002567 * flags:
2568 * GLV_QUIET: do not give error messages
2569 * GLV_NO_AUTOLOAD: do not use script autoloading
2570 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002571 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002572 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002574 */
2575 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002576get_lval(
2577 char_u *name,
2578 typval_T *rettv,
2579 lval_T *lp,
2580 int unlet,
2581 int skip,
2582 int flags, /* GLV_ values */
2583 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002584{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002585 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002586 char_u *expr_start, *expr_end;
2587 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002588 dictitem_T *v;
2589 typval_T var1;
2590 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002592 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002593 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002594 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002595 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002596 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002597
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002598 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002599 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600
2601 if (skip)
2602 {
2603 /* When skipping just find the end of the name. */
2604 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002605 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002606 }
2607
2608 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002609 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002610 if (expr_start != NULL)
2611 {
2612 /* Don't expand the name when we already know there is an error. */
2613 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2614 && *p != '[' && *p != '.')
2615 {
2616 EMSG(_(e_trailing));
2617 return NULL;
2618 }
2619
2620 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2621 if (lp->ll_exp_name == NULL)
2622 {
2623 /* Report an invalid expression in braces, unless the
2624 * expression evaluation has been cancelled due to an
2625 * aborting error, an interrupt, or an exception. */
2626 if (!aborting() && !quiet)
2627 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002628 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002629 EMSG2(_(e_invarg2), name);
2630 return NULL;
2631 }
2632 }
2633 lp->ll_name = lp->ll_exp_name;
2634 }
2635 else
2636 lp->ll_name = name;
2637
2638 /* Without [idx] or .key we are done. */
2639 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2640 return p;
2641
2642 cc = *p;
2643 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002644 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 if (v == NULL && !quiet)
2646 EMSG2(_(e_undefvar), lp->ll_name);
2647 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002648 if (v == NULL)
2649 return NULL;
2650
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 /*
2652 * Loop until no more [idx] or .key is following.
2653 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002654 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002656 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2658 && !(lp->ll_tv->v_type == VAR_DICT
2659 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002660 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 if (!quiet)
2662 EMSG(_("E689: Can only index a List or Dictionary"));
2663 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002664 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002665 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002666 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 if (!quiet)
2668 EMSG(_("E708: [:] must come last"));
2669 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002670 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002671
Bram Moolenaar8c711452005-01-14 21:53:12 +00002672 len = -1;
2673 if (*p == '.')
2674 {
2675 key = p + 1;
2676 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2677 ;
2678 if (len == 0)
2679 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 if (!quiet)
2681 EMSG(_(e_emptykey));
2682 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002683 }
2684 p = key + len;
2685 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002686 else
2687 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002689 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 if (*p == ':')
2691 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002692 else
2693 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 empty1 = FALSE;
2695 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002697 if (get_tv_string_chk(&var1) == NULL)
2698 {
2699 /* not a number or string */
2700 clear_tv(&var1);
2701 return NULL;
2702 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 }
2704
2705 /* Optionally get the second index [ :expr]. */
2706 if (*p == ':')
2707 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002711 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002712 if (!empty1)
2713 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002715 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 if (rettv != NULL && (rettv->v_type != VAR_LIST
2717 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 if (!quiet)
2720 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 if (!empty1)
2722 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002723 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 }
2725 p = skipwhite(p + 1);
2726 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002727 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002728 else
2729 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002730 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002731 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2732 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 if (!empty1)
2734 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002735 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002736 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002737 if (get_tv_string_chk(&var2) == NULL)
2738 {
2739 /* not a number or string */
2740 if (!empty1)
2741 clear_tv(&var1);
2742 clear_tv(&var2);
2743 return NULL;
2744 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002746 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002747 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002748 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002750
Bram Moolenaar8c711452005-01-14 21:53:12 +00002751 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002752 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 if (!quiet)
2754 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002755 if (!empty1)
2756 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002757 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002758 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002760 }
2761
2762 /* Skip to past ']'. */
2763 ++p;
2764 }
2765
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002766 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002767 {
2768 if (len == -1)
2769 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002770 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002771 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002772 if (*key == NUL)
2773 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002774 if (!quiet)
2775 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002776 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002777 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002778 }
2779 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002780 lp->ll_list = NULL;
2781 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002782 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002783
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002784 /* When assigning to a scope dictionary check that a function and
2785 * variable name is valid (only variable name unless it is l: or
2786 * g: dictionary). Disallow overwriting a builtin function. */
2787 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002788 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002789 int prevval;
2790 int wrong;
2791
2792 if (len != -1)
2793 {
2794 prevval = key[len];
2795 key[len] = NUL;
2796 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002797 else
2798 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002799 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2800 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002801 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002802 || !valid_varname(key);
2803 if (len != -1)
2804 key[len] = prevval;
2805 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002806 return NULL;
2807 }
2808
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002809 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002810 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002811 /* Can't add "v:" variable. */
2812 if (lp->ll_dict == &vimvardict)
2813 {
2814 EMSG2(_(e_illvar), name);
2815 return NULL;
2816 }
2817
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002818 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002819 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002820 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002821 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002822 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002823 if (len == -1)
2824 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002826 }
2827 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002828 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002829 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002830 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002831 if (len == -1)
2832 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002833 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002834 p = NULL;
2835 break;
2836 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002837 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002838 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002839 return NULL;
2840
Bram Moolenaar8c711452005-01-14 21:53:12 +00002841 if (len == -1)
2842 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002843 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002844 }
2845 else
2846 {
2847 /*
2848 * Get the number and item for the only or first index of the List.
2849 */
2850 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002851 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002852 else
2853 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002854 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002855 clear_tv(&var1);
2856 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002857 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002858 lp->ll_list = lp->ll_tv->vval.v_list;
2859 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2860 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002861 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002862 if (lp->ll_n1 < 0)
2863 {
2864 lp->ll_n1 = 0;
2865 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2866 }
2867 }
2868 if (lp->ll_li == NULL)
2869 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002871 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002872 if (!quiet)
2873 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002875 }
2876
2877 /*
2878 * May need to find the item or absolute index for the second
2879 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880 * When no index given: "lp->ll_empty2" is TRUE.
2881 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002882 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002884 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002885 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002886 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002887 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002888 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002889 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002890 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002891 {
2892 if (!quiet)
2893 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002894 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002895 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002897 }
2898
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002899 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2900 if (lp->ll_n1 < 0)
2901 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2902 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002903 {
2904 if (!quiet)
2905 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002907 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002908 }
2909
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002910 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002911 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002912 }
2913
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002914 return p;
2915}
2916
2917/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002918 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002919 */
2920 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002921clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002922{
2923 vim_free(lp->ll_exp_name);
2924 vim_free(lp->ll_newkey);
2925}
2926
2927/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002928 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002930 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002931 */
2932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002933set_var_lval(
2934 lval_T *lp,
2935 char_u *endp,
2936 typval_T *rettv,
2937 int copy,
2938 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002939{
2940 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002941 listitem_T *ri;
2942 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002943
2944 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002945 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002946 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002947 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002948 cc = *endp;
2949 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002950 if (op != NULL && *op != '=')
2951 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002952 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002953
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002954 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002955 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002956 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002957 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002958 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002959 if ((di == NULL
2960 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2961 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2962 FALSE)))
2963 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002964 set_var(lp->ll_name, &tv, FALSE);
2965 clear_tv(&tv);
2966 }
2967 }
2968 else
2969 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002970 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002971 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002972 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002973 else if (tv_check_lock(lp->ll_newkey == NULL
2974 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002975 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002976 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002977 else if (lp->ll_range)
2978 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002979 listitem_T *ll_li = lp->ll_li;
2980 int ll_n1 = lp->ll_n1;
2981
2982 /*
2983 * Check whether any of the list items is locked
2984 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002985 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002986 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002987 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002988 return;
2989 ri = ri->li_next;
2990 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2991 break;
2992 ll_li = ll_li->li_next;
2993 ++ll_n1;
2994 }
2995
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002996 /*
2997 * Assign the List values to the list items.
2998 */
2999 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003000 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003001 if (op != NULL && *op != '=')
3002 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3003 else
3004 {
3005 clear_tv(&lp->ll_li->li_tv);
3006 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3007 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003008 ri = ri->li_next;
3009 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3010 break;
3011 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003012 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003013 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003014 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003015 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003016 ri = NULL;
3017 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003018 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003019 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003020 lp->ll_li = lp->ll_li->li_next;
3021 ++lp->ll_n1;
3022 }
3023 if (ri != NULL)
3024 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003025 else if (lp->ll_empty2
3026 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003027 : lp->ll_n1 != lp->ll_n2)
3028 EMSG(_("E711: List value has not enough items"));
3029 }
3030 else
3031 {
3032 /*
3033 * Assign to a List or Dictionary item.
3034 */
3035 if (lp->ll_newkey != NULL)
3036 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003037 if (op != NULL && *op != '=')
3038 {
3039 EMSG2(_(e_letwrong), op);
3040 return;
3041 }
3042
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003043 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003044 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003045 if (di == NULL)
3046 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003047 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3048 {
3049 vim_free(di);
3050 return;
3051 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003052 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003053 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003054 else if (op != NULL && *op != '=')
3055 {
3056 tv_op(lp->ll_tv, rettv, op);
3057 return;
3058 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003059 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003060 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003061
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003062 /*
3063 * Assign the value to the variable or list item.
3064 */
3065 if (copy)
3066 copy_tv(rettv, lp->ll_tv);
3067 else
3068 {
3069 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003070 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003071 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003072 }
3073 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003074}
3075
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003076/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003077 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3078 * Returns OK or FAIL.
3079 */
3080 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003081tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003082{
3083 long n;
3084 char_u numbuf[NUMBUFLEN];
3085 char_u *s;
3086
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003087 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3088 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3089 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003090 {
3091 switch (tv1->v_type)
3092 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003093 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003094 case VAR_DICT:
3095 case VAR_FUNC:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003096 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003097 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003098 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003099 break;
3100
3101 case VAR_LIST:
3102 if (*op != '+' || tv2->v_type != VAR_LIST)
3103 break;
3104 /* List += List */
3105 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3106 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3107 return OK;
3108
3109 case VAR_NUMBER:
3110 case VAR_STRING:
3111 if (tv2->v_type == VAR_LIST)
3112 break;
3113 if (*op == '+' || *op == '-')
3114 {
3115 /* nr += nr or nr -= nr*/
3116 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003117#ifdef FEAT_FLOAT
3118 if (tv2->v_type == VAR_FLOAT)
3119 {
3120 float_T f = n;
3121
3122 if (*op == '+')
3123 f += tv2->vval.v_float;
3124 else
3125 f -= tv2->vval.v_float;
3126 clear_tv(tv1);
3127 tv1->v_type = VAR_FLOAT;
3128 tv1->vval.v_float = f;
3129 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003130 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003131#endif
3132 {
3133 if (*op == '+')
3134 n += get_tv_number(tv2);
3135 else
3136 n -= get_tv_number(tv2);
3137 clear_tv(tv1);
3138 tv1->v_type = VAR_NUMBER;
3139 tv1->vval.v_number = n;
3140 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003141 }
3142 else
3143 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003144 if (tv2->v_type == VAR_FLOAT)
3145 break;
3146
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003147 /* str .= str */
3148 s = get_tv_string(tv1);
3149 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3150 clear_tv(tv1);
3151 tv1->v_type = VAR_STRING;
3152 tv1->vval.v_string = s;
3153 }
3154 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003155
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003156 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003157#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003158 {
3159 float_T f;
3160
3161 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3162 && tv2->v_type != VAR_NUMBER
3163 && tv2->v_type != VAR_STRING))
3164 break;
3165 if (tv2->v_type == VAR_FLOAT)
3166 f = tv2->vval.v_float;
3167 else
3168 f = get_tv_number(tv2);
3169 if (*op == '+')
3170 tv1->vval.v_float += f;
3171 else
3172 tv1->vval.v_float -= f;
3173 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003174#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003175 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003176 }
3177 }
3178
3179 EMSG2(_(e_letwrong), op);
3180 return FAIL;
3181}
3182
3183/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003184 * Add a watcher to a list.
3185 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003186 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003187list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003188{
3189 lw->lw_next = l->lv_watch;
3190 l->lv_watch = lw;
3191}
3192
3193/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003194 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003195 * No warning when it isn't found...
3196 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003197 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003198list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003199{
Bram Moolenaar33570922005-01-25 22:26:29 +00003200 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003201
3202 lwp = &l->lv_watch;
3203 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3204 {
3205 if (lw == lwrem)
3206 {
3207 *lwp = lw->lw_next;
3208 break;
3209 }
3210 lwp = &lw->lw_next;
3211 }
3212}
3213
3214/*
3215 * Just before removing an item from a list: advance watchers to the next
3216 * item.
3217 */
3218 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003219list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003220{
Bram Moolenaar33570922005-01-25 22:26:29 +00003221 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003222
3223 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3224 if (lw->lw_item == item)
3225 lw->lw_item = item->li_next;
3226}
3227
3228/*
3229 * Evaluate the expression used in a ":for var in expr" command.
3230 * "arg" points to "var".
3231 * Set "*errp" to TRUE for an error, FALSE otherwise;
3232 * Return a pointer that holds the info. Null when there is an error.
3233 */
3234 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003235eval_for_line(
3236 char_u *arg,
3237 int *errp,
3238 char_u **nextcmdp,
3239 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003240{
Bram Moolenaar33570922005-01-25 22:26:29 +00003241 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003242 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003243 typval_T tv;
3244 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003245
3246 *errp = TRUE; /* default: there is an error */
3247
Bram Moolenaar33570922005-01-25 22:26:29 +00003248 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003249 if (fi == NULL)
3250 return NULL;
3251
3252 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3253 if (expr == NULL)
3254 return fi;
3255
3256 expr = skipwhite(expr);
3257 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3258 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003259 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003260 return fi;
3261 }
3262
3263 if (skip)
3264 ++emsg_skip;
3265 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3266 {
3267 *errp = FALSE;
3268 if (!skip)
3269 {
3270 l = tv.vval.v_list;
3271 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003272 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003273 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003274 clear_tv(&tv);
3275 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003276 else
3277 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003278 /* No need to increment the refcount, it's already set for the
3279 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003280 fi->fi_list = l;
3281 list_add_watch(l, &fi->fi_lw);
3282 fi->fi_lw.lw_item = l->lv_first;
3283 }
3284 }
3285 }
3286 if (skip)
3287 --emsg_skip;
3288
3289 return fi;
3290}
3291
3292/*
3293 * Use the first item in a ":for" list. Advance to the next.
3294 * Assign the values to the variable (list). "arg" points to the first one.
3295 * Return TRUE when a valid item was found, FALSE when at end of list or
3296 * something wrong.
3297 */
3298 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003299next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003300{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003301 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003302 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003303 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003304
3305 item = fi->fi_lw.lw_item;
3306 if (item == NULL)
3307 result = FALSE;
3308 else
3309 {
3310 fi->fi_lw.lw_item = item->li_next;
3311 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3312 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3313 }
3314 return result;
3315}
3316
3317/*
3318 * Free the structure used to store info used by ":for".
3319 */
3320 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003321free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003322{
Bram Moolenaar33570922005-01-25 22:26:29 +00003323 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003324
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003325 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003326 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003327 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003328 list_unref(fi->fi_list);
3329 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003330 vim_free(fi);
3331}
3332
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3334
3335 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003336set_context_for_expression(
3337 expand_T *xp,
3338 char_u *arg,
3339 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340{
3341 int got_eq = FALSE;
3342 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003343 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003345 if (cmdidx == CMD_let)
3346 {
3347 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003348 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003349 {
3350 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003351 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003352 {
3353 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003354 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003355 if (vim_iswhite(*p))
3356 break;
3357 }
3358 return;
3359 }
3360 }
3361 else
3362 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3363 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 while ((xp->xp_pattern = vim_strpbrk(arg,
3365 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3366 {
3367 c = *xp->xp_pattern;
3368 if (c == '&')
3369 {
3370 c = xp->xp_pattern[1];
3371 if (c == '&')
3372 {
3373 ++xp->xp_pattern;
3374 xp->xp_context = cmdidx != CMD_let || got_eq
3375 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3376 }
3377 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003378 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003380 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3381 xp->xp_pattern += 2;
3382
3383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 }
3385 else if (c == '$')
3386 {
3387 /* environment variable */
3388 xp->xp_context = EXPAND_ENV_VARS;
3389 }
3390 else if (c == '=')
3391 {
3392 got_eq = TRUE;
3393 xp->xp_context = EXPAND_EXPRESSION;
3394 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003395 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 && xp->xp_context == EXPAND_FUNCTIONS
3397 && vim_strchr(xp->xp_pattern, '(') == NULL)
3398 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003399 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 break;
3401 }
3402 else if (cmdidx != CMD_let || got_eq)
3403 {
3404 if (c == '"') /* string */
3405 {
3406 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3407 if (c == '\\' && xp->xp_pattern[1] != NUL)
3408 ++xp->xp_pattern;
3409 xp->xp_context = EXPAND_NOTHING;
3410 }
3411 else if (c == '\'') /* literal string */
3412 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003413 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3415 /* skip */ ;
3416 xp->xp_context = EXPAND_NOTHING;
3417 }
3418 else if (c == '|')
3419 {
3420 if (xp->xp_pattern[1] == '|')
3421 {
3422 ++xp->xp_pattern;
3423 xp->xp_context = EXPAND_EXPRESSION;
3424 }
3425 else
3426 xp->xp_context = EXPAND_COMMANDS;
3427 }
3428 else
3429 xp->xp_context = EXPAND_EXPRESSION;
3430 }
3431 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003432 /* Doesn't look like something valid, expand as an expression
3433 * anyway. */
3434 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 arg = xp->xp_pattern;
3436 if (*arg != NUL)
3437 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3438 /* skip */ ;
3439 }
3440 xp->xp_pattern = arg;
3441}
3442
3443#endif /* FEAT_CMDL_COMPL */
3444
3445/*
3446 * ":1,25call func(arg1, arg2)" function call.
3447 */
3448 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003449ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450{
3451 char_u *arg = eap->arg;
3452 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003454 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003456 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 linenr_T lnum;
3458 int doesrange;
3459 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003460 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003462 if (eap->skip)
3463 {
3464 /* trans_function_name() doesn't work well when skipping, use eval0()
3465 * instead to skip to any following command, e.g. for:
3466 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003467 ++emsg_skip;
3468 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3469 clear_tv(&rettv);
3470 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003471 return;
3472 }
3473
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003474 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003475 if (fudi.fd_newkey != NULL)
3476 {
3477 /* Still need to give an error message for missing key. */
3478 EMSG2(_(e_dictkey), fudi.fd_newkey);
3479 vim_free(fudi.fd_newkey);
3480 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003481 if (tofree == NULL)
3482 return;
3483
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003484 /* Increase refcount on dictionary, it could get deleted when evaluating
3485 * the arguments. */
3486 if (fudi.fd_dict != NULL)
3487 ++fudi.fd_dict->dv_refcount;
3488
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003489 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003490 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003491 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492
Bram Moolenaar532c7802005-01-27 14:44:31 +00003493 /* Skip white space to allow ":call func ()". Not good, but required for
3494 * backward compatibility. */
3495 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003496 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497
3498 if (*startarg != '(')
3499 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003500 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 goto end;
3502 }
3503
3504 /*
3505 * When skipping, evaluate the function once, to find the end of the
3506 * arguments.
3507 * When the function takes a range, this is discovered after the first
3508 * call, and the loop is broken.
3509 */
3510 if (eap->skip)
3511 {
3512 ++emsg_skip;
3513 lnum = eap->line2; /* do it once, also with an invalid range */
3514 }
3515 else
3516 lnum = eap->line1;
3517 for ( ; lnum <= eap->line2; ++lnum)
3518 {
3519 if (!eap->skip && eap->addr_count > 0)
3520 {
3521 curwin->w_cursor.lnum = lnum;
3522 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003523#ifdef FEAT_VIRTUALEDIT
3524 curwin->w_cursor.coladd = 0;
3525#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 }
3527 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003528 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003529 eap->line1, eap->line2, &doesrange,
3530 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 {
3532 failed = TRUE;
3533 break;
3534 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003535
3536 /* Handle a function returning a Funcref, Dictionary or List. */
3537 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3538 {
3539 failed = TRUE;
3540 break;
3541 }
3542
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003543 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 if (doesrange || eap->skip)
3545 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003546
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003548 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003549 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003550 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 if (aborting())
3552 break;
3553 }
3554 if (eap->skip)
3555 --emsg_skip;
3556
3557 if (!failed)
3558 {
3559 /* Check for trailing illegal characters and a following command. */
3560 if (!ends_excmd(*arg))
3561 {
3562 emsg_severe = TRUE;
3563 EMSG(_(e_trailing));
3564 }
3565 else
3566 eap->nextcmd = check_nextcmd(arg);
3567 }
3568
3569end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003570 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003571 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572}
3573
3574/*
3575 * ":unlet[!] var1 ... " command.
3576 */
3577 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003578ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003580 ex_unletlock(eap, eap->arg, 0);
3581}
3582
3583/*
3584 * ":lockvar" and ":unlockvar" commands
3585 */
3586 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003587ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003588{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003590 int deep = 2;
3591
3592 if (eap->forceit)
3593 deep = -1;
3594 else if (vim_isdigit(*arg))
3595 {
3596 deep = getdigits(&arg);
3597 arg = skipwhite(arg);
3598 }
3599
3600 ex_unletlock(eap, arg, deep);
3601}
3602
3603/*
3604 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3605 */
3606 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003607ex_unletlock(
3608 exarg_T *eap,
3609 char_u *argstart,
3610 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003611{
3612 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003615 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616
3617 do
3618 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003619 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003620 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003621 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003622 if (lv.ll_name == NULL)
3623 error = TRUE; /* error but continue parsing */
3624 if (name_end == NULL || (!vim_iswhite(*name_end)
3625 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003627 if (name_end != NULL)
3628 {
3629 emsg_severe = TRUE;
3630 EMSG(_(e_trailing));
3631 }
3632 if (!(eap->skip || error))
3633 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 break;
3635 }
3636
3637 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003638 {
3639 if (eap->cmdidx == CMD_unlet)
3640 {
3641 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3642 error = TRUE;
3643 }
3644 else
3645 {
3646 if (do_lock_var(&lv, name_end, deep,
3647 eap->cmdidx == CMD_lockvar) == FAIL)
3648 error = TRUE;
3649 }
3650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003652 if (!eap->skip)
3653 clear_lval(&lv);
3654
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 arg = skipwhite(name_end);
3656 } while (!ends_excmd(*arg));
3657
3658 eap->nextcmd = check_nextcmd(arg);
3659}
3660
Bram Moolenaar8c711452005-01-14 21:53:12 +00003661 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003662do_unlet_var(
3663 lval_T *lp,
3664 char_u *name_end,
3665 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003666{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003667 int ret = OK;
3668 int cc;
3669
3670 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003671 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003672 cc = *name_end;
3673 *name_end = NUL;
3674
3675 /* Normal name or expanded name. */
3676 if (check_changedtick(lp->ll_name))
3677 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003678 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003679 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003680 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003681 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003682 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003683 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003684 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003685 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003686 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003687 else if (lp->ll_range)
3688 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003689 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003690 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003691 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003692
3693 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3694 {
3695 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003696 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003697 return FAIL;
3698 ll_li = li;
3699 ++ll_n1;
3700 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003701
3702 /* Delete a range of List items. */
3703 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3704 {
3705 li = lp->ll_li->li_next;
3706 listitem_remove(lp->ll_list, lp->ll_li);
3707 lp->ll_li = li;
3708 ++lp->ll_n1;
3709 }
3710 }
3711 else
3712 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003713 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003714 /* unlet a List item. */
3715 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003716 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003717 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003718 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003719 }
3720
3721 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003722}
3723
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724/*
3725 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003726 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 */
3728 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003729do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730{
Bram Moolenaar33570922005-01-25 22:26:29 +00003731 hashtab_T *ht;
3732 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003733 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003734 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003735 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736
Bram Moolenaar33570922005-01-25 22:26:29 +00003737 ht = find_var_ht(name, &varname);
3738 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003740 if (ht == &globvarht)
3741 d = &globvardict;
3742 else if (current_funccal != NULL
3743 && ht == &current_funccal->l_vars.dv_hashtab)
3744 d = &current_funccal->l_vars;
3745 else if (ht == &compat_hashtab)
3746 d = &vimvardict;
3747 else
3748 {
3749 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3750 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3751 }
3752 if (d == NULL)
3753 {
3754 EMSG2(_(e_intern2), "do_unlet()");
3755 return FAIL;
3756 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003757 hi = hash_find(ht, varname);
3758 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003759 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003760 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003761 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003762 || var_check_ro(di->di_flags, name, FALSE)
3763 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003764 return FAIL;
3765
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003766 delete_var(ht, hi);
3767 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003770 if (forceit)
3771 return OK;
3772 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 return FAIL;
3774}
3775
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003776/*
3777 * Lock or unlock variable indicated by "lp".
3778 * "deep" is the levels to go (-1 for unlimited);
3779 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3780 */
3781 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003782do_lock_var(
3783 lval_T *lp,
3784 char_u *name_end,
3785 int deep,
3786 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003787{
3788 int ret = OK;
3789 int cc;
3790 dictitem_T *di;
3791
3792 if (deep == 0) /* nothing to do */
3793 return OK;
3794
3795 if (lp->ll_tv == NULL)
3796 {
3797 cc = *name_end;
3798 *name_end = NUL;
3799
3800 /* Normal name or expanded name. */
3801 if (check_changedtick(lp->ll_name))
3802 ret = FAIL;
3803 else
3804 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003805 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003806 if (di == NULL)
3807 ret = FAIL;
3808 else
3809 {
3810 if (lock)
3811 di->di_flags |= DI_FLAGS_LOCK;
3812 else
3813 di->di_flags &= ~DI_FLAGS_LOCK;
3814 item_lock(&di->di_tv, deep, lock);
3815 }
3816 }
3817 *name_end = cc;
3818 }
3819 else if (lp->ll_range)
3820 {
3821 listitem_T *li = lp->ll_li;
3822
3823 /* (un)lock a range of List items. */
3824 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3825 {
3826 item_lock(&li->li_tv, deep, lock);
3827 li = li->li_next;
3828 ++lp->ll_n1;
3829 }
3830 }
3831 else if (lp->ll_list != NULL)
3832 /* (un)lock a List item. */
3833 item_lock(&lp->ll_li->li_tv, deep, lock);
3834 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003835 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003836 item_lock(&lp->ll_di->di_tv, deep, lock);
3837
3838 return ret;
3839}
3840
3841/*
3842 * Lock or unlock an item. "deep" is nr of levels to go.
3843 */
3844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003845item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003846{
3847 static int recurse = 0;
3848 list_T *l;
3849 listitem_T *li;
3850 dict_T *d;
3851 hashitem_T *hi;
3852 int todo;
3853
3854 if (recurse >= DICT_MAXNEST)
3855 {
3856 EMSG(_("E743: variable nested too deep for (un)lock"));
3857 return;
3858 }
3859 if (deep == 0)
3860 return;
3861 ++recurse;
3862
3863 /* lock/unlock the item itself */
3864 if (lock)
3865 tv->v_lock |= VAR_LOCKED;
3866 else
3867 tv->v_lock &= ~VAR_LOCKED;
3868
3869 switch (tv->v_type)
3870 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003871 case VAR_UNKNOWN:
3872 case VAR_NUMBER:
3873 case VAR_STRING:
3874 case VAR_FUNC:
3875 case VAR_FLOAT:
3876 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003877 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003878 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003879 break;
3880
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003881 case VAR_LIST:
3882 if ((l = tv->vval.v_list) != NULL)
3883 {
3884 if (lock)
3885 l->lv_lock |= VAR_LOCKED;
3886 else
3887 l->lv_lock &= ~VAR_LOCKED;
3888 if (deep < 0 || deep > 1)
3889 /* recursive: lock/unlock the items the List contains */
3890 for (li = l->lv_first; li != NULL; li = li->li_next)
3891 item_lock(&li->li_tv, deep - 1, lock);
3892 }
3893 break;
3894 case VAR_DICT:
3895 if ((d = tv->vval.v_dict) != NULL)
3896 {
3897 if (lock)
3898 d->dv_lock |= VAR_LOCKED;
3899 else
3900 d->dv_lock &= ~VAR_LOCKED;
3901 if (deep < 0 || deep > 1)
3902 {
3903 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003904 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003905 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3906 {
3907 if (!HASHITEM_EMPTY(hi))
3908 {
3909 --todo;
3910 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3911 }
3912 }
3913 }
3914 }
3915 }
3916 --recurse;
3917}
3918
Bram Moolenaara40058a2005-07-11 22:42:07 +00003919/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003920 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3921 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003922 */
3923 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003924tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003925{
3926 return (tv->v_lock & VAR_LOCKED)
3927 || (tv->v_type == VAR_LIST
3928 && tv->vval.v_list != NULL
3929 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3930 || (tv->v_type == VAR_DICT
3931 && tv->vval.v_dict != NULL
3932 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3933}
3934
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3936/*
3937 * Delete all "menutrans_" variables.
3938 */
3939 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003940del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941{
Bram Moolenaar33570922005-01-25 22:26:29 +00003942 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003943 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944
Bram Moolenaar33570922005-01-25 22:26:29 +00003945 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003946 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003947 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003948 {
3949 if (!HASHITEM_EMPTY(hi))
3950 {
3951 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003952 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3953 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003954 }
3955 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003956 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957}
3958#endif
3959
3960#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3961
3962/*
3963 * Local string buffer for the next two functions to store a variable name
3964 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3965 * get_user_var_name().
3966 */
3967
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003968static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969
3970static char_u *varnamebuf = NULL;
3971static int varnamebuflen = 0;
3972
3973/*
3974 * Function to concatenate a prefix and a variable name.
3975 */
3976 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003977cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978{
3979 int len;
3980
3981 len = (int)STRLEN(name) + 3;
3982 if (len > varnamebuflen)
3983 {
3984 vim_free(varnamebuf);
3985 len += 10; /* some additional space */
3986 varnamebuf = alloc(len);
3987 if (varnamebuf == NULL)
3988 {
3989 varnamebuflen = 0;
3990 return NULL;
3991 }
3992 varnamebuflen = len;
3993 }
3994 *varnamebuf = prefix;
3995 varnamebuf[1] = ':';
3996 STRCPY(varnamebuf + 2, name);
3997 return varnamebuf;
3998}
3999
4000/*
4001 * Function given to ExpandGeneric() to obtain the list of user defined
4002 * (global/buffer/window/built-in) variable names.
4003 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004005get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004007 static long_u gdone;
4008 static long_u bdone;
4009 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004010#ifdef FEAT_WINDOWS
4011 static long_u tdone;
4012#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004013 static int vidx;
4014 static hashitem_T *hi;
4015 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016
4017 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004018 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004019 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004020#ifdef FEAT_WINDOWS
4021 tdone = 0;
4022#endif
4023 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004024
4025 /* Global variables */
4026 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004028 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004029 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004030 else
4031 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004032 while (HASHITEM_EMPTY(hi))
4033 ++hi;
4034 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4035 return cat_prefix_varname('g', hi->hi_key);
4036 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004038
4039 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004040 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004041 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004043 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004044 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004045 else
4046 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004047 while (HASHITEM_EMPTY(hi))
4048 ++hi;
4049 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004051 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004053 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 return (char_u *)"b:changedtick";
4055 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004056
4057 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004058 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004059 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004061 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004062 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004063 else
4064 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004065 while (HASHITEM_EMPTY(hi))
4066 ++hi;
4067 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004069
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004070#ifdef FEAT_WINDOWS
4071 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004072 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004073 if (tdone < ht->ht_used)
4074 {
4075 if (tdone++ == 0)
4076 hi = ht->ht_array;
4077 else
4078 ++hi;
4079 while (HASHITEM_EMPTY(hi))
4080 ++hi;
4081 return cat_prefix_varname('t', hi->hi_key);
4082 }
4083#endif
4084
Bram Moolenaar33570922005-01-25 22:26:29 +00004085 /* v: variables */
4086 if (vidx < VV_LEN)
4087 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088
4089 vim_free(varnamebuf);
4090 varnamebuf = NULL;
4091 varnamebuflen = 0;
4092 return NULL;
4093}
4094
4095#endif /* FEAT_CMDL_COMPL */
4096
4097/*
4098 * types for expressions.
4099 */
4100typedef enum
4101{
4102 TYPE_UNKNOWN = 0
4103 , TYPE_EQUAL /* == */
4104 , TYPE_NEQUAL /* != */
4105 , TYPE_GREATER /* > */
4106 , TYPE_GEQUAL /* >= */
4107 , TYPE_SMALLER /* < */
4108 , TYPE_SEQUAL /* <= */
4109 , TYPE_MATCH /* =~ */
4110 , TYPE_NOMATCH /* !~ */
4111} exptype_T;
4112
4113/*
4114 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004115 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4117 */
4118
4119/*
4120 * Handle zero level expression.
4121 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004122 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004123 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 * Return OK or FAIL.
4125 */
4126 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004127eval0(
4128 char_u *arg,
4129 typval_T *rettv,
4130 char_u **nextcmd,
4131 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132{
4133 int ret;
4134 char_u *p;
4135
4136 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004137 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 if (ret == FAIL || !ends_excmd(*p))
4139 {
4140 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004141 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 /*
4143 * Report the invalid expression unless the expression evaluation has
4144 * been cancelled due to an aborting error, an interrupt, or an
4145 * exception.
4146 */
4147 if (!aborting())
4148 EMSG2(_(e_invexpr2), arg);
4149 ret = FAIL;
4150 }
4151 if (nextcmd != NULL)
4152 *nextcmd = check_nextcmd(p);
4153
4154 return ret;
4155}
4156
4157/*
4158 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004159 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 *
4161 * "arg" must point to the first non-white of the expression.
4162 * "arg" is advanced to the next non-white after the recognized expression.
4163 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004164 * Note: "rettv.v_lock" is not set.
4165 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 * Return OK or FAIL.
4167 */
4168 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004169eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170{
4171 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004172 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173
4174 /*
4175 * Get the first variable.
4176 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004177 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 return FAIL;
4179
4180 if ((*arg)[0] == '?')
4181 {
4182 result = FALSE;
4183 if (evaluate)
4184 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004185 int error = FALSE;
4186
4187 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004189 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004190 if (error)
4191 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 }
4193
4194 /*
4195 * Get the second variable.
4196 */
4197 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004198 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 return FAIL;
4200
4201 /*
4202 * Check for the ":".
4203 */
4204 if ((*arg)[0] != ':')
4205 {
4206 EMSG(_("E109: Missing ':' after '?'"));
4207 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004208 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 return FAIL;
4210 }
4211
4212 /*
4213 * Get the third variable.
4214 */
4215 *arg = skipwhite(*arg + 1);
4216 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4217 {
4218 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004219 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 return FAIL;
4221 }
4222 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004223 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 }
4225
4226 return OK;
4227}
4228
4229/*
4230 * Handle first level expression:
4231 * expr2 || expr2 || expr2 logical OR
4232 *
4233 * "arg" must point to the first non-white of the expression.
4234 * "arg" is advanced to the next non-white after the recognized expression.
4235 *
4236 * Return OK or FAIL.
4237 */
4238 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004239eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240{
Bram Moolenaar33570922005-01-25 22:26:29 +00004241 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 long result;
4243 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004244 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245
4246 /*
4247 * Get the first variable.
4248 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004249 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 return FAIL;
4251
4252 /*
4253 * Repeat until there is no following "||".
4254 */
4255 first = TRUE;
4256 result = FALSE;
4257 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4258 {
4259 if (evaluate && first)
4260 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004261 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004263 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004264 if (error)
4265 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 first = FALSE;
4267 }
4268
4269 /*
4270 * Get the second variable.
4271 */
4272 *arg = skipwhite(*arg + 2);
4273 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4274 return FAIL;
4275
4276 /*
4277 * Compute the result.
4278 */
4279 if (evaluate && !result)
4280 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004281 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004283 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004284 if (error)
4285 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 }
4287 if (evaluate)
4288 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004289 rettv->v_type = VAR_NUMBER;
4290 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 }
4292 }
4293
4294 return OK;
4295}
4296
4297/*
4298 * Handle second level expression:
4299 * expr3 && expr3 && expr3 logical AND
4300 *
4301 * "arg" must point to the first non-white of the expression.
4302 * "arg" is advanced to the next non-white after the recognized expression.
4303 *
4304 * Return OK or FAIL.
4305 */
4306 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004307eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308{
Bram Moolenaar33570922005-01-25 22:26:29 +00004309 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 long result;
4311 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004312 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313
4314 /*
4315 * Get the first variable.
4316 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004317 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 return FAIL;
4319
4320 /*
4321 * Repeat until there is no following "&&".
4322 */
4323 first = TRUE;
4324 result = TRUE;
4325 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4326 {
4327 if (evaluate && first)
4328 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004329 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004331 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004332 if (error)
4333 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 first = FALSE;
4335 }
4336
4337 /*
4338 * Get the second variable.
4339 */
4340 *arg = skipwhite(*arg + 2);
4341 if (eval4(arg, &var2, evaluate && result) == FAIL)
4342 return FAIL;
4343
4344 /*
4345 * Compute the result.
4346 */
4347 if (evaluate && result)
4348 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004349 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004351 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004352 if (error)
4353 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 }
4355 if (evaluate)
4356 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004357 rettv->v_type = VAR_NUMBER;
4358 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 }
4360 }
4361
4362 return OK;
4363}
4364
4365/*
4366 * Handle third level expression:
4367 * var1 == var2
4368 * var1 =~ var2
4369 * var1 != var2
4370 * var1 !~ var2
4371 * var1 > var2
4372 * var1 >= var2
4373 * var1 < var2
4374 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004375 * var1 is var2
4376 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 *
4378 * "arg" must point to the first non-white of the expression.
4379 * "arg" is advanced to the next non-white after the recognized expression.
4380 *
4381 * Return OK or FAIL.
4382 */
4383 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004384eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385{
Bram Moolenaar33570922005-01-25 22:26:29 +00004386 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 char_u *p;
4388 int i;
4389 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004390 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 int len = 2;
4392 long n1, n2;
4393 char_u *s1, *s2;
4394 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4395 regmatch_T regmatch;
4396 int ic;
4397 char_u *save_cpo;
4398
4399 /*
4400 * Get the first variable.
4401 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004402 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 return FAIL;
4404
4405 p = *arg;
4406 switch (p[0])
4407 {
4408 case '=': if (p[1] == '=')
4409 type = TYPE_EQUAL;
4410 else if (p[1] == '~')
4411 type = TYPE_MATCH;
4412 break;
4413 case '!': if (p[1] == '=')
4414 type = TYPE_NEQUAL;
4415 else if (p[1] == '~')
4416 type = TYPE_NOMATCH;
4417 break;
4418 case '>': if (p[1] != '=')
4419 {
4420 type = TYPE_GREATER;
4421 len = 1;
4422 }
4423 else
4424 type = TYPE_GEQUAL;
4425 break;
4426 case '<': if (p[1] != '=')
4427 {
4428 type = TYPE_SMALLER;
4429 len = 1;
4430 }
4431 else
4432 type = TYPE_SEQUAL;
4433 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004434 case 'i': if (p[1] == 's')
4435 {
4436 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4437 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004438 i = p[len];
4439 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004440 {
4441 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4442 type_is = TRUE;
4443 }
4444 }
4445 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 }
4447
4448 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004449 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 */
4451 if (type != TYPE_UNKNOWN)
4452 {
4453 /* extra question mark appended: ignore case */
4454 if (p[len] == '?')
4455 {
4456 ic = TRUE;
4457 ++len;
4458 }
4459 /* extra '#' appended: match case */
4460 else if (p[len] == '#')
4461 {
4462 ic = FALSE;
4463 ++len;
4464 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004465 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 else
4467 ic = p_ic;
4468
4469 /*
4470 * Get the second variable.
4471 */
4472 *arg = skipwhite(p + len);
4473 if (eval5(arg, &var2, evaluate) == FAIL)
4474 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004475 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 return FAIL;
4477 }
4478
4479 if (evaluate)
4480 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004481 if (type_is && rettv->v_type != var2.v_type)
4482 {
4483 /* For "is" a different type always means FALSE, for "notis"
4484 * it means TRUE. */
4485 n1 = (type == TYPE_NEQUAL);
4486 }
4487 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4488 {
4489 if (type_is)
4490 {
4491 n1 = (rettv->v_type == var2.v_type
4492 && rettv->vval.v_list == var2.vval.v_list);
4493 if (type == TYPE_NEQUAL)
4494 n1 = !n1;
4495 }
4496 else if (rettv->v_type != var2.v_type
4497 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4498 {
4499 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004500 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004501 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004502 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004503 clear_tv(rettv);
4504 clear_tv(&var2);
4505 return FAIL;
4506 }
4507 else
4508 {
4509 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004510 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4511 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004512 if (type == TYPE_NEQUAL)
4513 n1 = !n1;
4514 }
4515 }
4516
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004517 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4518 {
4519 if (type_is)
4520 {
4521 n1 = (rettv->v_type == var2.v_type
4522 && rettv->vval.v_dict == var2.vval.v_dict);
4523 if (type == TYPE_NEQUAL)
4524 n1 = !n1;
4525 }
4526 else if (rettv->v_type != var2.v_type
4527 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4528 {
4529 if (rettv->v_type != var2.v_type)
4530 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4531 else
4532 EMSG(_("E736: Invalid operation for Dictionary"));
4533 clear_tv(rettv);
4534 clear_tv(&var2);
4535 return FAIL;
4536 }
4537 else
4538 {
4539 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004540 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4541 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004542 if (type == TYPE_NEQUAL)
4543 n1 = !n1;
4544 }
4545 }
4546
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004547 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4548 {
4549 if (rettv->v_type != var2.v_type
4550 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4551 {
4552 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004553 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004554 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004555 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004556 clear_tv(rettv);
4557 clear_tv(&var2);
4558 return FAIL;
4559 }
4560 else
4561 {
4562 /* Compare two Funcrefs for being equal or unequal. */
4563 if (rettv->vval.v_string == NULL
4564 || var2.vval.v_string == NULL)
4565 n1 = FALSE;
4566 else
4567 n1 = STRCMP(rettv->vval.v_string,
4568 var2.vval.v_string) == 0;
4569 if (type == TYPE_NEQUAL)
4570 n1 = !n1;
4571 }
4572 }
4573
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004574#ifdef FEAT_FLOAT
4575 /*
4576 * If one of the two variables is a float, compare as a float.
4577 * When using "=~" or "!~", always compare as string.
4578 */
4579 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4580 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4581 {
4582 float_T f1, f2;
4583
4584 if (rettv->v_type == VAR_FLOAT)
4585 f1 = rettv->vval.v_float;
4586 else
4587 f1 = get_tv_number(rettv);
4588 if (var2.v_type == VAR_FLOAT)
4589 f2 = var2.vval.v_float;
4590 else
4591 f2 = get_tv_number(&var2);
4592 n1 = FALSE;
4593 switch (type)
4594 {
4595 case TYPE_EQUAL: n1 = (f1 == f2); break;
4596 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4597 case TYPE_GREATER: n1 = (f1 > f2); break;
4598 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4599 case TYPE_SMALLER: n1 = (f1 < f2); break;
4600 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4601 case TYPE_UNKNOWN:
4602 case TYPE_MATCH:
4603 case TYPE_NOMATCH: break; /* avoid gcc warning */
4604 }
4605 }
4606#endif
4607
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 /*
4609 * If one of the two variables is a number, compare as a number.
4610 * When using "=~" or "!~", always compare as string.
4611 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004612 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4614 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004615 n1 = get_tv_number(rettv);
4616 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 switch (type)
4618 {
4619 case TYPE_EQUAL: n1 = (n1 == n2); break;
4620 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4621 case TYPE_GREATER: n1 = (n1 > n2); break;
4622 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4623 case TYPE_SMALLER: n1 = (n1 < n2); break;
4624 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4625 case TYPE_UNKNOWN:
4626 case TYPE_MATCH:
4627 case TYPE_NOMATCH: break; /* avoid gcc warning */
4628 }
4629 }
4630 else
4631 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004632 s1 = get_tv_string_buf(rettv, buf1);
4633 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4635 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4636 else
4637 i = 0;
4638 n1 = FALSE;
4639 switch (type)
4640 {
4641 case TYPE_EQUAL: n1 = (i == 0); break;
4642 case TYPE_NEQUAL: n1 = (i != 0); break;
4643 case TYPE_GREATER: n1 = (i > 0); break;
4644 case TYPE_GEQUAL: n1 = (i >= 0); break;
4645 case TYPE_SMALLER: n1 = (i < 0); break;
4646 case TYPE_SEQUAL: n1 = (i <= 0); break;
4647
4648 case TYPE_MATCH:
4649 case TYPE_NOMATCH:
4650 /* avoid 'l' flag in 'cpoptions' */
4651 save_cpo = p_cpo;
4652 p_cpo = (char_u *)"";
4653 regmatch.regprog = vim_regcomp(s2,
4654 RE_MAGIC + RE_STRING);
4655 regmatch.rm_ic = ic;
4656 if (regmatch.regprog != NULL)
4657 {
4658 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004659 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 if (type == TYPE_NOMATCH)
4661 n1 = !n1;
4662 }
4663 p_cpo = save_cpo;
4664 break;
4665
4666 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4667 }
4668 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004669 clear_tv(rettv);
4670 clear_tv(&var2);
4671 rettv->v_type = VAR_NUMBER;
4672 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 }
4674 }
4675
4676 return OK;
4677}
4678
4679/*
4680 * Handle fourth level expression:
4681 * + number addition
4682 * - number subtraction
4683 * . string concatenation
4684 *
4685 * "arg" must point to the first non-white of the expression.
4686 * "arg" is advanced to the next non-white after the recognized expression.
4687 *
4688 * Return OK or FAIL.
4689 */
4690 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004691eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692{
Bram Moolenaar33570922005-01-25 22:26:29 +00004693 typval_T var2;
4694 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 int op;
4696 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004697#ifdef FEAT_FLOAT
4698 float_T f1 = 0, f2 = 0;
4699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 char_u *s1, *s2;
4701 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4702 char_u *p;
4703
4704 /*
4705 * Get the first variable.
4706 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004707 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 return FAIL;
4709
4710 /*
4711 * Repeat computing, until no '+', '-' or '.' is following.
4712 */
4713 for (;;)
4714 {
4715 op = **arg;
4716 if (op != '+' && op != '-' && op != '.')
4717 break;
4718
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004719 if ((op != '+' || rettv->v_type != VAR_LIST)
4720#ifdef FEAT_FLOAT
4721 && (op == '.' || rettv->v_type != VAR_FLOAT)
4722#endif
4723 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004724 {
4725 /* For "list + ...", an illegal use of the first operand as
4726 * a number cannot be determined before evaluating the 2nd
4727 * operand: if this is also a list, all is ok.
4728 * For "something . ...", "something - ..." or "non-list + ...",
4729 * we know that the first operand needs to be a string or number
4730 * without evaluating the 2nd operand. So check before to avoid
4731 * side effects after an error. */
4732 if (evaluate && get_tv_string_chk(rettv) == NULL)
4733 {
4734 clear_tv(rettv);
4735 return FAIL;
4736 }
4737 }
4738
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 /*
4740 * Get the second variable.
4741 */
4742 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004743 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004745 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 return FAIL;
4747 }
4748
4749 if (evaluate)
4750 {
4751 /*
4752 * Compute the result.
4753 */
4754 if (op == '.')
4755 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004756 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4757 s2 = get_tv_string_buf_chk(&var2, buf2);
4758 if (s2 == NULL) /* type error ? */
4759 {
4760 clear_tv(rettv);
4761 clear_tv(&var2);
4762 return FAIL;
4763 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004764 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004765 clear_tv(rettv);
4766 rettv->v_type = VAR_STRING;
4767 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004769 else if (op == '+' && rettv->v_type == VAR_LIST
4770 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004771 {
4772 /* concatenate Lists */
4773 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4774 &var3) == FAIL)
4775 {
4776 clear_tv(rettv);
4777 clear_tv(&var2);
4778 return FAIL;
4779 }
4780 clear_tv(rettv);
4781 *rettv = var3;
4782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 else
4784 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004785 int error = FALSE;
4786
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004787#ifdef FEAT_FLOAT
4788 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004789 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004790 f1 = rettv->vval.v_float;
4791 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004792 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004793 else
4794#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004795 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004796 n1 = get_tv_number_chk(rettv, &error);
4797 if (error)
4798 {
4799 /* This can only happen for "list + non-list". For
4800 * "non-list + ..." or "something - ...", we returned
4801 * before evaluating the 2nd operand. */
4802 clear_tv(rettv);
4803 return FAIL;
4804 }
4805#ifdef FEAT_FLOAT
4806 if (var2.v_type == VAR_FLOAT)
4807 f1 = n1;
4808#endif
4809 }
4810#ifdef FEAT_FLOAT
4811 if (var2.v_type == VAR_FLOAT)
4812 {
4813 f2 = var2.vval.v_float;
4814 n2 = 0;
4815 }
4816 else
4817#endif
4818 {
4819 n2 = get_tv_number_chk(&var2, &error);
4820 if (error)
4821 {
4822 clear_tv(rettv);
4823 clear_tv(&var2);
4824 return FAIL;
4825 }
4826#ifdef FEAT_FLOAT
4827 if (rettv->v_type == VAR_FLOAT)
4828 f2 = n2;
4829#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004830 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004831 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004832
4833#ifdef FEAT_FLOAT
4834 /* If there is a float on either side the result is a float. */
4835 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4836 {
4837 if (op == '+')
4838 f1 = f1 + f2;
4839 else
4840 f1 = f1 - f2;
4841 rettv->v_type = VAR_FLOAT;
4842 rettv->vval.v_float = f1;
4843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004845#endif
4846 {
4847 if (op == '+')
4848 n1 = n1 + n2;
4849 else
4850 n1 = n1 - n2;
4851 rettv->v_type = VAR_NUMBER;
4852 rettv->vval.v_number = n1;
4853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004855 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 }
4857 }
4858 return OK;
4859}
4860
4861/*
4862 * Handle fifth level expression:
4863 * * number multiplication
4864 * / number division
4865 * % number modulo
4866 *
4867 * "arg" must point to the first non-white of the expression.
4868 * "arg" is advanced to the next non-white after the recognized expression.
4869 *
4870 * Return OK or FAIL.
4871 */
4872 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004873eval6(
4874 char_u **arg,
4875 typval_T *rettv,
4876 int evaluate,
4877 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878{
Bram Moolenaar33570922005-01-25 22:26:29 +00004879 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 int op;
4881 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004882#ifdef FEAT_FLOAT
4883 int use_float = FALSE;
4884 float_T f1 = 0, f2;
4885#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004886 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887
4888 /*
4889 * Get the first variable.
4890 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004891 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 return FAIL;
4893
4894 /*
4895 * Repeat computing, until no '*', '/' or '%' is following.
4896 */
4897 for (;;)
4898 {
4899 op = **arg;
4900 if (op != '*' && op != '/' && op != '%')
4901 break;
4902
4903 if (evaluate)
4904 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004905#ifdef FEAT_FLOAT
4906 if (rettv->v_type == VAR_FLOAT)
4907 {
4908 f1 = rettv->vval.v_float;
4909 use_float = TRUE;
4910 n1 = 0;
4911 }
4912 else
4913#endif
4914 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004915 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004916 if (error)
4917 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 }
4919 else
4920 n1 = 0;
4921
4922 /*
4923 * Get the second variable.
4924 */
4925 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004926 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 return FAIL;
4928
4929 if (evaluate)
4930 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004931#ifdef FEAT_FLOAT
4932 if (var2.v_type == VAR_FLOAT)
4933 {
4934 if (!use_float)
4935 {
4936 f1 = n1;
4937 use_float = TRUE;
4938 }
4939 f2 = var2.vval.v_float;
4940 n2 = 0;
4941 }
4942 else
4943#endif
4944 {
4945 n2 = get_tv_number_chk(&var2, &error);
4946 clear_tv(&var2);
4947 if (error)
4948 return FAIL;
4949#ifdef FEAT_FLOAT
4950 if (use_float)
4951 f2 = n2;
4952#endif
4953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954
4955 /*
4956 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004957 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004959#ifdef FEAT_FLOAT
4960 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004962 if (op == '*')
4963 f1 = f1 * f2;
4964 else if (op == '/')
4965 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004966# ifdef VMS
4967 /* VMS crashes on divide by zero, work around it */
4968 if (f2 == 0.0)
4969 {
4970 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004971 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004972 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004973 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004974 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004975 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004976 }
4977 else
4978 f1 = f1 / f2;
4979# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004980 /* We rely on the floating point library to handle divide
4981 * by zero to result in "inf" and not a crash. */
4982 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004983# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004986 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004987 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004988 return FAIL;
4989 }
4990 rettv->v_type = VAR_FLOAT;
4991 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 }
4993 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004996 if (op == '*')
4997 n1 = n1 * n2;
4998 else if (op == '/')
4999 {
5000 if (n2 == 0) /* give an error message? */
5001 {
5002 if (n1 == 0)
5003 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5004 else if (n1 < 0)
5005 n1 = -0x7fffffffL;
5006 else
5007 n1 = 0x7fffffffL;
5008 }
5009 else
5010 n1 = n1 / n2;
5011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005013 {
5014 if (n2 == 0) /* give an error message? */
5015 n1 = 0;
5016 else
5017 n1 = n1 % n2;
5018 }
5019 rettv->v_type = VAR_NUMBER;
5020 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 }
5023 }
5024
5025 return OK;
5026}
5027
5028/*
5029 * Handle sixth level expression:
5030 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005031 * "string" string constant
5032 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 * &option-name option value
5034 * @r register contents
5035 * identifier variable value
5036 * function() function call
5037 * $VAR environment variable
5038 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005039 * [expr, expr] List
5040 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 *
5042 * Also handle:
5043 * ! in front logical NOT
5044 * - in front unary minus
5045 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005046 * trailing [] subscript in String or List
5047 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 *
5049 * "arg" must point to the first non-white of the expression.
5050 * "arg" is advanced to the next non-white after the recognized expression.
5051 *
5052 * Return OK or FAIL.
5053 */
5054 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005055eval7(
5056 char_u **arg,
5057 typval_T *rettv,
5058 int evaluate,
5059 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 long n;
5062 int len;
5063 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 char_u *start_leader, *end_leader;
5065 int ret = OK;
5066 char_u *alias;
5067
5068 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005069 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005070 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005072 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073
5074 /*
5075 * Skip '!' and '-' characters. They are handled later.
5076 */
5077 start_leader = *arg;
5078 while (**arg == '!' || **arg == '-' || **arg == '+')
5079 *arg = skipwhite(*arg + 1);
5080 end_leader = *arg;
5081
5082 switch (**arg)
5083 {
5084 /*
5085 * Number constant.
5086 */
5087 case '0':
5088 case '1':
5089 case '2':
5090 case '3':
5091 case '4':
5092 case '5':
5093 case '6':
5094 case '7':
5095 case '8':
5096 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005097 {
5098#ifdef FEAT_FLOAT
5099 char_u *p = skipdigits(*arg + 1);
5100 int get_float = FALSE;
5101
5102 /* We accept a float when the format matches
5103 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005104 * strict to avoid backwards compatibility problems.
5105 * Don't look for a float after the "." operator, so that
5106 * ":let vers = 1.2.3" doesn't fail. */
5107 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005109 get_float = TRUE;
5110 p = skipdigits(p + 2);
5111 if (*p == 'e' || *p == 'E')
5112 {
5113 ++p;
5114 if (*p == '-' || *p == '+')
5115 ++p;
5116 if (!vim_isdigit(*p))
5117 get_float = FALSE;
5118 else
5119 p = skipdigits(p + 1);
5120 }
5121 if (ASCII_ISALPHA(*p) || *p == '.')
5122 get_float = FALSE;
5123 }
5124 if (get_float)
5125 {
5126 float_T f;
5127
5128 *arg += string2float(*arg, &f);
5129 if (evaluate)
5130 {
5131 rettv->v_type = VAR_FLOAT;
5132 rettv->vval.v_float = f;
5133 }
5134 }
5135 else
5136#endif
5137 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005138 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005139 *arg += len;
5140 if (evaluate)
5141 {
5142 rettv->v_type = VAR_NUMBER;
5143 rettv->vval.v_number = n;
5144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 }
5146 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148
5149 /*
5150 * String constant: "string".
5151 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005152 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 break;
5154
5155 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005156 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005158 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005159 break;
5160
5161 /*
5162 * List: [expr, expr]
5163 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005164 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 break;
5166
5167 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005168 * Dictionary: {key: val, key: val}
5169 */
5170 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5171 break;
5172
5173 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005174 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005176 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 break;
5178
5179 /*
5180 * Environment variable: $VAR.
5181 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005182 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 break;
5184
5185 /*
5186 * Register contents: @r.
5187 */
5188 case '@': ++*arg;
5189 if (evaluate)
5190 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005191 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005192 rettv->vval.v_string = get_reg_contents(**arg,
5193 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 }
5195 if (**arg != NUL)
5196 ++*arg;
5197 break;
5198
5199 /*
5200 * nested expression: (expression).
5201 */
5202 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005203 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 if (**arg == ')')
5205 ++*arg;
5206 else if (ret == OK)
5207 {
5208 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005209 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 ret = FAIL;
5211 }
5212 break;
5213
Bram Moolenaar8c711452005-01-14 21:53:12 +00005214 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 break;
5216 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005217
5218 if (ret == NOTDONE)
5219 {
5220 /*
5221 * Must be a variable or function name.
5222 * Can also be a curly-braces kind of name: {expr}.
5223 */
5224 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005225 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005226 if (alias != NULL)
5227 s = alias;
5228
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005229 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005230 ret = FAIL;
5231 else
5232 {
5233 if (**arg == '(') /* recursive! */
5234 {
5235 /* If "s" is the name of a variable of type VAR_FUNC
5236 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005237 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005238
5239 /* Invoke the function. */
5240 ret = get_func_tv(s, len, rettv, arg,
5241 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005242 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005243
5244 /* If evaluate is FALSE rettv->v_type was not set in
5245 * get_func_tv, but it's needed in handle_subscript() to parse
5246 * what follows. So set it here. */
5247 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5248 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005249 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005250 rettv->v_type = VAR_FUNC;
5251 }
5252
Bram Moolenaar8c711452005-01-14 21:53:12 +00005253 /* Stop the expression evaluation when immediately
5254 * aborting on error, or when an interrupt occurred or
5255 * an exception was thrown but not caught. */
5256 if (aborting())
5257 {
5258 if (ret == OK)
5259 clear_tv(rettv);
5260 ret = FAIL;
5261 }
5262 }
5263 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005264 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005265 else
5266 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005267 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005268 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005269 }
5270
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 *arg = skipwhite(*arg);
5272
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005273 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5274 * expr(expr). */
5275 if (ret == OK)
5276 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277
5278 /*
5279 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5280 */
5281 if (ret == OK && evaluate && end_leader > start_leader)
5282 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005283 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005284 int val = 0;
5285#ifdef FEAT_FLOAT
5286 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005287
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005288 if (rettv->v_type == VAR_FLOAT)
5289 f = rettv->vval.v_float;
5290 else
5291#endif
5292 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005293 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005295 clear_tv(rettv);
5296 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005298 else
5299 {
5300 while (end_leader > start_leader)
5301 {
5302 --end_leader;
5303 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005304 {
5305#ifdef FEAT_FLOAT
5306 if (rettv->v_type == VAR_FLOAT)
5307 f = !f;
5308 else
5309#endif
5310 val = !val;
5311 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005312 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005313 {
5314#ifdef FEAT_FLOAT
5315 if (rettv->v_type == VAR_FLOAT)
5316 f = -f;
5317 else
5318#endif
5319 val = -val;
5320 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005321 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005322#ifdef FEAT_FLOAT
5323 if (rettv->v_type == VAR_FLOAT)
5324 {
5325 clear_tv(rettv);
5326 rettv->vval.v_float = f;
5327 }
5328 else
5329#endif
5330 {
5331 clear_tv(rettv);
5332 rettv->v_type = VAR_NUMBER;
5333 rettv->vval.v_number = val;
5334 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 }
5337
5338 return ret;
5339}
5340
5341/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005342 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5343 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005344 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5345 */
5346 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005347eval_index(
5348 char_u **arg,
5349 typval_T *rettv,
5350 int evaluate,
5351 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005352{
5353 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005354 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005355 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005356 long len = -1;
5357 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005358 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005359 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005360
Bram Moolenaara03f2332016-02-06 18:09:59 +01005361 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005362 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005363 case VAR_FUNC:
5364 if (verbose)
5365 EMSG(_("E695: Cannot index a Funcref"));
5366 return FAIL;
5367 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005368#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005369 if (verbose)
5370 EMSG(_(e_float_as_string));
5371 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005372#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005373 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005374 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005375 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005376 if (verbose)
5377 EMSG(_("E909: Cannot index a special variable"));
5378 return FAIL;
5379 case VAR_UNKNOWN:
5380 if (evaluate)
5381 return FAIL;
5382 /* FALLTHROUGH */
5383
5384 case VAR_STRING:
5385 case VAR_NUMBER:
5386 case VAR_LIST:
5387 case VAR_DICT:
5388 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005389 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005390
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005391 init_tv(&var1);
5392 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005393 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005394 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005395 /*
5396 * dict.name
5397 */
5398 key = *arg + 1;
5399 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5400 ;
5401 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005402 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005403 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005404 }
5405 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005406 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005407 /*
5408 * something[idx]
5409 *
5410 * Get the (first) variable from inside the [].
5411 */
5412 *arg = skipwhite(*arg + 1);
5413 if (**arg == ':')
5414 empty1 = TRUE;
5415 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5416 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005417 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5418 {
5419 /* not a number or string */
5420 clear_tv(&var1);
5421 return FAIL;
5422 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005423
5424 /*
5425 * Get the second variable from inside the [:].
5426 */
5427 if (**arg == ':')
5428 {
5429 range = TRUE;
5430 *arg = skipwhite(*arg + 1);
5431 if (**arg == ']')
5432 empty2 = TRUE;
5433 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5434 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005435 if (!empty1)
5436 clear_tv(&var1);
5437 return FAIL;
5438 }
5439 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5440 {
5441 /* not a number or string */
5442 if (!empty1)
5443 clear_tv(&var1);
5444 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005445 return FAIL;
5446 }
5447 }
5448
5449 /* Check for the ']'. */
5450 if (**arg != ']')
5451 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005452 if (verbose)
5453 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005454 clear_tv(&var1);
5455 if (range)
5456 clear_tv(&var2);
5457 return FAIL;
5458 }
5459 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005460 }
5461
5462 if (evaluate)
5463 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005464 n1 = 0;
5465 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005466 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005467 n1 = get_tv_number(&var1);
5468 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005469 }
5470 if (range)
5471 {
5472 if (empty2)
5473 n2 = -1;
5474 else
5475 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005476 n2 = get_tv_number(&var2);
5477 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005478 }
5479 }
5480
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005481 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005482 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005483 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005484 case VAR_FUNC:
5485 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005486 case VAR_SPECIAL:
5487 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005488 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005489 break; /* not evaluating, skipping over subscript */
5490
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005491 case VAR_NUMBER:
5492 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005493 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005494 len = (long)STRLEN(s);
5495 if (range)
5496 {
5497 /* The resulting variable is a substring. If the indexes
5498 * are out of range the result is empty. */
5499 if (n1 < 0)
5500 {
5501 n1 = len + n1;
5502 if (n1 < 0)
5503 n1 = 0;
5504 }
5505 if (n2 < 0)
5506 n2 = len + n2;
5507 else if (n2 >= len)
5508 n2 = len;
5509 if (n1 >= len || n2 < 0 || n1 > n2)
5510 s = NULL;
5511 else
5512 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5513 }
5514 else
5515 {
5516 /* The resulting variable is a string of a single
5517 * character. If the index is too big or negative the
5518 * result is empty. */
5519 if (n1 >= len || n1 < 0)
5520 s = NULL;
5521 else
5522 s = vim_strnsave(s + n1, 1);
5523 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005524 clear_tv(rettv);
5525 rettv->v_type = VAR_STRING;
5526 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005527 break;
5528
5529 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005530 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005531 if (n1 < 0)
5532 n1 = len + n1;
5533 if (!empty1 && (n1 < 0 || n1 >= len))
5534 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005535 /* For a range we allow invalid values and return an empty
5536 * list. A list index out of range is an error. */
5537 if (!range)
5538 {
5539 if (verbose)
5540 EMSGN(_(e_listidx), n1);
5541 return FAIL;
5542 }
5543 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005544 }
5545 if (range)
5546 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005547 list_T *l;
5548 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005549
5550 if (n2 < 0)
5551 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005552 else if (n2 >= len)
5553 n2 = len - 1;
5554 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005555 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005556 l = list_alloc();
5557 if (l == NULL)
5558 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005559 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005560 n1 <= n2; ++n1)
5561 {
5562 if (list_append_tv(l, &item->li_tv) == FAIL)
5563 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005564 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005565 return FAIL;
5566 }
5567 item = item->li_next;
5568 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005569 clear_tv(rettv);
5570 rettv->v_type = VAR_LIST;
5571 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005572 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005573 }
5574 else
5575 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005576 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005577 clear_tv(rettv);
5578 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005579 }
5580 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005581
5582 case VAR_DICT:
5583 if (range)
5584 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005585 if (verbose)
5586 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005587 if (len == -1)
5588 clear_tv(&var1);
5589 return FAIL;
5590 }
5591 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005592 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005593
5594 if (len == -1)
5595 {
5596 key = get_tv_string(&var1);
5597 if (*key == NUL)
5598 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005599 if (verbose)
5600 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005601 clear_tv(&var1);
5602 return FAIL;
5603 }
5604 }
5605
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005606 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005607
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005608 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005609 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005610 if (len == -1)
5611 clear_tv(&var1);
5612 if (item == NULL)
5613 return FAIL;
5614
5615 copy_tv(&item->di_tv, &var1);
5616 clear_tv(rettv);
5617 *rettv = var1;
5618 }
5619 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005620 }
5621 }
5622
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005623 return OK;
5624}
5625
5626/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 * Get an option value.
5628 * "arg" points to the '&' or '+' before the option name.
5629 * "arg" is advanced to character after the option name.
5630 * Return OK or FAIL.
5631 */
5632 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005633get_option_tv(
5634 char_u **arg,
5635 typval_T *rettv, /* when NULL, only check if option exists */
5636 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637{
5638 char_u *option_end;
5639 long numval;
5640 char_u *stringval;
5641 int opt_type;
5642 int c;
5643 int working = (**arg == '+'); /* has("+option") */
5644 int ret = OK;
5645 int opt_flags;
5646
5647 /*
5648 * Isolate the option name and find its value.
5649 */
5650 option_end = find_option_end(arg, &opt_flags);
5651 if (option_end == NULL)
5652 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005653 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 EMSG2(_("E112: Option name missing: %s"), *arg);
5655 return FAIL;
5656 }
5657
5658 if (!evaluate)
5659 {
5660 *arg = option_end;
5661 return OK;
5662 }
5663
5664 c = *option_end;
5665 *option_end = NUL;
5666 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005667 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668
5669 if (opt_type == -3) /* invalid name */
5670 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005671 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 EMSG2(_("E113: Unknown option: %s"), *arg);
5673 ret = FAIL;
5674 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005675 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 {
5677 if (opt_type == -2) /* hidden string option */
5678 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005679 rettv->v_type = VAR_STRING;
5680 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 }
5682 else if (opt_type == -1) /* hidden number option */
5683 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005684 rettv->v_type = VAR_NUMBER;
5685 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 }
5687 else if (opt_type == 1) /* number option */
5688 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005689 rettv->v_type = VAR_NUMBER;
5690 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 }
5692 else /* string option */
5693 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005694 rettv->v_type = VAR_STRING;
5695 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 }
5697 }
5698 else if (working && (opt_type == -2 || opt_type == -1))
5699 ret = FAIL;
5700
5701 *option_end = c; /* put back for error messages */
5702 *arg = option_end;
5703
5704 return ret;
5705}
5706
5707/*
5708 * Allocate a variable for a string constant.
5709 * Return OK or FAIL.
5710 */
5711 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005712get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713{
5714 char_u *p;
5715 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 int extra = 0;
5717
5718 /*
5719 * Find the end of the string, skipping backslashed characters.
5720 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005721 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 {
5723 if (*p == '\\' && p[1] != NUL)
5724 {
5725 ++p;
5726 /* A "\<x>" form occupies at least 4 characters, and produces up
5727 * to 6 characters: reserve space for 2 extra */
5728 if (*p == '<')
5729 extra += 2;
5730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 }
5732
5733 if (*p != '"')
5734 {
5735 EMSG2(_("E114: Missing quote: %s"), *arg);
5736 return FAIL;
5737 }
5738
5739 /* If only parsing, set *arg and return here */
5740 if (!evaluate)
5741 {
5742 *arg = p + 1;
5743 return OK;
5744 }
5745
5746 /*
5747 * Copy the string into allocated memory, handling backslashed
5748 * characters.
5749 */
5750 name = alloc((unsigned)(p - *arg + extra));
5751 if (name == NULL)
5752 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005753 rettv->v_type = VAR_STRING;
5754 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755
Bram Moolenaar8c711452005-01-14 21:53:12 +00005756 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 {
5758 if (*p == '\\')
5759 {
5760 switch (*++p)
5761 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005762 case 'b': *name++ = BS; ++p; break;
5763 case 'e': *name++ = ESC; ++p; break;
5764 case 'f': *name++ = FF; ++p; break;
5765 case 'n': *name++ = NL; ++p; break;
5766 case 'r': *name++ = CAR; ++p; break;
5767 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768
5769 case 'X': /* hex: "\x1", "\x12" */
5770 case 'x':
5771 case 'u': /* Unicode: "\u0023" */
5772 case 'U':
5773 if (vim_isxdigit(p[1]))
5774 {
5775 int n, nr;
5776 int c = toupper(*p);
5777
5778 if (c == 'X')
5779 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005780 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005782 else
5783 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 nr = 0;
5785 while (--n >= 0 && vim_isxdigit(p[1]))
5786 {
5787 ++p;
5788 nr = (nr << 4) + hex2nr(*p);
5789 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005790 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791#ifdef FEAT_MBYTE
5792 /* For "\u" store the number according to
5793 * 'encoding'. */
5794 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005795 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 else
5797#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005798 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 break;
5801
5802 /* octal: "\1", "\12", "\123" */
5803 case '0':
5804 case '1':
5805 case '2':
5806 case '3':
5807 case '4':
5808 case '5':
5809 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005810 case '7': *name = *p++ - '0';
5811 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005813 *name = (*name << 3) + *p++ - '0';
5814 if (*p >= '0' && *p <= '7')
5815 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005817 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 break;
5819
5820 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005821 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 if (extra != 0)
5823 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005824 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 break;
5826 }
5827 /* FALLTHROUGH */
5828
Bram Moolenaar8c711452005-01-14 21:53:12 +00005829 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 break;
5831 }
5832 }
5833 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005834 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005837 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 *arg = p + 1;
5839
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 return OK;
5841}
5842
5843/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005844 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 * Return OK or FAIL.
5846 */
5847 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005848get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849{
5850 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005851 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005852 int reduce = 0;
5853
5854 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005855 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005856 */
5857 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5858 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005859 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005860 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005861 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005862 break;
5863 ++reduce;
5864 ++p;
5865 }
5866 }
5867
Bram Moolenaar8c711452005-01-14 21:53:12 +00005868 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005869 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005870 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005871 return FAIL;
5872 }
5873
Bram Moolenaar8c711452005-01-14 21:53:12 +00005874 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005875 if (!evaluate)
5876 {
5877 *arg = p + 1;
5878 return OK;
5879 }
5880
5881 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005882 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005883 */
5884 str = alloc((unsigned)((p - *arg) - reduce));
5885 if (str == NULL)
5886 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005887 rettv->v_type = VAR_STRING;
5888 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005889
Bram Moolenaar8c711452005-01-14 21:53:12 +00005890 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005891 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005892 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005893 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005894 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005895 break;
5896 ++p;
5897 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005898 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005899 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005900 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005901 *arg = p + 1;
5902
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005903 return OK;
5904}
5905
5906/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005907 * Allocate a variable for a List and fill it from "*arg".
5908 * Return OK or FAIL.
5909 */
5910 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005911get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005912{
Bram Moolenaar33570922005-01-25 22:26:29 +00005913 list_T *l = NULL;
5914 typval_T tv;
5915 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005916
5917 if (evaluate)
5918 {
5919 l = list_alloc();
5920 if (l == NULL)
5921 return FAIL;
5922 }
5923
5924 *arg = skipwhite(*arg + 1);
5925 while (**arg != ']' && **arg != NUL)
5926 {
5927 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5928 goto failret;
5929 if (evaluate)
5930 {
5931 item = listitem_alloc();
5932 if (item != NULL)
5933 {
5934 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005935 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005936 list_append(l, item);
5937 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005938 else
5939 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005940 }
5941
5942 if (**arg == ']')
5943 break;
5944 if (**arg != ',')
5945 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005946 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005947 goto failret;
5948 }
5949 *arg = skipwhite(*arg + 1);
5950 }
5951
5952 if (**arg != ']')
5953 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005954 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005955failret:
5956 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005957 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005958 return FAIL;
5959 }
5960
5961 *arg = skipwhite(*arg + 1);
5962 if (evaluate)
5963 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005964 rettv->v_type = VAR_LIST;
5965 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005966 ++l->lv_refcount;
5967 }
5968
5969 return OK;
5970}
5971
5972/*
5973 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005974 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005975 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005976 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005977list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005978{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005979 list_T *l;
5980
5981 l = (list_T *)alloc_clear(sizeof(list_T));
5982 if (l != NULL)
5983 {
5984 /* Prepend the list to the list of lists for garbage collection. */
5985 if (first_list != NULL)
5986 first_list->lv_used_prev = l;
5987 l->lv_used_prev = NULL;
5988 l->lv_used_next = first_list;
5989 first_list = l;
5990 }
5991 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005992}
5993
5994/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005995 * Allocate an empty list for a return value.
5996 * Returns OK or FAIL.
5997 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005998 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005999rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006000{
6001 list_T *l = list_alloc();
6002
6003 if (l == NULL)
6004 return FAIL;
6005
6006 rettv->vval.v_list = l;
6007 rettv->v_type = VAR_LIST;
6008 ++l->lv_refcount;
6009 return OK;
6010}
6011
6012/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006013 * Unreference a list: decrement the reference count and free it when it
6014 * becomes zero.
6015 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006016 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006017list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006018{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006019 if (l != NULL && --l->lv_refcount <= 0)
6020 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006021}
6022
6023/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006024 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006025 * Ignores the reference count.
6026 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006027 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006028list_free(
6029 list_T *l,
6030 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006031{
Bram Moolenaar33570922005-01-25 22:26:29 +00006032 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006033
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006034 /* Remove the list from the list of lists for garbage collection. */
6035 if (l->lv_used_prev == NULL)
6036 first_list = l->lv_used_next;
6037 else
6038 l->lv_used_prev->lv_used_next = l->lv_used_next;
6039 if (l->lv_used_next != NULL)
6040 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6041
Bram Moolenaard9fba312005-06-26 22:34:35 +00006042 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006043 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006044 /* Remove the item before deleting it. */
6045 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006046 if (recurse || (item->li_tv.v_type != VAR_LIST
6047 && item->li_tv.v_type != VAR_DICT))
6048 clear_tv(&item->li_tv);
6049 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006050 }
6051 vim_free(l);
6052}
6053
6054/*
6055 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006056 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006057 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006058 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006059listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006060{
Bram Moolenaar33570922005-01-25 22:26:29 +00006061 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006062}
6063
6064/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006065 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006066 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006067 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006068listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006069{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006070 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006071 vim_free(item);
6072}
6073
6074/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006075 * Remove a list item from a List and free it. Also clears the value.
6076 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006077 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006078listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006079{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006080 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006081 listitem_free(item);
6082}
6083
6084/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006085 * Get the number of items in a list.
6086 */
6087 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006088list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006089{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006090 if (l == NULL)
6091 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006092 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006093}
6094
6095/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006096 * Return TRUE when two lists have exactly the same values.
6097 */
6098 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006099list_equal(
6100 list_T *l1,
6101 list_T *l2,
6102 int ic, /* ignore case for strings */
6103 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006104{
Bram Moolenaar33570922005-01-25 22:26:29 +00006105 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006106
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006107 if (l1 == NULL || l2 == NULL)
6108 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006109 if (l1 == l2)
6110 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006111 if (list_len(l1) != list_len(l2))
6112 return FALSE;
6113
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006114 for (item1 = l1->lv_first, item2 = l2->lv_first;
6115 item1 != NULL && item2 != NULL;
6116 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006117 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006118 return FALSE;
6119 return item1 == NULL && item2 == NULL;
6120}
6121
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006122/*
6123 * Return the dictitem that an entry in a hashtable points to.
6124 */
6125 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006126dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006127{
6128 return HI2DI(hi);
6129}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006130
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006131/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006132 * Return TRUE when two dictionaries have exactly the same key/values.
6133 */
6134 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006135dict_equal(
6136 dict_T *d1,
6137 dict_T *d2,
6138 int ic, /* ignore case for strings */
6139 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006140{
Bram Moolenaar33570922005-01-25 22:26:29 +00006141 hashitem_T *hi;
6142 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006143 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006144
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006145 if (d1 == NULL || d2 == NULL)
6146 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006147 if (d1 == d2)
6148 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006149 if (dict_len(d1) != dict_len(d2))
6150 return FALSE;
6151
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006152 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006153 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006154 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006155 if (!HASHITEM_EMPTY(hi))
6156 {
6157 item2 = dict_find(d2, hi->hi_key, -1);
6158 if (item2 == NULL)
6159 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006160 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006161 return FALSE;
6162 --todo;
6163 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006164 }
6165 return TRUE;
6166}
6167
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006168static int tv_equal_recurse_limit;
6169
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006170/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006171 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006172 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006173 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006174 */
6175 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006176tv_equal(
6177 typval_T *tv1,
6178 typval_T *tv2,
6179 int ic, /* ignore case */
6180 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006181{
6182 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006183 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006184 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006185 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006186
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006187 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006188 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006189
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006190 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006191 * recursiveness to a limit. We guess they are equal then.
6192 * A fixed limit has the problem of still taking an awful long time.
6193 * Reduce the limit every time running into it. That should work fine for
6194 * deeply linked structures that are not recursively linked and catch
6195 * recursiveness quickly. */
6196 if (!recursive)
6197 tv_equal_recurse_limit = 1000;
6198 if (recursive_cnt >= tv_equal_recurse_limit)
6199 {
6200 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006201 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006202 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006203
6204 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006205 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006206 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006207 ++recursive_cnt;
6208 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6209 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006210 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006211
6212 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006213 ++recursive_cnt;
6214 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6215 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006216 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006217
6218 case VAR_FUNC:
6219 return (tv1->vval.v_string != NULL
6220 && tv2->vval.v_string != NULL
6221 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6222
6223 case VAR_NUMBER:
6224 return tv1->vval.v_number == tv2->vval.v_number;
6225
6226 case VAR_STRING:
6227 s1 = get_tv_string_buf(tv1, buf1);
6228 s2 = get_tv_string_buf(tv2, buf2);
6229 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006230
6231 case VAR_SPECIAL:
6232 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006233
6234 case VAR_FLOAT:
6235#ifdef FEAT_FLOAT
6236 return tv1->vval.v_float == tv2->vval.v_float;
6237#endif
6238 case VAR_JOB:
6239#ifdef FEAT_JOB
6240 return tv1->vval.v_job == tv2->vval.v_job;
6241#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006242 case VAR_CHANNEL:
6243#ifdef FEAT_CHANNEL
6244 return tv1->vval.v_channel == tv2->vval.v_channel;
6245#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006246 case VAR_UNKNOWN:
6247 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006248 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006249
Bram Moolenaara03f2332016-02-06 18:09:59 +01006250 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6251 * does not equal anything, not even itself. */
6252 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006253}
6254
6255/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006256 * Locate item with index "n" in list "l" and return it.
6257 * A negative index is counted from the end; -1 is the last item.
6258 * Returns NULL when "n" is out of range.
6259 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006260 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006261list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006262{
Bram Moolenaar33570922005-01-25 22:26:29 +00006263 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006264 long idx;
6265
6266 if (l == NULL)
6267 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006268
6269 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006270 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006271 n = l->lv_len + n;
6272
6273 /* Check for index out of range. */
6274 if (n < 0 || n >= l->lv_len)
6275 return NULL;
6276
6277 /* When there is a cached index may start search from there. */
6278 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006279 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006280 if (n < l->lv_idx / 2)
6281 {
6282 /* closest to the start of the list */
6283 item = l->lv_first;
6284 idx = 0;
6285 }
6286 else if (n > (l->lv_idx + l->lv_len) / 2)
6287 {
6288 /* closest to the end of the list */
6289 item = l->lv_last;
6290 idx = l->lv_len - 1;
6291 }
6292 else
6293 {
6294 /* closest to the cached index */
6295 item = l->lv_idx_item;
6296 idx = l->lv_idx;
6297 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006298 }
6299 else
6300 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006301 if (n < l->lv_len / 2)
6302 {
6303 /* closest to the start of the list */
6304 item = l->lv_first;
6305 idx = 0;
6306 }
6307 else
6308 {
6309 /* closest to the end of the list */
6310 item = l->lv_last;
6311 idx = l->lv_len - 1;
6312 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006313 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006314
6315 while (n > idx)
6316 {
6317 /* search forward */
6318 item = item->li_next;
6319 ++idx;
6320 }
6321 while (n < idx)
6322 {
6323 /* search backward */
6324 item = item->li_prev;
6325 --idx;
6326 }
6327
6328 /* cache the used index */
6329 l->lv_idx = idx;
6330 l->lv_idx_item = item;
6331
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006332 return item;
6333}
6334
6335/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006336 * Get list item "l[idx]" as a number.
6337 */
6338 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006339list_find_nr(
6340 list_T *l,
6341 long idx,
6342 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006343{
6344 listitem_T *li;
6345
6346 li = list_find(l, idx);
6347 if (li == NULL)
6348 {
6349 if (errorp != NULL)
6350 *errorp = TRUE;
6351 return -1L;
6352 }
6353 return get_tv_number_chk(&li->li_tv, errorp);
6354}
6355
6356/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006357 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6358 */
6359 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006360list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006361{
6362 listitem_T *li;
6363
6364 li = list_find(l, idx - 1);
6365 if (li == NULL)
6366 {
6367 EMSGN(_(e_listidx), idx);
6368 return NULL;
6369 }
6370 return get_tv_string(&li->li_tv);
6371}
6372
6373/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006374 * Locate "item" list "l" and return its index.
6375 * Returns -1 when "item" is not in the list.
6376 */
6377 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006378list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006379{
6380 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006381 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006382
6383 if (l == NULL)
6384 return -1;
6385 idx = 0;
6386 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6387 ++idx;
6388 if (li == NULL)
6389 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006390 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006391}
6392
6393/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006394 * Append item "item" to the end of list "l".
6395 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006396 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006397list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006398{
6399 if (l->lv_last == NULL)
6400 {
6401 /* empty list */
6402 l->lv_first = item;
6403 l->lv_last = item;
6404 item->li_prev = NULL;
6405 }
6406 else
6407 {
6408 l->lv_last->li_next = item;
6409 item->li_prev = l->lv_last;
6410 l->lv_last = item;
6411 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006412 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006413 item->li_next = NULL;
6414}
6415
6416/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006417 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006418 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006419 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006420 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006421list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006422{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006423 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006424
Bram Moolenaar05159a02005-02-26 23:04:13 +00006425 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006426 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006427 copy_tv(tv, &li->li_tv);
6428 list_append(l, li);
6429 return OK;
6430}
6431
6432/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006433 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006434 * Return FAIL when out of memory.
6435 */
6436 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006437list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006438{
6439 listitem_T *li = listitem_alloc();
6440
6441 if (li == NULL)
6442 return FAIL;
6443 li->li_tv.v_type = VAR_DICT;
6444 li->li_tv.v_lock = 0;
6445 li->li_tv.vval.v_dict = dict;
6446 list_append(list, li);
6447 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006448 return OK;
6449}
6450
6451/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006452 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006453 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006454 * Returns FAIL when out of memory.
6455 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006456 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006457list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006458{
6459 listitem_T *li = listitem_alloc();
6460
6461 if (li == NULL)
6462 return FAIL;
6463 list_append(l, li);
6464 li->li_tv.v_type = VAR_STRING;
6465 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006466 if (str == NULL)
6467 li->li_tv.vval.v_string = NULL;
6468 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006469 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006470 return FAIL;
6471 return OK;
6472}
6473
6474/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006475 * Append "n" to list "l".
6476 * Returns FAIL when out of memory.
6477 */
6478 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006479list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006480{
6481 listitem_T *li;
6482
6483 li = listitem_alloc();
6484 if (li == NULL)
6485 return FAIL;
6486 li->li_tv.v_type = VAR_NUMBER;
6487 li->li_tv.v_lock = 0;
6488 li->li_tv.vval.v_number = n;
6489 list_append(l, li);
6490 return OK;
6491}
6492
6493/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006494 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006495 * If "item" is NULL append at the end.
6496 * Return FAIL when out of memory.
6497 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006498 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006499list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006500{
Bram Moolenaar33570922005-01-25 22:26:29 +00006501 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006502
6503 if (ni == NULL)
6504 return FAIL;
6505 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006506 list_insert(l, ni, item);
6507 return OK;
6508}
6509
6510 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006511list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006512{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006513 if (item == NULL)
6514 /* Append new item at end of list. */
6515 list_append(l, ni);
6516 else
6517 {
6518 /* Insert new item before existing item. */
6519 ni->li_prev = item->li_prev;
6520 ni->li_next = item;
6521 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006522 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006523 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006524 ++l->lv_idx;
6525 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006526 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006527 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006528 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006529 l->lv_idx_item = NULL;
6530 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006531 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006532 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006533 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006534}
6535
6536/*
6537 * Extend "l1" with "l2".
6538 * If "bef" is NULL append at the end, otherwise insert before this item.
6539 * Returns FAIL when out of memory.
6540 */
6541 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006542list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006543{
Bram Moolenaar33570922005-01-25 22:26:29 +00006544 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006545 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006546
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006547 /* We also quit the loop when we have inserted the original item count of
6548 * the list, avoid a hang when we extend a list with itself. */
6549 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006550 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6551 return FAIL;
6552 return OK;
6553}
6554
6555/*
6556 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6557 * Return FAIL when out of memory.
6558 */
6559 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006560list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006561{
Bram Moolenaar33570922005-01-25 22:26:29 +00006562 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006563
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006564 if (l1 == NULL || l2 == NULL)
6565 return FAIL;
6566
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006567 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006568 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006569 if (l == NULL)
6570 return FAIL;
6571 tv->v_type = VAR_LIST;
6572 tv->vval.v_list = l;
6573
6574 /* append all items from the second list */
6575 return list_extend(l, l2, NULL);
6576}
6577
6578/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006579 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006580 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006581 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006582 * Returns NULL when out of memory.
6583 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006584 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006585list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006586{
Bram Moolenaar33570922005-01-25 22:26:29 +00006587 list_T *copy;
6588 listitem_T *item;
6589 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006590
6591 if (orig == NULL)
6592 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006593
6594 copy = list_alloc();
6595 if (copy != NULL)
6596 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006597 if (copyID != 0)
6598 {
6599 /* Do this before adding the items, because one of the items may
6600 * refer back to this list. */
6601 orig->lv_copyID = copyID;
6602 orig->lv_copylist = copy;
6603 }
6604 for (item = orig->lv_first; item != NULL && !got_int;
6605 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006606 {
6607 ni = listitem_alloc();
6608 if (ni == NULL)
6609 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006610 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006611 {
6612 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6613 {
6614 vim_free(ni);
6615 break;
6616 }
6617 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006618 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006619 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006620 list_append(copy, ni);
6621 }
6622 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006623 if (item != NULL)
6624 {
6625 list_unref(copy);
6626 copy = NULL;
6627 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006628 }
6629
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006630 return copy;
6631}
6632
6633/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006634 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006635 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006636 * This used to be called list_remove, but that conflicts with a Sun header
6637 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006638 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006639 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006640vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006641{
Bram Moolenaar33570922005-01-25 22:26:29 +00006642 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006643
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006644 /* notify watchers */
6645 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006646 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006647 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006648 list_fix_watch(l, ip);
6649 if (ip == item2)
6650 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006651 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006652
6653 if (item2->li_next == NULL)
6654 l->lv_last = item->li_prev;
6655 else
6656 item2->li_next->li_prev = item->li_prev;
6657 if (item->li_prev == NULL)
6658 l->lv_first = item2->li_next;
6659 else
6660 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006661 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006662}
6663
6664/*
6665 * Return an allocated string with the string representation of a list.
6666 * May return NULL.
6667 */
6668 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006669list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006670{
6671 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006672
6673 if (tv->vval.v_list == NULL)
6674 return NULL;
6675 ga_init2(&ga, (int)sizeof(char), 80);
6676 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006677 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006678 {
6679 vim_free(ga.ga_data);
6680 return NULL;
6681 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006682 ga_append(&ga, ']');
6683 ga_append(&ga, NUL);
6684 return (char_u *)ga.ga_data;
6685}
6686
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006687typedef struct join_S {
6688 char_u *s;
6689 char_u *tofree;
6690} join_T;
6691
6692 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006693list_join_inner(
6694 garray_T *gap, /* to store the result in */
6695 list_T *l,
6696 char_u *sep,
6697 int echo_style,
6698 int copyID,
6699 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006700{
6701 int i;
6702 join_T *p;
6703 int len;
6704 int sumlen = 0;
6705 int first = TRUE;
6706 char_u *tofree;
6707 char_u numbuf[NUMBUFLEN];
6708 listitem_T *item;
6709 char_u *s;
6710
6711 /* Stringify each item in the list. */
6712 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6713 {
6714 if (echo_style)
6715 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6716 else
6717 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6718 if (s == NULL)
6719 return FAIL;
6720
6721 len = (int)STRLEN(s);
6722 sumlen += len;
6723
Bram Moolenaarcde88542015-08-11 19:14:00 +02006724 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006725 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6726 if (tofree != NULL || s != numbuf)
6727 {
6728 p->s = s;
6729 p->tofree = tofree;
6730 }
6731 else
6732 {
6733 p->s = vim_strnsave(s, len);
6734 p->tofree = p->s;
6735 }
6736
6737 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006738 if (did_echo_string_emsg) /* recursion error, bail out */
6739 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006740 }
6741
6742 /* Allocate result buffer with its total size, avoid re-allocation and
6743 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6744 if (join_gap->ga_len >= 2)
6745 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6746 if (ga_grow(gap, sumlen + 2) == FAIL)
6747 return FAIL;
6748
6749 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6750 {
6751 if (first)
6752 first = FALSE;
6753 else
6754 ga_concat(gap, sep);
6755 p = ((join_T *)join_gap->ga_data) + i;
6756
6757 if (p->s != NULL)
6758 ga_concat(gap, p->s);
6759 line_breakcheck();
6760 }
6761
6762 return OK;
6763}
6764
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006765/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006766 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006767 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006768 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006769 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006770 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006771list_join(
6772 garray_T *gap,
6773 list_T *l,
6774 char_u *sep,
6775 int echo_style,
6776 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006777{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006778 garray_T join_ga;
6779 int retval;
6780 join_T *p;
6781 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006782
Bram Moolenaard39a7512015-04-16 22:51:22 +02006783 if (l->lv_len < 1)
6784 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006785 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6786 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6787
6788 /* Dispose each item in join_ga. */
6789 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006790 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006791 p = (join_T *)join_ga.ga_data;
6792 for (i = 0; i < join_ga.ga_len; ++i)
6793 {
6794 vim_free(p->tofree);
6795 ++p;
6796 }
6797 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006798 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006799
6800 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006801}
6802
6803/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006804 * Return the next (unique) copy ID.
6805 * Used for serializing nested structures.
6806 */
6807 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006808get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006809{
6810 current_copyID += COPYID_INC;
6811 return current_copyID;
6812}
6813
6814/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006815 * Garbage collection for lists and dictionaries.
6816 *
6817 * We use reference counts to be able to free most items right away when they
6818 * are no longer used. But for composite items it's possible that it becomes
6819 * unused while the reference count is > 0: When there is a recursive
6820 * reference. Example:
6821 * :let l = [1, 2, 3]
6822 * :let d = {9: l}
6823 * :let l[1] = d
6824 *
6825 * Since this is quite unusual we handle this with garbage collection: every
6826 * once in a while find out which lists and dicts are not referenced from any
6827 * variable.
6828 *
6829 * Here is a good reference text about garbage collection (refers to Python
6830 * but it applies to all reference-counting mechanisms):
6831 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006832 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006833
6834/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006835 * Do garbage collection for lists and dicts.
6836 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006837 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006838 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006839garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006840{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006841 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006842 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006843 buf_T *buf;
6844 win_T *wp;
6845 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006846 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006847 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006848 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006849#ifdef FEAT_WINDOWS
6850 tabpage_T *tp;
6851#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006852
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006853 /* Only do this once. */
6854 want_garbage_collect = FALSE;
6855 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006856 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006857
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006858 /* We advance by two because we add one for items referenced through
6859 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006860 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006861
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006862 /*
6863 * 1. Go through all accessible variables and mark all lists and dicts
6864 * with copyID.
6865 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006866
6867 /* Don't free variables in the previous_funccal list unless they are only
6868 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006869 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006870 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6871 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006872 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6873 NULL);
6874 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6875 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006876 }
6877
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006878 /* script-local variables */
6879 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006880 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006881
6882 /* buffer-local variables */
6883 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006884 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6885 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006886
6887 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006888 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006889 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6890 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006891#ifdef FEAT_AUTOCMD
6892 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006893 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6894 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006895#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006896
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006897#ifdef FEAT_WINDOWS
6898 /* tabpage-local variables */
6899 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006900 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6901 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006902#endif
6903
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006904 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006905 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006906
6907 /* function-local variables */
6908 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6909 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006910 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6911 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006912 }
6913
Bram Moolenaard812df62008-11-09 12:46:09 +00006914 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006915 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006916
Bram Moolenaar1dced572012-04-05 16:54:08 +02006917#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006918 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006919#endif
6920
Bram Moolenaardb913952012-06-29 12:54:53 +02006921#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006922 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006923#endif
6924
6925#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006926 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006927#endif
6928
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006929#ifdef FEAT_CHANNEL
6930 abort = abort || set_ref_in_channel(copyID);
6931#endif
6932
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006933 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006934 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006935 /*
6936 * 2. Free lists and dictionaries that are not referenced.
6937 */
6938 did_free = free_unref_items(copyID);
6939
6940 /*
6941 * 3. Check if any funccal can be freed now.
6942 */
6943 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006944 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006945 if (can_free_funccal(*pfc, copyID))
6946 {
6947 fc = *pfc;
6948 *pfc = fc->caller;
6949 free_funccal(fc, TRUE);
6950 did_free = TRUE;
6951 did_free_funccal = TRUE;
6952 }
6953 else
6954 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006955 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006956 if (did_free_funccal)
6957 /* When a funccal was freed some more items might be garbage
6958 * collected, so run again. */
6959 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006960 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006961 else if (p_verbose > 0)
6962 {
6963 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6964 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006965
6966 return did_free;
6967}
6968
6969/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01006970 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006971 */
6972 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006973free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006974{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006975 dict_T *dd, *dd_next;
6976 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006977 int did_free = FALSE;
6978
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006979 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006980 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006981 */
6982 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006983 {
6984 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006985 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006986 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006987 /* Free the Dictionary and ordinary items it contains, but don't
6988 * recurse into Lists and Dictionaries, they will be in the list
6989 * of dicts or list of lists. */
6990 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006991 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006992 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006993 dd = dd_next;
6994 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006995
6996 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006997 * Go through the list of lists and free items without the copyID.
6998 * But don't free a list that has a watcher (used in a for loop), these
6999 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007000 */
7001 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007002 {
7003 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007004 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7005 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007006 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007007 /* Free the List and ordinary items it contains, but don't recurse
7008 * into Lists and Dictionaries, they will be in the list of dicts
7009 * or list of lists. */
7010 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007011 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007012 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007013 ll = ll_next;
7014 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007015
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007016 return did_free;
7017}
7018
7019/*
7020 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007021 * "list_stack" is used to add lists to be marked. Can be NULL.
7022 *
7023 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007024 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007025 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007026set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007027{
7028 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007029 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007030 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007031 hashtab_T *cur_ht;
7032 ht_stack_T *ht_stack = NULL;
7033 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007034
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007035 cur_ht = ht;
7036 for (;;)
7037 {
7038 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007039 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007040 /* Mark each item in the hashtab. If the item contains a hashtab
7041 * it is added to ht_stack, if it contains a list it is added to
7042 * list_stack. */
7043 todo = (int)cur_ht->ht_used;
7044 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7045 if (!HASHITEM_EMPTY(hi))
7046 {
7047 --todo;
7048 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7049 &ht_stack, list_stack);
7050 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007051 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007052
7053 if (ht_stack == NULL)
7054 break;
7055
7056 /* take an item from the stack */
7057 cur_ht = ht_stack->ht;
7058 tempitem = ht_stack;
7059 ht_stack = ht_stack->prev;
7060 free(tempitem);
7061 }
7062
7063 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007064}
7065
7066/*
7067 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007068 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7069 *
7070 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007071 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007072 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007073set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007074{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007075 listitem_T *li;
7076 int abort = FALSE;
7077 list_T *cur_l;
7078 list_stack_T *list_stack = NULL;
7079 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007080
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007081 cur_l = l;
7082 for (;;)
7083 {
7084 if (!abort)
7085 /* Mark each item in the list. If the item contains a hashtab
7086 * it is added to ht_stack, if it contains a list it is added to
7087 * list_stack. */
7088 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7089 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7090 ht_stack, &list_stack);
7091 if (list_stack == NULL)
7092 break;
7093
7094 /* take an item from the stack */
7095 cur_l = list_stack->list;
7096 tempitem = list_stack;
7097 list_stack = list_stack->prev;
7098 free(tempitem);
7099 }
7100
7101 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007102}
7103
7104/*
7105 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007106 * "list_stack" is used to add lists to be marked. Can be NULL.
7107 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7108 *
7109 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007110 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007111 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007112set_ref_in_item(
7113 typval_T *tv,
7114 int copyID,
7115 ht_stack_T **ht_stack,
7116 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007117{
7118 dict_T *dd;
7119 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007120 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007121
Bram Moolenaara03f2332016-02-06 18:09:59 +01007122 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007123 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007124 dd = tv->vval.v_dict;
7125 if (dd != NULL && dd->dv_copyID != copyID)
7126 {
7127 /* Didn't see this dict yet. */
7128 dd->dv_copyID = copyID;
7129 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007130 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007131 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7132 }
7133 else
7134 {
7135 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7136 if (newitem == NULL)
7137 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007138 else
7139 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007140 newitem->ht = &dd->dv_hashtab;
7141 newitem->prev = *ht_stack;
7142 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007143 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007144 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007145 }
7146 }
7147 else if (tv->v_type == VAR_LIST)
7148 {
7149 ll = tv->vval.v_list;
7150 if (ll != NULL && ll->lv_copyID != copyID)
7151 {
7152 /* Didn't see this list yet. */
7153 ll->lv_copyID = copyID;
7154 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007155 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007156 abort = set_ref_in_list(ll, copyID, ht_stack);
7157 }
7158 else
7159 {
7160 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007161 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007162 if (newitem == NULL)
7163 abort = TRUE;
7164 else
7165 {
7166 newitem->list = ll;
7167 newitem->prev = *list_stack;
7168 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007169 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007170 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007171 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007172 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007173 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007174}
7175
7176/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007177 * Allocate an empty header for a dictionary.
7178 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007179 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007180dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007181{
Bram Moolenaar33570922005-01-25 22:26:29 +00007182 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007183
Bram Moolenaar33570922005-01-25 22:26:29 +00007184 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007185 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007186 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007187 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007188 if (first_dict != NULL)
7189 first_dict->dv_used_prev = d;
7190 d->dv_used_next = first_dict;
7191 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007192 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007193
Bram Moolenaar33570922005-01-25 22:26:29 +00007194 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007195 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007196 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007197 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007198 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007199 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007200 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007201}
7202
7203/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007204 * Allocate an empty dict for a return value.
7205 * Returns OK or FAIL.
7206 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007207 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007208rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007209{
7210 dict_T *d = dict_alloc();
7211
7212 if (d == NULL)
7213 return FAIL;
7214
7215 rettv->vval.v_dict = d;
7216 rettv->v_type = VAR_DICT;
7217 ++d->dv_refcount;
7218 return OK;
7219}
7220
7221
7222/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007223 * Unreference a Dictionary: decrement the reference count and free it when it
7224 * becomes zero.
7225 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007226 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007227dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007228{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007229 if (d != NULL && --d->dv_refcount <= 0)
7230 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007231}
7232
7233/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007234 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007235 * Ignores the reference count.
7236 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007237 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007238dict_free(
7239 dict_T *d,
7240 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007241{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007242 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007243 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007244 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007245
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007246 /* Remove the dict from the list of dicts for garbage collection. */
7247 if (d->dv_used_prev == NULL)
7248 first_dict = d->dv_used_next;
7249 else
7250 d->dv_used_prev->dv_used_next = d->dv_used_next;
7251 if (d->dv_used_next != NULL)
7252 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7253
7254 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007255 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007256 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007257 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007258 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007259 if (!HASHITEM_EMPTY(hi))
7260 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007261 /* Remove the item before deleting it, just in case there is
7262 * something recursive causing trouble. */
7263 di = HI2DI(hi);
7264 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007265 if (recurse || (di->di_tv.v_type != VAR_LIST
7266 && di->di_tv.v_type != VAR_DICT))
7267 clear_tv(&di->di_tv);
7268 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007269 --todo;
7270 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007271 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007272 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007273 vim_free(d);
7274}
7275
7276/*
7277 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007278 * The "key" is copied to the new item.
7279 * Note that the value of the item "di_tv" still needs to be initialized!
7280 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007281 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007282 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007283dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007284{
Bram Moolenaar33570922005-01-25 22:26:29 +00007285 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007286
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007287 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007288 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007289 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007290 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007291 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007292 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007293 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007294}
7295
7296/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007297 * Make a copy of a Dictionary item.
7298 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007299 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007300dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007301{
Bram Moolenaar33570922005-01-25 22:26:29 +00007302 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007303
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007304 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7305 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007306 if (di != NULL)
7307 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007308 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007309 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007310 copy_tv(&org->di_tv, &di->di_tv);
7311 }
7312 return di;
7313}
7314
7315/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007316 * Remove item "item" from Dictionary "dict" and free it.
7317 */
7318 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007319dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007320{
Bram Moolenaar33570922005-01-25 22:26:29 +00007321 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007322
Bram Moolenaar33570922005-01-25 22:26:29 +00007323 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007324 if (HASHITEM_EMPTY(hi))
7325 EMSG2(_(e_intern2), "dictitem_remove()");
7326 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007327 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007328 dictitem_free(item);
7329}
7330
7331/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007332 * Free a dict item. Also clears the value.
7333 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007334 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007335dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007336{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007337 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007338 if (item->di_flags & DI_FLAGS_ALLOC)
7339 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007340}
7341
7342/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007343 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7344 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007345 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007346 * Returns NULL when out of memory.
7347 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007348 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007349dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007350{
Bram Moolenaar33570922005-01-25 22:26:29 +00007351 dict_T *copy;
7352 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007353 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007354 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007355
7356 if (orig == NULL)
7357 return NULL;
7358
7359 copy = dict_alloc();
7360 if (copy != NULL)
7361 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007362 if (copyID != 0)
7363 {
7364 orig->dv_copyID = copyID;
7365 orig->dv_copydict = copy;
7366 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007367 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007368 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007369 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007370 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007371 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007372 --todo;
7373
7374 di = dictitem_alloc(hi->hi_key);
7375 if (di == NULL)
7376 break;
7377 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007378 {
7379 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7380 copyID) == FAIL)
7381 {
7382 vim_free(di);
7383 break;
7384 }
7385 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007386 else
7387 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7388 if (dict_add(copy, di) == FAIL)
7389 {
7390 dictitem_free(di);
7391 break;
7392 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007393 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007394 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007395
Bram Moolenaare9a41262005-01-15 22:18:47 +00007396 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007397 if (todo > 0)
7398 {
7399 dict_unref(copy);
7400 copy = NULL;
7401 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007402 }
7403
7404 return copy;
7405}
7406
7407/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007408 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007409 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007410 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007411 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007412dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007413{
Bram Moolenaar33570922005-01-25 22:26:29 +00007414 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007415}
7416
Bram Moolenaar8c711452005-01-14 21:53:12 +00007417/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007418 * Add a number or string entry to dictionary "d".
7419 * When "str" is NULL use number "nr", otherwise use "str".
7420 * Returns FAIL when out of memory and when key already exists.
7421 */
7422 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007423dict_add_nr_str(
7424 dict_T *d,
7425 char *key,
7426 long nr,
7427 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007428{
7429 dictitem_T *item;
7430
7431 item = dictitem_alloc((char_u *)key);
7432 if (item == NULL)
7433 return FAIL;
7434 item->di_tv.v_lock = 0;
7435 if (str == NULL)
7436 {
7437 item->di_tv.v_type = VAR_NUMBER;
7438 item->di_tv.vval.v_number = nr;
7439 }
7440 else
7441 {
7442 item->di_tv.v_type = VAR_STRING;
7443 item->di_tv.vval.v_string = vim_strsave(str);
7444 }
7445 if (dict_add(d, item) == FAIL)
7446 {
7447 dictitem_free(item);
7448 return FAIL;
7449 }
7450 return OK;
7451}
7452
7453/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007454 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007455 * Returns FAIL when out of memory and when key already exists.
7456 */
7457 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007458dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007459{
7460 dictitem_T *item;
7461
7462 item = dictitem_alloc((char_u *)key);
7463 if (item == NULL)
7464 return FAIL;
7465 item->di_tv.v_lock = 0;
7466 item->di_tv.v_type = VAR_LIST;
7467 item->di_tv.vval.v_list = list;
7468 if (dict_add(d, item) == FAIL)
7469 {
7470 dictitem_free(item);
7471 return FAIL;
7472 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007473 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007474 return OK;
7475}
7476
7477/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007478 * Get the number of items in a Dictionary.
7479 */
7480 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007481dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007482{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007483 if (d == NULL)
7484 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007485 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007486}
7487
7488/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007489 * Find item "key[len]" in Dictionary "d".
7490 * If "len" is negative use strlen(key).
7491 * Returns NULL when not found.
7492 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007493 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007494dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007495{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007496#define AKEYLEN 200
7497 char_u buf[AKEYLEN];
7498 char_u *akey;
7499 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007500 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007501
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007502 if (len < 0)
7503 akey = key;
7504 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007505 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007506 tofree = akey = vim_strnsave(key, len);
7507 if (akey == NULL)
7508 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007509 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007510 else
7511 {
7512 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007513 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007514 akey = buf;
7515 }
7516
Bram Moolenaar33570922005-01-25 22:26:29 +00007517 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007518 vim_free(tofree);
7519 if (HASHITEM_EMPTY(hi))
7520 return NULL;
7521 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007522}
7523
7524/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007525 * Get a string item from a dictionary.
7526 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007527 * Returns NULL if the entry doesn't exist or out of memory.
7528 */
7529 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007530get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007531{
7532 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007533 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007534
7535 di = dict_find(d, key, -1);
7536 if (di == NULL)
7537 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007538 s = get_tv_string(&di->di_tv);
7539 if (save && s != NULL)
7540 s = vim_strsave(s);
7541 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007542}
7543
7544/*
7545 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007546 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007547 */
7548 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007549get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007550{
7551 dictitem_T *di;
7552
7553 di = dict_find(d, key, -1);
7554 if (di == NULL)
7555 return 0;
7556 return get_tv_number(&di->di_tv);
7557}
7558
7559/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007560 * Return an allocated string with the string representation of a Dictionary.
7561 * May return NULL.
7562 */
7563 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007564dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007565{
7566 garray_T ga;
7567 int first = TRUE;
7568 char_u *tofree;
7569 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007570 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007571 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007572 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007573 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007574
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007575 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007576 return NULL;
7577 ga_init2(&ga, (int)sizeof(char), 80);
7578 ga_append(&ga, '{');
7579
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007580 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007581 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007582 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007583 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007584 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007585 --todo;
7586
7587 if (first)
7588 first = FALSE;
7589 else
7590 ga_concat(&ga, (char_u *)", ");
7591
7592 tofree = string_quote(hi->hi_key, FALSE);
7593 if (tofree != NULL)
7594 {
7595 ga_concat(&ga, tofree);
7596 vim_free(tofree);
7597 }
7598 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007599 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007600 if (s != NULL)
7601 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007602 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007603 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007604 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007605 line_breakcheck();
7606
Bram Moolenaar8c711452005-01-14 21:53:12 +00007607 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007608 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007609 if (todo > 0)
7610 {
7611 vim_free(ga.ga_data);
7612 return NULL;
7613 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007614
7615 ga_append(&ga, '}');
7616 ga_append(&ga, NUL);
7617 return (char_u *)ga.ga_data;
7618}
7619
7620/*
7621 * Allocate a variable for a Dictionary and fill it from "*arg".
7622 * Return OK or FAIL. Returns NOTDONE for {expr}.
7623 */
7624 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007625get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007626{
Bram Moolenaar33570922005-01-25 22:26:29 +00007627 dict_T *d = NULL;
7628 typval_T tvkey;
7629 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007630 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007631 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007632 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007633 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007634
7635 /*
7636 * First check if it's not a curly-braces thing: {expr}.
7637 * Must do this without evaluating, otherwise a function may be called
7638 * twice. Unfortunately this means we need to call eval1() twice for the
7639 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007640 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007641 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007642 if (*start != '}')
7643 {
7644 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7645 return FAIL;
7646 if (*start == '}')
7647 return NOTDONE;
7648 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007649
7650 if (evaluate)
7651 {
7652 d = dict_alloc();
7653 if (d == NULL)
7654 return FAIL;
7655 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007656 tvkey.v_type = VAR_UNKNOWN;
7657 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007658
7659 *arg = skipwhite(*arg + 1);
7660 while (**arg != '}' && **arg != NUL)
7661 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007662 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007663 goto failret;
7664 if (**arg != ':')
7665 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007666 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007667 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007668 goto failret;
7669 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007670 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007671 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007672 key = get_tv_string_buf_chk(&tvkey, buf);
7673 if (key == NULL || *key == NUL)
7674 {
7675 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7676 if (key != NULL)
7677 EMSG(_(e_emptykey));
7678 clear_tv(&tvkey);
7679 goto failret;
7680 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007681 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007682
7683 *arg = skipwhite(*arg + 1);
7684 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7685 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007686 if (evaluate)
7687 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007688 goto failret;
7689 }
7690 if (evaluate)
7691 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007692 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007693 if (item != NULL)
7694 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007695 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007696 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007697 clear_tv(&tv);
7698 goto failret;
7699 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007700 item = dictitem_alloc(key);
7701 clear_tv(&tvkey);
7702 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007703 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007704 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007705 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007706 if (dict_add(d, item) == FAIL)
7707 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007708 }
7709 }
7710
7711 if (**arg == '}')
7712 break;
7713 if (**arg != ',')
7714 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007715 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007716 goto failret;
7717 }
7718 *arg = skipwhite(*arg + 1);
7719 }
7720
7721 if (**arg != '}')
7722 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007723 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007724failret:
7725 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007726 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007727 return FAIL;
7728 }
7729
7730 *arg = skipwhite(*arg + 1);
7731 if (evaluate)
7732 {
7733 rettv->v_type = VAR_DICT;
7734 rettv->vval.v_dict = d;
7735 ++d->dv_refcount;
7736 }
7737
7738 return OK;
7739}
7740
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007741#if defined(FEAT_CHANNEL) || defined(PROTO)
7742/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01007743 * Decrement the reference count on "channel" and maybe free it when it goes
7744 * down to zero. Don't free it if there is a pending action.
Bram Moolenaard6051b52016-02-28 15:49:03 +01007745 * Returns TRUE when the channel is no longer referenced.
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007746 */
7747 int
Bram Moolenaar77073442016-02-13 23:23:53 +01007748channel_unref(channel_T *channel)
7749{
7750 if (channel != NULL && --channel->ch_refcount <= 0)
Bram Moolenaar70765942016-02-28 19:28:59 +01007751 return channel_may_free(channel);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007752 return FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01007753}
7754#endif
7755
Bram Moolenaar65edff82016-02-21 16:40:11 +01007756#if defined(FEAT_JOB) || defined(PROTO)
7757static job_T *first_job = NULL;
7758
Bram Moolenaar835dc632016-02-07 14:27:38 +01007759 static void
7760job_free(job_T *job)
7761{
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01007762# ifdef FEAT_CHANNEL
Bram Moolenaard6051b52016-02-28 15:49:03 +01007763 ch_log(job->jv_channel, "Freeing job");
Bram Moolenaar77073442016-02-13 23:23:53 +01007764 if (job->jv_channel != NULL)
7765 {
Bram Moolenaar46c85432016-02-26 11:17:46 +01007766 /* The link from the channel to the job doesn't count as a reference,
7767 * thus don't decrement the refcount of the job. The reference from
7768 * the job to the channel does count the refrence, decrement it and
7769 * NULL the reference. We don't set ch_job_killed, unreferencing the
7770 * job doesn't mean it stops running. */
Bram Moolenaar77073442016-02-13 23:23:53 +01007771 job->jv_channel->ch_job = NULL;
7772 channel_unref(job->jv_channel);
7773 }
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01007774# endif
Bram Moolenaar76467df2016-02-12 19:30:26 +01007775 mch_clear_job(job);
Bram Moolenaar65edff82016-02-21 16:40:11 +01007776
7777 if (job->jv_next != NULL)
7778 job->jv_next->jv_prev = job->jv_prev;
7779 if (job->jv_prev == NULL)
7780 first_job = job->jv_next;
7781 else
7782 job->jv_prev->jv_next = job->jv_next;
7783
7784 vim_free(job->jv_stoponexit);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007785 vim_free(job->jv_exit_cb);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007786 vim_free(job);
7787}
7788
7789 static void
7790job_unref(job_T *job)
7791{
7792 if (job != NULL && --job->jv_refcount <= 0)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007793 {
7794 /* Do not free the job when it has not ended yet and there is a
7795 * "stoponexit" flag or an exit callback. */
7796 if (job->jv_status != JOB_STARTED
7797 || (job->jv_stoponexit == NULL && job->jv_exit_cb == NULL))
Bram Moolenaard6051b52016-02-28 15:49:03 +01007798 {
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007799 job_free(job);
Bram Moolenaard6051b52016-02-28 15:49:03 +01007800 }
Bram Moolenaar8cc69772016-02-28 16:42:03 +01007801# ifdef FEAT_CHANNEL
Bram Moolenaard6051b52016-02-28 15:49:03 +01007802 else if (job->jv_channel != NULL)
7803 {
7804 /* Do remove the link to the channel, otherwise it hangs
7805 * around until Vim exits. See job_free() for refcount. */
7806 job->jv_channel->ch_job = NULL;
7807 channel_unref(job->jv_channel);
7808 job->jv_channel = NULL;
7809 }
Bram Moolenaar8cc69772016-02-28 16:42:03 +01007810# endif
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007811 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007812}
7813
7814/*
Bram Moolenaar65edff82016-02-21 16:40:11 +01007815 * Allocate a job. Sets the refcount to one and sets options default.
Bram Moolenaar835dc632016-02-07 14:27:38 +01007816 */
7817 static job_T *
7818job_alloc(void)
7819{
7820 job_T *job;
7821
7822 job = (job_T *)alloc_clear(sizeof(job_T));
7823 if (job != NULL)
Bram Moolenaar65edff82016-02-21 16:40:11 +01007824 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01007825 job->jv_refcount = 1;
Bram Moolenaar65edff82016-02-21 16:40:11 +01007826 job->jv_stoponexit = vim_strsave((char_u *)"term");
7827
7828 if (first_job != NULL)
7829 {
7830 first_job->jv_prev = job;
7831 job->jv_next = first_job;
7832 }
7833 first_job = job;
7834 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007835 return job;
7836}
7837
Bram Moolenaar65edff82016-02-21 16:40:11 +01007838 static void
7839job_set_options(job_T *job, jobopt_T *opt)
7840{
7841 if (opt->jo_set & JO_STOPONEXIT)
7842 {
7843 vim_free(job->jv_stoponexit);
7844 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
7845 job->jv_stoponexit = NULL;
7846 else
7847 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
7848 }
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007849 if (opt->jo_set & JO_EXIT_CB)
7850 {
7851 vim_free(job->jv_exit_cb);
7852 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
7853 job->jv_exit_cb = NULL;
7854 else
7855 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
7856 }
Bram Moolenaar65edff82016-02-21 16:40:11 +01007857}
7858
7859/*
7860 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
7861 */
7862 void
7863job_stop_on_exit()
7864{
7865 job_T *job;
7866
7867 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007868 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
Bram Moolenaar65edff82016-02-21 16:40:11 +01007869 mch_stop_job(job, job->jv_stoponexit);
7870}
Bram Moolenaar835dc632016-02-07 14:27:38 +01007871#endif
7872
Bram Moolenaar17a13432016-01-24 14:22:10 +01007873 static char *
7874get_var_special_name(int nr)
7875{
7876 switch (nr)
7877 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007878 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01007879 case VVAL_TRUE: return "v:true";
7880 case VVAL_NONE: return "v:none";
7881 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007882 }
7883 EMSG2(_(e_intern2), "get_var_special_name()");
7884 return "42";
7885}
7886
Bram Moolenaar8c711452005-01-14 21:53:12 +00007887/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007888 * Return a string with the string representation of a variable.
7889 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007890 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007891 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007892 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007893 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007894 */
7895 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007896echo_string(
7897 typval_T *tv,
7898 char_u **tofree,
7899 char_u *numbuf,
7900 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007901{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007902 static int recurse = 0;
7903 char_u *r = NULL;
7904
Bram Moolenaar33570922005-01-25 22:26:29 +00007905 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007906 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007907 if (!did_echo_string_emsg)
7908 {
7909 /* Only give this message once for a recursive call to avoid
7910 * flooding the user with errors. And stop iterating over lists
7911 * and dicts. */
7912 did_echo_string_emsg = TRUE;
7913 EMSG(_("E724: variable nested too deep for displaying"));
7914 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007915 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007916 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007917 }
7918 ++recurse;
7919
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007920 switch (tv->v_type)
7921 {
7922 case VAR_FUNC:
7923 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007924 r = tv->vval.v_string;
7925 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007926
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007927 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007928 if (tv->vval.v_list == NULL)
7929 {
7930 *tofree = NULL;
7931 r = NULL;
7932 }
7933 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7934 {
7935 *tofree = NULL;
7936 r = (char_u *)"[...]";
7937 }
7938 else
7939 {
7940 tv->vval.v_list->lv_copyID = copyID;
7941 *tofree = list2string(tv, copyID);
7942 r = *tofree;
7943 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007944 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007945
Bram Moolenaar8c711452005-01-14 21:53:12 +00007946 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007947 if (tv->vval.v_dict == NULL)
7948 {
7949 *tofree = NULL;
7950 r = NULL;
7951 }
7952 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7953 {
7954 *tofree = NULL;
7955 r = (char_u *)"{...}";
7956 }
7957 else
7958 {
7959 tv->vval.v_dict->dv_copyID = copyID;
7960 *tofree = dict2string(tv, copyID);
7961 r = *tofree;
7962 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007963 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007964
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007965 case VAR_STRING:
7966 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007967 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007968 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007969 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007970 *tofree = NULL;
7971 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007972 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007973
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007974 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007975#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007976 *tofree = NULL;
7977 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7978 r = numbuf;
7979 break;
7980#endif
7981
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007982 case VAR_SPECIAL:
7983 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007984 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007985 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007986 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007987
Bram Moolenaar8502c702014-06-17 12:51:16 +02007988 if (--recurse == 0)
7989 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007990 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007991}
7992
7993/*
7994 * Return a string with the string representation of a variable.
7995 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7996 * "numbuf" is used for a number.
7997 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007998 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007999 */
8000 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008001tv2string(
8002 typval_T *tv,
8003 char_u **tofree,
8004 char_u *numbuf,
8005 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008006{
8007 switch (tv->v_type)
8008 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008009 case VAR_FUNC:
8010 *tofree = string_quote(tv->vval.v_string, TRUE);
8011 return *tofree;
8012 case VAR_STRING:
8013 *tofree = string_quote(tv->vval.v_string, FALSE);
8014 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008015 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01008016#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008017 *tofree = NULL;
8018 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
8019 return numbuf;
8020#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00008021 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008022 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00008023 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008024 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008025 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008026 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008027 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008028 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008029 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008030 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008031}
8032
8033/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008034 * Return string "str" in ' quotes, doubling ' characters.
8035 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008036 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008037 */
8038 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008039string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008040{
Bram Moolenaar33570922005-01-25 22:26:29 +00008041 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008042 char_u *p, *r, *s;
8043
Bram Moolenaar33570922005-01-25 22:26:29 +00008044 len = (function ? 13 : 3);
8045 if (str != NULL)
8046 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008047 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008048 for (p = str; *p != NUL; mb_ptr_adv(p))
8049 if (*p == '\'')
8050 ++len;
8051 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008052 s = r = alloc(len);
8053 if (r != NULL)
8054 {
8055 if (function)
8056 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008057 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008058 r += 10;
8059 }
8060 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008061 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008062 if (str != NULL)
8063 for (p = str; *p != NUL; )
8064 {
8065 if (*p == '\'')
8066 *r++ = '\'';
8067 MB_COPY_CHAR(p, r);
8068 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008069 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008070 if (function)
8071 *r++ = ')';
8072 *r++ = NUL;
8073 }
8074 return s;
8075}
8076
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008077#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008078/*
8079 * Convert the string "text" to a floating point number.
8080 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8081 * this always uses a decimal point.
8082 * Returns the length of the text that was consumed.
8083 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008084 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008085string2float(
8086 char_u *text,
8087 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008088{
8089 char *s = (char *)text;
8090 float_T f;
8091
8092 f = strtod(s, &s);
8093 *value = f;
8094 return (int)((char_u *)s - text);
8095}
8096#endif
8097
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008098/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 * Get the value of an environment variable.
8100 * "arg" is pointing to the '$'. It is advanced to after the name.
8101 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008102 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 */
8104 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008105get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106{
8107 char_u *string = NULL;
8108 int len;
8109 int cc;
8110 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008111 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112
8113 ++*arg;
8114 name = *arg;
8115 len = get_env_len(arg);
8116 if (evaluate)
8117 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008118 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008119 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008120
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008121 cc = name[len];
8122 name[len] = NUL;
8123 /* first try vim_getenv(), fast for normal environment vars */
8124 string = vim_getenv(name, &mustfree);
8125 if (string != NULL && *string != NUL)
8126 {
8127 if (!mustfree)
8128 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008130 else
8131 {
8132 if (mustfree)
8133 vim_free(string);
8134
8135 /* next try expanding things like $VIM and ${HOME} */
8136 string = expand_env_save(name - 1);
8137 if (string != NULL && *string == '$')
8138 {
8139 vim_free(string);
8140 string = NULL;
8141 }
8142 }
8143 name[len] = cc;
8144
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008145 rettv->v_type = VAR_STRING;
8146 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 }
8148
8149 return OK;
8150}
8151
8152/*
8153 * Array with names and number of arguments of all internal functions
8154 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8155 */
8156static struct fst
8157{
8158 char *f_name; /* function name */
8159 char f_min_argc; /* minimal number of arguments */
8160 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008161 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008162 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163} functions[] =
8164{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008165#ifdef FEAT_FLOAT
8166 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008167 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008168#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008169 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008170 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008171 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 {"append", 2, 2, f_append},
8173 {"argc", 0, 0, f_argc},
8174 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008175 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008176 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008177#ifdef FEAT_FLOAT
8178 {"asin", 1, 1, f_asin}, /* WJMc */
8179#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008180 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008181 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008182 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008183 {"assert_false", 1, 2, f_assert_false},
8184 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008185#ifdef FEAT_FLOAT
8186 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008187 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008190 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 {"bufexists", 1, 1, f_bufexists},
8192 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8193 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8194 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8195 {"buflisted", 1, 1, f_buflisted},
8196 {"bufloaded", 1, 1, f_bufloaded},
8197 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008198 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 {"bufwinnr", 1, 1, f_bufwinnr},
8200 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008201 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008202 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008203 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008204#ifdef FEAT_FLOAT
8205 {"ceil", 1, 1, f_ceil},
8206#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008207#ifdef FEAT_CHANNEL
8208 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008209 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8210 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008211 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008212# ifdef FEAT_JOB
8213 {"ch_getjob", 1, 1, f_ch_getjob},
8214# endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008215 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008216 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008217 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008218 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008219 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008220 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8221 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008222 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008223 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008224#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008225 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008226 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008228 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008230#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008231 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008232 {"complete_add", 1, 1, f_complete_add},
8233 {"complete_check", 0, 0, f_complete_check},
8234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008236 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008237#ifdef FEAT_FLOAT
8238 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008239 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008240#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008241 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008243 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008244 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008245 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008247 {"diff_filler", 1, 1, f_diff_filler},
8248 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008249 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008250 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008252 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 {"eventhandler", 0, 0, f_eventhandler},
8254 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008255 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008257#ifdef FEAT_FLOAT
8258 {"exp", 1, 1, f_exp},
8259#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008260 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008261 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008262 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8264 {"filereadable", 1, 1, f_filereadable},
8265 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008266 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008267 {"finddir", 1, 3, f_finddir},
8268 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008269#ifdef FEAT_FLOAT
8270 {"float2nr", 1, 1, f_float2nr},
8271 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008272 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008273#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008274 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 {"fnamemodify", 2, 2, f_fnamemodify},
8276 {"foldclosed", 1, 1, f_foldclosed},
8277 {"foldclosedend", 1, 1, f_foldclosedend},
8278 {"foldlevel", 1, 1, f_foldlevel},
8279 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008280 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008282 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008283 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008284 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008285 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008286 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 {"getchar", 0, 1, f_getchar},
8288 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008289 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 {"getcmdline", 0, 0, f_getcmdline},
8291 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008292 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008293 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008294 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008295 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008296 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008297 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 {"getfsize", 1, 1, f_getfsize},
8299 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008300 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008301 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008302 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008303 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008304 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008305 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008306 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008307 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008309 {"gettabvar", 2, 3, f_gettabvar},
8310 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 {"getwinposx", 0, 0, f_getwinposx},
8312 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008313 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008314 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008315 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008316 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008318 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008319 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008320 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8322 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8323 {"histadd", 2, 2, f_histadd},
8324 {"histdel", 1, 2, f_histdel},
8325 {"histget", 1, 2, f_histget},
8326 {"histnr", 1, 1, f_histnr},
8327 {"hlID", 1, 1, f_hlID},
8328 {"hlexists", 1, 1, f_hlexists},
8329 {"hostname", 0, 0, f_hostname},
8330 {"iconv", 3, 3, f_iconv},
8331 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008332 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008333 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008335 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 {"inputrestore", 0, 0, f_inputrestore},
8337 {"inputsave", 0, 0, f_inputsave},
8338 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008339 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008340 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008342 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008343#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8344 {"isnan", 1, 1, f_isnan},
8345#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008346 {"items", 1, 1, f_items},
Bram Moolenaarcb4b0122016-02-07 14:53:21 +01008347#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01008348# ifdef FEAT_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008349 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01008350# endif
Bram Moolenaar65edff82016-02-21 16:40:11 +01008351 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008352 {"job_start", 1, 2, f_job_start},
8353 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008354 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008355#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008356 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008357 {"js_decode", 1, 1, f_js_decode},
8358 {"js_encode", 1, 1, f_js_encode},
8359 {"json_decode", 1, 1, f_json_decode},
8360 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008361 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008363 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 {"libcall", 3, 3, f_libcall},
8365 {"libcallnr", 3, 3, f_libcallnr},
8366 {"line", 1, 1, f_line},
8367 {"line2byte", 1, 1, f_line2byte},
8368 {"lispindent", 1, 1, f_lispindent},
8369 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008370#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008371 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008372 {"log10", 1, 1, f_log10},
8373#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008374#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008375 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008376#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008377 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008378 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008379 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008380 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008381 {"matchadd", 2, 5, f_matchadd},
8382 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008383 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008384 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008385 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008386 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008387 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008388 {"max", 1, 1, f_max},
8389 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008390#ifdef vim_mkdir
8391 {"mkdir", 1, 3, f_mkdir},
8392#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008393 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008394#ifdef FEAT_MZSCHEME
8395 {"mzeval", 1, 1, f_mzeval},
8396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008398 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008399 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008400 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008401#ifdef FEAT_PERL
8402 {"perleval", 1, 1, f_perleval},
8403#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008404#ifdef FEAT_FLOAT
8405 {"pow", 2, 2, f_pow},
8406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008408 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008409 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008410#ifdef FEAT_PYTHON3
8411 {"py3eval", 1, 1, f_py3eval},
8412#endif
8413#ifdef FEAT_PYTHON
8414 {"pyeval", 1, 1, f_pyeval},
8415#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008416 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008417 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008418 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008419#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008420 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008421#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008422 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 {"remote_expr", 2, 3, f_remote_expr},
8424 {"remote_foreground", 1, 1, f_remote_foreground},
8425 {"remote_peek", 1, 2, f_remote_peek},
8426 {"remote_read", 1, 1, f_remote_read},
8427 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008428 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008430 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008432 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008433#ifdef FEAT_FLOAT
8434 {"round", 1, 1, f_round},
8435#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008436 {"screenattr", 2, 2, f_screenattr},
8437 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008438 {"screencol", 0, 0, f_screencol},
8439 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008440 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008441 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008442 {"searchpair", 3, 7, f_searchpair},
8443 {"searchpairpos", 3, 7, f_searchpairpos},
8444 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 {"server2client", 2, 2, f_server2client},
8446 {"serverlist", 0, 0, f_serverlist},
8447 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008448 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008450 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008452 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008453 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008454 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008455 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008457 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008458 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008460#ifdef FEAT_CRYPT
8461 {"sha256", 1, 1, f_sha256},
8462#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008463 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008464 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008466#ifdef FEAT_FLOAT
8467 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008468 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008469#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008470 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008471 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008472 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008473 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008474 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008475#ifdef FEAT_FLOAT
8476 {"sqrt", 1, 1, f_sqrt},
8477 {"str2float", 1, 1, f_str2float},
8478#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008479 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008480 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008481 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482#ifdef HAVE_STRFTIME
8483 {"strftime", 1, 2, f_strftime},
8484#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008485 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008486 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 {"strlen", 1, 1, f_strlen},
8488 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008489 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008491 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008492 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 {"substitute", 4, 4, f_substitute},
8494 {"synID", 3, 3, f_synID},
8495 {"synIDattr", 2, 3, f_synIDattr},
8496 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008497 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008498 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008499 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008500 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008501 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008502 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008503 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008504 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008505 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008506#ifdef FEAT_FLOAT
8507 {"tan", 1, 1, f_tan},
8508 {"tanh", 1, 1, f_tanh},
8509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008511 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 {"tolower", 1, 1, f_tolower},
8513 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008514 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008515#ifdef FEAT_FLOAT
8516 {"trunc", 1, 1, f_trunc},
8517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008519 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008520 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008521 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008522 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 {"virtcol", 1, 1, f_virtcol},
8524 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008525 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 {"winbufnr", 1, 1, f_winbufnr},
8527 {"wincol", 0, 0, f_wincol},
8528 {"winheight", 1, 1, f_winheight},
8529 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008530 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008532 {"winrestview", 1, 1, f_winrestview},
8533 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008535 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008536 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008537 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538};
8539
8540#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8541
8542/*
8543 * Function given to ExpandGeneric() to obtain the list of internal
8544 * or user defined function names.
8545 */
8546 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008547get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548{
8549 static int intidx = -1;
8550 char_u *name;
8551
8552 if (idx == 0)
8553 intidx = -1;
8554 if (intidx < 0)
8555 {
8556 name = get_user_func_name(xp, idx);
8557 if (name != NULL)
8558 return name;
8559 }
8560 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8561 {
8562 STRCPY(IObuff, functions[intidx].f_name);
8563 STRCAT(IObuff, "(");
8564 if (functions[intidx].f_max_argc == 0)
8565 STRCAT(IObuff, ")");
8566 return IObuff;
8567 }
8568
8569 return NULL;
8570}
8571
8572/*
8573 * Function given to ExpandGeneric() to obtain the list of internal or
8574 * user defined variable or function names.
8575 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008577get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578{
8579 static int intidx = -1;
8580 char_u *name;
8581
8582 if (idx == 0)
8583 intidx = -1;
8584 if (intidx < 0)
8585 {
8586 name = get_function_name(xp, idx);
8587 if (name != NULL)
8588 return name;
8589 }
8590 return get_user_var_name(xp, ++intidx);
8591}
8592
8593#endif /* FEAT_CMDL_COMPL */
8594
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008595#if defined(EBCDIC) || defined(PROTO)
8596/*
8597 * Compare struct fst by function name.
8598 */
8599 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008600compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008601{
8602 struct fst *p1 = (struct fst *)s1;
8603 struct fst *p2 = (struct fst *)s2;
8604
8605 return STRCMP(p1->f_name, p2->f_name);
8606}
8607
8608/*
8609 * Sort the function table by function name.
8610 * The sorting of the table above is ASCII dependant.
8611 * On machines using EBCDIC we have to sort it.
8612 */
8613 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008614sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008615{
8616 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8617
8618 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8619}
8620#endif
8621
8622
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623/*
8624 * Find internal function in table above.
8625 * Return index, or -1 if not found
8626 */
8627 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008628find_internal_func(
8629 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630{
8631 int first = 0;
8632 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8633 int cmp;
8634 int x;
8635
8636 /*
8637 * Find the function name in the table. Binary search.
8638 */
8639 while (first <= last)
8640 {
8641 x = first + ((unsigned)(last - first) >> 1);
8642 cmp = STRCMP(name, functions[x].f_name);
8643 if (cmp < 0)
8644 last = x - 1;
8645 else if (cmp > 0)
8646 first = x + 1;
8647 else
8648 return x;
8649 }
8650 return -1;
8651}
8652
8653/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008654 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8655 * name it contains, otherwise return "name".
8656 */
8657 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008658deref_func_name(char_u *name, int *lenp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008659{
Bram Moolenaar33570922005-01-25 22:26:29 +00008660 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008661 int cc;
8662
8663 cc = name[*lenp];
8664 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008665 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008666 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008667 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008668 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008669 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008670 {
8671 *lenp = 0;
8672 return (char_u *)""; /* just in case */
8673 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008674 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008675 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008676 }
8677
8678 return name;
8679}
8680
8681/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682 * Allocate a variable for the result of a function.
8683 * Return OK or FAIL.
8684 */
8685 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008686get_func_tv(
8687 char_u *name, /* name of the function */
8688 int len, /* length of "name" */
8689 typval_T *rettv,
8690 char_u **arg, /* argument, pointing to the '(' */
8691 linenr_T firstline, /* first line of range */
8692 linenr_T lastline, /* last line of range */
8693 int *doesrange, /* return: function handled range */
8694 int evaluate,
8695 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696{
8697 char_u *argp;
8698 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008699 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 int argcount = 0; /* number of arguments found */
8701
8702 /*
8703 * Get the arguments.
8704 */
8705 argp = *arg;
8706 while (argcount < MAX_FUNC_ARGS)
8707 {
8708 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8709 if (*argp == ')' || *argp == ',' || *argp == NUL)
8710 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8712 {
8713 ret = FAIL;
8714 break;
8715 }
8716 ++argcount;
8717 if (*argp != ',')
8718 break;
8719 }
8720 if (*argp == ')')
8721 ++argp;
8722 else
8723 ret = FAIL;
8724
8725 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008726 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008727 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008729 {
8730 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008731 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008732 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008733 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735
8736 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008737 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738
8739 *arg = skipwhite(argp);
8740 return ret;
8741}
8742
8743
8744/*
8745 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008746 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008747 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008749 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008750call_func(
8751 char_u *funcname, /* name of the function */
8752 int len, /* length of "name" */
8753 typval_T *rettv, /* return value goes here */
8754 int argcount, /* number of "argvars" */
8755 typval_T *argvars, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008756 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008757 linenr_T firstline, /* first line of range */
8758 linenr_T lastline, /* last line of range */
8759 int *doesrange, /* return: function handled range */
8760 int evaluate,
8761 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762{
8763 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764#define ERROR_UNKNOWN 0
8765#define ERROR_TOOMANY 1
8766#define ERROR_TOOFEW 2
8767#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008768#define ERROR_DICT 4
8769#define ERROR_NONE 5
8770#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 int error = ERROR_NONE;
8772 int i;
8773 int llen;
8774 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775#define FLEN_FIXED 40
8776 char_u fname_buf[FLEN_FIXED + 1];
8777 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008778 char_u *name;
8779
8780 /* Make a copy of the name, if it comes from a funcref variable it could
8781 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008782 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008783 if (name == NULL)
8784 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785
8786 /*
8787 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8788 * Change <SNR>123_name() to K_SNR 123_name().
8789 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8790 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791 llen = eval_fname_script(name);
8792 if (llen > 0)
8793 {
8794 fname_buf[0] = K_SPECIAL;
8795 fname_buf[1] = KS_EXTRA;
8796 fname_buf[2] = (int)KE_SNR;
8797 i = 3;
8798 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8799 {
8800 if (current_SID <= 0)
8801 error = ERROR_SCRIPT;
8802 else
8803 {
8804 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8805 i = (int)STRLEN(fname_buf);
8806 }
8807 }
8808 if (i + STRLEN(name + llen) < FLEN_FIXED)
8809 {
8810 STRCPY(fname_buf + i, name + llen);
8811 fname = fname_buf;
8812 }
8813 else
8814 {
8815 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8816 if (fname == NULL)
8817 error = ERROR_OTHER;
8818 else
8819 {
8820 mch_memmove(fname, fname_buf, (size_t)i);
8821 STRCPY(fname + i, name + llen);
8822 }
8823 }
8824 }
8825 else
8826 fname = name;
8827
8828 *doesrange = FALSE;
8829
8830
8831 /* execute the function if no errors detected and executing */
8832 if (evaluate && error == ERROR_NONE)
8833 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008834 char_u *rfname = fname;
8835
8836 /* Ignore "g:" before a function name. */
8837 if (fname[0] == 'g' && fname[1] == ':')
8838 rfname = fname + 2;
8839
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008840 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8841 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 error = ERROR_UNKNOWN;
8843
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008844 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 {
8846 /*
8847 * User defined function.
8848 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008849 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008850
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008852 /* Trigger FuncUndefined event, may load the function. */
8853 if (fp == NULL
8854 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008855 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008856 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008858 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008859 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 }
8861#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008862 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008863 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008864 {
8865 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008866 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008867 }
8868
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 if (fp != NULL)
8870 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008871 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008873 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008875 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008877 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008878 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879 else
8880 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008881 int did_save_redo = FALSE;
8882
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 /*
8884 * Call the user function.
8885 * Save and restore search patterns, script variables and
8886 * redo buffer.
8887 */
8888 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008889#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008890 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008891#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008892 {
8893 saveRedobuff();
8894 did_save_redo = TRUE;
8895 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008896 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008897 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008898 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008899 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8900 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8901 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008902 /* Function was unreferenced while being used, free it
8903 * now. */
8904 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008905 if (did_save_redo)
8906 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 restore_search_patterns();
8908 error = ERROR_NONE;
8909 }
8910 }
8911 }
8912 else
8913 {
8914 /*
8915 * Find the function name in the table, call its implementation.
8916 */
8917 i = find_internal_func(fname);
8918 if (i >= 0)
8919 {
8920 if (argcount < functions[i].f_min_argc)
8921 error = ERROR_TOOFEW;
8922 else if (argcount > functions[i].f_max_argc)
8923 error = ERROR_TOOMANY;
8924 else
8925 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008926 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008927 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 error = ERROR_NONE;
8929 }
8930 }
8931 }
8932 /*
8933 * The function call (or "FuncUndefined" autocommand sequence) might
8934 * have been aborted by an error, an interrupt, or an explicitly thrown
8935 * exception that has not been caught so far. This situation can be
8936 * tested for by calling aborting(). For an error in an internal
8937 * function or for the "E132" error in call_user_func(), however, the
8938 * throw point at which the "force_abort" flag (temporarily reset by
8939 * emsg()) is normally updated has not been reached yet. We need to
8940 * update that flag first to make aborting() reliable.
8941 */
8942 update_force_abort();
8943 }
8944 if (error == ERROR_NONE)
8945 ret = OK;
8946
8947 /*
8948 * Report an error unless the argument evaluation or function call has been
8949 * cancelled due to an aborting error, an interrupt, or an exception.
8950 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008951 if (!aborting())
8952 {
8953 switch (error)
8954 {
8955 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008956 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008957 break;
8958 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008959 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008960 break;
8961 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008962 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008963 name);
8964 break;
8965 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008966 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008967 name);
8968 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008969 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008970 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008971 name);
8972 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008973 }
8974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976 if (fname != name && fname != fname_buf)
8977 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008978 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979
8980 return ret;
8981}
8982
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008983/*
8984 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008985 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008986 */
8987 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008988emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008989{
8990 char_u *p;
8991
8992 if (*name == K_SPECIAL)
8993 p = concat_str((char_u *)"<SNR>", name + 3);
8994 else
8995 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008996 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008997 if (p != name)
8998 vim_free(p);
8999}
9000
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009001/*
9002 * Return TRUE for a non-zero Number and a non-empty String.
9003 */
9004 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009005non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009006{
9007 return ((argvars[0].v_type == VAR_NUMBER
9008 && argvars[0].vval.v_number != 0)
9009 || (argvars[0].v_type == VAR_STRING
9010 && argvars[0].vval.v_string != NULL
9011 && *argvars[0].vval.v_string != NUL));
9012}
9013
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014/*********************************************
9015 * Implementation of the built-in functions
9016 */
9017
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009018#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009019static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009020
9021/*
9022 * Get the float value of "argvars[0]" into "f".
9023 * Returns FAIL when the argument is not a Number or Float.
9024 */
9025 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009026get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009027{
9028 if (argvars[0].v_type == VAR_FLOAT)
9029 {
9030 *f = argvars[0].vval.v_float;
9031 return OK;
9032 }
9033 if (argvars[0].v_type == VAR_NUMBER)
9034 {
9035 *f = (float_T)argvars[0].vval.v_number;
9036 return OK;
9037 }
9038 EMSG(_("E808: Number or Float required"));
9039 return FAIL;
9040}
9041
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009042/*
9043 * "abs(expr)" function
9044 */
9045 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009046f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009047{
9048 if (argvars[0].v_type == VAR_FLOAT)
9049 {
9050 rettv->v_type = VAR_FLOAT;
9051 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9052 }
9053 else
9054 {
9055 varnumber_T n;
9056 int error = FALSE;
9057
9058 n = get_tv_number_chk(&argvars[0], &error);
9059 if (error)
9060 rettv->vval.v_number = -1;
9061 else if (n > 0)
9062 rettv->vval.v_number = n;
9063 else
9064 rettv->vval.v_number = -n;
9065 }
9066}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009067
9068/*
9069 * "acos()" function
9070 */
9071 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009072f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009073{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009074 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009075
9076 rettv->v_type = VAR_FLOAT;
9077 if (get_float_arg(argvars, &f) == OK)
9078 rettv->vval.v_float = acos(f);
9079 else
9080 rettv->vval.v_float = 0.0;
9081}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009082#endif
9083
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009085 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086 */
9087 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009088f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089{
Bram Moolenaar33570922005-01-25 22:26:29 +00009090 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009092 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009093 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009095 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009096 && !tv_check_lock(l->lv_lock,
9097 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009098 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009099 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009100 }
9101 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009102 EMSG(_(e_listreq));
9103}
9104
9105/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009106 * "alloc_fail(id, countdown, repeat)" function
9107 */
9108 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009109f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009110{
9111 if (argvars[0].v_type != VAR_NUMBER
9112 || argvars[0].vval.v_number <= 0
9113 || argvars[1].v_type != VAR_NUMBER
9114 || argvars[1].vval.v_number < 0
9115 || argvars[2].v_type != VAR_NUMBER)
9116 EMSG(_(e_invarg));
9117 else
9118 {
9119 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009120 if (alloc_fail_id >= aid_last)
9121 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009122 alloc_fail_countdown = argvars[1].vval.v_number;
9123 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009124 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009125 }
9126}
9127
9128/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009129 * "and(expr, expr)" function
9130 */
9131 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009132f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009133{
9134 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9135 & get_tv_number_chk(&argvars[1], NULL);
9136}
9137
9138/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009139 * "append(lnum, string/list)" function
9140 */
9141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009142f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009143{
9144 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009145 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009146 list_T *l = NULL;
9147 listitem_T *li = NULL;
9148 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009149 long added = 0;
9150
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009151 /* When coming here from Insert mode, sync undo, so that this can be
9152 * undone separately from what was previously inserted. */
9153 if (u_sync_once == 2)
9154 {
9155 u_sync_once = 1; /* notify that u_sync() was called */
9156 u_sync(TRUE);
9157 }
9158
Bram Moolenaar0d660222005-01-07 21:51:51 +00009159 lnum = get_tv_lnum(argvars);
9160 if (lnum >= 0
9161 && lnum <= curbuf->b_ml.ml_line_count
9162 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009163 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009164 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009165 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009166 l = argvars[1].vval.v_list;
9167 if (l == NULL)
9168 return;
9169 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009170 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009171 for (;;)
9172 {
9173 if (l == NULL)
9174 tv = &argvars[1]; /* append a string */
9175 else if (li == NULL)
9176 break; /* end of list */
9177 else
9178 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009179 line = get_tv_string_chk(tv);
9180 if (line == NULL) /* type error */
9181 {
9182 rettv->vval.v_number = 1; /* Failed */
9183 break;
9184 }
9185 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009186 ++added;
9187 if (l == NULL)
9188 break;
9189 li = li->li_next;
9190 }
9191
9192 appended_lines_mark(lnum, added);
9193 if (curwin->w_cursor.lnum > lnum)
9194 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009196 else
9197 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198}
9199
9200/*
9201 * "argc()" function
9202 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009204f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009206 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207}
9208
9209/*
9210 * "argidx()" function
9211 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009213f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009215 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216}
9217
9218/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009219 * "arglistid()" function
9220 */
9221 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009222f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009223{
9224 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009225
9226 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009227 wp = find_tabwin(&argvars[0], &argvars[1]);
9228 if (wp != NULL)
9229 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009230}
9231
9232/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233 * "argv(nr)" function
9234 */
9235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009236f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237{
9238 int idx;
9239
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009240 if (argvars[0].v_type != VAR_UNKNOWN)
9241 {
9242 idx = get_tv_number_chk(&argvars[0], NULL);
9243 if (idx >= 0 && idx < ARGCOUNT)
9244 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9245 else
9246 rettv->vval.v_string = NULL;
9247 rettv->v_type = VAR_STRING;
9248 }
9249 else if (rettv_list_alloc(rettv) == OK)
9250 for (idx = 0; idx < ARGCOUNT; ++idx)
9251 list_append_string(rettv->vval.v_list,
9252 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253}
9254
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009255static void prepare_assert_error(garray_T*gap);
9256static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9257static void assert_error(garray_T *gap);
9258static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009259
9260/*
9261 * Prepare "gap" for an assert error and add the sourcing position.
9262 */
9263 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009264prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009265{
9266 char buf[NUMBUFLEN];
9267
9268 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009269 if (sourcing_name != NULL)
9270 {
9271 ga_concat(gap, sourcing_name);
9272 if (sourcing_lnum > 0)
9273 ga_concat(gap, (char_u *)" ");
9274 }
9275 if (sourcing_lnum > 0)
9276 {
9277 sprintf(buf, "line %ld", (long)sourcing_lnum);
9278 ga_concat(gap, (char_u *)buf);
9279 }
9280 if (sourcing_name != NULL || sourcing_lnum > 0)
9281 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009282}
9283
9284/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009285 * Append "str" to "gap", escaping unprintable characters.
9286 * Changes NL to \n, CR to \r, etc.
9287 */
9288 static void
9289ga_concat_esc(garray_T *gap, char_u *str)
9290{
9291 char_u *p;
9292 char_u buf[NUMBUFLEN];
9293
9294 for (p = str; *p != NUL; ++p)
9295 switch (*p)
9296 {
9297 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9298 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9299 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9300 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9301 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9302 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9303 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9304 default:
9305 if (*p < ' ')
9306 {
9307 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9308 ga_concat(gap, buf);
9309 }
9310 else
9311 ga_append(gap, *p);
9312 break;
9313 }
9314}
9315
9316/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009317 * Fill "gap" with information about an assert error.
9318 */
9319 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009320fill_assert_error(
9321 garray_T *gap,
9322 typval_T *opt_msg_tv,
9323 char_u *exp_str,
9324 typval_T *exp_tv,
9325 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009326{
9327 char_u numbuf[NUMBUFLEN];
9328 char_u *tofree;
9329
9330 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9331 {
9332 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9333 vim_free(tofree);
9334 }
9335 else
9336 {
9337 ga_concat(gap, (char_u *)"Expected ");
9338 if (exp_str == NULL)
9339 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009340 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009341 vim_free(tofree);
9342 }
9343 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009344 ga_concat_esc(gap, exp_str);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009345 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009346 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009347 vim_free(tofree);
9348 }
9349}
Bram Moolenaar43345542015-11-29 17:35:35 +01009350
9351/*
9352 * Add an assert error to v:errors.
9353 */
9354 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009355assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009356{
9357 struct vimvar *vp = &vimvars[VV_ERRORS];
9358
9359 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9360 /* Make sure v:errors is a list. */
9361 set_vim_var_list(VV_ERRORS, list_alloc());
9362 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9363}
9364
Bram Moolenaar43345542015-11-29 17:35:35 +01009365/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009366 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009367 */
9368 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009369f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009370{
9371 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009372
9373 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9374 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009375 prepare_assert_error(&ga);
9376 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9377 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009378 ga_clear(&ga);
9379 }
9380}
9381
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009382/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009383 * "assert_exception(string[, msg])" function
9384 */
9385 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009386f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009387{
9388 garray_T ga;
9389 char *error;
9390
9391 error = (char *)get_tv_string_chk(&argvars[0]);
9392 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9393 {
9394 prepare_assert_error(&ga);
9395 ga_concat(&ga, (char_u *)"v:exception is not set");
9396 assert_error(&ga);
9397 ga_clear(&ga);
9398 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009399 else if (error != NULL
9400 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009401 {
9402 prepare_assert_error(&ga);
9403 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9404 &vimvars[VV_EXCEPTION].vv_tv);
9405 assert_error(&ga);
9406 ga_clear(&ga);
9407 }
9408}
9409
9410/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009411 * "assert_fails(cmd [, error])" function
9412 */
9413 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009414f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009415{
9416 char_u *cmd = get_tv_string_chk(&argvars[0]);
9417 garray_T ga;
9418
9419 called_emsg = FALSE;
9420 suppress_errthrow = TRUE;
9421 emsg_silent = TRUE;
9422 do_cmdline_cmd(cmd);
9423 if (!called_emsg)
9424 {
9425 prepare_assert_error(&ga);
9426 ga_concat(&ga, (char_u *)"command did not fail: ");
9427 ga_concat(&ga, cmd);
9428 assert_error(&ga);
9429 ga_clear(&ga);
9430 }
9431 else if (argvars[1].v_type != VAR_UNKNOWN)
9432 {
9433 char_u buf[NUMBUFLEN];
9434 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9435
9436 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9437 {
9438 prepare_assert_error(&ga);
9439 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9440 &vimvars[VV_ERRMSG].vv_tv);
9441 assert_error(&ga);
9442 ga_clear(&ga);
9443 }
9444 }
9445
9446 called_emsg = FALSE;
9447 suppress_errthrow = FALSE;
9448 emsg_silent = FALSE;
9449 emsg_on_display = FALSE;
9450 set_vim_var_string(VV_ERRMSG, NULL, 0);
9451}
9452
9453/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009454 * Common for assert_true() and assert_false().
9455 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009456 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009457assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009458{
9459 int error = FALSE;
9460 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009461
Bram Moolenaar37127922016-02-06 20:29:28 +01009462 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009463 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009464 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009465 if (argvars[0].v_type != VAR_NUMBER
9466 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9467 || error)
9468 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009469 prepare_assert_error(&ga);
9470 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009471 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009472 NULL, &argvars[0]);
9473 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009474 ga_clear(&ga);
9475 }
9476}
9477
9478/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009479 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009480 */
9481 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009482f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009483{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009484 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009485}
9486
9487/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009488 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009489 */
9490 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009491f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009492{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009493 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009494}
9495
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009496#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009497/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009498 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009499 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009500 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009501f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009502{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009503 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009504
9505 rettv->v_type = VAR_FLOAT;
9506 if (get_float_arg(argvars, &f) == OK)
9507 rettv->vval.v_float = asin(f);
9508 else
9509 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009510}
9511
9512/*
9513 * "atan()" function
9514 */
9515 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009516f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009517{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009518 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009519
9520 rettv->v_type = VAR_FLOAT;
9521 if (get_float_arg(argvars, &f) == OK)
9522 rettv->vval.v_float = atan(f);
9523 else
9524 rettv->vval.v_float = 0.0;
9525}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009526
9527/*
9528 * "atan2()" function
9529 */
9530 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009531f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009532{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009533 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009534
9535 rettv->v_type = VAR_FLOAT;
9536 if (get_float_arg(argvars, &fx) == OK
9537 && get_float_arg(&argvars[1], &fy) == OK)
9538 rettv->vval.v_float = atan2(fx, fy);
9539 else
9540 rettv->vval.v_float = 0.0;
9541}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009542#endif
9543
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544/*
9545 * "browse(save, title, initdir, default)" function
9546 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009548f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549{
9550#ifdef FEAT_BROWSE
9551 int save;
9552 char_u *title;
9553 char_u *initdir;
9554 char_u *defname;
9555 char_u buf[NUMBUFLEN];
9556 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009557 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009559 save = get_tv_number_chk(&argvars[0], &error);
9560 title = get_tv_string_chk(&argvars[1]);
9561 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9562 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009564 if (error || title == NULL || initdir == NULL || defname == NULL)
9565 rettv->vval.v_string = NULL;
9566 else
9567 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009568 do_browse(save ? BROWSE_SAVE : 0,
9569 title, defname, NULL, initdir, NULL, curbuf);
9570#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009571 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009572#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009573 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009574}
9575
9576/*
9577 * "browsedir(title, initdir)" function
9578 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009579 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009580f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009581{
9582#ifdef FEAT_BROWSE
9583 char_u *title;
9584 char_u *initdir;
9585 char_u buf[NUMBUFLEN];
9586
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009587 title = get_tv_string_chk(&argvars[0]);
9588 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009589
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009590 if (title == NULL || initdir == NULL)
9591 rettv->vval.v_string = NULL;
9592 else
9593 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009594 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009596 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009598 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599}
9600
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009601static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009602
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603/*
9604 * Find a buffer by number or exact name.
9605 */
9606 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009607find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608{
9609 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009611 if (avar->v_type == VAR_NUMBER)
9612 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009613 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009615 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009616 if (buf == NULL)
9617 {
9618 /* No full path name match, try a match with a URL or a "nofile"
9619 * buffer, these don't use the full path. */
9620 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9621 if (buf->b_fname != NULL
9622 && (path_with_url(buf->b_fname)
9623#ifdef FEAT_QUICKFIX
9624 || bt_nofile(buf)
9625#endif
9626 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009627 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009628 break;
9629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630 }
9631 return buf;
9632}
9633
9634/*
9635 * "bufexists(expr)" function
9636 */
9637 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009638f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009640 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641}
9642
9643/*
9644 * "buflisted(expr)" function
9645 */
9646 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009647f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648{
9649 buf_T *buf;
9650
9651 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009652 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653}
9654
9655/*
9656 * "bufloaded(expr)" function
9657 */
9658 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009659f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660{
9661 buf_T *buf;
9662
9663 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009664 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665}
9666
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667 static buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +01009668buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670 int save_magic;
9671 char_u *save_cpo;
9672 buf_T *buf;
9673
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9675 save_magic = p_magic;
9676 p_magic = TRUE;
9677 save_cpo = p_cpo;
9678 p_cpo = (char_u *)"";
9679
9680 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009681 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682
9683 p_magic = save_magic;
9684 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +01009685 return buf;
9686}
9687
9688/*
9689 * Get buffer by number or pattern.
9690 */
9691 static buf_T *
9692get_buf_tv(typval_T *tv, int curtab_only)
9693{
9694 char_u *name = tv->vval.v_string;
9695 buf_T *buf;
9696
9697 if (tv->v_type == VAR_NUMBER)
9698 return buflist_findnr((int)tv->vval.v_number);
9699 if (tv->v_type != VAR_STRING)
9700 return NULL;
9701 if (name == NULL || *name == NUL)
9702 return curbuf;
9703 if (name[0] == '$' && name[1] == NUL)
9704 return lastbuf;
9705
9706 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707
9708 /* If not found, try expanding the name, like done for bufexists(). */
9709 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009710 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711
9712 return buf;
9713}
9714
9715/*
9716 * "bufname(expr)" function
9717 */
9718 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009719f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720{
9721 buf_T *buf;
9722
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009723 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009725 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009726 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009728 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009730 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731 --emsg_off;
9732}
9733
9734/*
9735 * "bufnr(expr)" function
9736 */
9737 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009738f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739{
9740 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009741 int error = FALSE;
9742 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009744 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009746 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009747 --emsg_off;
9748
9749 /* If the buffer isn't found and the second argument is not zero create a
9750 * new buffer. */
9751 if (buf == NULL
9752 && argvars[1].v_type != VAR_UNKNOWN
9753 && get_tv_number_chk(&argvars[1], &error) != 0
9754 && !error
9755 && (name = get_tv_string_chk(&argvars[0])) != NULL
9756 && !error)
9757 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9758
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009760 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009762 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763}
9764
9765/*
9766 * "bufwinnr(nr)" function
9767 */
9768 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009769f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770{
9771#ifdef FEAT_WINDOWS
9772 win_T *wp;
9773 int winnr = 0;
9774#endif
9775 buf_T *buf;
9776
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009777 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009779 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780#ifdef FEAT_WINDOWS
9781 for (wp = firstwin; wp; wp = wp->w_next)
9782 {
9783 ++winnr;
9784 if (wp->w_buffer == buf)
9785 break;
9786 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009787 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009789 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790#endif
9791 --emsg_off;
9792}
9793
9794/*
9795 * "byte2line(byte)" function
9796 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009798f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799{
9800#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009801 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802#else
9803 long boff = 0;
9804
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009805 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009807 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009809 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810 (linenr_T)0, &boff);
9811#endif
9812}
9813
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009815byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009816{
9817#ifdef FEAT_MBYTE
9818 char_u *t;
9819#endif
9820 char_u *str;
9821 long idx;
9822
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009823 str = get_tv_string_chk(&argvars[0]);
9824 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009825 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009826 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009827 return;
9828
9829#ifdef FEAT_MBYTE
9830 t = str;
9831 for ( ; idx > 0; idx--)
9832 {
9833 if (*t == NUL) /* EOL reached */
9834 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009835 if (enc_utf8 && comp)
9836 t += utf_ptr2len(t);
9837 else
9838 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009839 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009840 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009841#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009842 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009843 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009844#endif
9845}
9846
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009847/*
9848 * "byteidx()" function
9849 */
9850 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009851f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009852{
9853 byteidx(argvars, rettv, FALSE);
9854}
9855
9856/*
9857 * "byteidxcomp()" function
9858 */
9859 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009860f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009861{
9862 byteidx(argvars, rettv, TRUE);
9863}
9864
Bram Moolenaardb913952012-06-29 12:54:53 +02009865 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009866func_call(
9867 char_u *name,
9868 typval_T *args,
9869 dict_T *selfdict,
9870 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009871{
9872 listitem_T *item;
9873 typval_T argv[MAX_FUNC_ARGS + 1];
9874 int argc = 0;
9875 int dummy;
9876 int r = 0;
9877
9878 for (item = args->vval.v_list->lv_first; item != NULL;
9879 item = item->li_next)
9880 {
9881 if (argc == MAX_FUNC_ARGS)
9882 {
9883 EMSG(_("E699: Too many arguments"));
9884 break;
9885 }
9886 /* Make a copy of each argument. This is needed to be able to set
9887 * v_lock to VAR_FIXED in the copy without changing the original list.
9888 */
9889 copy_tv(&item->li_tv, &argv[argc++]);
9890 }
9891
9892 if (item == NULL)
9893 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9894 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9895 &dummy, TRUE, selfdict);
9896
9897 /* Free the arguments. */
9898 while (argc > 0)
9899 clear_tv(&argv[--argc]);
9900
9901 return r;
9902}
9903
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009904/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009905 * "call(func, arglist)" function
9906 */
9907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009908f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009909{
9910 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009911 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009912
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009913 if (argvars[1].v_type != VAR_LIST)
9914 {
9915 EMSG(_(e_listreq));
9916 return;
9917 }
9918 if (argvars[1].vval.v_list == NULL)
9919 return;
9920
9921 if (argvars[0].v_type == VAR_FUNC)
9922 func = argvars[0].vval.v_string;
9923 else
9924 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009925 if (*func == NUL)
9926 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009927
Bram Moolenaare9a41262005-01-15 22:18:47 +00009928 if (argvars[2].v_type != VAR_UNKNOWN)
9929 {
9930 if (argvars[2].v_type != VAR_DICT)
9931 {
9932 EMSG(_(e_dictreq));
9933 return;
9934 }
9935 selfdict = argvars[2].vval.v_dict;
9936 }
9937
Bram Moolenaardb913952012-06-29 12:54:53 +02009938 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009939}
9940
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009941#ifdef FEAT_FLOAT
9942/*
9943 * "ceil({float})" function
9944 */
9945 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009946f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009947{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009948 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009949
9950 rettv->v_type = VAR_FLOAT;
9951 if (get_float_arg(argvars, &f) == OK)
9952 rettv->vval.v_float = ceil(f);
9953 else
9954 rettv->vval.v_float = 0.0;
9955}
9956#endif
9957
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009958#if defined(FEAT_CHANNEL) || defined(FEAT_JOB)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009959/*
9960 * Get a callback from "arg". It can be a Funcref or a function name.
9961 * When "arg" is zero return an empty string.
9962 * Return NULL for an invalid argument.
9963 */
9964 static char_u *
9965get_callback(typval_T *arg)
9966{
9967 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
9968 return arg->vval.v_string;
9969 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
9970 return (char_u *)"";
9971 EMSG(_("E999: Invalid callback argument"));
9972 return NULL;
9973}
9974
Bram Moolenaarb6b52522016-02-20 23:30:07 +01009975 static int
9976handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
9977{
9978 char_u *val = get_tv_string(item);
9979
9980 opt->jo_set |= jo;
9981 if (STRCMP(val, "nl") == 0)
9982 *modep = MODE_NL;
9983 else if (STRCMP(val, "raw") == 0)
9984 *modep = MODE_RAW;
9985 else if (STRCMP(val, "js") == 0)
9986 *modep = MODE_JS;
9987 else if (STRCMP(val, "json") == 0)
9988 *modep = MODE_JSON;
9989 else
9990 {
9991 EMSG2(_(e_invarg2), val);
9992 return FAIL;
9993 }
9994 return OK;
9995}
9996
Bram Moolenaar187db502016-02-27 14:44:26 +01009997 static int
9998handle_io(typval_T *item, int part, jobopt_T *opt)
9999{
10000 char_u *val = get_tv_string(item);
10001
10002 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
10003 if (STRCMP(val, "null") == 0)
10004 opt->jo_io[part] = JIO_NULL;
10005 else if (STRCMP(val, "pipe") == 0)
10006 opt->jo_io[part] = JIO_PIPE;
10007 else if (STRCMP(val, "file") == 0)
10008 opt->jo_io[part] = JIO_FILE;
10009 else if (STRCMP(val, "buffer") == 0)
10010 opt->jo_io[part] = JIO_BUFFER;
10011 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
10012 opt->jo_io[part] = JIO_OUT;
10013 else
10014 {
10015 EMSG2(_(e_invarg2), val);
10016 return FAIL;
10017 }
10018 return OK;
10019}
10020
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010021 static void
10022clear_job_options(jobopt_T *opt)
10023{
10024 vim_memset(opt, 0, sizeof(jobopt_T));
10025}
10026
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010027/*
Bram Moolenaar187db502016-02-27 14:44:26 +010010028 * Get the PART_ number from the first character of an option name.
10029 */
10030 static int
10031part_from_char(int c)
10032{
10033 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
10034}
10035
10036/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010037 * Get the option entries from the dict in "tv", parse them and put the result
10038 * in "opt".
10039 * Only accept options in "supported".
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010040 * If an option value is invalid return FAIL.
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010041 */
10042 static int
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010043get_job_options(typval_T *tv, jobopt_T *opt, int supported)
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010044{
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010045 typval_T *item;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010046 char_u *val;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010047 dict_T *dict;
10048 int todo;
10049 hashitem_T *hi;
Bram Moolenaar187db502016-02-27 14:44:26 +010010050 int part;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010051
Bram Moolenaar8600ace2016-02-19 23:31:40 +010010052 opt->jo_set = 0;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010053 if (tv->v_type == VAR_UNKNOWN)
10054 return OK;
10055 if (tv->v_type != VAR_DICT)
10056 {
10057 EMSG(_(e_invarg));
10058 return FAIL;
10059 }
10060 dict = tv->vval.v_dict;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010061 if (dict == NULL)
10062 return OK;
10063
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010064 todo = (int)dict->dv_hashtab.ht_used;
10065 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
10066 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010067 {
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010068 item = &HI2DI(hi)->di_tv;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010069
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010070 if (STRCMP(hi->hi_key, "mode") == 0)
10071 {
10072 if (!(supported & JO_MODE))
10073 break;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010074 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010075 return FAIL;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010076 }
10077 else if (STRCMP(hi->hi_key, "in-mode") == 0)
10078 {
10079 if (!(supported & JO_IN_MODE))
10080 break;
10081 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
10082 == FAIL)
10083 return FAIL;
10084 }
10085 else if (STRCMP(hi->hi_key, "out-mode") == 0)
10086 {
10087 if (!(supported & JO_OUT_MODE))
10088 break;
10089 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
10090 == FAIL)
10091 return FAIL;
10092 }
10093 else if (STRCMP(hi->hi_key, "err-mode") == 0)
10094 {
10095 if (!(supported & JO_ERR_MODE))
10096 break;
10097 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
10098 == FAIL)
10099 return FAIL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010100 }
Bram Moolenaar187db502016-02-27 14:44:26 +010010101 else if (STRCMP(hi->hi_key, "in-io") == 0
10102 || STRCMP(hi->hi_key, "out-io") == 0
10103 || STRCMP(hi->hi_key, "err-io") == 0)
10104 {
10105 if (!(supported & JO_OUT_IO))
10106 break;
10107 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
10108 return FAIL;
10109 }
10110 else if (STRCMP(hi->hi_key, "in-name") == 0
10111 || STRCMP(hi->hi_key, "out-name") == 0
10112 || STRCMP(hi->hi_key, "err-name") == 0)
10113 {
10114 part = part_from_char(*hi->hi_key);
10115
10116 if (!(supported & JO_OUT_IO))
10117 break;
10118 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
10119 opt->jo_io_name[part] =
10120 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
10121 }
Bram Moolenaar014069a2016-03-03 22:51:40 +010010122 else if (STRCMP(hi->hi_key, "in-top") == 0
10123 || STRCMP(hi->hi_key, "in-bot") == 0)
10124 {
10125 linenr_T *lp;
10126
10127 if (!(supported & JO_OUT_IO))
10128 break;
10129 if (hi->hi_key[3] == 't')
10130 {
10131 lp = &opt->jo_in_top;
10132 opt->jo_set |= JO_IN_TOP;
10133 }
10134 else
10135 {
10136 lp = &opt->jo_in_bot;
10137 opt->jo_set |= JO_IN_BOT;
10138 }
10139 *lp = get_tv_number(item);
10140 if (*lp < 0)
10141 {
10142 EMSG2(_(e_invarg2), get_tv_string(item));
10143 return FAIL;
10144 }
10145 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010146 else if (STRCMP(hi->hi_key, "callback") == 0)
10147 {
10148 if (!(supported & JO_CALLBACK))
10149 break;
10150 opt->jo_set |= JO_CALLBACK;
10151 opt->jo_callback = get_callback(item);
10152 if (opt->jo_callback == NULL)
10153 {
10154 EMSG2(_(e_invarg2), "callback");
10155 return FAIL;
10156 }
10157 }
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010158 else if (STRCMP(hi->hi_key, "out-cb") == 0)
10159 {
10160 if (!(supported & JO_OUT_CALLBACK))
10161 break;
10162 opt->jo_set |= JO_OUT_CALLBACK;
10163 opt->jo_out_cb = get_callback(item);
10164 if (opt->jo_out_cb == NULL)
10165 {
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010166 EMSG2(_(e_invarg2), "out-cb");
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010167 return FAIL;
10168 }
10169 }
10170 else if (STRCMP(hi->hi_key, "err-cb") == 0)
10171 {
10172 if (!(supported & JO_ERR_CALLBACK))
10173 break;
10174 opt->jo_set |= JO_ERR_CALLBACK;
10175 opt->jo_err_cb = get_callback(item);
10176 if (opt->jo_err_cb == NULL)
10177 {
10178 EMSG2(_(e_invarg2), "err-cb");
10179 return FAIL;
10180 }
10181 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +010010182 else if (STRCMP(hi->hi_key, "close-cb") == 0)
10183 {
10184 if (!(supported & JO_CLOSE_CALLBACK))
10185 break;
10186 opt->jo_set |= JO_CLOSE_CALLBACK;
10187 opt->jo_close_cb = get_callback(item);
10188 if (opt->jo_close_cb == NULL)
10189 {
10190 EMSG2(_(e_invarg2), "close-cb");
10191 return FAIL;
10192 }
10193 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010194 else if (STRCMP(hi->hi_key, "waittime") == 0)
10195 {
10196 if (!(supported & JO_WAITTIME))
10197 break;
10198 opt->jo_set |= JO_WAITTIME;
10199 opt->jo_waittime = get_tv_number(item);
10200 }
10201 else if (STRCMP(hi->hi_key, "timeout") == 0)
10202 {
10203 if (!(supported & JO_TIMEOUT))
10204 break;
10205 opt->jo_set |= JO_TIMEOUT;
10206 opt->jo_timeout = get_tv_number(item);
10207 }
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010208 else if (STRCMP(hi->hi_key, "out-timeout") == 0)
10209 {
10210 if (!(supported & JO_OUT_TIMEOUT))
10211 break;
10212 opt->jo_set |= JO_OUT_TIMEOUT;
10213 opt->jo_out_timeout = get_tv_number(item);
10214 }
10215 else if (STRCMP(hi->hi_key, "err-timeout") == 0)
10216 {
10217 if (!(supported & JO_ERR_TIMEOUT))
10218 break;
10219 opt->jo_set |= JO_ERR_TIMEOUT;
10220 opt->jo_err_timeout = get_tv_number(item);
10221 }
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010222 else if (STRCMP(hi->hi_key, "part") == 0)
10223 {
10224 if (!(supported & JO_PART))
10225 break;
10226 opt->jo_set |= JO_PART;
10227 val = get_tv_string(item);
10228 if (STRCMP(val, "err") == 0)
10229 opt->jo_part = PART_ERR;
10230 else
10231 {
10232 EMSG2(_(e_invarg2), val);
10233 return FAIL;
10234 }
10235 }
10236 else if (STRCMP(hi->hi_key, "id") == 0)
10237 {
10238 if (!(supported & JO_ID))
10239 break;
10240 opt->jo_set |= JO_ID;
10241 opt->jo_id = get_tv_number(item);
10242 }
Bram Moolenaar65edff82016-02-21 16:40:11 +010010243 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
10244 {
10245 if (!(supported & JO_STOPONEXIT))
10246 break;
10247 opt->jo_set |= JO_STOPONEXIT;
10248 opt->jo_stoponexit = get_tv_string_buf_chk(item,
10249 opt->jo_soe_buf);
10250 if (opt->jo_stoponexit == NULL)
10251 {
10252 EMSG2(_(e_invarg2), "stoponexit");
10253 return FAIL;
10254 }
10255 }
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010256 else if (STRCMP(hi->hi_key, "exit-cb") == 0)
10257 {
10258 if (!(supported & JO_EXIT_CB))
10259 break;
10260 opt->jo_set |= JO_EXIT_CB;
10261 opt->jo_exit_cb = get_tv_string_buf_chk(item, opt->jo_ecb_buf);
Bram Moolenaar5e838402016-02-21 23:12:41 +010010262 if (opt->jo_exit_cb == NULL)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010263 {
10264 EMSG2(_(e_invarg2), "exit-cb");
10265 return FAIL;
10266 }
10267 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010268 else
10269 break;
10270 --todo;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010271 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010272 if (todo > 0)
10273 {
10274 EMSG2(_(e_invarg2), hi->hi_key);
10275 return FAIL;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010276 }
10277
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010278 return OK;
10279}
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010280#endif
10281
10282#ifdef FEAT_CHANNEL
10283/*
10284 * Get the channel from the argument.
10285 * Returns NULL if the handle is invalid.
10286 */
10287 static channel_T *
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010288get_channel_arg(typval_T *tv, int check_open)
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010289{
Bram Moolenaar151f6562016-03-07 21:19:38 +010010290 channel_T *channel = NULL;
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010291
Bram Moolenaar151f6562016-03-07 21:19:38 +010010292 if (tv->v_type == VAR_JOB)
10293 {
10294 if (tv->vval.v_job != NULL)
10295 channel = tv->vval.v_job->jv_channel;
10296 }
10297 else if (tv->v_type == VAR_CHANNEL)
10298 {
10299 channel = tv->vval.v_channel;
10300 }
10301 else
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010302 {
10303 EMSG2(_(e_invarg2), get_tv_string(tv));
10304 return NULL;
10305 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010306
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010307 if (check_open && (channel == NULL || !channel_is_open(channel)))
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010308 {
10309 EMSG(_("E906: not an open channel"));
10310 return NULL;
10311 }
10312 return channel;
10313}
10314
10315/*
10316 * "ch_close()" function
10317 */
10318 static void
10319f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10320{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010321 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010322
10323 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010324 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010325 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010326 channel_clear(channel);
10327 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010328}
10329
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010330/*
10331 * "ch_getbufnr()" function
10332 */
10333 static void
10334f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10335{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010336 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010337
10338 rettv->vval.v_number = -1;
10339 if (channel != NULL)
10340 {
10341 char_u *what = get_tv_string(&argvars[1]);
10342 int part;
10343
10344 if (STRCMP(what, "err") == 0)
10345 part = PART_ERR;
10346 else if (STRCMP(what, "out") == 0)
10347 part = PART_OUT;
10348 else if (STRCMP(what, "in") == 0)
10349 part = PART_IN;
10350 else
10351 part = PART_SOCK;
10352 if (channel->ch_part[part].ch_buffer != NULL)
10353 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10354 }
10355}
10356
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010357# ifdef FEAT_JOB
10358/*
10359 * "ch_getjob()" function
10360 */
10361 static void
10362f_ch_getjob(typval_T *argvars, typval_T *rettv)
10363{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010364 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010365
10366 if (channel != NULL)
10367 {
10368 rettv->v_type = VAR_JOB;
10369 rettv->vval.v_job = channel->ch_job;
10370 if (channel->ch_job != NULL)
10371 ++channel->ch_job->jv_refcount;
10372 }
10373}
10374# endif
10375
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010376/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010377 * "ch_log()" function
10378 */
10379 static void
10380f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10381{
10382 char_u *msg = get_tv_string(&argvars[0]);
10383 channel_T *channel = NULL;
10384
10385 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010386 channel = get_channel_arg(&argvars[1], TRUE);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010387
10388 ch_log(channel, (char *)msg);
10389}
10390
10391/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010392 * "ch_logfile()" function
10393 */
10394 static void
10395f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10396{
10397 char_u *fname;
10398 char_u *opt = (char_u *)"";
10399 char_u buf[NUMBUFLEN];
10400 FILE *file = NULL;
10401
10402 fname = get_tv_string(&argvars[0]);
10403 if (argvars[1].v_type == VAR_STRING)
10404 opt = get_tv_string_buf(&argvars[1], buf);
10405 if (*fname != NUL)
10406 {
10407 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
10408 if (file == NULL)
10409 {
10410 EMSG2(_(e_notopen), fname);
10411 return;
10412 }
10413 }
10414 ch_logfile(file);
10415}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010416
10417/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010418 * "ch_open()" function
10419 */
10420 static void
10421f_ch_open(typval_T *argvars, typval_T *rettv)
10422{
10423 char_u *address;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010424 char_u *p;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010425 char *rest;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010426 int port;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010427 jobopt_T opt;
Bram Moolenaar77073442016-02-13 23:23:53 +010010428 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010429
10430 /* default: fail */
Bram Moolenaar77073442016-02-13 23:23:53 +010010431 rettv->v_type = VAR_CHANNEL;
10432 rettv->vval.v_channel = NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010433
10434 address = get_tv_string(&argvars[0]);
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010435 if (argvars[1].v_type != VAR_UNKNOWN
10436 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010437 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010438 EMSG(_(e_invarg));
10439 return;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010440 }
10441
10442 /* parse address */
10443 p = vim_strchr(address, ':');
10444 if (p == NULL)
10445 {
10446 EMSG2(_(e_invarg2), address);
10447 return;
10448 }
10449 *p++ = NUL;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010450 port = strtol((char *)p, &rest, 10);
10451 if (*address == NUL || port <= 0 || *rest != NUL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010452 {
10453 p[-1] = ':';
10454 EMSG2(_(e_invarg2), address);
10455 return;
10456 }
10457
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010458 /* parse options */
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010459 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010460 opt.jo_mode = MODE_JSON;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010461 opt.jo_timeout = 2000;
10462 if (get_job_options(&argvars[1], &opt,
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010463 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010464 return;
10465 if (opt.jo_timeout < 0)
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010466 {
10467 EMSG(_(e_invarg));
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010468 return;
10469 }
10470
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010471 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
Bram Moolenaar77073442016-02-13 23:23:53 +010010472 if (channel != NULL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010473 {
Bram Moolenaar7b3ca762016-02-14 19:13:43 +010010474 rettv->vval.v_channel = channel;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010475 opt.jo_set = JO_ALL;
10476 channel_set_options(channel, &opt);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010477 }
10478}
10479
10480/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010481 * Common for ch_read() and ch_readraw().
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010482 */
10483 static void
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010484common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010485{
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010486 channel_T *channel;
10487 int part;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010488 jobopt_T opt;
10489 int mode;
10490 int timeout;
10491 int id = -1;
10492 typval_T *listtv = NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010493
10494 /* return an empty string by default */
10495 rettv->v_type = VAR_STRING;
10496 rettv->vval.v_string = NULL;
10497
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010498 clear_job_options(&opt);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010499 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
10500 == FAIL)
10501 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010502
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010503 channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar77073442016-02-13 23:23:53 +010010504 if (channel != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010505 {
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010506 if (opt.jo_set & JO_PART)
10507 part = opt.jo_part;
10508 else
10509 part = channel_part_read(channel);
10510 mode = channel_get_mode(channel, part);
10511 timeout = channel_get_timeout(channel, part);
10512 if (opt.jo_set & JO_TIMEOUT)
10513 timeout = opt.jo_timeout;
10514
10515 if (raw || mode == MODE_RAW || mode == MODE_NL)
10516 rettv->vval.v_string = channel_read_block(channel, part, timeout);
10517 else
10518 {
10519 if (opt.jo_set & JO_ID)
10520 id = opt.jo_id;
10521 channel_read_json_block(channel, part, timeout, id, &listtv);
10522 if (listtv != NULL)
Bram Moolenaard6051b52016-02-28 15:49:03 +010010523 {
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010524 *rettv = *listtv;
Bram Moolenaard6051b52016-02-28 15:49:03 +010010525 vim_free(listtv);
10526 }
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010527 else
10528 {
10529 rettv->v_type = VAR_SPECIAL;
10530 rettv->vval.v_number = VVAL_NONE;
10531 }
10532 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010533 }
Bram Moolenaar77073442016-02-13 23:23:53 +010010534}
10535
10536/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010537 * "ch_read()" function
10538 */
10539 static void
10540f_ch_read(typval_T *argvars, typval_T *rettv)
10541{
10542 common_channel_read(argvars, rettv, FALSE);
10543}
10544
10545/*
10546 * "ch_readraw()" function
10547 */
10548 static void
10549f_ch_readraw(typval_T *argvars, typval_T *rettv)
10550{
10551 common_channel_read(argvars, rettv, TRUE);
10552}
10553
10554/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010555 * common for "sendexpr()" and "sendraw()"
Bram Moolenaar77073442016-02-13 23:23:53 +010010556 * Returns the channel if the caller should read the response.
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010557 * Sets "part_read" to the the read fd.
Bram Moolenaar77073442016-02-13 23:23:53 +010010558 * Otherwise returns NULL.
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010559 */
Bram Moolenaar77073442016-02-13 23:23:53 +010010560 static channel_T *
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010561send_common(
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010562 typval_T *argvars,
10563 char_u *text,
10564 int id,
10565 int eval,
10566 jobopt_T *opt,
10567 char *fun,
10568 int *part_read)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010569{
Bram Moolenaar77073442016-02-13 23:23:53 +010010570 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010571 int part_send;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010572
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010573 channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar77073442016-02-13 23:23:53 +010010574 if (channel == NULL)
10575 return NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010576 part_send = channel_part_send(channel);
10577 *part_read = channel_part_read(channel);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010578
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010579 clear_job_options(opt);
10580 if (get_job_options(&argvars[2], opt, JO_CALLBACK + JO_TIMEOUT) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010581 return NULL;
10582
Bram Moolenaara07fec92016-02-05 21:04:08 +010010583 /* Set the callback. An empty callback means no callback and not reading
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010584 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
10585 * allowed. */
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010586 if (opt->jo_callback != NULL && *opt->jo_callback != NUL)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010587 {
10588 if (eval)
10589 {
10590 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
10591 return NULL;
10592 }
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010593 channel_set_req_callback(channel, part_send, opt->jo_callback, id);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010594 }
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010595
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010596 if (channel_send(channel, part_send, text, fun) == OK
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010597 && opt->jo_callback == NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +010010598 return channel;
10599 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010600}
10601
10602/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010603 * common for "ch_evalexpr()" and "ch_sendexpr()"
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010604 */
10605 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010606ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010607{
10608 char_u *text;
10609 typval_T *listtv;
Bram Moolenaar77073442016-02-13 23:23:53 +010010610 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010611 int id;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010612 ch_mode_T ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010613 int part_send;
10614 int part_read;
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010615 jobopt_T opt;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010616 int timeout;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010617
10618 /* return an empty string by default */
10619 rettv->v_type = VAR_STRING;
10620 rettv->vval.v_string = NULL;
10621
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010622 channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar77073442016-02-13 23:23:53 +010010623 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010624 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010625 part_send = channel_part_send(channel);
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010626
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010627 ch_mode = channel_get_mode(channel, part_send);
10628 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010629 {
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010630 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010631 return;
10632 }
10633
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010634 id = channel_get_id();
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010635 text = json_encode_nr_expr(id, &argvars[1],
10636 ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010637 if (text == NULL)
10638 return;
10639
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010640 channel = send_common(argvars, text, id, eval, &opt,
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010641 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +010010642 vim_free(text);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010643 if (channel != NULL && eval)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010644 {
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010645 if (opt.jo_set & JO_TIMEOUT)
10646 timeout = opt.jo_timeout;
10647 else
10648 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010649 timeout = channel_get_timeout(channel, part_read);
10650 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
10651 == OK)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010652 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010653 list_T *list = listtv->vval.v_list;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010654
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010655 /* Move the item from the list and then change the type to
10656 * avoid the value being freed. */
10657 *rettv = list->lv_last->li_tv;
10658 list->lv_last->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar77073442016-02-13 23:23:53 +010010659 free_tv(listtv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010660 }
10661 }
10662}
10663
10664/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010665 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010666 */
10667 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010668f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10669{
10670 ch_expr_common(argvars, rettv, TRUE);
10671}
10672
10673/*
10674 * "ch_sendexpr()" function
10675 */
10676 static void
10677f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10678{
10679 ch_expr_common(argvars, rettv, FALSE);
10680}
10681
10682/*
10683 * common for "ch_evalraw()" and "ch_sendraw()"
10684 */
10685 static void
10686ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010687{
10688 char_u buf[NUMBUFLEN];
10689 char_u *text;
Bram Moolenaar77073442016-02-13 23:23:53 +010010690 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010691 int part_read;
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010692 jobopt_T opt;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010693 int timeout;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010694
10695 /* return an empty string by default */
10696 rettv->v_type = VAR_STRING;
10697 rettv->vval.v_string = NULL;
10698
10699 text = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010700 channel = send_common(argvars, text, 0, eval, &opt,
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010701 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
10702 if (channel != NULL && eval)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010703 {
Bram Moolenaarda94fdf2016-03-03 18:09:10 +010010704 if (opt.jo_set & JO_TIMEOUT)
10705 timeout = opt.jo_timeout;
10706 else
10707 timeout = channel_get_timeout(channel, part_read);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010708 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
10709 }
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010710}
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010711
10712/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010713 * "ch_evalraw()" function
10714 */
10715 static void
10716f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10717{
10718 ch_raw_common(argvars, rettv, TRUE);
10719}
10720
10721/*
10722 * "ch_sendraw()" function
10723 */
10724 static void
10725f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10726{
10727 ch_raw_common(argvars, rettv, FALSE);
10728}
10729
10730/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010731 * "ch_setoptions()" function
10732 */
10733 static void
10734f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10735{
10736 channel_T *channel;
10737 jobopt_T opt;
10738
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010739 channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010740 if (channel == NULL)
10741 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010742 clear_job_options(&opt);
10743 if (get_job_options(&argvars[1], &opt,
10744 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == FAIL)
Bram Moolenaar132006c2016-02-19 22:38:15 +010010745 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010746 channel_set_options(channel, &opt);
10747}
10748
10749/*
10750 * "ch_status()" function
10751 */
10752 static void
10753f_ch_status(typval_T *argvars, typval_T *rettv)
10754{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010755 channel_T *channel;
10756
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010757 /* return an empty string by default */
10758 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010759 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010760
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010761 channel = get_channel_arg(&argvars[0], FALSE);
10762 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010763}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010764#endif
10765
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010766/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010767 * "changenr()" function
10768 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010769 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010770f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010771{
10772 rettv->vval.v_number = curbuf->b_u_seq_cur;
10773}
10774
10775/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776 * "char2nr(string)" function
10777 */
10778 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010779f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010780{
10781#ifdef FEAT_MBYTE
10782 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010783 {
10784 int utf8 = 0;
10785
10786 if (argvars[1].v_type != VAR_UNKNOWN)
10787 utf8 = get_tv_number_chk(&argvars[1], NULL);
10788
10789 if (utf8)
10790 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10791 else
10792 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010794 else
10795#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010796 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797}
10798
10799/*
10800 * "cindent(lnum)" function
10801 */
10802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010803f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010804{
10805#ifdef FEAT_CINDENT
10806 pos_T pos;
10807 linenr_T lnum;
10808
10809 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010810 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010811 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10812 {
10813 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010814 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010815 curwin->w_cursor = pos;
10816 }
10817 else
10818#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010819 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820}
10821
10822/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010823 * "clearmatches()" function
10824 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010826f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010827{
10828#ifdef FEAT_SEARCH_EXTRA
10829 clear_matches(curwin);
10830#endif
10831}
10832
10833/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834 * "col(string)" function
10835 */
10836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010837f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010838{
10839 colnr_T col = 0;
10840 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010841 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010843 fp = var2fpos(&argvars[0], FALSE, &fnum);
10844 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010845 {
10846 if (fp->col == MAXCOL)
10847 {
10848 /* '> can be MAXCOL, get the length of the line then */
10849 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010850 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851 else
10852 col = MAXCOL;
10853 }
10854 else
10855 {
10856 col = fp->col + 1;
10857#ifdef FEAT_VIRTUALEDIT
10858 /* col(".") when the cursor is on the NUL at the end of the line
10859 * because of "coladd" can be seen as an extra column. */
10860 if (virtual_active() && fp == &curwin->w_cursor)
10861 {
10862 char_u *p = ml_get_cursor();
10863
10864 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10865 curwin->w_virtcol - curwin->w_cursor.coladd))
10866 {
10867# ifdef FEAT_MBYTE
10868 int l;
10869
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010870 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010871 col += l;
10872# else
10873 if (*p != NUL && p[1] == NUL)
10874 ++col;
10875# endif
10876 }
10877 }
10878#endif
10879 }
10880 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010881 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010882}
10883
Bram Moolenaar572cb562005-08-05 21:35:02 +000010884#if defined(FEAT_INS_EXPAND)
10885/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010886 * "complete()" function
10887 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010888 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010889f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010890{
10891 int startcol;
10892
10893 if ((State & INSERT) == 0)
10894 {
10895 EMSG(_("E785: complete() can only be used in Insert mode"));
10896 return;
10897 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010898
10899 /* Check for undo allowed here, because if something was already inserted
10900 * the line was already saved for undo and this check isn't done. */
10901 if (!undo_allowed())
10902 return;
10903
Bram Moolenaarade00832006-03-10 21:46:58 +000010904 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10905 {
10906 EMSG(_(e_invarg));
10907 return;
10908 }
10909
10910 startcol = get_tv_number_chk(&argvars[0], NULL);
10911 if (startcol <= 0)
10912 return;
10913
10914 set_completion(startcol - 1, argvars[1].vval.v_list);
10915}
10916
10917/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010918 * "complete_add()" function
10919 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010920 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010921f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010922{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010923 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010924}
10925
10926/*
10927 * "complete_check()" function
10928 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010929 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010930f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010931{
10932 int saved = RedrawingDisabled;
10933
10934 RedrawingDisabled = 0;
10935 ins_compl_check_keys(0);
10936 rettv->vval.v_number = compl_interrupted;
10937 RedrawingDisabled = saved;
10938}
10939#endif
10940
Bram Moolenaar071d4272004-06-13 20:20:40 +000010941/*
10942 * "confirm(message, buttons[, default [, type]])" function
10943 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010945f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946{
10947#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10948 char_u *message;
10949 char_u *buttons = NULL;
10950 char_u buf[NUMBUFLEN];
10951 char_u buf2[NUMBUFLEN];
10952 int def = 1;
10953 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010954 char_u *typestr;
10955 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010957 message = get_tv_string_chk(&argvars[0]);
10958 if (message == NULL)
10959 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010960 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010962 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10963 if (buttons == NULL)
10964 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010965 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010967 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010968 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010969 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010970 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10971 if (typestr == NULL)
10972 error = TRUE;
10973 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010974 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010975 switch (TOUPPER_ASC(*typestr))
10976 {
10977 case 'E': type = VIM_ERROR; break;
10978 case 'Q': type = VIM_QUESTION; break;
10979 case 'I': type = VIM_INFO; break;
10980 case 'W': type = VIM_WARNING; break;
10981 case 'G': type = VIM_GENERIC; break;
10982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010983 }
10984 }
10985 }
10986 }
10987
10988 if (buttons == NULL || *buttons == NUL)
10989 buttons = (char_u *)_("&Ok");
10990
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010991 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010992 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010993 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010994#endif
10995}
10996
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010997/*
10998 * "copy()" function
10999 */
11000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011001f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011002{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011003 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011004}
Bram Moolenaar071d4272004-06-13 20:20:40 +000011005
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011006#ifdef FEAT_FLOAT
11007/*
11008 * "cos()" function
11009 */
11010 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011011f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011012{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011013 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011014
11015 rettv->v_type = VAR_FLOAT;
11016 if (get_float_arg(argvars, &f) == OK)
11017 rettv->vval.v_float = cos(f);
11018 else
11019 rettv->vval.v_float = 0.0;
11020}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011021
11022/*
11023 * "cosh()" function
11024 */
11025 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011026f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011027{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011028 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011029
11030 rettv->v_type = VAR_FLOAT;
11031 if (get_float_arg(argvars, &f) == OK)
11032 rettv->vval.v_float = cosh(f);
11033 else
11034 rettv->vval.v_float = 0.0;
11035}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011036#endif
11037
Bram Moolenaar071d4272004-06-13 20:20:40 +000011038/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011039 * "count()" function
11040 */
11041 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011042f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011043{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011044 long n = 0;
11045 int ic = FALSE;
11046
Bram Moolenaare9a41262005-01-15 22:18:47 +000011047 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011048 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011049 listitem_T *li;
11050 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011051 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011052
Bram Moolenaare9a41262005-01-15 22:18:47 +000011053 if ((l = argvars[0].vval.v_list) != NULL)
11054 {
11055 li = l->lv_first;
11056 if (argvars[2].v_type != VAR_UNKNOWN)
11057 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011058 int error = FALSE;
11059
11060 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011061 if (argvars[3].v_type != VAR_UNKNOWN)
11062 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011063 idx = get_tv_number_chk(&argvars[3], &error);
11064 if (!error)
11065 {
11066 li = list_find(l, idx);
11067 if (li == NULL)
11068 EMSGN(_(e_listidx), idx);
11069 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011070 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011071 if (error)
11072 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011073 }
11074
11075 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010011076 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011077 ++n;
11078 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011079 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011080 else if (argvars[0].v_type == VAR_DICT)
11081 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011082 int todo;
11083 dict_T *d;
11084 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011085
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011086 if ((d = argvars[0].vval.v_dict) != NULL)
11087 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011088 int error = FALSE;
11089
Bram Moolenaare9a41262005-01-15 22:18:47 +000011090 if (argvars[2].v_type != VAR_UNKNOWN)
11091 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011092 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011093 if (argvars[3].v_type != VAR_UNKNOWN)
11094 EMSG(_(e_invarg));
11095 }
11096
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011097 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011098 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011099 {
11100 if (!HASHITEM_EMPTY(hi))
11101 {
11102 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010011103 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011104 ++n;
11105 }
11106 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011107 }
11108 }
11109 else
11110 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011111 rettv->vval.v_number = n;
11112}
11113
11114/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
11116 *
11117 * Checks the existence of a cscope connection.
11118 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011119 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011120f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011121{
11122#ifdef FEAT_CSCOPE
11123 int num = 0;
11124 char_u *dbpath = NULL;
11125 char_u *prepend = NULL;
11126 char_u buf[NUMBUFLEN];
11127
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011128 if (argvars[0].v_type != VAR_UNKNOWN
11129 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011131 num = (int)get_tv_number(&argvars[0]);
11132 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011133 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011134 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011135 }
11136
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011137 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011138#endif
11139}
11140
11141/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011142 * "cursor(lnum, col)" function, or
11143 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011144 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011145 * Moves the cursor to the specified line and column.
11146 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011147 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011148 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011149f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011150{
11151 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011152#ifdef FEAT_VIRTUALEDIT
11153 long coladd = 0;
11154#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011155 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011156
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011157 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011158 if (argvars[1].v_type == VAR_UNKNOWN)
11159 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011160 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020011161 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011162
Bram Moolenaar493c1782014-05-28 14:34:46 +020011163 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011164 {
11165 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000011166 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011167 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011168 line = pos.lnum;
11169 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011170#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011171 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000011172#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020011173 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011174 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020011175 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011176 set_curswant = FALSE;
11177 }
Bram Moolenaara5525202006-03-02 22:52:09 +000011178 }
11179 else
11180 {
11181 line = get_tv_lnum(argvars);
11182 col = get_tv_number_chk(&argvars[1], NULL);
11183#ifdef FEAT_VIRTUALEDIT
11184 if (argvars[2].v_type != VAR_UNKNOWN)
11185 coladd = get_tv_number_chk(&argvars[2], NULL);
11186#endif
11187 }
11188 if (line < 0 || col < 0
11189#ifdef FEAT_VIRTUALEDIT
11190 || coladd < 0
11191#endif
11192 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011193 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011194 if (line > 0)
11195 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011196 if (col > 0)
11197 curwin->w_cursor.col = col - 1;
11198#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000011199 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011200#endif
11201
11202 /* Make sure the cursor is in a valid position. */
11203 check_cursor();
11204#ifdef FEAT_MBYTE
11205 /* Correct cursor for multi-byte character. */
11206 if (has_mbyte)
11207 mb_adjust_cursor();
11208#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000011209
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011210 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011211 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011212}
11213
11214/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011215 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011216 */
11217 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011218f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011219{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011220 int noref = 0;
11221
11222 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011223 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011224 if (noref < 0 || noref > 1)
11225 EMSG(_(e_invarg));
11226 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000011227 {
11228 current_copyID += COPYID_INC;
11229 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
11230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011231}
11232
11233/*
11234 * "delete()" function
11235 */
11236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011237f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011238{
Bram Moolenaarda440d22016-01-16 21:27:23 +010011239 char_u nbuf[NUMBUFLEN];
11240 char_u *name;
11241 char_u *flags;
11242
11243 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011244 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010011245 return;
11246
11247 name = get_tv_string(&argvars[0]);
11248 if (name == NULL || *name == NUL)
11249 {
11250 EMSG(_(e_invarg));
11251 return;
11252 }
11253
11254 if (argvars[1].v_type != VAR_UNKNOWN)
11255 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011256 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010011257 flags = (char_u *)"";
11258
11259 if (*flags == NUL)
11260 /* delete a file */
11261 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
11262 else if (STRCMP(flags, "d") == 0)
11263 /* delete an empty directory */
11264 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
11265 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010011266 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010011267 rettv->vval.v_number = delete_recursive(name);
11268 else
11269 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011270}
11271
11272/*
11273 * "did_filetype()" function
11274 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011275 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011276f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011277{
11278#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011279 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011280#endif
11281}
11282
11283/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000011284 * "diff_filler()" function
11285 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011286 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011287f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011288{
11289#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011290 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000011291#endif
11292}
11293
11294/*
11295 * "diff_hlID()" function
11296 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011297 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011298f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011299{
11300#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011301 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011302 static linenr_T prev_lnum = 0;
11303 static int changedtick = 0;
11304 static int fnum = 0;
11305 static int change_start = 0;
11306 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011307 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011308 int filler_lines;
11309 int col;
11310
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011311 if (lnum < 0) /* ignore type error in {lnum} arg */
11312 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011313 if (lnum != prev_lnum
11314 || changedtick != curbuf->b_changedtick
11315 || fnum != curbuf->b_fnum)
11316 {
11317 /* New line, buffer, change: need to get the values. */
11318 filler_lines = diff_check(curwin, lnum);
11319 if (filler_lines < 0)
11320 {
11321 if (filler_lines == -1)
11322 {
11323 change_start = MAXCOL;
11324 change_end = -1;
11325 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11326 hlID = HLF_ADD; /* added line */
11327 else
11328 hlID = HLF_CHD; /* changed line */
11329 }
11330 else
11331 hlID = HLF_ADD; /* added line */
11332 }
11333 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011334 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011335 prev_lnum = lnum;
11336 changedtick = curbuf->b_changedtick;
11337 fnum = curbuf->b_fnum;
11338 }
11339
11340 if (hlID == HLF_CHD || hlID == HLF_TXD)
11341 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011342 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011343 if (col >= change_start && col <= change_end)
11344 hlID = HLF_TXD; /* changed text */
11345 else
11346 hlID = HLF_CHD; /* changed line */
11347 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011348 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011349#endif
11350}
11351
11352/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010011353 * "disable_char_avail_for_testing({expr})" function
11354 */
11355 static void
11356f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
11357{
11358 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
11359}
11360
11361/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011362 * "empty({expr})" function
11363 */
11364 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011365f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011366{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011367 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011368
11369 switch (argvars[0].v_type)
11370 {
11371 case VAR_STRING:
11372 case VAR_FUNC:
11373 n = argvars[0].vval.v_string == NULL
11374 || *argvars[0].vval.v_string == NUL;
11375 break;
11376 case VAR_NUMBER:
11377 n = argvars[0].vval.v_number == 0;
11378 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011379 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011380#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011381 n = argvars[0].vval.v_float == 0.0;
11382 break;
11383#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011384 case VAR_LIST:
11385 n = argvars[0].vval.v_list == NULL
11386 || argvars[0].vval.v_list->lv_first == NULL;
11387 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011388 case VAR_DICT:
11389 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011390 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011391 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011392 case VAR_SPECIAL:
11393 n = argvars[0].vval.v_number != VVAL_TRUE;
11394 break;
11395
Bram Moolenaar835dc632016-02-07 14:27:38 +010011396 case VAR_JOB:
11397#ifdef FEAT_JOB
Bram Moolenaar77073442016-02-13 23:23:53 +010011398 n = argvars[0].vval.v_job == NULL
11399 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11400 break;
11401#endif
11402 case VAR_CHANNEL:
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010011403#ifdef FEAT_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011404 n = argvars[0].vval.v_channel == NULL
11405 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011406 break;
11407#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011408 case VAR_UNKNOWN:
11409 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11410 n = TRUE;
11411 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011412 }
11413
11414 rettv->vval.v_number = n;
11415}
11416
11417/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011418 * "escape({string}, {chars})" function
11419 */
11420 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011421f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011422{
11423 char_u buf[NUMBUFLEN];
11424
Bram Moolenaar758711c2005-02-02 23:11:38 +000011425 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11426 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011427 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011428}
11429
11430/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011431 * "eval()" function
11432 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011433 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011434f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011435{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011436 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011437
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011438 s = get_tv_string_chk(&argvars[0]);
11439 if (s != NULL)
11440 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011441
Bram Moolenaar615b9972015-01-14 17:15:05 +010011442 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011443 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11444 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011445 if (p != NULL && !aborting())
11446 EMSG2(_(e_invexpr2), p);
11447 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011448 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011449 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011450 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011451 else if (*s != NUL)
11452 EMSG(_(e_trailing));
11453}
11454
11455/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011456 * "eventhandler()" function
11457 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011458 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011459f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011460{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011461 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011462}
11463
11464/*
11465 * "executable()" function
11466 */
11467 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011468f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011469{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011470 char_u *name = get_tv_string(&argvars[0]);
11471
11472 /* Check in $PATH and also check directly if there is a directory name. */
11473 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11474 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011475}
11476
11477/*
11478 * "exepath()" function
11479 */
11480 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011481f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011482{
11483 char_u *p = NULL;
11484
Bram Moolenaarb5971142015-03-21 17:32:19 +010011485 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011486 rettv->v_type = VAR_STRING;
11487 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011488}
11489
11490/*
11491 * "exists()" function
11492 */
11493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011494f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011495{
11496 char_u *p;
11497 char_u *name;
11498 int n = FALSE;
11499 int len = 0;
11500
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011501 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011502 if (*p == '$') /* environment variable */
11503 {
11504 /* first try "normal" environment variables (fast) */
11505 if (mch_getenv(p + 1) != NULL)
11506 n = TRUE;
11507 else
11508 {
11509 /* try expanding things like $VIM and ${HOME} */
11510 p = expand_env_save(p);
11511 if (p != NULL && *p != '$')
11512 n = TRUE;
11513 vim_free(p);
11514 }
11515 }
11516 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011517 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011518 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011519 if (*skipwhite(p) != NUL)
11520 n = FALSE; /* trailing garbage */
11521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011522 else if (*p == '*') /* internal or user defined function */
11523 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011524 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011525 }
11526 else if (*p == ':')
11527 {
11528 n = cmd_exists(p + 1);
11529 }
11530 else if (*p == '#')
11531 {
11532#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011533 if (p[1] == '#')
11534 n = autocmd_supported(p + 2);
11535 else
11536 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011537#endif
11538 }
11539 else /* internal variable */
11540 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011541 char_u *tofree;
11542 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011543
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011544 /* get_name_len() takes care of expanding curly braces */
11545 name = p;
11546 len = get_name_len(&p, &tofree, TRUE, FALSE);
11547 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011548 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011549 if (tofree != NULL)
11550 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011551 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011552 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011553 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011554 /* handle d.key, l[idx], f(expr) */
11555 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11556 if (n)
11557 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011558 }
11559 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011560 if (*p != NUL)
11561 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011562
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011563 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011564 }
11565
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011566 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011567}
11568
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011569#ifdef FEAT_FLOAT
11570/*
11571 * "exp()" function
11572 */
11573 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011574f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011575{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011576 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011577
11578 rettv->v_type = VAR_FLOAT;
11579 if (get_float_arg(argvars, &f) == OK)
11580 rettv->vval.v_float = exp(f);
11581 else
11582 rettv->vval.v_float = 0.0;
11583}
11584#endif
11585
Bram Moolenaar071d4272004-06-13 20:20:40 +000011586/*
11587 * "expand()" function
11588 */
11589 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011590f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011591{
11592 char_u *s;
11593 int len;
11594 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011595 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011596 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011597 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011598 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011599
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011600 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011601 if (argvars[1].v_type != VAR_UNKNOWN
11602 && argvars[2].v_type != VAR_UNKNOWN
11603 && get_tv_number_chk(&argvars[2], &error)
11604 && !error)
11605 {
11606 rettv->v_type = VAR_LIST;
11607 rettv->vval.v_list = NULL;
11608 }
11609
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011610 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011611 if (*s == '%' || *s == '#' || *s == '<')
11612 {
11613 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011614 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011615 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011616 if (rettv->v_type == VAR_LIST)
11617 {
11618 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11619 list_append_string(rettv->vval.v_list, result, -1);
11620 else
11621 vim_free(result);
11622 }
11623 else
11624 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011625 }
11626 else
11627 {
11628 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011629 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011630 if (argvars[1].v_type != VAR_UNKNOWN
11631 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011632 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011633 if (!error)
11634 {
11635 ExpandInit(&xpc);
11636 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011637 if (p_wic)
11638 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011639 if (rettv->v_type == VAR_STRING)
11640 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11641 options, WILD_ALL);
11642 else if (rettv_list_alloc(rettv) != FAIL)
11643 {
11644 int i;
11645
11646 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11647 for (i = 0; i < xpc.xp_numfiles; i++)
11648 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11649 ExpandCleanup(&xpc);
11650 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011651 }
11652 else
11653 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011654 }
11655}
11656
11657/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011658 * Go over all entries in "d2" and add them to "d1".
11659 * When "action" is "error" then a duplicate key is an error.
11660 * When "action" is "force" then a duplicate key is overwritten.
11661 * Otherwise duplicate keys are ignored ("action" is "keep").
11662 */
11663 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011664dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011665{
11666 dictitem_T *di1;
11667 hashitem_T *hi2;
11668 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011669 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011670
11671 todo = (int)d2->dv_hashtab.ht_used;
11672 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11673 {
11674 if (!HASHITEM_EMPTY(hi2))
11675 {
11676 --todo;
11677 di1 = dict_find(d1, hi2->hi_key, -1);
11678 if (d1->dv_scope != 0)
11679 {
11680 /* Disallow replacing a builtin function in l: and g:.
11681 * Check the key to be valid when adding to any
11682 * scope. */
11683 if (d1->dv_scope == VAR_DEF_SCOPE
11684 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11685 && var_check_func_name(hi2->hi_key,
11686 di1 == NULL))
11687 break;
11688 if (!valid_varname(hi2->hi_key))
11689 break;
11690 }
11691 if (di1 == NULL)
11692 {
11693 di1 = dictitem_copy(HI2DI(hi2));
11694 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11695 dictitem_free(di1);
11696 }
11697 else if (*action == 'e')
11698 {
11699 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11700 break;
11701 }
11702 else if (*action == 'f' && HI2DI(hi2) != di1)
11703 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011704 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11705 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011706 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011707 clear_tv(&di1->di_tv);
11708 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11709 }
11710 }
11711 }
11712}
11713
11714/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011715 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011716 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011717 */
11718 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011719f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011720{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011721 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011722
Bram Moolenaare9a41262005-01-15 22:18:47 +000011723 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011724 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011725 list_T *l1, *l2;
11726 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011727 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011728 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011729
Bram Moolenaare9a41262005-01-15 22:18:47 +000011730 l1 = argvars[0].vval.v_list;
11731 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011732 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011733 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011734 {
11735 if (argvars[2].v_type != VAR_UNKNOWN)
11736 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011737 before = get_tv_number_chk(&argvars[2], &error);
11738 if (error)
11739 return; /* type error; errmsg already given */
11740
Bram Moolenaar758711c2005-02-02 23:11:38 +000011741 if (before == l1->lv_len)
11742 item = NULL;
11743 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011744 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011745 item = list_find(l1, before);
11746 if (item == NULL)
11747 {
11748 EMSGN(_(e_listidx), before);
11749 return;
11750 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011751 }
11752 }
11753 else
11754 item = NULL;
11755 list_extend(l1, l2, item);
11756
Bram Moolenaare9a41262005-01-15 22:18:47 +000011757 copy_tv(&argvars[0], rettv);
11758 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011759 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011760 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11761 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011762 dict_T *d1, *d2;
11763 char_u *action;
11764 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011765
11766 d1 = argvars[0].vval.v_dict;
11767 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011768 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011769 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011770 {
11771 /* Check the third argument. */
11772 if (argvars[2].v_type != VAR_UNKNOWN)
11773 {
11774 static char *(av[]) = {"keep", "force", "error"};
11775
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011776 action = get_tv_string_chk(&argvars[2]);
11777 if (action == NULL)
11778 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011779 for (i = 0; i < 3; ++i)
11780 if (STRCMP(action, av[i]) == 0)
11781 break;
11782 if (i == 3)
11783 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011784 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011785 return;
11786 }
11787 }
11788 else
11789 action = (char_u *)"force";
11790
Bram Moolenaara9922d62013-05-30 13:01:18 +020011791 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011792
Bram Moolenaare9a41262005-01-15 22:18:47 +000011793 copy_tv(&argvars[0], rettv);
11794 }
11795 }
11796 else
11797 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011798}
11799
11800/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011801 * "feedkeys()" function
11802 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011803 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011804f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011805{
11806 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011807 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011808 char_u *keys, *flags;
11809 char_u nbuf[NUMBUFLEN];
11810 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011811 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011812 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011813
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011814 /* This is not allowed in the sandbox. If the commands would still be
11815 * executed in the sandbox it would be OK, but it probably happens later,
11816 * when "sandbox" is no longer set. */
11817 if (check_secure())
11818 return;
11819
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011820 keys = get_tv_string(&argvars[0]);
11821 if (*keys != NUL)
11822 {
11823 if (argvars[1].v_type != VAR_UNKNOWN)
11824 {
11825 flags = get_tv_string_buf(&argvars[1], nbuf);
11826 for ( ; *flags != NUL; ++flags)
11827 {
11828 switch (*flags)
11829 {
11830 case 'n': remap = FALSE; break;
11831 case 'm': remap = TRUE; break;
11832 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011833 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011834 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011835 }
11836 }
11837 }
11838
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011839 /* Need to escape K_SPECIAL and CSI before putting the string in the
11840 * typeahead buffer. */
11841 keys_esc = vim_strsave_escape_csi(keys);
11842 if (keys_esc != NULL)
11843 {
11844 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011845 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011846 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011847 if (vgetc_busy)
11848 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011849 if (execute)
11850 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011851 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011852 }
11853}
11854
11855/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011856 * "filereadable()" function
11857 */
11858 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011859f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011860{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011861 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011862 char_u *p;
11863 int n;
11864
Bram Moolenaarc236c162008-07-13 17:41:49 +000011865#ifndef O_NONBLOCK
11866# define O_NONBLOCK 0
11867#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011868 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011869 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11870 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011871 {
11872 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011873 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011874 }
11875 else
11876 n = FALSE;
11877
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011878 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011879}
11880
11881/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011882 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011883 * rights to write into.
11884 */
11885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011886f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011887{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011888 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011889}
11890
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011891 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011892findfilendir(
11893 typval_T *argvars UNUSED,
11894 typval_T *rettv,
11895 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011896{
11897#ifdef FEAT_SEARCHPATH
11898 char_u *fname;
11899 char_u *fresult = NULL;
11900 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11901 char_u *p;
11902 char_u pathbuf[NUMBUFLEN];
11903 int count = 1;
11904 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011905 int error = FALSE;
11906#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011907
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011908 rettv->vval.v_string = NULL;
11909 rettv->v_type = VAR_STRING;
11910
11911#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011912 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011913
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011914 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011915 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011916 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11917 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011918 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011919 else
11920 {
11921 if (*p != NUL)
11922 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011923
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011924 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011925 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011926 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011927 }
11928
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011929 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11930 error = TRUE;
11931
11932 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011933 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011934 do
11935 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011936 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011937 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011938 fresult = find_file_in_path_option(first ? fname : NULL,
11939 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011940 0, first, path,
11941 find_what,
11942 curbuf->b_ffname,
11943 find_what == FINDFILE_DIR
11944 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011945 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011946
11947 if (fresult != NULL && rettv->v_type == VAR_LIST)
11948 list_append_string(rettv->vval.v_list, fresult, -1);
11949
11950 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011951 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011952
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011953 if (rettv->v_type == VAR_STRING)
11954 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011955#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011956}
11957
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011958static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11959static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011960
11961/*
11962 * Implementation of map() and filter().
11963 */
11964 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011965filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011966{
11967 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011968 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011969 listitem_T *li, *nli;
11970 list_T *l = NULL;
11971 dictitem_T *di;
11972 hashtab_T *ht;
11973 hashitem_T *hi;
11974 dict_T *d = NULL;
11975 typval_T save_val;
11976 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011977 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011978 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011979 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011980 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011981 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011982 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011983 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011984
Bram Moolenaare9a41262005-01-15 22:18:47 +000011985 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011986 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011987 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011988 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011989 return;
11990 }
11991 else if (argvars[0].v_type == VAR_DICT)
11992 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011993 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011994 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011995 return;
11996 }
11997 else
11998 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011999 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012000 return;
12001 }
12002
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012003 expr = get_tv_string_buf_chk(&argvars[1], buf);
12004 /* On type errors, the preceding call has already displayed an error
12005 * message. Avoid a misleading error message for an empty string that
12006 * was not passed as argument. */
12007 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012008 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012009 prepare_vimvar(VV_VAL, &save_val);
12010 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012011
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012012 /* We reset "did_emsg" to be able to detect whether an error
12013 * occurred during evaluation of the expression. */
12014 save_did_emsg = did_emsg;
12015 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000012016
Bram Moolenaar627b1d32009-11-17 11:20:35 +000012017 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012018 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012019 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012020 vimvars[VV_KEY].vv_type = VAR_STRING;
12021
12022 ht = &d->dv_hashtab;
12023 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012024 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012025 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012026 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012027 if (!HASHITEM_EMPTY(hi))
12028 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010012029 int r;
12030
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012031 --todo;
12032 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012033 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020012034 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
12035 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012036 break;
12037 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010012038 r = filter_map_one(&di->di_tv, expr, map, &rem);
12039 clear_tv(&vimvars[VV_KEY].vv_tv);
12040 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012041 break;
12042 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012043 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020012044 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
12045 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012046 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012047 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012048 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012049 }
12050 }
12051 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012052 }
12053 else
12054 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000012055 vimvars[VV_KEY].vv_type = VAR_NUMBER;
12056
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012057 for (li = l->lv_first; li != NULL; li = nli)
12058 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020012059 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012060 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012061 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020012062 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000012063 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012064 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012065 break;
12066 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012067 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020012068 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012069 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012070 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012071
Bram Moolenaar627b1d32009-11-17 11:20:35 +000012072 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012073 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012074
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012075 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012076 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012077
12078 copy_tv(&argvars[0], rettv);
12079}
12080
12081 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010012082filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012083{
Bram Moolenaar33570922005-01-25 22:26:29 +000012084 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012085 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012086 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012087
Bram Moolenaar33570922005-01-25 22:26:29 +000012088 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012089 s = expr;
12090 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012091 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012092 if (*s != NUL) /* check for trailing chars after expr */
12093 {
12094 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010012095 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012096 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012097 }
12098 if (map)
12099 {
12100 /* map(): replace the list item value */
12101 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000012102 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012103 *tv = rettv;
12104 }
12105 else
12106 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012107 int error = FALSE;
12108
Bram Moolenaare9a41262005-01-15 22:18:47 +000012109 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012110 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012111 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012112 /* On type error, nothing has been removed; return FAIL to stop the
12113 * loop. The error message was given by get_tv_number_chk(). */
12114 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012115 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012116 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012117 retval = OK;
12118theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000012119 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012120 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012121}
12122
12123/*
12124 * "filter()" function
12125 */
12126 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012127f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012128{
12129 filter_map(argvars, rettv, FALSE);
12130}
12131
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012132/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012133 * "finddir({fname}[, {path}[, {count}]])" function
12134 */
12135 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012136f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012137{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012138 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012139}
12140
12141/*
12142 * "findfile({fname}[, {path}[, {count}]])" function
12143 */
12144 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012145f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012146{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012147 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012148}
12149
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012150#ifdef FEAT_FLOAT
12151/*
12152 * "float2nr({float})" function
12153 */
12154 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012155f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012156{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012157 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012158
12159 if (get_float_arg(argvars, &f) == OK)
12160 {
12161 if (f < -0x7fffffff)
12162 rettv->vval.v_number = -0x7fffffff;
12163 else if (f > 0x7fffffff)
12164 rettv->vval.v_number = 0x7fffffff;
12165 else
12166 rettv->vval.v_number = (varnumber_T)f;
12167 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012168}
12169
12170/*
12171 * "floor({float})" function
12172 */
12173 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012174f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012175{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012176 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012177
12178 rettv->v_type = VAR_FLOAT;
12179 if (get_float_arg(argvars, &f) == OK)
12180 rettv->vval.v_float = floor(f);
12181 else
12182 rettv->vval.v_float = 0.0;
12183}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012184
12185/*
12186 * "fmod()" function
12187 */
12188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012189f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012190{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012191 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012192
12193 rettv->v_type = VAR_FLOAT;
12194 if (get_float_arg(argvars, &fx) == OK
12195 && get_float_arg(&argvars[1], &fy) == OK)
12196 rettv->vval.v_float = fmod(fx, fy);
12197 else
12198 rettv->vval.v_float = 0.0;
12199}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012200#endif
12201
Bram Moolenaar0d660222005-01-07 21:51:51 +000012202/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012203 * "fnameescape({string})" function
12204 */
12205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012206f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012207{
12208 rettv->vval.v_string = vim_strsave_fnameescape(
12209 get_tv_string(&argvars[0]), FALSE);
12210 rettv->v_type = VAR_STRING;
12211}
12212
12213/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012214 * "fnamemodify({fname}, {mods})" function
12215 */
12216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012217f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012218{
12219 char_u *fname;
12220 char_u *mods;
12221 int usedlen = 0;
12222 int len;
12223 char_u *fbuf = NULL;
12224 char_u buf[NUMBUFLEN];
12225
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012226 fname = get_tv_string_chk(&argvars[0]);
12227 mods = get_tv_string_buf_chk(&argvars[1], buf);
12228 if (fname == NULL || mods == NULL)
12229 fname = NULL;
12230 else
12231 {
12232 len = (int)STRLEN(fname);
12233 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
12234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012235
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012236 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012237 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012238 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012239 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012240 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012241 vim_free(fbuf);
12242}
12243
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012244static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012245
12246/*
12247 * "foldclosed()" function
12248 */
12249 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012250foldclosed_both(
12251 typval_T *argvars UNUSED,
12252 typval_T *rettv,
12253 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012254{
12255#ifdef FEAT_FOLDING
12256 linenr_T lnum;
12257 linenr_T first, last;
12258
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012259 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012260 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12261 {
12262 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12263 {
12264 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012265 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012266 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012267 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012268 return;
12269 }
12270 }
12271#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012272 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012273}
12274
12275/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012276 * "foldclosed()" function
12277 */
12278 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012279f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012280{
12281 foldclosed_both(argvars, rettv, FALSE);
12282}
12283
12284/*
12285 * "foldclosedend()" function
12286 */
12287 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012288f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012289{
12290 foldclosed_both(argvars, rettv, TRUE);
12291}
12292
12293/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012294 * "foldlevel()" function
12295 */
12296 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012297f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012298{
12299#ifdef FEAT_FOLDING
12300 linenr_T lnum;
12301
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012302 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012303 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012304 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012306}
12307
12308/*
12309 * "foldtext()" function
12310 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012311 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012312f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012313{
12314#ifdef FEAT_FOLDING
12315 linenr_T lnum;
12316 char_u *s;
12317 char_u *r;
12318 int len;
12319 char *txt;
12320#endif
12321
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012322 rettv->v_type = VAR_STRING;
12323 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012324#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012325 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12326 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12327 <= curbuf->b_ml.ml_line_count
12328 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012329 {
12330 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012331 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12332 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012333 {
12334 if (!linewhite(lnum))
12335 break;
12336 ++lnum;
12337 }
12338
12339 /* Find interesting text in this line. */
12340 s = skipwhite(ml_get(lnum));
12341 /* skip C comment-start */
12342 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012343 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012344 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012345 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012346 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012347 {
12348 s = skipwhite(ml_get(lnum + 1));
12349 if (*s == '*')
12350 s = skipwhite(s + 1);
12351 }
12352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012353 txt = _("+-%s%3ld lines: ");
12354 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012355 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012356 + 20 /* for %3ld */
12357 + STRLEN(s))); /* concatenated */
12358 if (r != NULL)
12359 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012360 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12361 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12362 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012363 len = (int)STRLEN(r);
12364 STRCAT(r, s);
12365 /* remove 'foldmarker' and 'commentstring' */
12366 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012367 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012368 }
12369 }
12370#endif
12371}
12372
12373/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012374 * "foldtextresult(lnum)" function
12375 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012376 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012377f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012378{
12379#ifdef FEAT_FOLDING
12380 linenr_T lnum;
12381 char_u *text;
12382 char_u buf[51];
12383 foldinfo_T foldinfo;
12384 int fold_count;
12385#endif
12386
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012387 rettv->v_type = VAR_STRING;
12388 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012389#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012390 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012391 /* treat illegal types and illegal string values for {lnum} the same */
12392 if (lnum < 0)
12393 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012394 fold_count = foldedCount(curwin, lnum, &foldinfo);
12395 if (fold_count > 0)
12396 {
12397 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12398 &foldinfo, buf);
12399 if (text == buf)
12400 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012401 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012402 }
12403#endif
12404}
12405
12406/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012407 * "foreground()" function
12408 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012409 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012410f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012411{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012412#ifdef FEAT_GUI
12413 if (gui.in_use)
12414 gui_mch_set_foreground();
12415#else
12416# ifdef WIN32
12417 win32_set_foreground();
12418# endif
12419#endif
12420}
12421
12422/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012423 * "function()" function
12424 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012426f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012427{
12428 char_u *s;
12429
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012430 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012431 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012432 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012433 /* Don't check an autoload name for existence here. */
12434 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012435 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012436 else
12437 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012438 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012439 {
12440 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012441 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012442
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012443 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12444 * also be called from another script. Using trans_function_name()
12445 * would also work, but some plugins depend on the name being
12446 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012447 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020012448 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012449 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012450 if (rettv->vval.v_string != NULL)
12451 {
12452 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012453 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012454 }
12455 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012456 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012457 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012458 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012459 }
12460}
12461
12462/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012463 * "garbagecollect()" function
12464 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012465 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012466f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012467{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012468 /* This is postponed until we are back at the toplevel, because we may be
12469 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12470 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012471
12472 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12473 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012474}
12475
12476/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012477 * "get()" function
12478 */
12479 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012480f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012481{
Bram Moolenaar33570922005-01-25 22:26:29 +000012482 listitem_T *li;
12483 list_T *l;
12484 dictitem_T *di;
12485 dict_T *d;
12486 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012487
Bram Moolenaare9a41262005-01-15 22:18:47 +000012488 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012489 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012490 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012491 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012492 int error = FALSE;
12493
12494 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12495 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012496 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012497 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012498 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012499 else if (argvars[0].v_type == VAR_DICT)
12500 {
12501 if ((d = argvars[0].vval.v_dict) != NULL)
12502 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012503 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012504 if (di != NULL)
12505 tv = &di->di_tv;
12506 }
12507 }
12508 else
12509 EMSG2(_(e_listdictarg), "get()");
12510
12511 if (tv == NULL)
12512 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012513 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012514 copy_tv(&argvars[2], rettv);
12515 }
12516 else
12517 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012518}
12519
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012520static 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 +000012521
12522/*
12523 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012524 * Return a range (from start to end) of lines in rettv from the specified
12525 * buffer.
12526 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012527 */
12528 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012529get_buffer_lines(
12530 buf_T *buf,
12531 linenr_T start,
12532 linenr_T end,
12533 int retlist,
12534 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012535{
12536 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012537
Bram Moolenaar959a1432013-12-14 12:17:38 +010012538 rettv->v_type = VAR_STRING;
12539 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012540 if (retlist && rettv_list_alloc(rettv) == FAIL)
12541 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012542
12543 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12544 return;
12545
12546 if (!retlist)
12547 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012548 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12549 p = ml_get_buf(buf, start, FALSE);
12550 else
12551 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012552 rettv->vval.v_string = vim_strsave(p);
12553 }
12554 else
12555 {
12556 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012557 return;
12558
12559 if (start < 1)
12560 start = 1;
12561 if (end > buf->b_ml.ml_line_count)
12562 end = buf->b_ml.ml_line_count;
12563 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012564 if (list_append_string(rettv->vval.v_list,
12565 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012566 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012567 }
12568}
12569
12570/*
12571 * "getbufline()" function
12572 */
12573 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012574f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012575{
12576 linenr_T lnum;
12577 linenr_T end;
12578 buf_T *buf;
12579
12580 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12581 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012582 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012583 --emsg_off;
12584
Bram Moolenaar661b1822005-07-28 22:36:45 +000012585 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012586 if (argvars[2].v_type == VAR_UNKNOWN)
12587 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012588 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012589 end = get_tv_lnum_buf(&argvars[2], buf);
12590
Bram Moolenaar342337a2005-07-21 21:11:17 +000012591 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012592}
12593
Bram Moolenaar0d660222005-01-07 21:51:51 +000012594/*
12595 * "getbufvar()" function
12596 */
12597 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012598f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012599{
12600 buf_T *buf;
12601 buf_T *save_curbuf;
12602 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012603 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012604 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012605
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012606 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12607 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012608 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012609 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012610
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012611 rettv->v_type = VAR_STRING;
12612 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012613
12614 if (buf != NULL && varname != NULL)
12615 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012616 /* set curbuf to be our buf, temporarily */
12617 save_curbuf = curbuf;
12618 curbuf = buf;
12619
Bram Moolenaar0d660222005-01-07 21:51:51 +000012620 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012621 {
12622 if (get_option_tv(&varname, rettv, TRUE) == OK)
12623 done = TRUE;
12624 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012625 else if (STRCMP(varname, "changedtick") == 0)
12626 {
12627 rettv->v_type = VAR_NUMBER;
12628 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012629 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012630 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012631 else
12632 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012633 /* Look up the variable. */
12634 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12635 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12636 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012637 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012638 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012639 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012640 done = TRUE;
12641 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012642 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012643
12644 /* restore previous notion of curbuf */
12645 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012646 }
12647
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012648 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12649 /* use the default value */
12650 copy_tv(&argvars[2], rettv);
12651
Bram Moolenaar0d660222005-01-07 21:51:51 +000012652 --emsg_off;
12653}
12654
12655/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012656 * "getchar()" function
12657 */
12658 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012659f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012660{
12661 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012662 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012663
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012664 /* Position the cursor. Needed after a message that ends in a space. */
12665 windgoto(msg_row, msg_col);
12666
Bram Moolenaar071d4272004-06-13 20:20:40 +000012667 ++no_mapping;
12668 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012669 for (;;)
12670 {
12671 if (argvars[0].v_type == VAR_UNKNOWN)
12672 /* getchar(): blocking wait. */
12673 n = safe_vgetc();
12674 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12675 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012676 n = vpeekc_any();
12677 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012678 /* illegal argument or getchar(0) and no char avail: return zero */
12679 n = 0;
12680 else
12681 /* getchar(0) and char avail: return char */
12682 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012683
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012684 if (n == K_IGNORE)
12685 continue;
12686 break;
12687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012688 --no_mapping;
12689 --allow_keys;
12690
Bram Moolenaar219b8702006-11-01 14:32:36 +000012691 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12692 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12693 vimvars[VV_MOUSE_COL].vv_nr = 0;
12694
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012695 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012696 if (IS_SPECIAL(n) || mod_mask != 0)
12697 {
12698 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12699 int i = 0;
12700
12701 /* Turn a special key into three bytes, plus modifier. */
12702 if (mod_mask != 0)
12703 {
12704 temp[i++] = K_SPECIAL;
12705 temp[i++] = KS_MODIFIER;
12706 temp[i++] = mod_mask;
12707 }
12708 if (IS_SPECIAL(n))
12709 {
12710 temp[i++] = K_SPECIAL;
12711 temp[i++] = K_SECOND(n);
12712 temp[i++] = K_THIRD(n);
12713 }
12714#ifdef FEAT_MBYTE
12715 else if (has_mbyte)
12716 i += (*mb_char2bytes)(n, temp + i);
12717#endif
12718 else
12719 temp[i++] = n;
12720 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012721 rettv->v_type = VAR_STRING;
12722 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012723
12724#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012725 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012726 {
12727 int row = mouse_row;
12728 int col = mouse_col;
12729 win_T *win;
12730 linenr_T lnum;
12731# ifdef FEAT_WINDOWS
12732 win_T *wp;
12733# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012734 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012735
12736 if (row >= 0 && col >= 0)
12737 {
12738 /* Find the window at the mouse coordinates and compute the
12739 * text position. */
12740 win = mouse_find_win(&row, &col);
12741 (void)mouse_comp_pos(win, &row, &col, &lnum);
12742# ifdef FEAT_WINDOWS
12743 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012744 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012745# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012746 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012747 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12748 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12749 }
12750 }
12751#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012752 }
12753}
12754
12755/*
12756 * "getcharmod()" function
12757 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012758 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012759f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012760{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012761 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012762}
12763
12764/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012765 * "getcharsearch()" function
12766 */
12767 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012768f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012769{
12770 if (rettv_dict_alloc(rettv) != FAIL)
12771 {
12772 dict_T *dict = rettv->vval.v_dict;
12773
12774 dict_add_nr_str(dict, "char", 0L, last_csearch());
12775 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12776 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12777 }
12778}
12779
12780/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012781 * "getcmdline()" function
12782 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012783 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012784f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012785{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012786 rettv->v_type = VAR_STRING;
12787 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012788}
12789
12790/*
12791 * "getcmdpos()" function
12792 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012793 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012794f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012795{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012796 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012797}
12798
12799/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012800 * "getcmdtype()" function
12801 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012803f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012804{
12805 rettv->v_type = VAR_STRING;
12806 rettv->vval.v_string = alloc(2);
12807 if (rettv->vval.v_string != NULL)
12808 {
12809 rettv->vval.v_string[0] = get_cmdline_type();
12810 rettv->vval.v_string[1] = NUL;
12811 }
12812}
12813
12814/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012815 * "getcmdwintype()" function
12816 */
12817 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012818f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012819{
12820 rettv->v_type = VAR_STRING;
12821 rettv->vval.v_string = NULL;
12822#ifdef FEAT_CMDWIN
12823 rettv->vval.v_string = alloc(2);
12824 if (rettv->vval.v_string != NULL)
12825 {
12826 rettv->vval.v_string[0] = cmdwin_type;
12827 rettv->vval.v_string[1] = NUL;
12828 }
12829#endif
12830}
12831
12832/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012833 * "getcwd()" function
12834 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012835 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012836f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012837{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012838 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012839 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012840
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012841 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012842 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012843
12844 wp = find_tabwin(&argvars[0], &argvars[1]);
12845 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012846 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012847 if (wp->w_localdir != NULL)
12848 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12849 else if(globaldir != NULL)
12850 rettv->vval.v_string = vim_strsave(globaldir);
12851 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012852 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012853 cwd = alloc(MAXPATHL);
12854 if (cwd != NULL)
12855 {
12856 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12857 rettv->vval.v_string = vim_strsave(cwd);
12858 vim_free(cwd);
12859 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012860 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012861#ifdef BACKSLASH_IN_FILENAME
12862 if (rettv->vval.v_string != NULL)
12863 slash_adjust(rettv->vval.v_string);
12864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012865 }
12866}
12867
12868/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012869 * "getfontname()" function
12870 */
12871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012872f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012873{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012874 rettv->v_type = VAR_STRING;
12875 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012876#ifdef FEAT_GUI
12877 if (gui.in_use)
12878 {
12879 GuiFont font;
12880 char_u *name = NULL;
12881
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012882 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012883 {
12884 /* Get the "Normal" font. Either the name saved by
12885 * hl_set_font_name() or from the font ID. */
12886 font = gui.norm_font;
12887 name = hl_get_font_name();
12888 }
12889 else
12890 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012891 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012892 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12893 return;
12894 font = gui_mch_get_font(name, FALSE);
12895 if (font == NOFONT)
12896 return; /* Invalid font name, return empty string. */
12897 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012898 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012899 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012900 gui_mch_free_font(font);
12901 }
12902#endif
12903}
12904
12905/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012906 * "getfperm({fname})" function
12907 */
12908 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012909f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012910{
12911 char_u *fname;
12912 struct stat st;
12913 char_u *perm = NULL;
12914 char_u flags[] = "rwx";
12915 int i;
12916
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012917 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012918
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012919 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012920 if (mch_stat((char *)fname, &st) >= 0)
12921 {
12922 perm = vim_strsave((char_u *)"---------");
12923 if (perm != NULL)
12924 {
12925 for (i = 0; i < 9; i++)
12926 {
12927 if (st.st_mode & (1 << (8 - i)))
12928 perm[i] = flags[i % 3];
12929 }
12930 }
12931 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012932 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012933}
12934
12935/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012936 * "getfsize({fname})" function
12937 */
12938 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012939f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012940{
12941 char_u *fname;
12942 struct stat st;
12943
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012944 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012945
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012946 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012947
12948 if (mch_stat((char *)fname, &st) >= 0)
12949 {
12950 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012951 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012952 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012953 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012954 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012955
12956 /* non-perfect check for overflow */
12957 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12958 rettv->vval.v_number = -2;
12959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012960 }
12961 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012962 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012963}
12964
12965/*
12966 * "getftime({fname})" function
12967 */
12968 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012969f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012970{
12971 char_u *fname;
12972 struct stat st;
12973
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012974 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012975
12976 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012977 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012978 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012979 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012980}
12981
12982/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012983 * "getftype({fname})" function
12984 */
12985 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012986f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012987{
12988 char_u *fname;
12989 struct stat st;
12990 char_u *type = NULL;
12991 char *t;
12992
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012993 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012994
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012995 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012996 if (mch_lstat((char *)fname, &st) >= 0)
12997 {
12998#ifdef S_ISREG
12999 if (S_ISREG(st.st_mode))
13000 t = "file";
13001 else if (S_ISDIR(st.st_mode))
13002 t = "dir";
13003# ifdef S_ISLNK
13004 else if (S_ISLNK(st.st_mode))
13005 t = "link";
13006# endif
13007# ifdef S_ISBLK
13008 else if (S_ISBLK(st.st_mode))
13009 t = "bdev";
13010# endif
13011# ifdef S_ISCHR
13012 else if (S_ISCHR(st.st_mode))
13013 t = "cdev";
13014# endif
13015# ifdef S_ISFIFO
13016 else if (S_ISFIFO(st.st_mode))
13017 t = "fifo";
13018# endif
13019# ifdef S_ISSOCK
13020 else if (S_ISSOCK(st.st_mode))
13021 t = "fifo";
13022# endif
13023 else
13024 t = "other";
13025#else
13026# ifdef S_IFMT
13027 switch (st.st_mode & S_IFMT)
13028 {
13029 case S_IFREG: t = "file"; break;
13030 case S_IFDIR: t = "dir"; break;
13031# ifdef S_IFLNK
13032 case S_IFLNK: t = "link"; break;
13033# endif
13034# ifdef S_IFBLK
13035 case S_IFBLK: t = "bdev"; break;
13036# endif
13037# ifdef S_IFCHR
13038 case S_IFCHR: t = "cdev"; break;
13039# endif
13040# ifdef S_IFIFO
13041 case S_IFIFO: t = "fifo"; break;
13042# endif
13043# ifdef S_IFSOCK
13044 case S_IFSOCK: t = "socket"; break;
13045# endif
13046 default: t = "other";
13047 }
13048# else
13049 if (mch_isdir(fname))
13050 t = "dir";
13051 else
13052 t = "file";
13053# endif
13054#endif
13055 type = vim_strsave((char_u *)t);
13056 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013057 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013058}
13059
13060/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013061 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000013062 */
13063 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013064f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013065{
13066 linenr_T lnum;
13067 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013068 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013069
13070 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013071 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000013072 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013073 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013074 retlist = FALSE;
13075 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013076 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000013077 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013078 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000013079 retlist = TRUE;
13080 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013081
Bram Moolenaar342337a2005-07-21 21:11:17 +000013082 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013083}
13084
13085/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013086 * "getmatches()" function
13087 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013088 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013089f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013090{
13091#ifdef FEAT_SEARCH_EXTRA
13092 dict_T *dict;
13093 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013094 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013095
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013096 if (rettv_list_alloc(rettv) == OK)
13097 {
13098 while (cur != NULL)
13099 {
13100 dict = dict_alloc();
13101 if (dict == NULL)
13102 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013103 if (cur->match.regprog == NULL)
13104 {
13105 /* match added with matchaddpos() */
13106 for (i = 0; i < MAXPOSMATCH; ++i)
13107 {
13108 llpos_T *llpos;
13109 char buf[6];
13110 list_T *l;
13111
13112 llpos = &cur->pos.pos[i];
13113 if (llpos->lnum == 0)
13114 break;
13115 l = list_alloc();
13116 if (l == NULL)
13117 break;
13118 list_append_number(l, (varnumber_T)llpos->lnum);
13119 if (llpos->col > 0)
13120 {
13121 list_append_number(l, (varnumber_T)llpos->col);
13122 list_append_number(l, (varnumber_T)llpos->len);
13123 }
13124 sprintf(buf, "pos%d", i + 1);
13125 dict_add_list(dict, buf, l);
13126 }
13127 }
13128 else
13129 {
13130 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
13131 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013132 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013133 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
13134 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020013135# ifdef FEAT_CONCEAL
13136 if (cur->conceal_char)
13137 {
13138 char_u buf[MB_MAXBYTES + 1];
13139
13140 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
13141 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
13142 }
13143# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013144 list_append_dict(rettv->vval.v_list, dict);
13145 cur = cur->next;
13146 }
13147 }
13148#endif
13149}
13150
13151/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000013152 * "getpid()" function
13153 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000013154 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013155f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013156{
13157 rettv->vval.v_number = mch_get_pid();
13158}
13159
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013160static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013161
13162/*
13163 * "getcurpos()" function
13164 */
13165 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013166f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013167{
13168 getpos_both(argvars, rettv, TRUE);
13169}
13170
Bram Moolenaar18081e32008-02-20 19:11:07 +000013171/*
Bram Moolenaara5525202006-03-02 22:52:09 +000013172 * "getpos(string)" function
13173 */
13174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013175f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000013176{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013177 getpos_both(argvars, rettv, FALSE);
13178}
13179
13180 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013181getpos_both(
13182 typval_T *argvars,
13183 typval_T *rettv,
13184 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013185{
Bram Moolenaara5525202006-03-02 22:52:09 +000013186 pos_T *fp;
13187 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013188 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013189
13190 if (rettv_list_alloc(rettv) == OK)
13191 {
13192 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013193 if (getcurpos)
13194 fp = &curwin->w_cursor;
13195 else
13196 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013197 if (fnum != -1)
13198 list_append_number(l, (varnumber_T)fnum);
13199 else
13200 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013201 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13202 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013203 list_append_number(l, (fp != NULL)
13204 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013205 : (varnumber_T)0);
13206 list_append_number(l,
13207#ifdef FEAT_VIRTUALEDIT
13208 (fp != NULL) ? (varnumber_T)fp->coladd :
13209#endif
13210 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013211 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013212 {
13213 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013214 list_append_number(l, curwin->w_curswant == MAXCOL ?
13215 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013216 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013217 }
13218 else
13219 rettv->vval.v_number = FALSE;
13220}
13221
13222/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013223 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013224 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013226f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013227{
13228#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013229 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013230#endif
13231
Bram Moolenaar2641f772005-03-25 21:58:17 +000013232#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013233 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013234 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013235 wp = NULL;
13236 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13237 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013238 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013239 if (wp == NULL)
13240 return;
13241 }
13242
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013243 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013244 }
13245#endif
13246}
13247
13248/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013249 * "getreg()" function
13250 */
13251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013252f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013253{
13254 char_u *strregname;
13255 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013256 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013257 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013258 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013259
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013260 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013261 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013262 strregname = get_tv_string_chk(&argvars[0]);
13263 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013264 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013265 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013266 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013267 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13268 return_list = get_tv_number_chk(&argvars[2], &error);
13269 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013271 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013272 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013273
13274 if (error)
13275 return;
13276
Bram Moolenaar071d4272004-06-13 20:20:40 +000013277 regname = (strregname == NULL ? '"' : *strregname);
13278 if (regname == 0)
13279 regname = '"';
13280
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013281 if (return_list)
13282 {
13283 rettv->v_type = VAR_LIST;
13284 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13285 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013286 if (rettv->vval.v_list != NULL)
13287 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013288 }
13289 else
13290 {
13291 rettv->v_type = VAR_STRING;
13292 rettv->vval.v_string = get_reg_contents(regname,
13293 arg2 ? GREG_EXPR_SRC : 0);
13294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013295}
13296
13297/*
13298 * "getregtype()" function
13299 */
13300 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013301f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013302{
13303 char_u *strregname;
13304 int regname;
13305 char_u buf[NUMBUFLEN + 2];
13306 long reglen = 0;
13307
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013308 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013309 {
13310 strregname = get_tv_string_chk(&argvars[0]);
13311 if (strregname == NULL) /* type error; errmsg already given */
13312 {
13313 rettv->v_type = VAR_STRING;
13314 rettv->vval.v_string = NULL;
13315 return;
13316 }
13317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013318 else
13319 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013320 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013321
13322 regname = (strregname == NULL ? '"' : *strregname);
13323 if (regname == 0)
13324 regname = '"';
13325
13326 buf[0] = NUL;
13327 buf[1] = NUL;
13328 switch (get_reg_type(regname, &reglen))
13329 {
13330 case MLINE: buf[0] = 'V'; break;
13331 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013332 case MBLOCK:
13333 buf[0] = Ctrl_V;
13334 sprintf((char *)buf + 1, "%ld", reglen + 1);
13335 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013336 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013337 rettv->v_type = VAR_STRING;
13338 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013339}
13340
13341/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013342 * "gettabvar()" function
13343 */
13344 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013345f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013346{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013347 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013348 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013349 dictitem_T *v;
13350 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013351 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013352
13353 rettv->v_type = VAR_STRING;
13354 rettv->vval.v_string = NULL;
13355
13356 varname = get_tv_string_chk(&argvars[1]);
13357 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13358 if (tp != NULL && varname != NULL)
13359 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013360 /* Set tp to be our tabpage, temporarily. Also set the window to the
13361 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013362 if (switch_win(&oldcurwin, &oldtabpage,
13363 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013364 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013365 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013366 /* look up the variable */
13367 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13368 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13369 if (v != NULL)
13370 {
13371 copy_tv(&v->di_tv, rettv);
13372 done = TRUE;
13373 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013374 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013375
13376 /* restore previous notion of curwin */
13377 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013378 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013379
13380 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013381 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013382}
13383
13384/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013385 * "gettabwinvar()" function
13386 */
13387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013388f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013389{
13390 getwinvar(argvars, rettv, 1);
13391}
13392
13393/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013394 * "getwinposx()" function
13395 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013397f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013398{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013399 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013400#ifdef FEAT_GUI
13401 if (gui.in_use)
13402 {
13403 int x, y;
13404
13405 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013406 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013407 }
13408#endif
13409}
13410
13411/*
13412 * "getwinposy()" function
13413 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013414 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013415f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013416{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013417 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013418#ifdef FEAT_GUI
13419 if (gui.in_use)
13420 {
13421 int x, y;
13422
13423 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013424 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013425 }
13426#endif
13427}
13428
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013429/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013430 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013431 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013432 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013433find_win_by_nr(
13434 typval_T *vp,
13435 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013436{
13437#ifdef FEAT_WINDOWS
13438 win_T *wp;
13439#endif
13440 int nr;
13441
13442 nr = get_tv_number_chk(vp, NULL);
13443
13444#ifdef FEAT_WINDOWS
13445 if (nr < 0)
13446 return NULL;
13447 if (nr == 0)
13448 return curwin;
13449
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013450 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13451 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013452 if (--nr <= 0)
13453 break;
13454 return wp;
13455#else
13456 if (nr == 0 || nr == 1)
13457 return curwin;
13458 return NULL;
13459#endif
13460}
13461
Bram Moolenaar071d4272004-06-13 20:20:40 +000013462/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013463 * Find window specified by "wvp" in tabpage "tvp".
13464 */
13465 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013466find_tabwin(
13467 typval_T *wvp, /* VAR_UNKNOWN for current window */
13468 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013469{
13470 win_T *wp = NULL;
13471 tabpage_T *tp = NULL;
13472 long n;
13473
13474 if (wvp->v_type != VAR_UNKNOWN)
13475 {
13476 if (tvp->v_type != VAR_UNKNOWN)
13477 {
13478 n = get_tv_number(tvp);
13479 if (n >= 0)
13480 tp = find_tabpage(n);
13481 }
13482 else
13483 tp = curtab;
13484
13485 if (tp != NULL)
13486 wp = find_win_by_nr(wvp, tp);
13487 }
13488 else
13489 wp = curwin;
13490
13491 return wp;
13492}
13493
13494/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013495 * "getwinvar()" function
13496 */
13497 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013498f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013499{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013500 getwinvar(argvars, rettv, 0);
13501}
13502
13503/*
13504 * getwinvar() and gettabwinvar()
13505 */
13506 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013507getwinvar(
13508 typval_T *argvars,
13509 typval_T *rettv,
13510 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013511{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013512 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013513 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013514 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013515 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013516 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013517#ifdef FEAT_WINDOWS
13518 win_T *oldcurwin;
13519 tabpage_T *oldtabpage;
13520 int need_switch_win;
13521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013522
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013523#ifdef FEAT_WINDOWS
13524 if (off == 1)
13525 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13526 else
13527 tp = curtab;
13528#endif
13529 win = find_win_by_nr(&argvars[off], tp);
13530 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013531 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013532
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013533 rettv->v_type = VAR_STRING;
13534 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013535
13536 if (win != NULL && varname != NULL)
13537 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013538#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013539 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013540 * otherwise the window is not valid. Only do this when needed,
13541 * autocommands get blocked. */
13542 need_switch_win = !(tp == curtab && win == curwin);
13543 if (!need_switch_win
13544 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13545#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013546 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013547 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013548 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013549 if (get_option_tv(&varname, rettv, 1) == OK)
13550 done = TRUE;
13551 }
13552 else
13553 {
13554 /* Look up the variable. */
13555 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13556 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13557 varname, FALSE);
13558 if (v != NULL)
13559 {
13560 copy_tv(&v->di_tv, rettv);
13561 done = TRUE;
13562 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013564 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013565
Bram Moolenaarba117c22015-09-29 16:53:22 +020013566#ifdef FEAT_WINDOWS
13567 if (need_switch_win)
13568 /* restore previous notion of curwin */
13569 restore_win(oldcurwin, oldtabpage, TRUE);
13570#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013571 }
13572
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013573 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13574 /* use the default return value */
13575 copy_tv(&argvars[off + 2], rettv);
13576
Bram Moolenaar071d4272004-06-13 20:20:40 +000013577 --emsg_off;
13578}
13579
13580/*
13581 * "glob()" function
13582 */
13583 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013584f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013585{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013586 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013587 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013588 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013589
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013590 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013591 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013592 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013593 if (argvars[1].v_type != VAR_UNKNOWN)
13594 {
13595 if (get_tv_number_chk(&argvars[1], &error))
13596 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013597 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013598 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013599 if (get_tv_number_chk(&argvars[2], &error))
13600 {
13601 rettv->v_type = VAR_LIST;
13602 rettv->vval.v_list = NULL;
13603 }
13604 if (argvars[3].v_type != VAR_UNKNOWN
13605 && get_tv_number_chk(&argvars[3], &error))
13606 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013607 }
13608 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013609 if (!error)
13610 {
13611 ExpandInit(&xpc);
13612 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013613 if (p_wic)
13614 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013615 if (rettv->v_type == VAR_STRING)
13616 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013617 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013618 else if (rettv_list_alloc(rettv) != FAIL)
13619 {
13620 int i;
13621
13622 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13623 NULL, options, WILD_ALL_KEEP);
13624 for (i = 0; i < xpc.xp_numfiles; i++)
13625 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13626
13627 ExpandCleanup(&xpc);
13628 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013629 }
13630 else
13631 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013632}
13633
13634/*
13635 * "globpath()" function
13636 */
13637 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013638f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013640 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013641 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013642 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013643 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013644 garray_T ga;
13645 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013646
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013647 /* When the optional second argument is non-zero, don't remove matches
13648 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013649 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013650 if (argvars[2].v_type != VAR_UNKNOWN)
13651 {
13652 if (get_tv_number_chk(&argvars[2], &error))
13653 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013654 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013655 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013656 if (get_tv_number_chk(&argvars[3], &error))
13657 {
13658 rettv->v_type = VAR_LIST;
13659 rettv->vval.v_list = NULL;
13660 }
13661 if (argvars[4].v_type != VAR_UNKNOWN
13662 && get_tv_number_chk(&argvars[4], &error))
13663 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013664 }
13665 }
13666 if (file != NULL && !error)
13667 {
13668 ga_init2(&ga, (int)sizeof(char_u *), 10);
13669 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13670 if (rettv->v_type == VAR_STRING)
13671 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13672 else if (rettv_list_alloc(rettv) != FAIL)
13673 for (i = 0; i < ga.ga_len; ++i)
13674 list_append_string(rettv->vval.v_list,
13675 ((char_u **)(ga.ga_data))[i], -1);
13676 ga_clear_strings(&ga);
13677 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013678 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013679 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013680}
13681
13682/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013683 * "glob2regpat()" function
13684 */
13685 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013686f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013687{
13688 char_u *pat = get_tv_string_chk(&argvars[0]);
13689
13690 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013691 rettv->vval.v_string = (pat == NULL)
13692 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013693}
13694
13695/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013696 * "has()" function
13697 */
13698 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013699f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013700{
13701 int i;
13702 char_u *name;
13703 int n = FALSE;
13704 static char *(has_list[]) =
13705 {
13706#ifdef AMIGA
13707 "amiga",
13708# ifdef FEAT_ARP
13709 "arp",
13710# endif
13711#endif
13712#ifdef __BEOS__
13713 "beos",
13714#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013715#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013716 "mac",
13717#endif
13718#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013719 "macunix", /* built with 'darwin' enabled */
13720#endif
13721#if defined(__APPLE__) && __APPLE__ == 1
13722 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013724#ifdef __QNX__
13725 "qnx",
13726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013727#ifdef UNIX
13728 "unix",
13729#endif
13730#ifdef VMS
13731 "vms",
13732#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013733#ifdef WIN32
13734 "win32",
13735#endif
13736#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13737 "win32unix",
13738#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013739#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013740 "win64",
13741#endif
13742#ifdef EBCDIC
13743 "ebcdic",
13744#endif
13745#ifndef CASE_INSENSITIVE_FILENAME
13746 "fname_case",
13747#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013748#ifdef HAVE_ACL
13749 "acl",
13750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013751#ifdef FEAT_ARABIC
13752 "arabic",
13753#endif
13754#ifdef FEAT_AUTOCMD
13755 "autocmd",
13756#endif
13757#ifdef FEAT_BEVAL
13758 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013759# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13760 "balloon_multiline",
13761# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013762#endif
13763#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13764 "builtin_terms",
13765# ifdef ALL_BUILTIN_TCAPS
13766 "all_builtin_terms",
13767# endif
13768#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013769#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13770 || defined(FEAT_GUI_W32) \
13771 || defined(FEAT_GUI_MOTIF))
13772 "browsefilter",
13773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013774#ifdef FEAT_BYTEOFF
13775 "byte_offset",
13776#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010013777#ifdef FEAT_CHANNEL
13778 "channel",
13779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013780#ifdef FEAT_CINDENT
13781 "cindent",
13782#endif
13783#ifdef FEAT_CLIENTSERVER
13784 "clientserver",
13785#endif
13786#ifdef FEAT_CLIPBOARD
13787 "clipboard",
13788#endif
13789#ifdef FEAT_CMDL_COMPL
13790 "cmdline_compl",
13791#endif
13792#ifdef FEAT_CMDHIST
13793 "cmdline_hist",
13794#endif
13795#ifdef FEAT_COMMENTS
13796 "comments",
13797#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013798#ifdef FEAT_CONCEAL
13799 "conceal",
13800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013801#ifdef FEAT_CRYPT
13802 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013803 "crypt-blowfish",
13804 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013805#endif
13806#ifdef FEAT_CSCOPE
13807 "cscope",
13808#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013809#ifdef FEAT_CURSORBIND
13810 "cursorbind",
13811#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013812#ifdef CURSOR_SHAPE
13813 "cursorshape",
13814#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013815#ifdef DEBUG
13816 "debug",
13817#endif
13818#ifdef FEAT_CON_DIALOG
13819 "dialog_con",
13820#endif
13821#ifdef FEAT_GUI_DIALOG
13822 "dialog_gui",
13823#endif
13824#ifdef FEAT_DIFF
13825 "diff",
13826#endif
13827#ifdef FEAT_DIGRAPHS
13828 "digraphs",
13829#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013830#ifdef FEAT_DIRECTX
13831 "directx",
13832#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013833#ifdef FEAT_DND
13834 "dnd",
13835#endif
13836#ifdef FEAT_EMACS_TAGS
13837 "emacs_tags",
13838#endif
13839 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013840 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013841#ifdef FEAT_SEARCH_EXTRA
13842 "extra_search",
13843#endif
13844#ifdef FEAT_FKMAP
13845 "farsi",
13846#endif
13847#ifdef FEAT_SEARCHPATH
13848 "file_in_path",
13849#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013850#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013851 "filterpipe",
13852#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013853#ifdef FEAT_FIND_ID
13854 "find_in_path",
13855#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013856#ifdef FEAT_FLOAT
13857 "float",
13858#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013859#ifdef FEAT_FOLDING
13860 "folding",
13861#endif
13862#ifdef FEAT_FOOTER
13863 "footer",
13864#endif
13865#if !defined(USE_SYSTEM) && defined(UNIX)
13866 "fork",
13867#endif
13868#ifdef FEAT_GETTEXT
13869 "gettext",
13870#endif
13871#ifdef FEAT_GUI
13872 "gui",
13873#endif
13874#ifdef FEAT_GUI_ATHENA
13875# ifdef FEAT_GUI_NEXTAW
13876 "gui_neXtaw",
13877# else
13878 "gui_athena",
13879# endif
13880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013881#ifdef FEAT_GUI_GTK
13882 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013883# ifdef USE_GTK3
13884 "gui_gtk3",
13885# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013886 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013887# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013888#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013889#ifdef FEAT_GUI_GNOME
13890 "gui_gnome",
13891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013892#ifdef FEAT_GUI_MAC
13893 "gui_mac",
13894#endif
13895#ifdef FEAT_GUI_MOTIF
13896 "gui_motif",
13897#endif
13898#ifdef FEAT_GUI_PHOTON
13899 "gui_photon",
13900#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013901#ifdef FEAT_GUI_W32
13902 "gui_win32",
13903#endif
13904#ifdef FEAT_HANGULIN
13905 "hangul_input",
13906#endif
13907#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13908 "iconv",
13909#endif
13910#ifdef FEAT_INS_EXPAND
13911 "insert_expand",
13912#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010013913#ifdef FEAT_JOB
13914 "job",
13915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013916#ifdef FEAT_JUMPLIST
13917 "jumplist",
13918#endif
13919#ifdef FEAT_KEYMAP
13920 "keymap",
13921#endif
13922#ifdef FEAT_LANGMAP
13923 "langmap",
13924#endif
13925#ifdef FEAT_LIBCALL
13926 "libcall",
13927#endif
13928#ifdef FEAT_LINEBREAK
13929 "linebreak",
13930#endif
13931#ifdef FEAT_LISP
13932 "lispindent",
13933#endif
13934#ifdef FEAT_LISTCMDS
13935 "listcmds",
13936#endif
13937#ifdef FEAT_LOCALMAP
13938 "localmap",
13939#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013940#ifdef FEAT_LUA
13941# ifndef DYNAMIC_LUA
13942 "lua",
13943# endif
13944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013945#ifdef FEAT_MENU
13946 "menu",
13947#endif
13948#ifdef FEAT_SESSION
13949 "mksession",
13950#endif
13951#ifdef FEAT_MODIFY_FNAME
13952 "modify_fname",
13953#endif
13954#ifdef FEAT_MOUSE
13955 "mouse",
13956#endif
13957#ifdef FEAT_MOUSESHAPE
13958 "mouseshape",
13959#endif
13960#if defined(UNIX) || defined(VMS)
13961# ifdef FEAT_MOUSE_DEC
13962 "mouse_dec",
13963# endif
13964# ifdef FEAT_MOUSE_GPM
13965 "mouse_gpm",
13966# endif
13967# ifdef FEAT_MOUSE_JSB
13968 "mouse_jsbterm",
13969# endif
13970# ifdef FEAT_MOUSE_NET
13971 "mouse_netterm",
13972# endif
13973# ifdef FEAT_MOUSE_PTERM
13974 "mouse_pterm",
13975# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013976# ifdef FEAT_MOUSE_SGR
13977 "mouse_sgr",
13978# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013979# ifdef FEAT_SYSMOUSE
13980 "mouse_sysmouse",
13981# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013982# ifdef FEAT_MOUSE_URXVT
13983 "mouse_urxvt",
13984# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013985# ifdef FEAT_MOUSE_XTERM
13986 "mouse_xterm",
13987# endif
13988#endif
13989#ifdef FEAT_MBYTE
13990 "multi_byte",
13991#endif
13992#ifdef FEAT_MBYTE_IME
13993 "multi_byte_ime",
13994#endif
13995#ifdef FEAT_MULTI_LANG
13996 "multi_lang",
13997#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013998#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013999#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014000 "mzscheme",
14001#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014003#ifdef FEAT_OLE
14004 "ole",
14005#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010014006 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014007#ifdef FEAT_PATH_EXTRA
14008 "path_extra",
14009#endif
14010#ifdef FEAT_PERL
14011#ifndef DYNAMIC_PERL
14012 "perl",
14013#endif
14014#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020014015#ifdef FEAT_PERSISTENT_UNDO
14016 "persistent_undo",
14017#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014018#ifdef FEAT_PYTHON
14019#ifndef DYNAMIC_PYTHON
14020 "python",
14021#endif
14022#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014023#ifdef FEAT_PYTHON3
14024#ifndef DYNAMIC_PYTHON3
14025 "python3",
14026#endif
14027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014028#ifdef FEAT_POSTSCRIPT
14029 "postscript",
14030#endif
14031#ifdef FEAT_PRINTER
14032 "printer",
14033#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000014034#ifdef FEAT_PROFILE
14035 "profile",
14036#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014037#ifdef FEAT_RELTIME
14038 "reltime",
14039#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014040#ifdef FEAT_QUICKFIX
14041 "quickfix",
14042#endif
14043#ifdef FEAT_RIGHTLEFT
14044 "rightleft",
14045#endif
14046#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
14047 "ruby",
14048#endif
14049#ifdef FEAT_SCROLLBIND
14050 "scrollbind",
14051#endif
14052#ifdef FEAT_CMDL_INFO
14053 "showcmd",
14054 "cmdline_info",
14055#endif
14056#ifdef FEAT_SIGNS
14057 "signs",
14058#endif
14059#ifdef FEAT_SMARTINDENT
14060 "smartindent",
14061#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000014062#ifdef STARTUPTIME
14063 "startuptime",
14064#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014065#ifdef FEAT_STL_OPT
14066 "statusline",
14067#endif
14068#ifdef FEAT_SUN_WORKSHOP
14069 "sun_workshop",
14070#endif
14071#ifdef FEAT_NETBEANS_INTG
14072 "netbeans_intg",
14073#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014074#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000014075 "spell",
14076#endif
14077#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000014078 "syntax",
14079#endif
14080#if defined(USE_SYSTEM) || !defined(UNIX)
14081 "system",
14082#endif
14083#ifdef FEAT_TAG_BINS
14084 "tag_binary",
14085#endif
14086#ifdef FEAT_TAG_OLDSTATIC
14087 "tag_old_static",
14088#endif
14089#ifdef FEAT_TAG_ANYWHITE
14090 "tag_any_white",
14091#endif
14092#ifdef FEAT_TCL
14093# ifndef DYNAMIC_TCL
14094 "tcl",
14095# endif
14096#endif
14097#ifdef TERMINFO
14098 "terminfo",
14099#endif
14100#ifdef FEAT_TERMRESPONSE
14101 "termresponse",
14102#endif
14103#ifdef FEAT_TEXTOBJ
14104 "textobjects",
14105#endif
14106#ifdef HAVE_TGETENT
14107 "tgetent",
14108#endif
14109#ifdef FEAT_TITLE
14110 "title",
14111#endif
14112#ifdef FEAT_TOOLBAR
14113 "toolbar",
14114#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014115#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14116 "unnamedplus",
14117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014118#ifdef FEAT_USR_CMDS
14119 "user-commands", /* was accidentally included in 5.4 */
14120 "user_commands",
14121#endif
14122#ifdef FEAT_VIMINFO
14123 "viminfo",
14124#endif
14125#ifdef FEAT_VERTSPLIT
14126 "vertsplit",
14127#endif
14128#ifdef FEAT_VIRTUALEDIT
14129 "virtualedit",
14130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014131 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014132#ifdef FEAT_VISUALEXTRA
14133 "visualextra",
14134#endif
14135#ifdef FEAT_VREPLACE
14136 "vreplace",
14137#endif
14138#ifdef FEAT_WILDIGN
14139 "wildignore",
14140#endif
14141#ifdef FEAT_WILDMENU
14142 "wildmenu",
14143#endif
14144#ifdef FEAT_WINDOWS
14145 "windows",
14146#endif
14147#ifdef FEAT_WAK
14148 "winaltkeys",
14149#endif
14150#ifdef FEAT_WRITEBACKUP
14151 "writebackup",
14152#endif
14153#ifdef FEAT_XIM
14154 "xim",
14155#endif
14156#ifdef FEAT_XFONTSET
14157 "xfontset",
14158#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014159#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014160 "xpm",
14161 "xpm_w32", /* for backward compatibility */
14162#else
14163# if defined(HAVE_XPM)
14164 "xpm",
14165# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014167#ifdef USE_XSMP
14168 "xsmp",
14169#endif
14170#ifdef USE_XSMP_INTERACT
14171 "xsmp_interact",
14172#endif
14173#ifdef FEAT_XCLIPBOARD
14174 "xterm_clipboard",
14175#endif
14176#ifdef FEAT_XTERM_SAVE
14177 "xterm_save",
14178#endif
14179#if defined(UNIX) && defined(FEAT_X11)
14180 "X11",
14181#endif
14182 NULL
14183 };
14184
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014185 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014186 for (i = 0; has_list[i] != NULL; ++i)
14187 if (STRICMP(name, has_list[i]) == 0)
14188 {
14189 n = TRUE;
14190 break;
14191 }
14192
14193 if (n == FALSE)
14194 {
14195 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014196 {
14197 if (name[5] == '-'
14198 && STRLEN(name) > 11
14199 && vim_isdigit(name[6])
14200 && vim_isdigit(name[8])
14201 && vim_isdigit(name[10]))
14202 {
14203 int major = atoi((char *)name + 6);
14204 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014205
14206 /* Expect "patch-9.9.01234". */
14207 n = (major < VIM_VERSION_MAJOR
14208 || (major == VIM_VERSION_MAJOR
14209 && (minor < VIM_VERSION_MINOR
14210 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014211 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014212 }
14213 else
14214 n = has_patch(atoi((char *)name + 5));
14215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014216 else if (STRICMP(name, "vim_starting") == 0)
14217 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014218#ifdef FEAT_MBYTE
14219 else if (STRICMP(name, "multi_byte_encoding") == 0)
14220 n = has_mbyte;
14221#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014222#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14223 else if (STRICMP(name, "balloon_multiline") == 0)
14224 n = multiline_balloon_available();
14225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014226#ifdef DYNAMIC_TCL
14227 else if (STRICMP(name, "tcl") == 0)
14228 n = tcl_enabled(FALSE);
14229#endif
14230#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14231 else if (STRICMP(name, "iconv") == 0)
14232 n = iconv_enabled(FALSE);
14233#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014234#ifdef DYNAMIC_LUA
14235 else if (STRICMP(name, "lua") == 0)
14236 n = lua_enabled(FALSE);
14237#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014238#ifdef DYNAMIC_MZSCHEME
14239 else if (STRICMP(name, "mzscheme") == 0)
14240 n = mzscheme_enabled(FALSE);
14241#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014242#ifdef DYNAMIC_RUBY
14243 else if (STRICMP(name, "ruby") == 0)
14244 n = ruby_enabled(FALSE);
14245#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014246#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014247#ifdef DYNAMIC_PYTHON
14248 else if (STRICMP(name, "python") == 0)
14249 n = python_enabled(FALSE);
14250#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014251#endif
14252#ifdef FEAT_PYTHON3
14253#ifdef DYNAMIC_PYTHON3
14254 else if (STRICMP(name, "python3") == 0)
14255 n = python3_enabled(FALSE);
14256#endif
14257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014258#ifdef DYNAMIC_PERL
14259 else if (STRICMP(name, "perl") == 0)
14260 n = perl_enabled(FALSE);
14261#endif
14262#ifdef FEAT_GUI
14263 else if (STRICMP(name, "gui_running") == 0)
14264 n = (gui.in_use || gui.starting);
14265# ifdef FEAT_GUI_W32
14266 else if (STRICMP(name, "gui_win32s") == 0)
14267 n = gui_is_win32s();
14268# endif
14269# ifdef FEAT_BROWSE
14270 else if (STRICMP(name, "browse") == 0)
14271 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14272# endif
14273#endif
14274#ifdef FEAT_SYN_HL
14275 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014276 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014277#endif
14278#if defined(WIN3264)
14279 else if (STRICMP(name, "win95") == 0)
14280 n = mch_windows95();
14281#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014282#ifdef FEAT_NETBEANS_INTG
14283 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014284 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014285#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014286 }
14287
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014288 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014289}
14290
14291/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014292 * "has_key()" function
14293 */
14294 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014295f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014296{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014297 if (argvars[0].v_type != VAR_DICT)
14298 {
14299 EMSG(_(e_dictreq));
14300 return;
14301 }
14302 if (argvars[0].vval.v_dict == NULL)
14303 return;
14304
14305 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014306 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014307}
14308
14309/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014310 * "haslocaldir()" function
14311 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014312 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014313f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014314{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014315 win_T *wp = NULL;
14316
14317 wp = find_tabwin(&argvars[0], &argvars[1]);
14318 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014319}
14320
14321/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014322 * "hasmapto()" function
14323 */
14324 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014325f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014326{
14327 char_u *name;
14328 char_u *mode;
14329 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014330 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014331
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014332 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014333 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014334 mode = (char_u *)"nvo";
14335 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014336 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014337 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014338 if (argvars[2].v_type != VAR_UNKNOWN)
14339 abbr = get_tv_number(&argvars[2]);
14340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014341
Bram Moolenaar2c932302006-03-18 21:42:09 +000014342 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014343 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014344 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014345 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014346}
14347
14348/*
14349 * "histadd()" function
14350 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014351 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014352f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014353{
14354#ifdef FEAT_CMDHIST
14355 int histype;
14356 char_u *str;
14357 char_u buf[NUMBUFLEN];
14358#endif
14359
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014360 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014361 if (check_restricted() || check_secure())
14362 return;
14363#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014364 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14365 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014366 if (histype >= 0)
14367 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014368 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014369 if (*str != NUL)
14370 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014371 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014372 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014373 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014374 return;
14375 }
14376 }
14377#endif
14378}
14379
14380/*
14381 * "histdel()" function
14382 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014383 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014384f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014385{
14386#ifdef FEAT_CMDHIST
14387 int n;
14388 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014389 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014390
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014391 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14392 if (str == NULL)
14393 n = 0;
14394 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014395 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014396 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014397 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014398 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014399 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014400 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014401 else
14402 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014403 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014404 get_tv_string_buf(&argvars[1], buf));
14405 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014406#endif
14407}
14408
14409/*
14410 * "histget()" function
14411 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014412 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014413f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014414{
14415#ifdef FEAT_CMDHIST
14416 int type;
14417 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014418 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014419
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014420 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14421 if (str == NULL)
14422 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014423 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014424 {
14425 type = get_histtype(str);
14426 if (argvars[1].v_type == VAR_UNKNOWN)
14427 idx = get_history_idx(type);
14428 else
14429 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14430 /* -1 on type error */
14431 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014433#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014434 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014435#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014436 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014437}
14438
14439/*
14440 * "histnr()" function
14441 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014442 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014443f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014444{
14445 int i;
14446
14447#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014448 char_u *history = get_tv_string_chk(&argvars[0]);
14449
14450 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014451 if (i >= HIST_CMD && i < HIST_COUNT)
14452 i = get_history_idx(i);
14453 else
14454#endif
14455 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014456 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014457}
14458
14459/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014460 * "highlightID(name)" function
14461 */
14462 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014463f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014464{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014465 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014466}
14467
14468/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014469 * "highlight_exists()" function
14470 */
14471 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014472f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014473{
14474 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14475}
14476
14477/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014478 * "hostname()" function
14479 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014480 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014481f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014482{
14483 char_u hostname[256];
14484
14485 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014486 rettv->v_type = VAR_STRING;
14487 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014488}
14489
14490/*
14491 * iconv() function
14492 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014494f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014495{
14496#ifdef FEAT_MBYTE
14497 char_u buf1[NUMBUFLEN];
14498 char_u buf2[NUMBUFLEN];
14499 char_u *from, *to, *str;
14500 vimconv_T vimconv;
14501#endif
14502
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014503 rettv->v_type = VAR_STRING;
14504 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014505
14506#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014507 str = get_tv_string(&argvars[0]);
14508 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14509 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014510 vimconv.vc_type = CONV_NONE;
14511 convert_setup(&vimconv, from, to);
14512
14513 /* If the encodings are equal, no conversion needed. */
14514 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014515 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014516 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014517 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014518
14519 convert_setup(&vimconv, NULL, NULL);
14520 vim_free(from);
14521 vim_free(to);
14522#endif
14523}
14524
14525/*
14526 * "indent()" function
14527 */
14528 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014529f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014530{
14531 linenr_T lnum;
14532
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014533 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014534 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014535 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014536 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014537 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014538}
14539
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014540/*
14541 * "index()" function
14542 */
14543 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014544f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014545{
Bram Moolenaar33570922005-01-25 22:26:29 +000014546 list_T *l;
14547 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014548 long idx = 0;
14549 int ic = FALSE;
14550
14551 rettv->vval.v_number = -1;
14552 if (argvars[0].v_type != VAR_LIST)
14553 {
14554 EMSG(_(e_listreq));
14555 return;
14556 }
14557 l = argvars[0].vval.v_list;
14558 if (l != NULL)
14559 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014560 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014561 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014562 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014563 int error = FALSE;
14564
Bram Moolenaar758711c2005-02-02 23:11:38 +000014565 /* Start at specified item. Use the cached index that list_find()
14566 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014567 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014568 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014569 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014570 ic = get_tv_number_chk(&argvars[3], &error);
14571 if (error)
14572 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014573 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014574
Bram Moolenaar758711c2005-02-02 23:11:38 +000014575 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014576 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014577 {
14578 rettv->vval.v_number = idx;
14579 break;
14580 }
14581 }
14582}
14583
Bram Moolenaar071d4272004-06-13 20:20:40 +000014584static int inputsecret_flag = 0;
14585
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014586static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014587
Bram Moolenaar071d4272004-06-13 20:20:40 +000014588/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014589 * This function is used by f_input() and f_inputdialog() functions. The third
14590 * argument to f_input() specifies the type of completion to use at the
14591 * prompt. The third argument to f_inputdialog() specifies the value to return
14592 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014593 */
14594 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014595get_user_input(
14596 typval_T *argvars,
14597 typval_T *rettv,
14598 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014599{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014600 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014601 char_u *p = NULL;
14602 int c;
14603 char_u buf[NUMBUFLEN];
14604 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014605 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014606 int xp_type = EXPAND_NOTHING;
14607 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014608
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014609 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014610 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014611
14612#ifdef NO_CONSOLE_INPUT
14613 /* While starting up, there is no place to enter text. */
14614 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014615 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014616#endif
14617
14618 cmd_silent = FALSE; /* Want to see the prompt. */
14619 if (prompt != NULL)
14620 {
14621 /* Only the part of the message after the last NL is considered as
14622 * prompt for the command line */
14623 p = vim_strrchr(prompt, '\n');
14624 if (p == NULL)
14625 p = prompt;
14626 else
14627 {
14628 ++p;
14629 c = *p;
14630 *p = NUL;
14631 msg_start();
14632 msg_clr_eos();
14633 msg_puts_attr(prompt, echo_attr);
14634 msg_didout = FALSE;
14635 msg_starthere();
14636 *p = c;
14637 }
14638 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014639
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014640 if (argvars[1].v_type != VAR_UNKNOWN)
14641 {
14642 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14643 if (defstr != NULL)
14644 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014645
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014646 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014647 {
14648 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014649 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014650 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014651
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014652 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014653 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014654
Bram Moolenaar4463f292005-09-25 22:20:24 +000014655 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14656 if (xp_name == NULL)
14657 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014658
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014659 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014660
Bram Moolenaar4463f292005-09-25 22:20:24 +000014661 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14662 &xp_arg) == FAIL)
14663 return;
14664 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014665 }
14666
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014667 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014668 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014669 int save_ex_normal_busy = ex_normal_busy;
14670 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014671 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014672 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14673 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014674 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014675 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014676 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014677 && argvars[1].v_type != VAR_UNKNOWN
14678 && argvars[2].v_type != VAR_UNKNOWN)
14679 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14680 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014681
14682 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014683
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014684 /* since the user typed this, no need to wait for return */
14685 need_wait_return = FALSE;
14686 msg_didout = FALSE;
14687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014688 cmd_silent = cmd_silent_save;
14689}
14690
14691/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014692 * "input()" function
14693 * Also handles inputsecret() when inputsecret is set.
14694 */
14695 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014696f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014697{
14698 get_user_input(argvars, rettv, FALSE);
14699}
14700
14701/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014702 * "inputdialog()" function
14703 */
14704 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014705f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014706{
14707#if defined(FEAT_GUI_TEXTDIALOG)
14708 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14709 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14710 {
14711 char_u *message;
14712 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014713 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014714
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014715 message = get_tv_string_chk(&argvars[0]);
14716 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014717 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014718 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014719 else
14720 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014721 if (message != NULL && defstr != NULL
14722 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014723 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014724 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014725 else
14726 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014727 if (message != NULL && defstr != NULL
14728 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014729 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014730 rettv->vval.v_string = vim_strsave(
14731 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014732 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014733 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014734 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014735 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014736 }
14737 else
14738#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014739 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014740}
14741
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014742/*
14743 * "inputlist()" function
14744 */
14745 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014746f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014747{
14748 listitem_T *li;
14749 int selected;
14750 int mouse_used;
14751
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014752#ifdef NO_CONSOLE_INPUT
14753 /* While starting up, there is no place to enter text. */
14754 if (no_console_input())
14755 return;
14756#endif
14757 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14758 {
14759 EMSG2(_(e_listarg), "inputlist()");
14760 return;
14761 }
14762
14763 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014764 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014765 lines_left = Rows; /* avoid more prompt */
14766 msg_scroll = TRUE;
14767 msg_clr_eos();
14768
14769 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14770 {
14771 msg_puts(get_tv_string(&li->li_tv));
14772 msg_putchar('\n');
14773 }
14774
14775 /* Ask for choice. */
14776 selected = prompt_for_number(&mouse_used);
14777 if (mouse_used)
14778 selected -= lines_left;
14779
14780 rettv->vval.v_number = selected;
14781}
14782
14783
Bram Moolenaar071d4272004-06-13 20:20:40 +000014784static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14785
14786/*
14787 * "inputrestore()" function
14788 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014789 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014790f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014791{
14792 if (ga_userinput.ga_len > 0)
14793 {
14794 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014795 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14796 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014797 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014798 }
14799 else if (p_verbose > 1)
14800 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014801 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014802 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014803 }
14804}
14805
14806/*
14807 * "inputsave()" function
14808 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014809 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014810f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014811{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014812 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014813 if (ga_grow(&ga_userinput, 1) == OK)
14814 {
14815 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14816 + ga_userinput.ga_len);
14817 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014818 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014819 }
14820 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014821 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014822}
14823
14824/*
14825 * "inputsecret()" function
14826 */
14827 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014828f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014829{
14830 ++cmdline_star;
14831 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014832 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014833 --cmdline_star;
14834 --inputsecret_flag;
14835}
14836
14837/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014838 * "insert()" function
14839 */
14840 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014841f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014842{
14843 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014844 listitem_T *item;
14845 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014846 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014847
14848 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014849 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014850 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014851 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014852 {
14853 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014854 before = get_tv_number_chk(&argvars[2], &error);
14855 if (error)
14856 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014857
Bram Moolenaar758711c2005-02-02 23:11:38 +000014858 if (before == l->lv_len)
14859 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014860 else
14861 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014862 item = list_find(l, before);
14863 if (item == NULL)
14864 {
14865 EMSGN(_(e_listidx), before);
14866 l = NULL;
14867 }
14868 }
14869 if (l != NULL)
14870 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014871 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014872 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014873 }
14874 }
14875}
14876
14877/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014878 * "invert(expr)" function
14879 */
14880 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014881f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014882{
14883 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14884}
14885
14886/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014887 * "isdirectory()" function
14888 */
14889 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014890f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014891{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014892 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014893}
14894
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014895/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014896 * "islocked()" function
14897 */
14898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014899f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014900{
14901 lval_T lv;
14902 char_u *end;
14903 dictitem_T *di;
14904
14905 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014906 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14907 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014908 if (end != NULL && lv.ll_name != NULL)
14909 {
14910 if (*end != NUL)
14911 EMSG(_(e_trailing));
14912 else
14913 {
14914 if (lv.ll_tv == NULL)
14915 {
14916 if (check_changedtick(lv.ll_name))
14917 rettv->vval.v_number = 1; /* always locked */
14918 else
14919 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014920 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014921 if (di != NULL)
14922 {
14923 /* Consider a variable locked when:
14924 * 1. the variable itself is locked
14925 * 2. the value of the variable is locked.
14926 * 3. the List or Dict value is locked.
14927 */
14928 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14929 || tv_islocked(&di->di_tv));
14930 }
14931 }
14932 }
14933 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014934 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014935 else if (lv.ll_newkey != NULL)
14936 EMSG2(_(e_dictkey), lv.ll_newkey);
14937 else if (lv.ll_list != NULL)
14938 /* List item. */
14939 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14940 else
14941 /* Dictionary item. */
14942 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14943 }
14944 }
14945
14946 clear_lval(&lv);
14947}
14948
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014949#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14950/*
14951 * "isnan()" function
14952 */
14953 static void
14954f_isnan(typval_T *argvars, typval_T *rettv)
14955{
14956 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14957 && isnan(argvars[0].vval.v_float);
14958}
14959#endif
14960
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014961static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014962
14963/*
14964 * Turn a dict into a list:
14965 * "what" == 0: list of keys
14966 * "what" == 1: list of values
14967 * "what" == 2: list of items
14968 */
14969 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014970dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014971{
Bram Moolenaar33570922005-01-25 22:26:29 +000014972 list_T *l2;
14973 dictitem_T *di;
14974 hashitem_T *hi;
14975 listitem_T *li;
14976 listitem_T *li2;
14977 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014978 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014979
Bram Moolenaar8c711452005-01-14 21:53:12 +000014980 if (argvars[0].v_type != VAR_DICT)
14981 {
14982 EMSG(_(e_dictreq));
14983 return;
14984 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014985 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014986 return;
14987
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014988 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014989 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014990
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014991 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014992 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014993 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014994 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014995 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014996 --todo;
14997 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014998
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014999 li = listitem_alloc();
15000 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015001 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015002 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015003
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015004 if (what == 0)
15005 {
15006 /* keys() */
15007 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015008 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015009 li->li_tv.vval.v_string = vim_strsave(di->di_key);
15010 }
15011 else if (what == 1)
15012 {
15013 /* values() */
15014 copy_tv(&di->di_tv, &li->li_tv);
15015 }
15016 else
15017 {
15018 /* items() */
15019 l2 = list_alloc();
15020 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015021 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015022 li->li_tv.vval.v_list = l2;
15023 if (l2 == NULL)
15024 break;
15025 ++l2->lv_refcount;
15026
15027 li2 = listitem_alloc();
15028 if (li2 == NULL)
15029 break;
15030 list_append(l2, li2);
15031 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015032 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015033 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
15034
15035 li2 = listitem_alloc();
15036 if (li2 == NULL)
15037 break;
15038 list_append(l2, li2);
15039 copy_tv(&di->di_tv, &li2->li_tv);
15040 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015041 }
15042 }
15043}
15044
15045/*
15046 * "items(dict)" function
15047 */
15048 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015049f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015050{
15051 dict_list(argvars, rettv, 2);
15052}
15053
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015054#if defined(FEAT_JOB) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015055/*
15056 * Get the job from the argument.
15057 * Returns NULL if the job is invalid.
15058 */
15059 static job_T *
15060get_job_arg(typval_T *tv)
15061{
15062 job_T *job;
15063
15064 if (tv->v_type != VAR_JOB)
15065 {
15066 EMSG2(_(e_invarg2), get_tv_string(tv));
15067 return NULL;
15068 }
15069 job = tv->vval.v_job;
15070
15071 if (job == NULL)
15072 EMSG(_("E916: not a valid job"));
15073 return job;
15074}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015075
15076# ifdef FEAT_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010015077/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015078 * "job_getchannel()" function
15079 */
15080 static void
15081f_job_getchannel(typval_T *argvars, typval_T *rettv)
15082{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015083 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015084
Bram Moolenaar65edff82016-02-21 16:40:11 +010015085 if (job != NULL)
15086 {
Bram Moolenaar77073442016-02-13 23:23:53 +010015087 rettv->v_type = VAR_CHANNEL;
15088 rettv->vval.v_channel = job->jv_channel;
15089 if (job->jv_channel != NULL)
15090 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015091 }
15092}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015093# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015094
15095/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015096 * "job_setoptions()" function
15097 */
15098 static void
15099f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15100{
15101 job_T *job = get_job_arg(&argvars[0]);
15102 jobopt_T opt;
15103
15104 if (job == NULL)
15105 return;
15106 clear_job_options(&opt);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015107 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015108 return;
15109 job_set_options(job, &opt);
15110}
15111
15112/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015113 * "job_start()" function
15114 */
15115 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010015116f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015117{
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015118 job_T *job;
15119 char_u *cmd = NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015120#if defined(UNIX)
15121# define USE_ARGV
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015122 char **argv = NULL;
15123 int argc = 0;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015124#else
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015125 garray_T ga;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015126#endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015127 jobopt_T opt;
Bram Moolenaarb69fccf2016-03-06 23:06:25 +010015128 int part;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015129
15130 rettv->v_type = VAR_JOB;
15131 job = job_alloc();
15132 rettv->vval.v_job = job;
15133 if (job == NULL)
15134 return;
15135
15136 rettv->vval.v_job->jv_status = JOB_FAILED;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015137
15138 /* Default mode is NL. */
Bram Moolenaarb6b52522016-02-20 23:30:07 +010015139 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015140 opt.jo_mode = MODE_NL;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010015141 if (get_job_options(&argvars[1], &opt,
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015142 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL
Bram Moolenaar187db502016-02-27 14:44:26 +010015143 + JO_STOPONEXIT + JO_EXIT_CB + JO_OUT_IO) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015144 return;
Bram Moolenaar014069a2016-03-03 22:51:40 +010015145
Bram Moolenaarb69fccf2016-03-06 23:06:25 +010015146 /* Check that when io is "file" that there is a file name. */
15147 for (part = PART_OUT; part <= PART_IN; ++part)
15148 if ((opt.jo_set & (JO_OUT_IO << (part - PART_OUT)))
15149 && opt.jo_io[part] == JIO_FILE
15150 && (!(opt.jo_set & (JO_OUT_NAME << (part - PART_OUT)))
15151 || *opt.jo_io_name[part] == NUL))
15152 {
15153 EMSG(_("E920: -io file requires -name to be set"));
15154 return;
15155 }
15156
Bram Moolenaar014069a2016-03-03 22:51:40 +010015157 if ((opt.jo_set & JO_IN_IO) && opt.jo_io[PART_IN] == JIO_BUFFER)
15158 {
15159 buf_T *buf;
15160
15161 /* check that we can find the buffer before starting the job */
15162 if (!(opt.jo_set & JO_IN_NAME))
15163 {
15164 EMSG(_("E915: in-io buffer requires in-name to be set"));
15165 return;
15166 }
15167 buf = buflist_find_by_name(opt.jo_io_name[PART_IN], FALSE);
15168 if (buf == NULL)
15169 return;
15170 if (buf->b_ml.ml_mfp == NULL)
15171 {
15172 EMSG2(_("E918: buffer must be loaded: %s"),
15173 opt.jo_io_name[PART_IN]);
15174 return;
15175 }
15176 job->jv_in_buf = buf;
15177 }
15178
Bram Moolenaar65edff82016-02-21 16:40:11 +010015179 job_set_options(job, &opt);
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015180
Bram Moolenaar835dc632016-02-07 14:27:38 +010015181#ifndef USE_ARGV
Bram Moolenaar942d6b22016-02-07 19:57:16 +010015182 ga_init2(&ga, (int)sizeof(char*), 20);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015183#endif
15184
15185 if (argvars[0].v_type == VAR_STRING)
15186 {
15187 /* Command is a string. */
15188 cmd = argvars[0].vval.v_string;
15189#ifdef USE_ARGV
15190 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
15191 return;
15192 argv[argc] = NULL;
15193#endif
15194 }
15195 else if (argvars[0].v_type != VAR_LIST
15196 || argvars[0].vval.v_list == NULL
15197 || argvars[0].vval.v_list->lv_len < 1)
15198 {
15199 EMSG(_(e_invarg));
15200 return;
15201 }
15202 else
15203 {
15204 list_T *l = argvars[0].vval.v_list;
15205 listitem_T *li;
15206 char_u *s;
15207
15208#ifdef USE_ARGV
15209 /* Pass argv[] to mch_call_shell(). */
15210 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
15211 if (argv == NULL)
15212 return;
15213#endif
15214 for (li = l->lv_first; li != NULL; li = li->li_next)
15215 {
15216 s = get_tv_string_chk(&li->li_tv);
15217 if (s == NULL)
15218 goto theend;
15219#ifdef USE_ARGV
15220 argv[argc++] = (char *)s;
15221#else
Bram Moolenaara86f14a2016-02-29 21:05:48 +010015222 /* Only escape when needed, double quotes are not always allowed. */
15223 if (li != l->lv_first && vim_strpbrk(s, (char_u *)" \t\"") != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015224 {
15225 s = vim_strsave_shellescape(s, FALSE, TRUE);
15226 if (s == NULL)
15227 goto theend;
Bram Moolenaar76467df2016-02-12 19:30:26 +010015228 ga_concat(&ga, s);
15229 vim_free(s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015230 }
Bram Moolenaar76467df2016-02-12 19:30:26 +010015231 else
15232 ga_concat(&ga, s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015233 if (li->li_next != NULL)
15234 ga_append(&ga, ' ');
15235#endif
15236 }
15237#ifdef USE_ARGV
15238 argv[argc] = NULL;
15239#else
15240 cmd = ga.ga_data;
15241#endif
15242 }
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015243
Bram Moolenaar835dc632016-02-07 14:27:38 +010015244#ifdef USE_ARGV
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015245# ifdef FEAT_CHANNEL
15246 if (ch_log_active())
15247 {
15248 garray_T ga;
15249 int i;
15250
15251 ga_init2(&ga, (int)sizeof(char), 200);
15252 for (i = 0; i < argc; ++i)
15253 {
15254 if (i > 0)
15255 ga_concat(&ga, (char_u *)" ");
15256 ga_concat(&ga, (char_u *)argv[i]);
15257 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +010015258 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015259 ga_clear(&ga);
15260 }
15261# endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015262 mch_start_job(argv, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015263#else
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015264# ifdef FEAT_CHANNEL
Bram Moolenaared5a78e2016-02-19 21:05:03 +010015265 ch_logs(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015266# endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015267 mch_start_job((char *)cmd, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015268#endif
15269
Bram Moolenaar014069a2016-03-03 22:51:40 +010015270#ifdef FEAT_CHANNEL
Bram Moolenaar839fd112016-03-06 21:34:03 +010015271 /* If the channel is reading from a buffer, write lines now. */
Bram Moolenaar4e329fc2016-03-07 15:24:03 +010015272 if (job->jv_channel != NULL)
15273 channel_write_in(job->jv_channel);
Bram Moolenaar014069a2016-03-03 22:51:40 +010015274#endif
15275
Bram Moolenaar835dc632016-02-07 14:27:38 +010015276theend:
15277#ifdef USE_ARGV
Bram Moolenaaree5aeae2016-02-07 22:30:47 +010015278 vim_free(argv);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015279#else
15280 vim_free(ga.ga_data);
15281#endif
15282}
15283
15284/*
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015285 * Get the status of "job" and invoke the exit callback when needed.
15286 * The returned string is not allocated.
15287 */
15288 static char *
15289job_status(job_T *job)
15290{
15291 char *result;
15292
15293 if (job->jv_status == JOB_ENDED)
15294 /* No need to check, dead is dead. */
15295 result = "dead";
15296 else if (job->jv_status == JOB_FAILED)
15297 result = "fail";
15298 else
15299 {
15300 result = mch_job_status(job);
15301# ifdef FEAT_CHANNEL
15302 if (job->jv_status == JOB_ENDED)
15303 ch_log(job->jv_channel, "Job ended");
15304# endif
15305 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
15306 {
15307 typval_T argv[3];
15308 typval_T rettv;
15309 int dummy;
15310
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015311 /* invoke the exit callback; make sure the refcount is > 0 */
15312 ++job->jv_refcount;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015313 argv[0].v_type = VAR_JOB;
15314 argv[0].vval.v_job = job;
15315 argv[1].v_type = VAR_NUMBER;
15316 argv[1].vval.v_number = job->jv_exitval;
15317 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
15318 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
15319 clear_tv(&rettv);
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015320 --job->jv_refcount;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015321 }
15322 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
15323 {
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015324 /* The job was already unreferenced, now that it ended it can be
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015325 * freed. Careful: caller must not use "job" after this! */
15326 job_free(job);
15327 }
15328 }
15329 return result;
15330}
15331
15332/*
15333 * Called once in a while: check if any jobs with an "exit-cb" have ended.
15334 */
15335 void
Bram Moolenaarb2bd6a02016-02-22 20:20:25 +010015336job_check_ended(void)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015337{
15338 static time_t last_check = 0;
15339 time_t now;
15340 job_T *job;
15341 job_T *next;
15342
15343 /* Only do this once in 10 seconds. */
15344 now = time(NULL);
15345 if (last_check + 10 < now)
15346 {
15347 last_check = now;
15348 for (job = first_job; job != NULL; job = next)
15349 {
15350 next = job->jv_next;
15351 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL)
15352 job_status(job); /* may free "job" */
15353 }
15354 }
15355}
15356
15357/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015358 * "job_status()" function
15359 */
15360 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015361f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015362{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015363 job_T *job = get_job_arg(&argvars[0]);
15364 char *result;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015365
Bram Moolenaar65edff82016-02-21 16:40:11 +010015366 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015367 {
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015368 result = job_status(job);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015369 rettv->v_type = VAR_STRING;
15370 rettv->vval.v_string = vim_strsave((char_u *)result);
15371 }
15372}
15373
15374/*
15375 * "job_stop()" function
15376 */
15377 static void
15378f_job_stop(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
15379{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015380 job_T *job = get_job_arg(&argvars[0]);
15381
15382 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015383 {
15384 char_u *arg;
15385
15386 if (argvars[1].v_type == VAR_UNKNOWN)
15387 arg = (char_u *)"";
15388 else
15389 {
15390 arg = get_tv_string_chk(&argvars[1]);
15391 if (arg == NULL)
15392 {
15393 EMSG(_(e_invarg));
15394 return;
15395 }
15396 }
Bram Moolenaard6051b52016-02-28 15:49:03 +010015397# ifdef FEAT_CHANNEL
15398 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
15399# endif
Bram Moolenaar65edff82016-02-21 16:40:11 +010015400 if (mch_stop_job(job, arg) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015401 rettv->vval.v_number = 0;
15402 else
Bram Moolenaar46c85432016-02-26 11:17:46 +010015403 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015404 rettv->vval.v_number = 1;
Bram Moolenaar46c85432016-02-26 11:17:46 +010015405 /* Assume that "hup" does not kill the job. */
15406 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
15407 job->jv_channel->ch_job_killed = TRUE;
15408 }
15409 /* We don't try freeing the job, obviously the caller still has a
15410 * reference to it. */
Bram Moolenaar835dc632016-02-07 14:27:38 +010015411 }
15412}
15413#endif
15414
Bram Moolenaar071d4272004-06-13 20:20:40 +000015415/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015416 * "join()" function
15417 */
15418 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015419f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015420{
15421 garray_T ga;
15422 char_u *sep;
15423
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015424 if (argvars[0].v_type != VAR_LIST)
15425 {
15426 EMSG(_(e_listreq));
15427 return;
15428 }
15429 if (argvars[0].vval.v_list == NULL)
15430 return;
15431 if (argvars[1].v_type == VAR_UNKNOWN)
15432 sep = (char_u *)" ";
15433 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015434 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015435
15436 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015437
15438 if (sep != NULL)
15439 {
15440 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015441 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015442 ga_append(&ga, NUL);
15443 rettv->vval.v_string = (char_u *)ga.ga_data;
15444 }
15445 else
15446 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015447}
15448
15449/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015450 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015451 */
15452 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015453f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015454{
15455 js_read_T reader;
15456
15457 reader.js_buf = get_tv_string(&argvars[0]);
15458 reader.js_fill = NULL;
15459 reader.js_used = 0;
15460 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15461 EMSG(_(e_invarg));
15462}
15463
15464/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015465 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015466 */
15467 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015468f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015469{
15470 rettv->v_type = VAR_STRING;
15471 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15472}
15473
15474/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015475 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015476 */
15477 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015478f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015479{
15480 js_read_T reader;
15481
15482 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015483 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015484 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015485 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015486 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015487}
15488
15489/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015490 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015491 */
15492 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015493f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015494{
15495 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015496 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015497}
15498
15499/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015500 * "keys()" function
15501 */
15502 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015503f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015504{
15505 dict_list(argvars, rettv, 0);
15506}
15507
15508/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015509 * "last_buffer_nr()" function.
15510 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015512f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015513{
15514 int n = 0;
15515 buf_T *buf;
15516
15517 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15518 if (n < buf->b_fnum)
15519 n = buf->b_fnum;
15520
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015521 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015522}
15523
15524/*
15525 * "len()" function
15526 */
15527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015528f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015529{
15530 switch (argvars[0].v_type)
15531 {
15532 case VAR_STRING:
15533 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015534 rettv->vval.v_number = (varnumber_T)STRLEN(
15535 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015536 break;
15537 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015538 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015539 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015540 case VAR_DICT:
15541 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15542 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015543 case VAR_UNKNOWN:
15544 case VAR_SPECIAL:
15545 case VAR_FLOAT:
15546 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015547 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015548 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015549 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015550 break;
15551 }
15552}
15553
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015554static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015555
15556 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015557libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015558{
15559#ifdef FEAT_LIBCALL
15560 char_u *string_in;
15561 char_u **string_result;
15562 int nr_result;
15563#endif
15564
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015565 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015566 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015567 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015568
15569 if (check_restricted() || check_secure())
15570 return;
15571
15572#ifdef FEAT_LIBCALL
15573 /* The first two args must be strings, otherwise its meaningless */
15574 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15575 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015576 string_in = NULL;
15577 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015578 string_in = argvars[2].vval.v_string;
15579 if (type == VAR_NUMBER)
15580 string_result = NULL;
15581 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015582 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015583 if (mch_libcall(argvars[0].vval.v_string,
15584 argvars[1].vval.v_string,
15585 string_in,
15586 argvars[2].vval.v_number,
15587 string_result,
15588 &nr_result) == OK
15589 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015590 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015591 }
15592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015593}
15594
15595/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015596 * "libcall()" function
15597 */
15598 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015599f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015600{
15601 libcall_common(argvars, rettv, VAR_STRING);
15602}
15603
15604/*
15605 * "libcallnr()" function
15606 */
15607 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015608f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015609{
15610 libcall_common(argvars, rettv, VAR_NUMBER);
15611}
15612
15613/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015614 * "line(string)" function
15615 */
15616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015617f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015618{
15619 linenr_T lnum = 0;
15620 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015621 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015622
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015623 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015624 if (fp != NULL)
15625 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015626 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015627}
15628
15629/*
15630 * "line2byte(lnum)" function
15631 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015632 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015633f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015634{
15635#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015636 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015637#else
15638 linenr_T lnum;
15639
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015640 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015641 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015642 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015643 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015644 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15645 if (rettv->vval.v_number >= 0)
15646 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015647#endif
15648}
15649
15650/*
15651 * "lispindent(lnum)" function
15652 */
15653 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015654f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015655{
15656#ifdef FEAT_LISP
15657 pos_T pos;
15658 linenr_T lnum;
15659
15660 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015661 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015662 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15663 {
15664 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015665 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015666 curwin->w_cursor = pos;
15667 }
15668 else
15669#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015670 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015671}
15672
15673/*
15674 * "localtime()" function
15675 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015676 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015677f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015678{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015679 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015680}
15681
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015682static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015683
15684 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015685get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015686{
15687 char_u *keys;
15688 char_u *which;
15689 char_u buf[NUMBUFLEN];
15690 char_u *keys_buf = NULL;
15691 char_u *rhs;
15692 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015693 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015694 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015695 mapblock_T *mp;
15696 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015697
15698 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015699 rettv->v_type = VAR_STRING;
15700 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015701
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015702 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015703 if (*keys == NUL)
15704 return;
15705
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015706 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015707 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015708 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015709 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015710 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015711 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015712 if (argvars[3].v_type != VAR_UNKNOWN)
15713 get_dict = get_tv_number(&argvars[3]);
15714 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015716 else
15717 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015718 if (which == NULL)
15719 return;
15720
Bram Moolenaar071d4272004-06-13 20:20:40 +000015721 mode = get_map_mode(&which, 0);
15722
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015723 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015724 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015725 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015726
15727 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015728 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015729 /* Return a string. */
15730 if (rhs != NULL)
15731 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015732
Bram Moolenaarbd743252010-10-20 21:23:33 +020015733 }
15734 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15735 {
15736 /* Return a dictionary. */
15737 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15738 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15739 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015740
Bram Moolenaarbd743252010-10-20 21:23:33 +020015741 dict_add_nr_str(dict, "lhs", 0L, lhs);
15742 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15743 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15744 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15745 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15746 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15747 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015748 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015749 dict_add_nr_str(dict, "mode", 0L, mapmode);
15750
15751 vim_free(lhs);
15752 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015753 }
15754}
15755
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015756#ifdef FEAT_FLOAT
15757/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015758 * "log()" function
15759 */
15760 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015761f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015762{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015763 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015764
15765 rettv->v_type = VAR_FLOAT;
15766 if (get_float_arg(argvars, &f) == OK)
15767 rettv->vval.v_float = log(f);
15768 else
15769 rettv->vval.v_float = 0.0;
15770}
15771
15772/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015773 * "log10()" function
15774 */
15775 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015776f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015777{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015778 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015779
15780 rettv->v_type = VAR_FLOAT;
15781 if (get_float_arg(argvars, &f) == OK)
15782 rettv->vval.v_float = log10(f);
15783 else
15784 rettv->vval.v_float = 0.0;
15785}
15786#endif
15787
Bram Moolenaar1dced572012-04-05 16:54:08 +020015788#ifdef FEAT_LUA
15789/*
15790 * "luaeval()" function
15791 */
15792 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015793f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015794{
15795 char_u *str;
15796 char_u buf[NUMBUFLEN];
15797
15798 str = get_tv_string_buf(&argvars[0], buf);
15799 do_luaeval(str, argvars + 1, rettv);
15800}
15801#endif
15802
Bram Moolenaar071d4272004-06-13 20:20:40 +000015803/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015804 * "map()" function
15805 */
15806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015807f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015808{
15809 filter_map(argvars, rettv, TRUE);
15810}
15811
15812/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015813 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015814 */
15815 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015816f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015817{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015818 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015819}
15820
15821/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015822 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015823 */
15824 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015825f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015826{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015827 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015828}
15829
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015830static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015831
15832 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015833find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015834{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015835 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015836 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015837 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015838 char_u *pat;
15839 regmatch_T regmatch;
15840 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015841 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015842 char_u *save_cpo;
15843 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015844 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015845 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015846 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015847 list_T *l = NULL;
15848 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015849 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015850 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015851
15852 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15853 save_cpo = p_cpo;
15854 p_cpo = (char_u *)"";
15855
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015856 rettv->vval.v_number = -1;
15857 if (type == 3)
15858 {
15859 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015860 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015861 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015862 }
15863 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015864 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015865 rettv->v_type = VAR_STRING;
15866 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015868
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015869 if (argvars[0].v_type == VAR_LIST)
15870 {
15871 if ((l = argvars[0].vval.v_list) == NULL)
15872 goto theend;
15873 li = l->lv_first;
15874 }
15875 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015876 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015877 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015878 len = (long)STRLEN(str);
15879 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015880
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015881 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15882 if (pat == NULL)
15883 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015884
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015885 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015886 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015887 int error = FALSE;
15888
15889 start = get_tv_number_chk(&argvars[2], &error);
15890 if (error)
15891 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015892 if (l != NULL)
15893 {
15894 li = list_find(l, start);
15895 if (li == NULL)
15896 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015897 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015898 }
15899 else
15900 {
15901 if (start < 0)
15902 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015903 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015904 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015905 /* When "count" argument is there ignore matches before "start",
15906 * otherwise skip part of the string. Differs when pattern is "^"
15907 * or "\<". */
15908 if (argvars[3].v_type != VAR_UNKNOWN)
15909 startcol = start;
15910 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015911 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015912 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015913 len -= start;
15914 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015915 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015916
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015917 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015918 nth = get_tv_number_chk(&argvars[3], &error);
15919 if (error)
15920 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015921 }
15922
15923 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15924 if (regmatch.regprog != NULL)
15925 {
15926 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015927
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015928 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015929 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015930 if (l != NULL)
15931 {
15932 if (li == NULL)
15933 {
15934 match = FALSE;
15935 break;
15936 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015937 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015938 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015939 if (str == NULL)
15940 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015941 }
15942
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015943 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015944
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015945 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015946 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015947 if (l == NULL && !match)
15948 break;
15949
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015950 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015951 if (l != NULL)
15952 {
15953 li = li->li_next;
15954 ++idx;
15955 }
15956 else
15957 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015958#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015959 startcol = (colnr_T)(regmatch.startp[0]
15960 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015961#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015962 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015963#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015964 if (startcol > (colnr_T)len
15965 || str + startcol <= regmatch.startp[0])
15966 {
15967 match = FALSE;
15968 break;
15969 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015970 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015971 }
15972
15973 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015974 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015975 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015976 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015977 int i;
15978
15979 /* return list with matched string and submatches */
15980 for (i = 0; i < NSUBEXP; ++i)
15981 {
15982 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015983 {
15984 if (list_append_string(rettv->vval.v_list,
15985 (char_u *)"", 0) == FAIL)
15986 break;
15987 }
15988 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015989 regmatch.startp[i],
15990 (int)(regmatch.endp[i] - regmatch.startp[i]))
15991 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015992 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015993 }
15994 }
15995 else if (type == 2)
15996 {
15997 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015998 if (l != NULL)
15999 copy_tv(&li->li_tv, rettv);
16000 else
16001 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000016002 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016003 }
16004 else if (l != NULL)
16005 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016006 else
16007 {
16008 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016009 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000016010 (varnumber_T)(regmatch.startp[0] - str);
16011 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016012 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000016013 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016014 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016015 }
16016 }
Bram Moolenaar473de612013-06-08 18:19:48 +020016017 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016018 }
16019
16020theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016021 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016022 p_cpo = save_cpo;
16023}
16024
16025/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016026 * "match()" function
16027 */
16028 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016029f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016030{
16031 find_some_match(argvars, rettv, 1);
16032}
16033
16034/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016035 * "matchadd()" function
16036 */
16037 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016038f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016039{
16040#ifdef FEAT_SEARCH_EXTRA
16041 char_u buf[NUMBUFLEN];
16042 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
16043 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
16044 int prio = 10; /* default priority */
16045 int id = -1;
16046 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020016047 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016048
16049 rettv->vval.v_number = -1;
16050
16051 if (grp == NULL || pat == NULL)
16052 return;
16053 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016054 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016055 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016056 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020016057 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016058 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020016059 if (argvars[4].v_type != VAR_UNKNOWN)
16060 {
16061 if (argvars[4].v_type != VAR_DICT)
16062 {
16063 EMSG(_(e_dictreq));
16064 return;
16065 }
16066 if (dict_find(argvars[4].vval.v_dict,
16067 (char_u *)"conceal", -1) != NULL)
16068 conceal_char = get_dict_string(argvars[4].vval.v_dict,
16069 (char_u *)"conceal", FALSE);
16070 }
16071 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016072 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016073 if (error == TRUE)
16074 return;
16075 if (id >= 1 && id <= 3)
16076 {
16077 EMSGN("E798: ID is reserved for \":match\": %ld", id);
16078 return;
16079 }
16080
Bram Moolenaar6561d522015-07-21 15:48:27 +020016081 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
16082 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020016083#endif
16084}
16085
16086/*
16087 * "matchaddpos()" function
16088 */
16089 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016090f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020016091{
16092#ifdef FEAT_SEARCH_EXTRA
16093 char_u buf[NUMBUFLEN];
16094 char_u *group;
16095 int prio = 10;
16096 int id = -1;
16097 int error = FALSE;
16098 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020016099 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020016100
16101 rettv->vval.v_number = -1;
16102
16103 group = get_tv_string_buf_chk(&argvars[0], buf);
16104 if (group == NULL)
16105 return;
16106
16107 if (argvars[1].v_type != VAR_LIST)
16108 {
16109 EMSG2(_(e_listarg), "matchaddpos()");
16110 return;
16111 }
16112 l = argvars[1].vval.v_list;
16113 if (l == NULL)
16114 return;
16115
16116 if (argvars[2].v_type != VAR_UNKNOWN)
16117 {
16118 prio = get_tv_number_chk(&argvars[2], &error);
16119 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020016120 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020016121 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020016122 if (argvars[4].v_type != VAR_UNKNOWN)
16123 {
16124 if (argvars[4].v_type != VAR_DICT)
16125 {
16126 EMSG(_(e_dictreq));
16127 return;
16128 }
16129 if (dict_find(argvars[4].vval.v_dict,
16130 (char_u *)"conceal", -1) != NULL)
16131 conceal_char = get_dict_string(argvars[4].vval.v_dict,
16132 (char_u *)"conceal", FALSE);
16133 }
16134 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020016135 }
16136 if (error == TRUE)
16137 return;
16138
16139 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
16140 if (id == 1 || id == 2)
16141 {
16142 EMSGN("E798: ID is reserved for \":match\": %ld", id);
16143 return;
16144 }
16145
Bram Moolenaar6561d522015-07-21 15:48:27 +020016146 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
16147 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016148#endif
16149}
16150
16151/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016152 * "matcharg()" function
16153 */
16154 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016155f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016156{
16157 if (rettv_list_alloc(rettv) == OK)
16158 {
16159#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016160 int id = get_tv_number(&argvars[0]);
16161 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016162
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016163 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016164 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016165 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
16166 {
16167 list_append_string(rettv->vval.v_list,
16168 syn_id2name(m->hlg_id), -1);
16169 list_append_string(rettv->vval.v_list, m->pattern, -1);
16170 }
16171 else
16172 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010016173 list_append_string(rettv->vval.v_list, NULL, -1);
16174 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016175 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016176 }
16177#endif
16178 }
16179}
16180
16181/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016182 * "matchdelete()" function
16183 */
16184 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016185f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016186{
16187#ifdef FEAT_SEARCH_EXTRA
16188 rettv->vval.v_number = match_delete(curwin,
16189 (int)get_tv_number(&argvars[0]), TRUE);
16190#endif
16191}
16192
16193/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016194 * "matchend()" function
16195 */
16196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016197f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016198{
16199 find_some_match(argvars, rettv, 0);
16200}
16201
16202/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016203 * "matchlist()" function
16204 */
16205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016206f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016207{
16208 find_some_match(argvars, rettv, 3);
16209}
16210
16211/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016212 * "matchstr()" function
16213 */
16214 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016215f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016216{
16217 find_some_match(argvars, rettv, 2);
16218}
16219
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016220static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016221
16222 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016223max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016224{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016225 long n = 0;
16226 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016227 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016228
16229 if (argvars[0].v_type == VAR_LIST)
16230 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016231 list_T *l;
16232 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016233
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016234 l = argvars[0].vval.v_list;
16235 if (l != NULL)
16236 {
16237 li = l->lv_first;
16238 if (li != NULL)
16239 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016240 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000016241 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016242 {
16243 li = li->li_next;
16244 if (li == NULL)
16245 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016246 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016247 if (domax ? i > n : i < n)
16248 n = i;
16249 }
16250 }
16251 }
16252 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016253 else if (argvars[0].v_type == VAR_DICT)
16254 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016255 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016256 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016257 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016258 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016259
16260 d = argvars[0].vval.v_dict;
16261 if (d != NULL)
16262 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016263 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016264 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016265 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016266 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016267 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016268 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016269 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016270 if (first)
16271 {
16272 n = i;
16273 first = FALSE;
16274 }
16275 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016276 n = i;
16277 }
16278 }
16279 }
16280 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016281 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016282 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016283 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016284}
16285
16286/*
16287 * "max()" function
16288 */
16289 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016290f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016291{
16292 max_min(argvars, rettv, TRUE);
16293}
16294
16295/*
16296 * "min()" function
16297 */
16298 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016299f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016300{
16301 max_min(argvars, rettv, FALSE);
16302}
16303
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016304static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016305
16306/*
16307 * Create the directory in which "dir" is located, and higher levels when
16308 * needed.
16309 */
16310 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016311mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016312{
16313 char_u *p;
16314 char_u *updir;
16315 int r = FAIL;
16316
16317 /* Get end of directory name in "dir".
16318 * We're done when it's "/" or "c:/". */
16319 p = gettail_sep(dir);
16320 if (p <= get_past_head(dir))
16321 return OK;
16322
16323 /* If the directory exists we're done. Otherwise: create it.*/
16324 updir = vim_strnsave(dir, (int)(p - dir));
16325 if (updir == NULL)
16326 return FAIL;
16327 if (mch_isdir(updir))
16328 r = OK;
16329 else if (mkdir_recurse(updir, prot) == OK)
16330 r = vim_mkdir_emsg(updir, prot);
16331 vim_free(updir);
16332 return r;
16333}
16334
16335#ifdef vim_mkdir
16336/*
16337 * "mkdir()" function
16338 */
16339 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016340f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016341{
16342 char_u *dir;
16343 char_u buf[NUMBUFLEN];
16344 int prot = 0755;
16345
16346 rettv->vval.v_number = FAIL;
16347 if (check_restricted() || check_secure())
16348 return;
16349
16350 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016351 if (*dir == NUL)
16352 rettv->vval.v_number = FAIL;
16353 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016354 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016355 if (*gettail(dir) == NUL)
16356 /* remove trailing slashes */
16357 *gettail_sep(dir) = NUL;
16358
16359 if (argvars[1].v_type != VAR_UNKNOWN)
16360 {
16361 if (argvars[2].v_type != VAR_UNKNOWN)
16362 prot = get_tv_number_chk(&argvars[2], NULL);
16363 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16364 mkdir_recurse(dir, prot);
16365 }
16366 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016367 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016368}
16369#endif
16370
Bram Moolenaar0d660222005-01-07 21:51:51 +000016371/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016372 * "mode()" function
16373 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016374 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016375f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016376{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016377 char_u buf[3];
16378
16379 buf[1] = NUL;
16380 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016381
Bram Moolenaar071d4272004-06-13 20:20:40 +000016382 if (VIsual_active)
16383 {
16384 if (VIsual_select)
16385 buf[0] = VIsual_mode + 's' - 'v';
16386 else
16387 buf[0] = VIsual_mode;
16388 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016389 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016390 || State == CONFIRM)
16391 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016392 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016393 if (State == ASKMORE)
16394 buf[1] = 'm';
16395 else if (State == CONFIRM)
16396 buf[1] = '?';
16397 }
16398 else if (State == EXTERNCMD)
16399 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016400 else if (State & INSERT)
16401 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016402#ifdef FEAT_VREPLACE
16403 if (State & VREPLACE_FLAG)
16404 {
16405 buf[0] = 'R';
16406 buf[1] = 'v';
16407 }
16408 else
16409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016410 if (State & REPLACE_FLAG)
16411 buf[0] = 'R';
16412 else
16413 buf[0] = 'i';
16414 }
16415 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016416 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016417 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016418 if (exmode_active)
16419 buf[1] = 'v';
16420 }
16421 else if (exmode_active)
16422 {
16423 buf[0] = 'c';
16424 buf[1] = 'e';
16425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016426 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016427 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016428 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016429 if (finish_op)
16430 buf[1] = 'o';
16431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016432
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016433 /* Clear out the minor mode when the argument is not a non-zero number or
16434 * non-empty string. */
16435 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016436 buf[1] = NUL;
16437
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016438 rettv->vval.v_string = vim_strsave(buf);
16439 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016440}
16441
Bram Moolenaar429fa852013-04-15 12:27:36 +020016442#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016443/*
16444 * "mzeval()" function
16445 */
16446 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016447f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016448{
16449 char_u *str;
16450 char_u buf[NUMBUFLEN];
16451
16452 str = get_tv_string_buf(&argvars[0], buf);
16453 do_mzeval(str, rettv);
16454}
Bram Moolenaar75676462013-01-30 14:55:42 +010016455
16456 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016457mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016458{
16459 typval_T argvars[3];
16460
16461 argvars[0].v_type = VAR_STRING;
16462 argvars[0].vval.v_string = name;
16463 copy_tv(args, &argvars[1]);
16464 argvars[2].v_type = VAR_UNKNOWN;
16465 f_call(argvars, rettv);
16466 clear_tv(&argvars[1]);
16467}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016468#endif
16469
Bram Moolenaar071d4272004-06-13 20:20:40 +000016470/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016471 * "nextnonblank()" function
16472 */
16473 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016474f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016475{
16476 linenr_T lnum;
16477
16478 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16479 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016480 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016481 {
16482 lnum = 0;
16483 break;
16484 }
16485 if (*skipwhite(ml_get(lnum)) != NUL)
16486 break;
16487 }
16488 rettv->vval.v_number = lnum;
16489}
16490
16491/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016492 * "nr2char()" function
16493 */
16494 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016495f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016496{
16497 char_u buf[NUMBUFLEN];
16498
16499#ifdef FEAT_MBYTE
16500 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016501 {
16502 int utf8 = 0;
16503
16504 if (argvars[1].v_type != VAR_UNKNOWN)
16505 utf8 = get_tv_number_chk(&argvars[1], NULL);
16506 if (utf8)
16507 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16508 else
16509 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016511 else
16512#endif
16513 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016514 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016515 buf[1] = NUL;
16516 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016517 rettv->v_type = VAR_STRING;
16518 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016519}
16520
16521/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016522 * "or(expr, expr)" function
16523 */
16524 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016525f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016526{
16527 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16528 | get_tv_number_chk(&argvars[1], NULL);
16529}
16530
16531/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016532 * "pathshorten()" function
16533 */
16534 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016535f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016536{
16537 char_u *p;
16538
16539 rettv->v_type = VAR_STRING;
16540 p = get_tv_string_chk(&argvars[0]);
16541 if (p == NULL)
16542 rettv->vval.v_string = NULL;
16543 else
16544 {
16545 p = vim_strsave(p);
16546 rettv->vval.v_string = p;
16547 if (p != NULL)
16548 shorten_dir(p);
16549 }
16550}
16551
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016552#ifdef FEAT_PERL
16553/*
16554 * "perleval()" function
16555 */
16556 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016557f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016558{
16559 char_u *str;
16560 char_u buf[NUMBUFLEN];
16561
16562 str = get_tv_string_buf(&argvars[0], buf);
16563 do_perleval(str, rettv);
16564}
16565#endif
16566
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016567#ifdef FEAT_FLOAT
16568/*
16569 * "pow()" function
16570 */
16571 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016572f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016573{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016574 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016575
16576 rettv->v_type = VAR_FLOAT;
16577 if (get_float_arg(argvars, &fx) == OK
16578 && get_float_arg(&argvars[1], &fy) == OK)
16579 rettv->vval.v_float = pow(fx, fy);
16580 else
16581 rettv->vval.v_float = 0.0;
16582}
16583#endif
16584
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016585/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016586 * "prevnonblank()" function
16587 */
16588 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016589f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016590{
16591 linenr_T lnum;
16592
16593 lnum = get_tv_lnum(argvars);
16594 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16595 lnum = 0;
16596 else
16597 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16598 --lnum;
16599 rettv->vval.v_number = lnum;
16600}
16601
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016602/* This dummy va_list is here because:
16603 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16604 * - locally in the function results in a "used before set" warning
16605 * - using va_start() to initialize it gives "function with fixed args" error */
16606static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016607
Bram Moolenaar8c711452005-01-14 21:53:12 +000016608/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016609 * "printf()" function
16610 */
16611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016612f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016613{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016614 char_u buf[NUMBUFLEN];
16615 int len;
16616 char_u *s;
16617 int saved_did_emsg = did_emsg;
16618 char *fmt;
16619
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016620 rettv->v_type = VAR_STRING;
16621 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016622
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016623 /* Get the required length, allocate the buffer and do it for real. */
16624 did_emsg = FALSE;
16625 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16626 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16627 if (!did_emsg)
16628 {
16629 s = alloc(len + 1);
16630 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016631 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016632 rettv->vval.v_string = s;
16633 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016634 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016635 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016636 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016637}
16638
16639/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016640 * "pumvisible()" function
16641 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016643f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016644{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016645#ifdef FEAT_INS_EXPAND
16646 if (pum_visible())
16647 rettv->vval.v_number = 1;
16648#endif
16649}
16650
Bram Moolenaardb913952012-06-29 12:54:53 +020016651#ifdef FEAT_PYTHON3
16652/*
16653 * "py3eval()" function
16654 */
16655 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016656f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016657{
16658 char_u *str;
16659 char_u buf[NUMBUFLEN];
16660
16661 str = get_tv_string_buf(&argvars[0], buf);
16662 do_py3eval(str, rettv);
16663}
16664#endif
16665
16666#ifdef FEAT_PYTHON
16667/*
16668 * "pyeval()" function
16669 */
16670 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016671f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016672{
16673 char_u *str;
16674 char_u buf[NUMBUFLEN];
16675
16676 str = get_tv_string_buf(&argvars[0], buf);
16677 do_pyeval(str, rettv);
16678}
16679#endif
16680
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016681/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016682 * "range()" function
16683 */
16684 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016685f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016686{
16687 long start;
16688 long end;
16689 long stride = 1;
16690 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016691 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016692
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016693 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016694 if (argvars[1].v_type == VAR_UNKNOWN)
16695 {
16696 end = start - 1;
16697 start = 0;
16698 }
16699 else
16700 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016701 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016702 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016703 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016704 }
16705
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016706 if (error)
16707 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016708 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016709 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016710 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016711 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016712 else
16713 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016714 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016715 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016716 if (list_append_number(rettv->vval.v_list,
16717 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016718 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016719 }
16720}
16721
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016722/*
16723 * "readfile()" function
16724 */
16725 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016726f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016727{
16728 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016729 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016730 char_u *fname;
16731 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016732 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16733 int io_size = sizeof(buf);
16734 int readlen; /* size of last fread() */
16735 char_u *prev = NULL; /* previously read bytes, if any */
16736 long prevlen = 0; /* length of data in prev */
16737 long prevsize = 0; /* size of prev buffer */
16738 long maxline = MAXLNUM;
16739 long cnt = 0;
16740 char_u *p; /* position in buf */
16741 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016742
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016743 if (argvars[1].v_type != VAR_UNKNOWN)
16744 {
16745 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16746 binary = TRUE;
16747 if (argvars[2].v_type != VAR_UNKNOWN)
16748 maxline = get_tv_number(&argvars[2]);
16749 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016750
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016751 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016752 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016753
16754 /* Always open the file in binary mode, library functions have a mind of
16755 * their own about CR-LF conversion. */
16756 fname = get_tv_string(&argvars[0]);
16757 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16758 {
16759 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16760 return;
16761 }
16762
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016763 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016764 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016765 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016766
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016767 /* This for loop processes what was read, but is also entered at end
16768 * of file so that either:
16769 * - an incomplete line gets written
16770 * - a "binary" file gets an empty line at the end if it ends in a
16771 * newline. */
16772 for (p = buf, start = buf;
16773 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16774 ++p)
16775 {
16776 if (*p == '\n' || readlen <= 0)
16777 {
16778 listitem_T *li;
16779 char_u *s = NULL;
16780 long_u len = p - start;
16781
16782 /* Finished a line. Remove CRs before NL. */
16783 if (readlen > 0 && !binary)
16784 {
16785 while (len > 0 && start[len - 1] == '\r')
16786 --len;
16787 /* removal may cross back to the "prev" string */
16788 if (len == 0)
16789 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16790 --prevlen;
16791 }
16792 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016793 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016794 else
16795 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016796 /* Change "prev" buffer to be the right size. This way
16797 * the bytes are only copied once, and very long lines are
16798 * allocated only once. */
16799 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016800 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016801 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016802 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016803 prev = NULL; /* the list will own the string */
16804 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016805 }
16806 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016807 if (s == NULL)
16808 {
16809 do_outofmem_msg((long_u) prevlen + len + 1);
16810 failed = TRUE;
16811 break;
16812 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016813
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016814 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016815 {
16816 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016817 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016818 break;
16819 }
16820 li->li_tv.v_type = VAR_STRING;
16821 li->li_tv.v_lock = 0;
16822 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016823 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016824
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016825 start = p + 1; /* step over newline */
16826 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016827 break;
16828 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016829 else if (*p == NUL)
16830 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016831#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016832 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16833 * when finding the BF and check the previous two bytes. */
16834 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016835 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016836 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16837 * + 1, these may be in the "prev" string. */
16838 char_u back1 = p >= buf + 1 ? p[-1]
16839 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16840 char_u back2 = p >= buf + 2 ? p[-2]
16841 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16842 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016843
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016844 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016845 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016846 char_u *dest = p - 2;
16847
16848 /* Usually a BOM is at the beginning of a file, and so at
16849 * the beginning of a line; then we can just step over it.
16850 */
16851 if (start == dest)
16852 start = p + 1;
16853 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016854 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016855 /* have to shuffle buf to close gap */
16856 int adjust_prevlen = 0;
16857
16858 if (dest < buf)
16859 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016860 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016861 dest = buf;
16862 }
16863 if (readlen > p - buf + 1)
16864 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16865 readlen -= 3 - adjust_prevlen;
16866 prevlen -= adjust_prevlen;
16867 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016868 }
16869 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016870 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016871#endif
16872 } /* for */
16873
16874 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16875 break;
16876 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016877 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016878 /* There's part of a line in buf, store it in "prev". */
16879 if (p - start + prevlen >= prevsize)
16880 {
16881 /* need bigger "prev" buffer */
16882 char_u *newprev;
16883
16884 /* A common use case is ordinary text files and "prev" gets a
16885 * fragment of a line, so the first allocation is made
16886 * small, to avoid repeatedly 'allocing' large and
16887 * 'reallocing' small. */
16888 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016889 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016890 else
16891 {
16892 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016893 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016894 prevsize = grow50pc > growmin ? grow50pc : growmin;
16895 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016896 newprev = prev == NULL ? alloc(prevsize)
16897 : vim_realloc(prev, prevsize);
16898 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016899 {
16900 do_outofmem_msg((long_u)prevsize);
16901 failed = TRUE;
16902 break;
16903 }
16904 prev = newprev;
16905 }
16906 /* Add the line part to end of "prev". */
16907 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016908 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016909 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016910 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016911
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016912 /*
16913 * For a negative line count use only the lines at the end of the file,
16914 * free the rest.
16915 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016916 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016917 while (cnt > -maxline)
16918 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016919 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016920 --cnt;
16921 }
16922
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016923 if (failed)
16924 {
16925 list_free(rettv->vval.v_list, TRUE);
16926 /* readfile doc says an empty list is returned on error */
16927 rettv->vval.v_list = list_alloc();
16928 }
16929
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016930 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016931 fclose(fd);
16932}
16933
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016934#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016935static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016936
16937/*
16938 * Convert a List to proftime_T.
16939 * Return FAIL when there is something wrong.
16940 */
16941 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016942list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016943{
16944 long n1, n2;
16945 int error = FALSE;
16946
16947 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16948 || arg->vval.v_list->lv_len != 2)
16949 return FAIL;
16950 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16951 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16952# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016953 tm->HighPart = n1;
16954 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016955# else
16956 tm->tv_sec = n1;
16957 tm->tv_usec = n2;
16958# endif
16959 return error ? FAIL : OK;
16960}
16961#endif /* FEAT_RELTIME */
16962
16963/*
16964 * "reltime()" function
16965 */
16966 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016967f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016968{
16969#ifdef FEAT_RELTIME
16970 proftime_T res;
16971 proftime_T start;
16972
16973 if (argvars[0].v_type == VAR_UNKNOWN)
16974 {
16975 /* No arguments: get current time. */
16976 profile_start(&res);
16977 }
16978 else if (argvars[1].v_type == VAR_UNKNOWN)
16979 {
16980 if (list2proftime(&argvars[0], &res) == FAIL)
16981 return;
16982 profile_end(&res);
16983 }
16984 else
16985 {
16986 /* Two arguments: compute the difference. */
16987 if (list2proftime(&argvars[0], &start) == FAIL
16988 || list2proftime(&argvars[1], &res) == FAIL)
16989 return;
16990 profile_sub(&res, &start);
16991 }
16992
16993 if (rettv_list_alloc(rettv) == OK)
16994 {
16995 long n1, n2;
16996
16997# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016998 n1 = res.HighPart;
16999 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017000# else
17001 n1 = res.tv_sec;
17002 n2 = res.tv_usec;
17003# endif
17004 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
17005 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
17006 }
17007#endif
17008}
17009
Bram Moolenaar79c2c882016-02-07 21:19:28 +010017010#ifdef FEAT_FLOAT
17011/*
17012 * "reltimefloat()" function
17013 */
17014 static void
17015f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
17016{
17017# ifdef FEAT_RELTIME
17018 proftime_T tm;
17019# endif
17020
17021 rettv->v_type = VAR_FLOAT;
17022 rettv->vval.v_float = 0;
17023# ifdef FEAT_RELTIME
17024 if (list2proftime(&argvars[0], &tm) == OK)
17025 rettv->vval.v_float = profile_float(&tm);
17026# endif
17027}
17028#endif
17029
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017030/*
17031 * "reltimestr()" function
17032 */
17033 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017034f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017035{
17036#ifdef FEAT_RELTIME
17037 proftime_T tm;
17038#endif
17039
17040 rettv->v_type = VAR_STRING;
17041 rettv->vval.v_string = NULL;
17042#ifdef FEAT_RELTIME
17043 if (list2proftime(&argvars[0], &tm) == OK)
17044 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
17045#endif
17046}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017047
Bram Moolenaar0d660222005-01-07 21:51:51 +000017048#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017049static void make_connection(void);
17050static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017051
17052 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017053make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017054{
17055 if (X_DISPLAY == NULL
17056# ifdef FEAT_GUI
17057 && !gui.in_use
17058# endif
17059 )
17060 {
17061 x_force_connect = TRUE;
17062 setup_term_clip();
17063 x_force_connect = FALSE;
17064 }
17065}
17066
17067 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017068check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017069{
17070 make_connection();
17071 if (X_DISPLAY == NULL)
17072 {
17073 EMSG(_("E240: No connection to Vim server"));
17074 return FAIL;
17075 }
17076 return OK;
17077}
17078#endif
17079
17080#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017081static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017082
17083 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017084remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017085{
17086 char_u *server_name;
17087 char_u *keys;
17088 char_u *r = NULL;
17089 char_u buf[NUMBUFLEN];
17090# ifdef WIN32
17091 HWND w;
17092# else
17093 Window w;
17094# endif
17095
17096 if (check_restricted() || check_secure())
17097 return;
17098
17099# ifdef FEAT_X11
17100 if (check_connection() == FAIL)
17101 return;
17102# endif
17103
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017104 server_name = get_tv_string_chk(&argvars[0]);
17105 if (server_name == NULL)
17106 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017107 keys = get_tv_string_buf(&argvars[1], buf);
17108# ifdef WIN32
17109 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
17110# else
17111 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
17112 < 0)
17113# endif
17114 {
17115 if (r != NULL)
17116 EMSG(r); /* sending worked but evaluation failed */
17117 else
17118 EMSG2(_("E241: Unable to send to %s"), server_name);
17119 return;
17120 }
17121
17122 rettv->vval.v_string = r;
17123
17124 if (argvars[2].v_type != VAR_UNKNOWN)
17125 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017126 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000017127 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017128 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017129
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017130 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000017131 v.di_tv.v_type = VAR_STRING;
17132 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017133 idvar = get_tv_string_chk(&argvars[2]);
17134 if (idvar != NULL)
17135 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017136 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017137 }
17138}
17139#endif
17140
17141/*
17142 * "remote_expr()" function
17143 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017144 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017145f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017146{
17147 rettv->v_type = VAR_STRING;
17148 rettv->vval.v_string = NULL;
17149#ifdef FEAT_CLIENTSERVER
17150 remote_common(argvars, rettv, TRUE);
17151#endif
17152}
17153
17154/*
17155 * "remote_foreground()" function
17156 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017157 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017158f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017159{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017160#ifdef FEAT_CLIENTSERVER
17161# ifdef WIN32
17162 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017163 {
17164 char_u *server_name = get_tv_string_chk(&argvars[0]);
17165
17166 if (server_name != NULL)
17167 serverForeground(server_name);
17168 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017169# else
17170 /* Send a foreground() expression to the server. */
17171 argvars[1].v_type = VAR_STRING;
17172 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
17173 argvars[2].v_type = VAR_UNKNOWN;
17174 remote_common(argvars, rettv, TRUE);
17175 vim_free(argvars[1].vval.v_string);
17176# endif
17177#endif
17178}
17179
Bram Moolenaar0d660222005-01-07 21:51:51 +000017180 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017181f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017182{
17183#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000017184 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017185 char_u *s = NULL;
17186# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017187 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017188# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017189 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017190
17191 if (check_restricted() || check_secure())
17192 {
17193 rettv->vval.v_number = -1;
17194 return;
17195 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017196 serverid = get_tv_string_chk(&argvars[0]);
17197 if (serverid == NULL)
17198 {
17199 rettv->vval.v_number = -1;
17200 return; /* type error; errmsg already given */
17201 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017202# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017203 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017204 if (n == 0)
17205 rettv->vval.v_number = -1;
17206 else
17207 {
17208 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
17209 rettv->vval.v_number = (s != NULL);
17210 }
17211# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017212 if (check_connection() == FAIL)
17213 return;
17214
17215 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017216 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017217# endif
17218
17219 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
17220 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017221 char_u *retvar;
17222
Bram Moolenaar33570922005-01-25 22:26:29 +000017223 v.di_tv.v_type = VAR_STRING;
17224 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017225 retvar = get_tv_string_chk(&argvars[1]);
17226 if (retvar != NULL)
17227 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017228 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017229 }
17230#else
17231 rettv->vval.v_number = -1;
17232#endif
17233}
17234
Bram Moolenaar0d660222005-01-07 21:51:51 +000017235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017236f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017237{
17238 char_u *r = NULL;
17239
17240#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017241 char_u *serverid = get_tv_string_chk(&argvars[0]);
17242
17243 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000017244 {
17245# ifdef WIN32
17246 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017247 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017248
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017249 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017250 if (n != 0)
17251 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
17252 if (r == NULL)
17253# else
17254 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017255 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017256# endif
17257 EMSG(_("E277: Unable to read a server reply"));
17258 }
17259#endif
17260 rettv->v_type = VAR_STRING;
17261 rettv->vval.v_string = r;
17262}
17263
17264/*
17265 * "remote_send()" function
17266 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017267 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017268f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017269{
17270 rettv->v_type = VAR_STRING;
17271 rettv->vval.v_string = NULL;
17272#ifdef FEAT_CLIENTSERVER
17273 remote_common(argvars, rettv, FALSE);
17274#endif
17275}
17276
17277/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017278 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017279 */
17280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017281f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017282{
Bram Moolenaar33570922005-01-25 22:26:29 +000017283 list_T *l;
17284 listitem_T *item, *item2;
17285 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017286 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017287 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017288 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017289 dict_T *d;
17290 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017291 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017292
Bram Moolenaar8c711452005-01-14 21:53:12 +000017293 if (argvars[0].v_type == VAR_DICT)
17294 {
17295 if (argvars[2].v_type != VAR_UNKNOWN)
17296 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017297 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017298 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017299 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017300 key = get_tv_string_chk(&argvars[1]);
17301 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017302 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017303 di = dict_find(d, key, -1);
17304 if (di == NULL)
17305 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017306 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17307 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017308 {
17309 *rettv = di->di_tv;
17310 init_tv(&di->di_tv);
17311 dictitem_remove(d, di);
17312 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017313 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017314 }
17315 }
17316 else if (argvars[0].v_type != VAR_LIST)
17317 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017318 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017319 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017320 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017321 int error = FALSE;
17322
17323 idx = get_tv_number_chk(&argvars[1], &error);
17324 if (error)
17325 ; /* type error: do nothing, errmsg already given */
17326 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017327 EMSGN(_(e_listidx), idx);
17328 else
17329 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017330 if (argvars[2].v_type == VAR_UNKNOWN)
17331 {
17332 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017333 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017334 *rettv = item->li_tv;
17335 vim_free(item);
17336 }
17337 else
17338 {
17339 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017340 end = get_tv_number_chk(&argvars[2], &error);
17341 if (error)
17342 ; /* type error: do nothing */
17343 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017344 EMSGN(_(e_listidx), end);
17345 else
17346 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017347 int cnt = 0;
17348
17349 for (li = item; li != NULL; li = li->li_next)
17350 {
17351 ++cnt;
17352 if (li == item2)
17353 break;
17354 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017355 if (li == NULL) /* didn't find "item2" after "item" */
17356 EMSG(_(e_invrange));
17357 else
17358 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017359 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017360 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017361 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017362 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017363 l->lv_first = item;
17364 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017365 item->li_prev = NULL;
17366 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017367 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017368 }
17369 }
17370 }
17371 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017372 }
17373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017374}
17375
17376/*
17377 * "rename({from}, {to})" function
17378 */
17379 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017380f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017381{
17382 char_u buf[NUMBUFLEN];
17383
17384 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017385 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017386 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017387 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17388 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017389}
17390
17391/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017392 * "repeat()" function
17393 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017395f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017396{
17397 char_u *p;
17398 int n;
17399 int slen;
17400 int len;
17401 char_u *r;
17402 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017403
17404 n = get_tv_number(&argvars[1]);
17405 if (argvars[0].v_type == VAR_LIST)
17406 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017407 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017408 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017409 if (list_extend(rettv->vval.v_list,
17410 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017411 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017412 }
17413 else
17414 {
17415 p = get_tv_string(&argvars[0]);
17416 rettv->v_type = VAR_STRING;
17417 rettv->vval.v_string = NULL;
17418
17419 slen = (int)STRLEN(p);
17420 len = slen * n;
17421 if (len <= 0)
17422 return;
17423
17424 r = alloc(len + 1);
17425 if (r != NULL)
17426 {
17427 for (i = 0; i < n; i++)
17428 mch_memmove(r + i * slen, p, (size_t)slen);
17429 r[len] = NUL;
17430 }
17431
17432 rettv->vval.v_string = r;
17433 }
17434}
17435
17436/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017437 * "resolve()" function
17438 */
17439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017440f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017441{
17442 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017443#ifdef HAVE_READLINK
17444 char_u *buf = NULL;
17445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017446
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017447 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017448#ifdef FEAT_SHORTCUT
17449 {
17450 char_u *v = NULL;
17451
17452 v = mch_resolve_shortcut(p);
17453 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017454 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017455 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017456 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017457 }
17458#else
17459# ifdef HAVE_READLINK
17460 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017461 char_u *cpy;
17462 int len;
17463 char_u *remain = NULL;
17464 char_u *q;
17465 int is_relative_to_current = FALSE;
17466 int has_trailing_pathsep = FALSE;
17467 int limit = 100;
17468
17469 p = vim_strsave(p);
17470
17471 if (p[0] == '.' && (vim_ispathsep(p[1])
17472 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17473 is_relative_to_current = TRUE;
17474
17475 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017476 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017477 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017478 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017479 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017481
17482 q = getnextcomp(p);
17483 if (*q != NUL)
17484 {
17485 /* Separate the first path component in "p", and keep the
17486 * remainder (beginning with the path separator). */
17487 remain = vim_strsave(q - 1);
17488 q[-1] = NUL;
17489 }
17490
Bram Moolenaard9462e32011-04-11 21:35:11 +020017491 buf = alloc(MAXPATHL + 1);
17492 if (buf == NULL)
17493 goto fail;
17494
Bram Moolenaar071d4272004-06-13 20:20:40 +000017495 for (;;)
17496 {
17497 for (;;)
17498 {
17499 len = readlink((char *)p, (char *)buf, MAXPATHL);
17500 if (len <= 0)
17501 break;
17502 buf[len] = NUL;
17503
17504 if (limit-- == 0)
17505 {
17506 vim_free(p);
17507 vim_free(remain);
17508 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017509 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017510 goto fail;
17511 }
17512
17513 /* Ensure that the result will have a trailing path separator
17514 * if the argument has one. */
17515 if (remain == NULL && has_trailing_pathsep)
17516 add_pathsep(buf);
17517
17518 /* Separate the first path component in the link value and
17519 * concatenate the remainders. */
17520 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17521 if (*q != NUL)
17522 {
17523 if (remain == NULL)
17524 remain = vim_strsave(q - 1);
17525 else
17526 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017527 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017528 if (cpy != NULL)
17529 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017530 vim_free(remain);
17531 remain = cpy;
17532 }
17533 }
17534 q[-1] = NUL;
17535 }
17536
17537 q = gettail(p);
17538 if (q > p && *q == NUL)
17539 {
17540 /* Ignore trailing path separator. */
17541 q[-1] = NUL;
17542 q = gettail(p);
17543 }
17544 if (q > p && !mch_isFullName(buf))
17545 {
17546 /* symlink is relative to directory of argument */
17547 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17548 if (cpy != NULL)
17549 {
17550 STRCPY(cpy, p);
17551 STRCPY(gettail(cpy), buf);
17552 vim_free(p);
17553 p = cpy;
17554 }
17555 }
17556 else
17557 {
17558 vim_free(p);
17559 p = vim_strsave(buf);
17560 }
17561 }
17562
17563 if (remain == NULL)
17564 break;
17565
17566 /* Append the first path component of "remain" to "p". */
17567 q = getnextcomp(remain + 1);
17568 len = q - remain - (*q != NUL);
17569 cpy = vim_strnsave(p, STRLEN(p) + len);
17570 if (cpy != NULL)
17571 {
17572 STRNCAT(cpy, remain, len);
17573 vim_free(p);
17574 p = cpy;
17575 }
17576 /* Shorten "remain". */
17577 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017578 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017579 else
17580 {
17581 vim_free(remain);
17582 remain = NULL;
17583 }
17584 }
17585
17586 /* If the result is a relative path name, make it explicitly relative to
17587 * the current directory if and only if the argument had this form. */
17588 if (!vim_ispathsep(*p))
17589 {
17590 if (is_relative_to_current
17591 && *p != NUL
17592 && !(p[0] == '.'
17593 && (p[1] == NUL
17594 || vim_ispathsep(p[1])
17595 || (p[1] == '.'
17596 && (p[2] == NUL
17597 || vim_ispathsep(p[2]))))))
17598 {
17599 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017600 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017601 if (cpy != NULL)
17602 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017603 vim_free(p);
17604 p = cpy;
17605 }
17606 }
17607 else if (!is_relative_to_current)
17608 {
17609 /* Strip leading "./". */
17610 q = p;
17611 while (q[0] == '.' && vim_ispathsep(q[1]))
17612 q += 2;
17613 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017614 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017615 }
17616 }
17617
17618 /* Ensure that the result will have no trailing path separator
17619 * if the argument had none. But keep "/" or "//". */
17620 if (!has_trailing_pathsep)
17621 {
17622 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017623 if (after_pathsep(p, q))
17624 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017625 }
17626
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017627 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017628 }
17629# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017630 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017631# endif
17632#endif
17633
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017634 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017635
17636#ifdef HAVE_READLINK
17637fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017638 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017639#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017640 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017641}
17642
17643/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017644 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017645 */
17646 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017647f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017648{
Bram Moolenaar33570922005-01-25 22:26:29 +000017649 list_T *l;
17650 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017651
Bram Moolenaar0d660222005-01-07 21:51:51 +000017652 if (argvars[0].v_type != VAR_LIST)
17653 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017654 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017655 && !tv_check_lock(l->lv_lock,
17656 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017657 {
17658 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017659 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017660 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017661 while (li != NULL)
17662 {
17663 ni = li->li_prev;
17664 list_append(l, li);
17665 li = ni;
17666 }
17667 rettv->vval.v_list = l;
17668 rettv->v_type = VAR_LIST;
17669 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017670 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017672}
17673
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017674#define SP_NOMOVE 0x01 /* don't move cursor */
17675#define SP_REPEAT 0x02 /* repeat to find outer pair */
17676#define SP_RETCOUNT 0x04 /* return matchcount */
17677#define SP_SETPCMARK 0x08 /* set previous context mark */
17678#define SP_START 0x10 /* accept match at start position */
17679#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17680#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017681#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017682
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017683static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017684
17685/*
17686 * Get flags for a search function.
17687 * Possibly sets "p_ws".
17688 * Returns BACKWARD, FORWARD or zero (for an error).
17689 */
17690 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017691get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017692{
17693 int dir = FORWARD;
17694 char_u *flags;
17695 char_u nbuf[NUMBUFLEN];
17696 int mask;
17697
17698 if (varp->v_type != VAR_UNKNOWN)
17699 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017700 flags = get_tv_string_buf_chk(varp, nbuf);
17701 if (flags == NULL)
17702 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017703 while (*flags != NUL)
17704 {
17705 switch (*flags)
17706 {
17707 case 'b': dir = BACKWARD; break;
17708 case 'w': p_ws = TRUE; break;
17709 case 'W': p_ws = FALSE; break;
17710 default: mask = 0;
17711 if (flagsp != NULL)
17712 switch (*flags)
17713 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017714 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017715 case 'e': mask = SP_END; break;
17716 case 'm': mask = SP_RETCOUNT; break;
17717 case 'n': mask = SP_NOMOVE; break;
17718 case 'p': mask = SP_SUBPAT; break;
17719 case 'r': mask = SP_REPEAT; break;
17720 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017721 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017722 }
17723 if (mask == 0)
17724 {
17725 EMSG2(_(e_invarg2), flags);
17726 dir = 0;
17727 }
17728 else
17729 *flagsp |= mask;
17730 }
17731 if (dir == 0)
17732 break;
17733 ++flags;
17734 }
17735 }
17736 return dir;
17737}
17738
Bram Moolenaar071d4272004-06-13 20:20:40 +000017739/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017740 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017741 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017742 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017743search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017744{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017745 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017746 char_u *pat;
17747 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017748 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017749 int save_p_ws = p_ws;
17750 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017751 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017752 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017753 proftime_T tm;
17754#ifdef FEAT_RELTIME
17755 long time_limit = 0;
17756#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017757 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017758 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017759
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017760 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017761 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017762 if (dir == 0)
17763 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017764 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017765 if (flags & SP_START)
17766 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017767 if (flags & SP_END)
17768 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017769 if (flags & SP_COLUMN)
17770 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017771
Bram Moolenaar76929292008-01-06 19:07:36 +000017772 /* Optional arguments: line number to stop searching and timeout. */
17773 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017774 {
17775 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17776 if (lnum_stop < 0)
17777 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017778#ifdef FEAT_RELTIME
17779 if (argvars[3].v_type != VAR_UNKNOWN)
17780 {
17781 time_limit = get_tv_number_chk(&argvars[3], NULL);
17782 if (time_limit < 0)
17783 goto theend;
17784 }
17785#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017786 }
17787
Bram Moolenaar76929292008-01-06 19:07:36 +000017788#ifdef FEAT_RELTIME
17789 /* Set the time limit, if there is one. */
17790 profile_setlimit(time_limit, &tm);
17791#endif
17792
Bram Moolenaar231334e2005-07-25 20:46:57 +000017793 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017794 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017795 * Check to make sure only those flags are set.
17796 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17797 * flags cannot be set. Check for that condition also.
17798 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017799 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017800 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017801 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017802 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017803 goto theend;
17804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017805
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017806 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017807 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017808 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017809 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017810 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017811 if (flags & SP_SUBPAT)
17812 retval = subpatnum;
17813 else
17814 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017815 if (flags & SP_SETPCMARK)
17816 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017817 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017818 if (match_pos != NULL)
17819 {
17820 /* Store the match cursor position */
17821 match_pos->lnum = pos.lnum;
17822 match_pos->col = pos.col + 1;
17823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017824 /* "/$" will put the cursor after the end of the line, may need to
17825 * correct that here */
17826 check_cursor();
17827 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017828
17829 /* If 'n' flag is used: restore cursor position. */
17830 if (flags & SP_NOMOVE)
17831 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017832 else
17833 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017834theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017835 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017836
17837 return retval;
17838}
17839
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017840#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017841
17842/*
17843 * round() is not in C90, use ceil() or floor() instead.
17844 */
17845 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017846vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017847{
17848 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17849}
17850
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017851/*
17852 * "round({float})" function
17853 */
17854 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017855f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017856{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017857 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017858
17859 rettv->v_type = VAR_FLOAT;
17860 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017861 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017862 else
17863 rettv->vval.v_float = 0.0;
17864}
17865#endif
17866
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017867/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017868 * "screenattr()" function
17869 */
17870 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017871f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017872{
17873 int row;
17874 int col;
17875 int c;
17876
17877 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17878 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17879 if (row < 0 || row >= screen_Rows
17880 || col < 0 || col >= screen_Columns)
17881 c = -1;
17882 else
17883 c = ScreenAttrs[LineOffset[row] + col];
17884 rettv->vval.v_number = c;
17885}
17886
17887/*
17888 * "screenchar()" function
17889 */
17890 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017891f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017892{
17893 int row;
17894 int col;
17895 int off;
17896 int c;
17897
17898 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17899 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17900 if (row < 0 || row >= screen_Rows
17901 || col < 0 || col >= screen_Columns)
17902 c = -1;
17903 else
17904 {
17905 off = LineOffset[row] + col;
17906#ifdef FEAT_MBYTE
17907 if (enc_utf8 && ScreenLinesUC[off] != 0)
17908 c = ScreenLinesUC[off];
17909 else
17910#endif
17911 c = ScreenLines[off];
17912 }
17913 rettv->vval.v_number = c;
17914}
17915
17916/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017917 * "screencol()" function
17918 *
17919 * First column is 1 to be consistent with virtcol().
17920 */
17921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017922f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017923{
17924 rettv->vval.v_number = screen_screencol() + 1;
17925}
17926
17927/*
17928 * "screenrow()" function
17929 */
17930 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017931f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017932{
17933 rettv->vval.v_number = screen_screenrow() + 1;
17934}
17935
17936/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017937 * "search()" function
17938 */
17939 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017940f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017941{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017942 int flags = 0;
17943
17944 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017945}
17946
Bram Moolenaar071d4272004-06-13 20:20:40 +000017947/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017948 * "searchdecl()" function
17949 */
17950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017951f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017952{
17953 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017954 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017955 int error = FALSE;
17956 char_u *name;
17957
17958 rettv->vval.v_number = 1; /* default: FAIL */
17959
17960 name = get_tv_string_chk(&argvars[0]);
17961 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017962 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017963 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017964 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17965 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17966 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017967 if (!error && name != NULL)
17968 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017969 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017970}
17971
17972/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017973 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017974 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017975 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017976searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017977{
17978 char_u *spat, *mpat, *epat;
17979 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017980 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017981 int dir;
17982 int flags = 0;
17983 char_u nbuf1[NUMBUFLEN];
17984 char_u nbuf2[NUMBUFLEN];
17985 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017986 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017987 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017988 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017989
Bram Moolenaar071d4272004-06-13 20:20:40 +000017990 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017991 spat = get_tv_string_chk(&argvars[0]);
17992 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17993 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17994 if (spat == NULL || mpat == NULL || epat == NULL)
17995 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017996
Bram Moolenaar071d4272004-06-13 20:20:40 +000017997 /* Handle the optional fourth argument: flags */
17998 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017999 if (dir == 0)
18000 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018001
18002 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000018003 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
18004 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018005 if ((flags & (SP_END | SP_SUBPAT)) != 0
18006 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000018007 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018008 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000018009 goto theend;
18010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018011
Bram Moolenaar92de73d2008-01-22 10:59:38 +000018012 /* Using 'r' implies 'W', otherwise it doesn't work. */
18013 if (flags & SP_REPEAT)
18014 p_ws = FALSE;
18015
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018016 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018017 if (argvars[3].v_type == VAR_UNKNOWN
18018 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018019 skip = (char_u *)"";
18020 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018021 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018022 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018023 if (argvars[5].v_type != VAR_UNKNOWN)
18024 {
18025 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
18026 if (lnum_stop < 0)
18027 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000018028#ifdef FEAT_RELTIME
18029 if (argvars[6].v_type != VAR_UNKNOWN)
18030 {
18031 time_limit = get_tv_number_chk(&argvars[6], NULL);
18032 if (time_limit < 0)
18033 goto theend;
18034 }
18035#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018036 }
18037 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018038 if (skip == NULL)
18039 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018040
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018041 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000018042 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018043
18044theend:
18045 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018046
18047 return retval;
18048}
18049
18050/*
18051 * "searchpair()" function
18052 */
18053 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018054f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018055{
18056 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
18057}
18058
18059/*
18060 * "searchpairpos()" function
18061 */
18062 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018063f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018064{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018065 pos_T match_pos;
18066 int lnum = 0;
18067 int col = 0;
18068
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018069 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018070 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018071
18072 if (searchpair_cmn(argvars, &match_pos) > 0)
18073 {
18074 lnum = match_pos.lnum;
18075 col = match_pos.col;
18076 }
18077
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018078 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18079 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018080}
18081
18082/*
18083 * Search for a start/middle/end thing.
18084 * Used by searchpair(), see its documentation for the details.
18085 * Returns 0 or -1 for no match,
18086 */
18087 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010018088do_searchpair(
18089 char_u *spat, /* start pattern */
18090 char_u *mpat, /* middle pattern */
18091 char_u *epat, /* end pattern */
18092 int dir, /* BACKWARD or FORWARD */
18093 char_u *skip, /* skip expression */
18094 int flags, /* SP_SETPCMARK and other SP_ values */
18095 pos_T *match_pos,
18096 linenr_T lnum_stop, /* stop at this line if not zero */
18097 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018098{
18099 char_u *save_cpo;
18100 char_u *pat, *pat2 = NULL, *pat3 = NULL;
18101 long retval = 0;
18102 pos_T pos;
18103 pos_T firstpos;
18104 pos_T foundpos;
18105 pos_T save_cursor;
18106 pos_T save_pos;
18107 int n;
18108 int r;
18109 int nest = 1;
18110 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018111 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000018112 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018113
18114 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18115 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018116 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018117
Bram Moolenaar76929292008-01-06 19:07:36 +000018118#ifdef FEAT_RELTIME
18119 /* Set the time limit, if there is one. */
18120 profile_setlimit(time_limit, &tm);
18121#endif
18122
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018123 /* Make two search patterns: start/end (pat2, for in nested pairs) and
18124 * start/middle/end (pat3, for the top pair). */
18125 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
18126 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
18127 if (pat2 == NULL || pat3 == NULL)
18128 goto theend;
18129 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
18130 if (*mpat == NUL)
18131 STRCPY(pat3, pat2);
18132 else
18133 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
18134 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018135 if (flags & SP_START)
18136 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018137
Bram Moolenaar071d4272004-06-13 20:20:40 +000018138 save_cursor = curwin->w_cursor;
18139 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018140 clearpos(&firstpos);
18141 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018142 pat = pat3;
18143 for (;;)
18144 {
18145 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000018146 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018147 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
18148 /* didn't find it or found the first match again: FAIL */
18149 break;
18150
18151 if (firstpos.lnum == 0)
18152 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000018153 if (equalpos(pos, foundpos))
18154 {
18155 /* Found the same position again. Can happen with a pattern that
18156 * has "\zs" at the end and searching backwards. Advance one
18157 * character and try again. */
18158 if (dir == BACKWARD)
18159 decl(&pos);
18160 else
18161 incl(&pos);
18162 }
18163 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018164
Bram Moolenaar92de73d2008-01-22 10:59:38 +000018165 /* clear the start flag to avoid getting stuck here */
18166 options &= ~SEARCH_START;
18167
Bram Moolenaar071d4272004-06-13 20:20:40 +000018168 /* If the skip pattern matches, ignore this match. */
18169 if (*skip != NUL)
18170 {
18171 save_pos = curwin->w_cursor;
18172 curwin->w_cursor = pos;
18173 r = eval_to_bool(skip, &err, NULL, FALSE);
18174 curwin->w_cursor = save_pos;
18175 if (err)
18176 {
18177 /* Evaluating {skip} caused an error, break here. */
18178 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018179 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018180 break;
18181 }
18182 if (r)
18183 continue;
18184 }
18185
18186 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
18187 {
18188 /* Found end when searching backwards or start when searching
18189 * forward: nested pair. */
18190 ++nest;
18191 pat = pat2; /* nested, don't search for middle */
18192 }
18193 else
18194 {
18195 /* Found end when searching forward or start when searching
18196 * backward: end of (nested) pair; or found middle in outer pair. */
18197 if (--nest == 1)
18198 pat = pat3; /* outer level, search for middle */
18199 }
18200
18201 if (nest == 0)
18202 {
18203 /* Found the match: return matchcount or line number. */
18204 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018205 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018206 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018207 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000018208 if (flags & SP_SETPCMARK)
18209 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018210 curwin->w_cursor = pos;
18211 if (!(flags & SP_REPEAT))
18212 break;
18213 nest = 1; /* search for next unmatched */
18214 }
18215 }
18216
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018217 if (match_pos != NULL)
18218 {
18219 /* Store the match cursor position */
18220 match_pos->lnum = curwin->w_cursor.lnum;
18221 match_pos->col = curwin->w_cursor.col + 1;
18222 }
18223
Bram Moolenaar071d4272004-06-13 20:20:40 +000018224 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018225 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018226 curwin->w_cursor = save_cursor;
18227
18228theend:
18229 vim_free(pat2);
18230 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018231 if (p_cpo == empty_option)
18232 p_cpo = save_cpo;
18233 else
18234 /* Darn, evaluating the {skip} expression changed the value. */
18235 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018236
18237 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018238}
18239
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018240/*
18241 * "searchpos()" function
18242 */
18243 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018244f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018245{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018246 pos_T match_pos;
18247 int lnum = 0;
18248 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018249 int n;
18250 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018251
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018252 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018253 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018254
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018255 n = search_cmn(argvars, &match_pos, &flags);
18256 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018257 {
18258 lnum = match_pos.lnum;
18259 col = match_pos.col;
18260 }
18261
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018262 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18263 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018264 if (flags & SP_SUBPAT)
18265 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018266}
18267
Bram Moolenaar0d660222005-01-07 21:51:51 +000018268 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018269f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018270{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018271#ifdef FEAT_CLIENTSERVER
18272 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018273 char_u *server = get_tv_string_chk(&argvars[0]);
18274 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018275
Bram Moolenaar0d660222005-01-07 21:51:51 +000018276 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018277 if (server == NULL || reply == NULL)
18278 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018279 if (check_restricted() || check_secure())
18280 return;
18281# ifdef FEAT_X11
18282 if (check_connection() == FAIL)
18283 return;
18284# endif
18285
18286 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018287 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018288 EMSG(_("E258: Unable to send to client"));
18289 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018290 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018291 rettv->vval.v_number = 0;
18292#else
18293 rettv->vval.v_number = -1;
18294#endif
18295}
18296
Bram Moolenaar0d660222005-01-07 21:51:51 +000018297 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018298f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018299{
18300 char_u *r = NULL;
18301
18302#ifdef FEAT_CLIENTSERVER
18303# ifdef WIN32
18304 r = serverGetVimNames();
18305# else
18306 make_connection();
18307 if (X_DISPLAY != NULL)
18308 r = serverGetVimNames(X_DISPLAY);
18309# endif
18310#endif
18311 rettv->v_type = VAR_STRING;
18312 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018313}
18314
18315/*
18316 * "setbufvar()" function
18317 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018318 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018319f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018320{
18321 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018322 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018323 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018324 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018325 char_u nbuf[NUMBUFLEN];
18326
18327 if (check_restricted() || check_secure())
18328 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018329 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18330 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018331 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018332 varp = &argvars[2];
18333
18334 if (buf != NULL && varname != NULL && varp != NULL)
18335 {
18336 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018337 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018338
18339 if (*varname == '&')
18340 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018341 long numval;
18342 char_u *strval;
18343 int error = FALSE;
18344
Bram Moolenaar071d4272004-06-13 20:20:40 +000018345 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018346 numval = get_tv_number_chk(varp, &error);
18347 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018348 if (!error && strval != NULL)
18349 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018350 }
18351 else
18352 {
18353 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18354 if (bufvarname != NULL)
18355 {
18356 STRCPY(bufvarname, "b:");
18357 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018358 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018359 vim_free(bufvarname);
18360 }
18361 }
18362
18363 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018364 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018366}
18367
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018368 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018369f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018370{
18371 dict_T *d;
18372 dictitem_T *di;
18373 char_u *csearch;
18374
18375 if (argvars[0].v_type != VAR_DICT)
18376 {
18377 EMSG(_(e_dictreq));
18378 return;
18379 }
18380
18381 if ((d = argvars[0].vval.v_dict) != NULL)
18382 {
18383 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18384 if (csearch != NULL)
18385 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018386#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018387 if (enc_utf8)
18388 {
18389 int pcc[MAX_MCO];
18390 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018391
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018392 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18393 }
18394 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018395#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018396 set_last_csearch(PTR2CHAR(csearch),
18397 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018398 }
18399
18400 di = dict_find(d, (char_u *)"forward", -1);
18401 if (di != NULL)
18402 set_csearch_direction(get_tv_number(&di->di_tv)
18403 ? FORWARD : BACKWARD);
18404
18405 di = dict_find(d, (char_u *)"until", -1);
18406 if (di != NULL)
18407 set_csearch_until(!!get_tv_number(&di->di_tv));
18408 }
18409}
18410
Bram Moolenaar071d4272004-06-13 20:20:40 +000018411/*
18412 * "setcmdpos()" function
18413 */
18414 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018415f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018416{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018417 int pos = (int)get_tv_number(&argvars[0]) - 1;
18418
18419 if (pos >= 0)
18420 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018421}
18422
18423/*
Bram Moolenaar80492532016-03-08 17:08:53 +010018424 * "setfperm({fname}, {mode})" function
18425 */
18426 static void
18427f_setfperm(typval_T *argvars, typval_T *rettv)
18428{
18429 char_u *fname;
18430 char_u modebuf[NUMBUFLEN];
18431 char_u *mode_str;
18432 int i;
18433 int mask;
18434 int mode = 0;
18435
18436 rettv->vval.v_number = 0;
18437 fname = get_tv_string_chk(&argvars[0]);
18438 if (fname == NULL)
18439 return;
18440 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
18441 if (mode_str == NULL)
18442 return;
18443 if (STRLEN(mode_str) != 9)
18444 {
18445 EMSG2(_(e_invarg2), mode_str);
18446 return;
18447 }
18448
18449 mask = 1;
18450 for (i = 8; i >= 0; --i)
18451 {
18452 if (mode_str[i] != '-')
18453 mode |= mask;
18454 mask = mask << 1;
18455 }
18456 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
18457}
18458
18459/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018460 * "setline()" function
18461 */
18462 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018463f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018464{
18465 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018466 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018467 list_T *l = NULL;
18468 listitem_T *li = NULL;
18469 long added = 0;
18470 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018471
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018472 lnum = get_tv_lnum(&argvars[0]);
18473 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018474 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018475 l = argvars[1].vval.v_list;
18476 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018477 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018478 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018479 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018480
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018481 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018482 for (;;)
18483 {
18484 if (l != NULL)
18485 {
18486 /* list argument, get next string */
18487 if (li == NULL)
18488 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018489 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018490 li = li->li_next;
18491 }
18492
18493 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018494 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018495 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018496
18497 /* When coming here from Insert mode, sync undo, so that this can be
18498 * undone separately from what was previously inserted. */
18499 if (u_sync_once == 2)
18500 {
18501 u_sync_once = 1; /* notify that u_sync() was called */
18502 u_sync(TRUE);
18503 }
18504
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018505 if (lnum <= curbuf->b_ml.ml_line_count)
18506 {
18507 /* existing line, replace it */
18508 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18509 {
18510 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018511 if (lnum == curwin->w_cursor.lnum)
18512 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018513 rettv->vval.v_number = 0; /* OK */
18514 }
18515 }
18516 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18517 {
18518 /* lnum is one past the last line, append the line */
18519 ++added;
18520 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18521 rettv->vval.v_number = 0; /* OK */
18522 }
18523
18524 if (l == NULL) /* only one string argument */
18525 break;
18526 ++lnum;
18527 }
18528
18529 if (added > 0)
18530 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018531}
18532
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018533static 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 +000018534
Bram Moolenaar071d4272004-06-13 20:20:40 +000018535/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018536 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018537 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018538 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018539set_qf_ll_list(
18540 win_T *wp UNUSED,
18541 typval_T *list_arg UNUSED,
18542 typval_T *action_arg UNUSED,
18543 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018544{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018545#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018546 char_u *act;
18547 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018548#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018549
Bram Moolenaar2641f772005-03-25 21:58:17 +000018550 rettv->vval.v_number = -1;
18551
18552#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018553 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018554 EMSG(_(e_listreq));
18555 else
18556 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018557 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018558
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018559 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018560 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018561 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018562 if (act == NULL)
18563 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018564 if (*act == 'a' || *act == 'r')
18565 action = *act;
18566 }
18567
Bram Moolenaar81484f42012-12-05 15:16:47 +010018568 if (l != NULL && set_errorlist(wp, l, action,
18569 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018570 rettv->vval.v_number = 0;
18571 }
18572#endif
18573}
18574
18575/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018576 * "setloclist()" function
18577 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018578 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018579f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018580{
18581 win_T *win;
18582
18583 rettv->vval.v_number = -1;
18584
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018585 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018586 if (win != NULL)
18587 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18588}
18589
18590/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018591 * "setmatches()" function
18592 */
18593 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018594f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018595{
18596#ifdef FEAT_SEARCH_EXTRA
18597 list_T *l;
18598 listitem_T *li;
18599 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018600 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018601
18602 rettv->vval.v_number = -1;
18603 if (argvars[0].v_type != VAR_LIST)
18604 {
18605 EMSG(_(e_listreq));
18606 return;
18607 }
18608 if ((l = argvars[0].vval.v_list) != NULL)
18609 {
18610
18611 /* To some extent make sure that we are dealing with a list from
18612 * "getmatches()". */
18613 li = l->lv_first;
18614 while (li != NULL)
18615 {
18616 if (li->li_tv.v_type != VAR_DICT
18617 || (d = li->li_tv.vval.v_dict) == NULL)
18618 {
18619 EMSG(_(e_invarg));
18620 return;
18621 }
18622 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018623 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18624 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018625 && dict_find(d, (char_u *)"priority", -1) != NULL
18626 && dict_find(d, (char_u *)"id", -1) != NULL))
18627 {
18628 EMSG(_(e_invarg));
18629 return;
18630 }
18631 li = li->li_next;
18632 }
18633
18634 clear_matches(curwin);
18635 li = l->lv_first;
18636 while (li != NULL)
18637 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018638 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018639 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018640 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018641 char_u *group;
18642 int priority;
18643 int id;
18644 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018645
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018646 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018647 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18648 {
18649 if (s == NULL)
18650 {
18651 s = list_alloc();
18652 if (s == NULL)
18653 return;
18654 }
18655
18656 /* match from matchaddpos() */
18657 for (i = 1; i < 9; i++)
18658 {
18659 sprintf((char *)buf, (char *)"pos%d", i);
18660 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18661 {
18662 if (di->di_tv.v_type != VAR_LIST)
18663 return;
18664
18665 list_append_tv(s, &di->di_tv);
18666 s->lv_refcount++;
18667 }
18668 else
18669 break;
18670 }
18671 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018672
18673 group = get_dict_string(d, (char_u *)"group", FALSE);
18674 priority = (int)get_dict_number(d, (char_u *)"priority");
18675 id = (int)get_dict_number(d, (char_u *)"id");
18676 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18677 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18678 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018679 if (i == 0)
18680 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018681 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018682 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018683 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018684 }
18685 else
18686 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018687 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018688 list_unref(s);
18689 s = NULL;
18690 }
18691
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018692 li = li->li_next;
18693 }
18694 rettv->vval.v_number = 0;
18695 }
18696#endif
18697}
18698
18699/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018700 * "setpos()" function
18701 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018702 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018703f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018704{
18705 pos_T pos;
18706 int fnum;
18707 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018708 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018709
Bram Moolenaar08250432008-02-13 11:42:46 +000018710 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018711 name = get_tv_string_chk(argvars);
18712 if (name != NULL)
18713 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018714 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018715 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018716 if (--pos.col < 0)
18717 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018718 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018719 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018720 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018721 if (fnum == curbuf->b_fnum)
18722 {
18723 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018724 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018725 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018726 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018727 curwin->w_set_curswant = FALSE;
18728 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018729 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018730 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018731 }
18732 else
18733 EMSG(_(e_invarg));
18734 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018735 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18736 {
18737 /* set mark */
18738 if (setmark_pos(name[1], &pos, fnum) == OK)
18739 rettv->vval.v_number = 0;
18740 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018741 else
18742 EMSG(_(e_invarg));
18743 }
18744 }
18745}
18746
18747/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018748 * "setqflist()" function
18749 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018750 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018751f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018752{
18753 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18754}
18755
18756/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018757 * "setreg()" function
18758 */
18759 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018760f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018761{
18762 int regname;
18763 char_u *strregname;
18764 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018765 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018766 int append;
18767 char_u yank_type;
18768 long block_len;
18769
18770 block_len = -1;
18771 yank_type = MAUTO;
18772 append = FALSE;
18773
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018774 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018775 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018776
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018777 if (strregname == NULL)
18778 return; /* type error; errmsg already given */
18779 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018780 if (regname == 0 || regname == '@')
18781 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018782
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018783 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018784 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018785 stropt = get_tv_string_chk(&argvars[2]);
18786 if (stropt == NULL)
18787 return; /* type error */
18788 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018789 switch (*stropt)
18790 {
18791 case 'a': case 'A': /* append */
18792 append = TRUE;
18793 break;
18794 case 'v': case 'c': /* character-wise selection */
18795 yank_type = MCHAR;
18796 break;
18797 case 'V': case 'l': /* line-wise selection */
18798 yank_type = MLINE;
18799 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018800 case 'b': case Ctrl_V: /* block-wise selection */
18801 yank_type = MBLOCK;
18802 if (VIM_ISDIGIT(stropt[1]))
18803 {
18804 ++stropt;
18805 block_len = getdigits(&stropt) - 1;
18806 --stropt;
18807 }
18808 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018809 }
18810 }
18811
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018812 if (argvars[1].v_type == VAR_LIST)
18813 {
18814 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018815 char_u **allocval;
18816 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018817 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018818 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018819 int len = argvars[1].vval.v_list->lv_len;
18820 listitem_T *li;
18821
Bram Moolenaar7d647822014-04-05 21:28:56 +020018822 /* First half: use for pointers to result lines; second half: use for
18823 * pointers to allocated copies. */
18824 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018825 if (lstval == NULL)
18826 return;
18827 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018828 allocval = lstval + len + 2;
18829 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018830
18831 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18832 li = li->li_next)
18833 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018834 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018835 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018836 goto free_lstval;
18837 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018838 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018839 /* Need to make a copy, next get_tv_string_buf_chk() will
18840 * overwrite the string. */
18841 strval = vim_strsave(buf);
18842 if (strval == NULL)
18843 goto free_lstval;
18844 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018845 }
18846 *curval++ = strval;
18847 }
18848 *curval++ = NULL;
18849
18850 write_reg_contents_lst(regname, lstval, -1,
18851 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018852free_lstval:
18853 while (curallocval > allocval)
18854 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018855 vim_free(lstval);
18856 }
18857 else
18858 {
18859 strval = get_tv_string_chk(&argvars[1]);
18860 if (strval == NULL)
18861 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018862 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018863 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018864 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018865 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018866}
18867
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018868/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018869 * "settabvar()" function
18870 */
18871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018872f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018873{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018874#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018875 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018876 tabpage_T *tp;
18877#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018878 char_u *varname, *tabvarname;
18879 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018880
18881 rettv->vval.v_number = 0;
18882
18883 if (check_restricted() || check_secure())
18884 return;
18885
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018886#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018887 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018888#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018889 varname = get_tv_string_chk(&argvars[1]);
18890 varp = &argvars[2];
18891
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018892 if (varname != NULL && varp != NULL
18893#ifdef FEAT_WINDOWS
18894 && tp != NULL
18895#endif
18896 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018897 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018898#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018899 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018900 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018901#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018902
18903 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18904 if (tabvarname != NULL)
18905 {
18906 STRCPY(tabvarname, "t:");
18907 STRCPY(tabvarname + 2, varname);
18908 set_var(tabvarname, varp, TRUE);
18909 vim_free(tabvarname);
18910 }
18911
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018912#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018913 /* Restore current tabpage */
18914 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018915 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018916#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018917 }
18918}
18919
18920/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018921 * "settabwinvar()" function
18922 */
18923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018924f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018925{
18926 setwinvar(argvars, rettv, 1);
18927}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018928
18929/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018930 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018931 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018933f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018934{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018935 setwinvar(argvars, rettv, 0);
18936}
18937
18938/*
18939 * "setwinvar()" and "settabwinvar()" functions
18940 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018941
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018942 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018943setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018944{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018945 win_T *win;
18946#ifdef FEAT_WINDOWS
18947 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018948 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018949 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018950#endif
18951 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018952 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018953 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018954 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018955
18956 if (check_restricted() || check_secure())
18957 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018958
18959#ifdef FEAT_WINDOWS
18960 if (off == 1)
18961 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18962 else
18963 tp = curtab;
18964#endif
18965 win = find_win_by_nr(&argvars[off], tp);
18966 varname = get_tv_string_chk(&argvars[off + 1]);
18967 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018968
18969 if (win != NULL && varname != NULL && varp != NULL)
18970 {
18971#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018972 need_switch_win = !(tp == curtab && win == curwin);
18973 if (!need_switch_win
18974 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018976 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018977 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018978 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018979 long numval;
18980 char_u *strval;
18981 int error = FALSE;
18982
18983 ++varname;
18984 numval = get_tv_number_chk(varp, &error);
18985 strval = get_tv_string_buf_chk(varp, nbuf);
18986 if (!error && strval != NULL)
18987 set_option_value(varname, numval, strval, OPT_LOCAL);
18988 }
18989 else
18990 {
18991 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18992 if (winvarname != NULL)
18993 {
18994 STRCPY(winvarname, "w:");
18995 STRCPY(winvarname + 2, varname);
18996 set_var(winvarname, varp, TRUE);
18997 vim_free(winvarname);
18998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018999 }
19000 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019001#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020019002 if (need_switch_win)
19003 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019004#endif
19005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019006}
19007
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010019008#ifdef FEAT_CRYPT
19009/*
19010 * "sha256({string})" function
19011 */
19012 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019013f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010019014{
19015 char_u *p;
19016
19017 p = get_tv_string(&argvars[0]);
19018 rettv->vval.v_string = vim_strsave(
19019 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
19020 rettv->v_type = VAR_STRING;
19021}
19022#endif /* FEAT_CRYPT */
19023
Bram Moolenaar071d4272004-06-13 20:20:40 +000019024/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000019025 * "shellescape({string})" function
19026 */
19027 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019028f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000019029{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019030 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010019031 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000019032 rettv->v_type = VAR_STRING;
19033}
19034
19035/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020019036 * shiftwidth() function
19037 */
19038 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019039f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020019040{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010019041 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020019042}
19043
19044/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019045 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019046 */
19047 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019048f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019049{
Bram Moolenaar0d660222005-01-07 21:51:51 +000019050 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019051
Bram Moolenaar0d660222005-01-07 21:51:51 +000019052 p = get_tv_string(&argvars[0]);
19053 rettv->vval.v_string = vim_strsave(p);
19054 simplify_filename(rettv->vval.v_string); /* simplify in place */
19055 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019056}
19057
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019058#ifdef FEAT_FLOAT
19059/*
19060 * "sin()" function
19061 */
19062 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019063f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019064{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019065 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019066
19067 rettv->v_type = VAR_FLOAT;
19068 if (get_float_arg(argvars, &f) == OK)
19069 rettv->vval.v_float = sin(f);
19070 else
19071 rettv->vval.v_float = 0.0;
19072}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019073
19074/*
19075 * "sinh()" function
19076 */
19077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019078f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019079{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019080 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019081
19082 rettv->v_type = VAR_FLOAT;
19083 if (get_float_arg(argvars, &f) == OK)
19084 rettv->vval.v_float = sinh(f);
19085 else
19086 rettv->vval.v_float = 0.0;
19087}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019088#endif
19089
Bram Moolenaar0d660222005-01-07 21:51:51 +000019090static int
19091#ifdef __BORLANDC__
19092 _RTLENTRYF
19093#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019094 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019095static int
19096#ifdef __BORLANDC__
19097 _RTLENTRYF
19098#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019099 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019100
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019101/* struct used in the array that's given to qsort() */
19102typedef struct
19103{
19104 listitem_T *item;
19105 int idx;
19106} sortItem_T;
19107
Bram Moolenaar0b962472016-02-22 22:51:33 +010019108/* struct storing information about current sort */
19109typedef struct
19110{
19111 int item_compare_ic;
19112 int item_compare_numeric;
19113 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019114#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019115 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019116#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019117 char_u *item_compare_func;
19118 dict_T *item_compare_selfdict;
19119 int item_compare_func_err;
19120 int item_compare_keep_zero;
19121} sortinfo_T;
19122static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019123static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019124#define ITEM_COMPARE_FAIL 999
19125
Bram Moolenaar071d4272004-06-13 20:20:40 +000019126/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019127 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019128 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019129 static int
19130#ifdef __BORLANDC__
19131_RTLENTRYF
19132#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019133item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019134{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019135 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019136 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019137 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019138 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019139 int res;
19140 char_u numbuf1[NUMBUFLEN];
19141 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019142
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019143 si1 = (sortItem_T *)s1;
19144 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019145 tv1 = &si1->item->li_tv;
19146 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019147
Bram Moolenaar0b962472016-02-22 22:51:33 +010019148 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019149 {
19150 long v1 = get_tv_number(tv1);
19151 long v2 = get_tv_number(tv2);
19152
19153 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19154 }
19155
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019156#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019157 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019158 {
19159 float_T v1 = get_tv_float(tv1);
19160 float_T v2 = get_tv_float(tv2);
19161
19162 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19163 }
19164#endif
19165
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019166 /* tv2string() puts quotes around a string and allocates memory. Don't do
19167 * that for string variables. Use a single quote when comparing with a
19168 * non-string to do what the docs promise. */
19169 if (tv1->v_type == VAR_STRING)
19170 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019171 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019172 p1 = (char_u *)"'";
19173 else
19174 p1 = tv1->vval.v_string;
19175 }
19176 else
19177 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
19178 if (tv2->v_type == VAR_STRING)
19179 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019180 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019181 p2 = (char_u *)"'";
19182 else
19183 p2 = tv2->vval.v_string;
19184 }
19185 else
19186 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019187 if (p1 == NULL)
19188 p1 = (char_u *)"";
19189 if (p2 == NULL)
19190 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010019191 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019192 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019193 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019194 res = STRICMP(p1, p2);
19195 else
19196 res = STRCMP(p1, p2);
19197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019198 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020019199 {
19200 double n1, n2;
19201 n1 = strtod((char *)p1, (char **)&p1);
19202 n2 = strtod((char *)p2, (char **)&p2);
19203 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
19204 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019205
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019206 /* When the result would be zero, compare the item indexes. Makes the
19207 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019208 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019209 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019210
Bram Moolenaar0d660222005-01-07 21:51:51 +000019211 vim_free(tofree1);
19212 vim_free(tofree2);
19213 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019214}
19215
19216 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000019217#ifdef __BORLANDC__
19218_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000019219#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019220item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019221{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019222 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019223 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000019224 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019225 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000019226 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019227
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019228 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019229 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019230 return 0;
19231
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019232 si1 = (sortItem_T *)s1;
19233 si2 = (sortItem_T *)s2;
19234
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019235 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019236 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019237 copy_tv(&si1->item->li_tv, &argv[0]);
19238 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019239
19240 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019241 res = call_func(sortinfo->item_compare_func,
Bram Moolenaar4e221c92016-02-23 13:20:22 +010019242 (int)STRLEN(sortinfo->item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020019243 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar0b962472016-02-22 22:51:33 +010019244 sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019245 clear_tv(&argv[0]);
19246 clear_tv(&argv[1]);
19247
19248 if (res == FAIL)
19249 res = ITEM_COMPARE_FAIL;
19250 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019251 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
19252 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000019253 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019254 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019255
19256 /* When the result would be zero, compare the pointers themselves. Makes
19257 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019258 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019259 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019260
Bram Moolenaar0d660222005-01-07 21:51:51 +000019261 return res;
19262}
19263
19264/*
19265 * "sort({list})" function
19266 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019267 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019268do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019269{
Bram Moolenaar33570922005-01-25 22:26:29 +000019270 list_T *l;
19271 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019272 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019273 sortinfo_T *old_sortinfo;
19274 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019275 long len;
19276 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019277
Bram Moolenaar0b962472016-02-22 22:51:33 +010019278 /* Pointer to current info struct used in compare function. Save and
19279 * restore the current one for nested calls. */
19280 old_sortinfo = sortinfo;
19281 sortinfo = &info;
19282
Bram Moolenaar0d660222005-01-07 21:51:51 +000019283 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019284 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019285 else
19286 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019287 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019288 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019289 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19290 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019291 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019292 rettv->vval.v_list = l;
19293 rettv->v_type = VAR_LIST;
19294 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019295
Bram Moolenaar0d660222005-01-07 21:51:51 +000019296 len = list_len(l);
19297 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019298 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019299
Bram Moolenaar0b962472016-02-22 22:51:33 +010019300 info.item_compare_ic = FALSE;
19301 info.item_compare_numeric = FALSE;
19302 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019303#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019304 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019305#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019306 info.item_compare_func = NULL;
19307 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019308 if (argvars[1].v_type != VAR_UNKNOWN)
19309 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019310 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019311 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019312 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019313 else
19314 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019315 int error = FALSE;
19316
19317 i = get_tv_number_chk(&argvars[1], &error);
19318 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019319 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019320 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019321 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010019322 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019323 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010019324 else if (i != 0)
19325 {
19326 EMSG(_(e_invarg));
19327 goto theend;
19328 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019329 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019330 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010019331 if (*info.item_compare_func == NUL)
19332 {
19333 /* empty string means default sort */
19334 info.item_compare_func = NULL;
19335 }
19336 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019337 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019338 info.item_compare_func = NULL;
19339 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019340 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019341 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019342 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019343 info.item_compare_func = NULL;
19344 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019345 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019346#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019347 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019348 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019349 info.item_compare_func = NULL;
19350 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019351 }
19352#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019353 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019354 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019355 info.item_compare_func = NULL;
19356 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019357 }
19358 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019359 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019360
19361 if (argvars[2].v_type != VAR_UNKNOWN)
19362 {
19363 /* optional third argument: {dict} */
19364 if (argvars[2].v_type != VAR_DICT)
19365 {
19366 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019367 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019368 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019369 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019370 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019372
Bram Moolenaar0d660222005-01-07 21:51:51 +000019373 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019374 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019375 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019376 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019377
Bram Moolenaar327aa022014-03-25 18:24:23 +010019378 i = 0;
19379 if (sort)
19380 {
19381 /* sort(): ptrs will be the list to sort */
19382 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019383 {
19384 ptrs[i].item = li;
19385 ptrs[i].idx = i;
19386 ++i;
19387 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019388
Bram Moolenaar0b962472016-02-22 22:51:33 +010019389 info.item_compare_func_err = FALSE;
19390 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019391 /* test the compare function */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019392 if (info.item_compare_func != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019393 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019394 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019395 EMSG(_("E702: Sort compare function failed"));
19396 else
19397 {
19398 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019399 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019400 info.item_compare_func == NULL
19401 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019402
Bram Moolenaar0b962472016-02-22 22:51:33 +010019403 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019404 {
19405 /* Clear the List and append the items in sorted order. */
19406 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19407 l->lv_len = 0;
19408 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019409 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019410 }
19411 }
19412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019413 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019414 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019415 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019416
19417 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019418 info.item_compare_func_err = FALSE;
19419 info.item_compare_keep_zero = TRUE;
19420 item_compare_func_ptr = info.item_compare_func
Bram Moolenaar327aa022014-03-25 18:24:23 +010019421 ? item_compare2 : item_compare;
19422
19423 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19424 li = li->li_next)
19425 {
19426 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19427 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019428 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019429 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019430 {
19431 EMSG(_("E882: Uniq compare function failed"));
19432 break;
19433 }
19434 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019435
Bram Moolenaar0b962472016-02-22 22:51:33 +010019436 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019437 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019438 while (--i >= 0)
19439 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019440 li = ptrs[i].item->li_next;
19441 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019442 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019443 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019444 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019445 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019446 list_fix_watch(l, li);
19447 listitem_free(li);
19448 l->lv_len--;
19449 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019450 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019451 }
19452
19453 vim_free(ptrs);
19454 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019455theend:
19456 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019457}
19458
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019459/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019460 * "sort({list})" function
19461 */
19462 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019463f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019464{
19465 do_sort_uniq(argvars, rettv, TRUE);
19466}
19467
19468/*
19469 * "uniq({list})" function
19470 */
19471 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019472f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019473{
19474 do_sort_uniq(argvars, rettv, FALSE);
19475}
19476
19477/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019478 * "soundfold({word})" function
19479 */
19480 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019481f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019482{
19483 char_u *s;
19484
19485 rettv->v_type = VAR_STRING;
19486 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019487#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019488 rettv->vval.v_string = eval_soundfold(s);
19489#else
19490 rettv->vval.v_string = vim_strsave(s);
19491#endif
19492}
19493
19494/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019495 * "spellbadword()" function
19496 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019497 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019498f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019499{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019500 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019501 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019502 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019503
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019504 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019505 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019506
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019507#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019508 if (argvars[0].v_type == VAR_UNKNOWN)
19509 {
19510 /* Find the start and length of the badly spelled word. */
19511 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19512 if (len != 0)
19513 word = ml_get_cursor();
19514 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019515 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019516 {
19517 char_u *str = get_tv_string_chk(&argvars[0]);
19518 int capcol = -1;
19519
19520 if (str != NULL)
19521 {
19522 /* Check the argument for spelling. */
19523 while (*str != NUL)
19524 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019525 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019526 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019527 {
19528 word = str;
19529 break;
19530 }
19531 str += len;
19532 }
19533 }
19534 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019535#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019536
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019537 list_append_string(rettv->vval.v_list, word, len);
19538 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019539 attr == HLF_SPB ? "bad" :
19540 attr == HLF_SPR ? "rare" :
19541 attr == HLF_SPL ? "local" :
19542 attr == HLF_SPC ? "caps" :
19543 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019544}
19545
19546/*
19547 * "spellsuggest()" function
19548 */
19549 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019550f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019551{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019552#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019553 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019554 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019555 int maxcount;
19556 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019557 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019558 listitem_T *li;
19559 int need_capital = FALSE;
19560#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019561
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019562 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019563 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019564
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019565#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019566 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019567 {
19568 str = get_tv_string(&argvars[0]);
19569 if (argvars[1].v_type != VAR_UNKNOWN)
19570 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019571 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019572 if (maxcount <= 0)
19573 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019574 if (argvars[2].v_type != VAR_UNKNOWN)
19575 {
19576 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19577 if (typeerr)
19578 return;
19579 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019580 }
19581 else
19582 maxcount = 25;
19583
Bram Moolenaar4770d092006-01-12 23:22:24 +000019584 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019585
19586 for (i = 0; i < ga.ga_len; ++i)
19587 {
19588 str = ((char_u **)ga.ga_data)[i];
19589
19590 li = listitem_alloc();
19591 if (li == NULL)
19592 vim_free(str);
19593 else
19594 {
19595 li->li_tv.v_type = VAR_STRING;
19596 li->li_tv.v_lock = 0;
19597 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019598 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019599 }
19600 }
19601 ga_clear(&ga);
19602 }
19603#endif
19604}
19605
Bram Moolenaar0d660222005-01-07 21:51:51 +000019606 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019607f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019608{
19609 char_u *str;
19610 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019611 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019612 regmatch_T regmatch;
19613 char_u patbuf[NUMBUFLEN];
19614 char_u *save_cpo;
19615 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019616 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019617 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019618 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019619
19620 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19621 save_cpo = p_cpo;
19622 p_cpo = (char_u *)"";
19623
19624 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019625 if (argvars[1].v_type != VAR_UNKNOWN)
19626 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019627 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19628 if (pat == NULL)
19629 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019630 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019631 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019632 }
19633 if (pat == NULL || *pat == NUL)
19634 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019635
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019636 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019637 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019638 if (typeerr)
19639 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019640
Bram Moolenaar0d660222005-01-07 21:51:51 +000019641 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19642 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019643 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019644 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019645 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019646 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019647 if (*str == NUL)
19648 match = FALSE; /* empty item at the end */
19649 else
19650 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019651 if (match)
19652 end = regmatch.startp[0];
19653 else
19654 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019655 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19656 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019657 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019658 if (list_append_string(rettv->vval.v_list, str,
19659 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019660 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019661 }
19662 if (!match)
19663 break;
19664 /* Advance to just after the match. */
19665 if (regmatch.endp[0] > str)
19666 col = 0;
19667 else
19668 {
19669 /* Don't get stuck at the same match. */
19670#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019671 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019672#else
19673 col = 1;
19674#endif
19675 }
19676 str = regmatch.endp[0];
19677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019678
Bram Moolenaar473de612013-06-08 18:19:48 +020019679 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019681
Bram Moolenaar0d660222005-01-07 21:51:51 +000019682 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019683}
19684
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019685#ifdef FEAT_FLOAT
19686/*
19687 * "sqrt()" function
19688 */
19689 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019690f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019691{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019692 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019693
19694 rettv->v_type = VAR_FLOAT;
19695 if (get_float_arg(argvars, &f) == OK)
19696 rettv->vval.v_float = sqrt(f);
19697 else
19698 rettv->vval.v_float = 0.0;
19699}
19700
19701/*
19702 * "str2float()" function
19703 */
19704 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019705f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019706{
19707 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19708
19709 if (*p == '+')
19710 p = skipwhite(p + 1);
19711 (void)string2float(p, &rettv->vval.v_float);
19712 rettv->v_type = VAR_FLOAT;
19713}
19714#endif
19715
Bram Moolenaar2c932302006-03-18 21:42:09 +000019716/*
19717 * "str2nr()" function
19718 */
19719 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019720f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019721{
19722 int base = 10;
19723 char_u *p;
19724 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019725 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019726
19727 if (argvars[1].v_type != VAR_UNKNOWN)
19728 {
19729 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019730 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019731 {
19732 EMSG(_(e_invarg));
19733 return;
19734 }
19735 }
19736
19737 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019738 if (*p == '+')
19739 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019740 switch (base)
19741 {
19742 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19743 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19744 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19745 default: what = 0;
19746 }
19747 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019748 rettv->vval.v_number = n;
19749}
19750
Bram Moolenaar071d4272004-06-13 20:20:40 +000019751#ifdef HAVE_STRFTIME
19752/*
19753 * "strftime({format}[, {time}])" function
19754 */
19755 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019756f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019757{
19758 char_u result_buf[256];
19759 struct tm *curtime;
19760 time_t seconds;
19761 char_u *p;
19762
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019763 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019764
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019765 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019766 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019767 seconds = time(NULL);
19768 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019769 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019770 curtime = localtime(&seconds);
19771 /* MSVC returns NULL for an invalid value of seconds. */
19772 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019773 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019774 else
19775 {
19776# ifdef FEAT_MBYTE
19777 vimconv_T conv;
19778 char_u *enc;
19779
19780 conv.vc_type = CONV_NONE;
19781 enc = enc_locale();
19782 convert_setup(&conv, p_enc, enc);
19783 if (conv.vc_type != CONV_NONE)
19784 p = string_convert(&conv, p, NULL);
19785# endif
19786 if (p != NULL)
19787 (void)strftime((char *)result_buf, sizeof(result_buf),
19788 (char *)p, curtime);
19789 else
19790 result_buf[0] = NUL;
19791
19792# ifdef FEAT_MBYTE
19793 if (conv.vc_type != CONV_NONE)
19794 vim_free(p);
19795 convert_setup(&conv, enc, p_enc);
19796 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019797 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019798 else
19799# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019800 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019801
19802# ifdef FEAT_MBYTE
19803 /* Release conversion descriptors */
19804 convert_setup(&conv, NULL, NULL);
19805 vim_free(enc);
19806# endif
19807 }
19808}
19809#endif
19810
19811/*
19812 * "stridx()" function
19813 */
19814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019815f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019816{
19817 char_u buf[NUMBUFLEN];
19818 char_u *needle;
19819 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019820 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019821 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019822 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019823
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019824 needle = get_tv_string_chk(&argvars[1]);
19825 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019826 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019827 if (needle == NULL || haystack == NULL)
19828 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019829
Bram Moolenaar33570922005-01-25 22:26:29 +000019830 if (argvars[2].v_type != VAR_UNKNOWN)
19831 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019832 int error = FALSE;
19833
19834 start_idx = get_tv_number_chk(&argvars[2], &error);
19835 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019836 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019837 if (start_idx >= 0)
19838 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019839 }
19840
19841 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19842 if (pos != NULL)
19843 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019844}
19845
19846/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019847 * "string()" function
19848 */
19849 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019850f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019851{
19852 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019853 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019854
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019855 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019856 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019857 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019858 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019859 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019860}
19861
19862/*
19863 * "strlen()" function
19864 */
19865 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019866f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019867{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019868 rettv->vval.v_number = (varnumber_T)(STRLEN(
19869 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019870}
19871
19872/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019873 * "strchars()" function
19874 */
19875 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019876f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019877{
19878 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019879 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019880#ifdef FEAT_MBYTE
19881 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019882 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019883#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019884
19885 if (argvars[1].v_type != VAR_UNKNOWN)
19886 skipcc = get_tv_number_chk(&argvars[1], NULL);
19887 if (skipcc < 0 || skipcc > 1)
19888 EMSG(_(e_invarg));
19889 else
19890 {
19891#ifdef FEAT_MBYTE
19892 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19893 while (*s != NUL)
19894 {
19895 func_mb_ptr2char_adv(&s);
19896 ++len;
19897 }
19898 rettv->vval.v_number = len;
19899#else
19900 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19901#endif
19902 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019903}
19904
19905/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019906 * "strdisplaywidth()" function
19907 */
19908 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019909f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019910{
19911 char_u *s = get_tv_string(&argvars[0]);
19912 int col = 0;
19913
19914 if (argvars[1].v_type != VAR_UNKNOWN)
19915 col = get_tv_number(&argvars[1]);
19916
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019917 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019918}
19919
19920/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019921 * "strwidth()" function
19922 */
19923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019924f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019925{
19926 char_u *s = get_tv_string(&argvars[0]);
19927
19928 rettv->vval.v_number = (varnumber_T)(
19929#ifdef FEAT_MBYTE
19930 mb_string2cells(s, -1)
19931#else
19932 STRLEN(s)
19933#endif
19934 );
19935}
19936
19937/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019938 * "strpart()" function
19939 */
19940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019941f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019942{
19943 char_u *p;
19944 int n;
19945 int len;
19946 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019947 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019948
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019949 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019950 slen = (int)STRLEN(p);
19951
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019952 n = get_tv_number_chk(&argvars[1], &error);
19953 if (error)
19954 len = 0;
19955 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019956 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019957 else
19958 len = slen - n; /* default len: all bytes that are available. */
19959
19960 /*
19961 * Only return the overlap between the specified part and the actual
19962 * string.
19963 */
19964 if (n < 0)
19965 {
19966 len += n;
19967 n = 0;
19968 }
19969 else if (n > slen)
19970 n = slen;
19971 if (len < 0)
19972 len = 0;
19973 else if (n + len > slen)
19974 len = slen - n;
19975
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019976 rettv->v_type = VAR_STRING;
19977 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019978}
19979
19980/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019981 * "strridx()" function
19982 */
19983 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019984f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019985{
19986 char_u buf[NUMBUFLEN];
19987 char_u *needle;
19988 char_u *haystack;
19989 char_u *rest;
19990 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019991 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019992
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019993 needle = get_tv_string_chk(&argvars[1]);
19994 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019995
19996 rettv->vval.v_number = -1;
19997 if (needle == NULL || haystack == NULL)
19998 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019999
20000 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020001 if (argvars[2].v_type != VAR_UNKNOWN)
20002 {
20003 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020004 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020005 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020006 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020007 }
20008 else
20009 end_idx = haystack_len;
20010
Bram Moolenaar0d660222005-01-07 21:51:51 +000020011 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020012 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000020013 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020014 lastmatch = haystack + end_idx;
20015 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020016 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000020017 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000020018 for (rest = haystack; *rest != '\0'; ++rest)
20019 {
20020 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000020021 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020022 break;
20023 lastmatch = rest;
20024 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000020025 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020026
20027 if (lastmatch == NULL)
20028 rettv->vval.v_number = -1;
20029 else
20030 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
20031}
20032
20033/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020034 * "strtrans()" function
20035 */
20036 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020037f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020038{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020039 rettv->v_type = VAR_STRING;
20040 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020041}
20042
20043/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000020044 * "submatch()" function
20045 */
20046 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020047f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020048{
Bram Moolenaar41571762014-04-02 19:00:58 +020020049 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020020050 int no;
20051 int retList = 0;
20052
20053 no = (int)get_tv_number_chk(&argvars[0], &error);
20054 if (error)
20055 return;
20056 error = FALSE;
20057 if (argvars[1].v_type != VAR_UNKNOWN)
20058 retList = get_tv_number_chk(&argvars[1], &error);
20059 if (error)
20060 return;
20061
20062 if (retList == 0)
20063 {
20064 rettv->v_type = VAR_STRING;
20065 rettv->vval.v_string = reg_submatch(no);
20066 }
20067 else
20068 {
20069 rettv->v_type = VAR_LIST;
20070 rettv->vval.v_list = reg_submatch_list(no);
20071 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020072}
20073
20074/*
20075 * "substitute()" function
20076 */
20077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020078f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020079{
20080 char_u patbuf[NUMBUFLEN];
20081 char_u subbuf[NUMBUFLEN];
20082 char_u flagsbuf[NUMBUFLEN];
20083
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020084 char_u *str = get_tv_string_chk(&argvars[0]);
20085 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
20086 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
20087 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
20088
Bram Moolenaar0d660222005-01-07 21:51:51 +000020089 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020090 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
20091 rettv->vval.v_string = NULL;
20092 else
20093 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000020094}
20095
20096/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020097 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000020098 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020099 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020100f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020101{
20102 int id = 0;
20103#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020104 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020105 long col;
20106 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020107 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020108
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020109 lnum = get_tv_lnum(argvars); /* -1 on type error */
20110 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20111 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020112
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020113 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020114 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020115 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020116#endif
20117
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020118 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020119}
20120
20121/*
20122 * "synIDattr(id, what [, mode])" function
20123 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020125f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020126{
20127 char_u *p = NULL;
20128#ifdef FEAT_SYN_HL
20129 int id;
20130 char_u *what;
20131 char_u *mode;
20132 char_u modebuf[NUMBUFLEN];
20133 int modec;
20134
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020135 id = get_tv_number(&argvars[0]);
20136 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020137 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020138 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020139 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020140 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020020141 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000020142 modec = 0; /* replace invalid with current */
20143 }
20144 else
20145 {
20146#ifdef FEAT_GUI
20147 if (gui.in_use)
20148 modec = 'g';
20149 else
20150#endif
20151 if (t_colors > 1)
20152 modec = 'c';
20153 else
20154 modec = 't';
20155 }
20156
20157
20158 switch (TOLOWER_ASC(what[0]))
20159 {
20160 case 'b':
20161 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
20162 p = highlight_color(id, what, modec);
20163 else /* bold */
20164 p = highlight_has_attr(id, HL_BOLD, modec);
20165 break;
20166
Bram Moolenaar12682fd2010-03-10 13:43:49 +010020167 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020168 p = highlight_color(id, what, modec);
20169 break;
20170
20171 case 'i':
20172 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
20173 p = highlight_has_attr(id, HL_INVERSE, modec);
20174 else /* italic */
20175 p = highlight_has_attr(id, HL_ITALIC, modec);
20176 break;
20177
20178 case 'n': /* name */
20179 p = get_highlight_name(NULL, id - 1);
20180 break;
20181
20182 case 'r': /* reverse */
20183 p = highlight_has_attr(id, HL_INVERSE, modec);
20184 break;
20185
Bram Moolenaar6f507d62008-11-28 10:16:05 +000020186 case 's':
20187 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
20188 p = highlight_color(id, what, modec);
20189 else /* standout */
20190 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020191 break;
20192
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000020193 case 'u':
20194 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
20195 /* underline */
20196 p = highlight_has_attr(id, HL_UNDERLINE, modec);
20197 else
20198 /* undercurl */
20199 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020200 break;
20201 }
20202
20203 if (p != NULL)
20204 p = vim_strsave(p);
20205#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020206 rettv->v_type = VAR_STRING;
20207 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020208}
20209
20210/*
20211 * "synIDtrans(id)" function
20212 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020214f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020215{
20216 int id;
20217
20218#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020219 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020220
20221 if (id > 0)
20222 id = syn_get_final_id(id);
20223 else
20224#endif
20225 id = 0;
20226
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020227 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020228}
20229
20230/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020231 * "synconcealed(lnum, col)" function
20232 */
20233 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020234f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020235{
20236#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20237 long lnum;
20238 long col;
20239 int syntax_flags = 0;
20240 int cchar;
20241 int matchid = 0;
20242 char_u str[NUMBUFLEN];
20243#endif
20244
20245 rettv->v_type = VAR_LIST;
20246 rettv->vval.v_list = NULL;
20247
20248#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20249 lnum = get_tv_lnum(argvars); /* -1 on type error */
20250 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20251
20252 vim_memset(str, NUL, sizeof(str));
20253
20254 if (rettv_list_alloc(rettv) != FAIL)
20255 {
20256 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20257 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20258 && curwin->w_p_cole > 0)
20259 {
20260 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20261 syntax_flags = get_syntax_info(&matchid);
20262
20263 /* get the conceal character */
20264 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20265 {
20266 cchar = syn_get_sub_char();
20267 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20268 cchar = lcs_conceal;
20269 if (cchar != NUL)
20270 {
20271# ifdef FEAT_MBYTE
20272 if (has_mbyte)
20273 (*mb_char2bytes)(cchar, str);
20274 else
20275# endif
20276 str[0] = cchar;
20277 }
20278 }
20279 }
20280
20281 list_append_number(rettv->vval.v_list,
20282 (syntax_flags & HL_CONCEAL) != 0);
20283 /* -1 to auto-determine strlen */
20284 list_append_string(rettv->vval.v_list, str, -1);
20285 list_append_number(rettv->vval.v_list, matchid);
20286 }
20287#endif
20288}
20289
20290/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020291 * "synstack(lnum, col)" function
20292 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020293 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020294f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020295{
20296#ifdef FEAT_SYN_HL
20297 long lnum;
20298 long col;
20299 int i;
20300 int id;
20301#endif
20302
20303 rettv->v_type = VAR_LIST;
20304 rettv->vval.v_list = NULL;
20305
20306#ifdef FEAT_SYN_HL
20307 lnum = get_tv_lnum(argvars); /* -1 on type error */
20308 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20309
20310 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020311 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020312 && rettv_list_alloc(rettv) != FAIL)
20313 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020314 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020315 for (i = 0; ; ++i)
20316 {
20317 id = syn_get_stack_item(i);
20318 if (id < 0)
20319 break;
20320 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20321 break;
20322 }
20323 }
20324#endif
20325}
20326
Bram Moolenaar071d4272004-06-13 20:20:40 +000020327 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020328get_cmd_output_as_rettv(
20329 typval_T *argvars,
20330 typval_T *rettv,
20331 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020332{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020333 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020334 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020335 char_u *infile = NULL;
20336 char_u buf[NUMBUFLEN];
20337 int err = FALSE;
20338 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020339 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020340 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020341
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020342 rettv->v_type = VAR_STRING;
20343 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020344 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020345 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020346
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020347 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020348 {
20349 /*
20350 * Write the string to a temp file, to be used for input of the shell
20351 * command.
20352 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020353 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020354 {
20355 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020356 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020357 }
20358
20359 fd = mch_fopen((char *)infile, WRITEBIN);
20360 if (fd == NULL)
20361 {
20362 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020363 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020364 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020365 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020366 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020367 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20368 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020369 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020370 else
20371 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020372 size_t len;
20373
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020374 p = get_tv_string_buf_chk(&argvars[1], buf);
20375 if (p == NULL)
20376 {
20377 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020378 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020379 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020380 len = STRLEN(p);
20381 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020382 err = TRUE;
20383 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020384 if (fclose(fd) != 0)
20385 err = TRUE;
20386 if (err)
20387 {
20388 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020389 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020390 }
20391 }
20392
Bram Moolenaar52a72462014-08-29 15:53:52 +020020393 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20394 * echoes typeahead, that messes up the display. */
20395 if (!msg_silent)
20396 flags += SHELL_COOKED;
20397
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020398 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020399 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020400 int len;
20401 listitem_T *li;
20402 char_u *s = NULL;
20403 char_u *start;
20404 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020405 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020406
Bram Moolenaar52a72462014-08-29 15:53:52 +020020407 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020408 if (res == NULL)
20409 goto errret;
20410
20411 list = list_alloc();
20412 if (list == NULL)
20413 goto errret;
20414
20415 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020416 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020417 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020418 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020419 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020420 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020421
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020422 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020423 if (s == NULL)
20424 goto errret;
20425
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020426 for (p = s; start < end; ++p, ++start)
20427 *p = *start == NUL ? NL : *start;
20428 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020429
20430 li = listitem_alloc();
20431 if (li == NULL)
20432 {
20433 vim_free(s);
20434 goto errret;
20435 }
20436 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020437 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020438 li->li_tv.vval.v_string = s;
20439 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020440 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020441
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020442 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020443 rettv->v_type = VAR_LIST;
20444 rettv->vval.v_list = list;
20445 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020446 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020447 else
20448 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020449 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020450#ifdef USE_CR
20451 /* translate <CR> into <NL> */
20452 if (res != NULL)
20453 {
20454 char_u *s;
20455
20456 for (s = res; *s; ++s)
20457 {
20458 if (*s == CAR)
20459 *s = NL;
20460 }
20461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020462#else
20463# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020464 /* translate <CR><NL> into <NL> */
20465 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020466 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020467 char_u *s, *d;
20468
20469 d = res;
20470 for (s = res; *s; ++s)
20471 {
20472 if (s[0] == CAR && s[1] == NL)
20473 ++s;
20474 *d++ = *s;
20475 }
20476 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020478# endif
20479#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020480 rettv->vval.v_string = res;
20481 res = NULL;
20482 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020483
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020484errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020485 if (infile != NULL)
20486 {
20487 mch_remove(infile);
20488 vim_free(infile);
20489 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020490 if (res != NULL)
20491 vim_free(res);
20492 if (list != NULL)
20493 list_free(list, TRUE);
20494}
20495
20496/*
20497 * "system()" function
20498 */
20499 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020500f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020501{
20502 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20503}
20504
20505/*
20506 * "systemlist()" function
20507 */
20508 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020509f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020510{
20511 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020512}
20513
20514/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020515 * "tabpagebuflist()" function
20516 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020517 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020518f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020519{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020520#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020521 tabpage_T *tp;
20522 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020523
20524 if (argvars[0].v_type == VAR_UNKNOWN)
20525 wp = firstwin;
20526 else
20527 {
20528 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20529 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020530 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020531 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020532 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020533 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020534 for (; wp != NULL; wp = wp->w_next)
20535 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020536 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020537 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020538 }
20539#endif
20540}
20541
20542
20543/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020544 * "tabpagenr()" function
20545 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020546 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020547f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020548{
20549 int nr = 1;
20550#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020551 char_u *arg;
20552
20553 if (argvars[0].v_type != VAR_UNKNOWN)
20554 {
20555 arg = get_tv_string_chk(&argvars[0]);
20556 nr = 0;
20557 if (arg != NULL)
20558 {
20559 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020560 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020561 else
20562 EMSG2(_(e_invexpr2), arg);
20563 }
20564 }
20565 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020566 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020567#endif
20568 rettv->vval.v_number = nr;
20569}
20570
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020571
20572#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020573static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020574
20575/*
20576 * Common code for tabpagewinnr() and winnr().
20577 */
20578 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020579get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020580{
20581 win_T *twin;
20582 int nr = 1;
20583 win_T *wp;
20584 char_u *arg;
20585
20586 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20587 if (argvar->v_type != VAR_UNKNOWN)
20588 {
20589 arg = get_tv_string_chk(argvar);
20590 if (arg == NULL)
20591 nr = 0; /* type error; errmsg already given */
20592 else if (STRCMP(arg, "$") == 0)
20593 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20594 else if (STRCMP(arg, "#") == 0)
20595 {
20596 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20597 if (twin == NULL)
20598 nr = 0;
20599 }
20600 else
20601 {
20602 EMSG2(_(e_invexpr2), arg);
20603 nr = 0;
20604 }
20605 }
20606
20607 if (nr > 0)
20608 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20609 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020610 {
20611 if (wp == NULL)
20612 {
20613 /* didn't find it in this tabpage */
20614 nr = 0;
20615 break;
20616 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020617 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020618 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020619 return nr;
20620}
20621#endif
20622
20623/*
20624 * "tabpagewinnr()" function
20625 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020626 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020627f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020628{
20629 int nr = 1;
20630#ifdef FEAT_WINDOWS
20631 tabpage_T *tp;
20632
20633 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20634 if (tp == NULL)
20635 nr = 0;
20636 else
20637 nr = get_winnr(tp, &argvars[1]);
20638#endif
20639 rettv->vval.v_number = nr;
20640}
20641
20642
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020643/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020644 * "tagfiles()" function
20645 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020646 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020647f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020648{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020649 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020650 tagname_T tn;
20651 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020652
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020653 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020654 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020655 fname = alloc(MAXPATHL);
20656 if (fname == NULL)
20657 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020658
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020659 for (first = TRUE; ; first = FALSE)
20660 if (get_tagfname(&tn, first, fname) == FAIL
20661 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020662 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020663 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020664 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020665}
20666
20667/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020668 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020669 */
20670 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020671f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020672{
20673 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020674
20675 tag_pattern = get_tv_string(&argvars[0]);
20676
20677 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020678 if (*tag_pattern == NUL)
20679 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020680
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020681 if (rettv_list_alloc(rettv) == OK)
20682 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020683}
20684
20685/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020686 * "tempname()" function
20687 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020688 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020689f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020690{
20691 static int x = 'A';
20692
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020693 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020694 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020695
20696 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20697 * names. Skip 'I' and 'O', they are used for shell redirection. */
20698 do
20699 {
20700 if (x == 'Z')
20701 x = '0';
20702 else if (x == '9')
20703 x = 'A';
20704 else
20705 {
20706#ifdef EBCDIC
20707 if (x == 'I')
20708 x = 'J';
20709 else if (x == 'R')
20710 x = 'S';
20711 else
20712#endif
20713 ++x;
20714 }
20715 } while (x == 'I' || x == 'O');
20716}
20717
20718/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020719 * "test(list)" function: Just checking the walls...
20720 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020721 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020722f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020723{
20724 /* Used for unit testing. Change the code below to your liking. */
20725#if 0
20726 listitem_T *li;
20727 list_T *l;
20728 char_u *bad, *good;
20729
20730 if (argvars[0].v_type != VAR_LIST)
20731 return;
20732 l = argvars[0].vval.v_list;
20733 if (l == NULL)
20734 return;
20735 li = l->lv_first;
20736 if (li == NULL)
20737 return;
20738 bad = get_tv_string(&li->li_tv);
20739 li = li->li_next;
20740 if (li == NULL)
20741 return;
20742 good = get_tv_string(&li->li_tv);
20743 rettv->vval.v_number = test_edit_score(bad, good);
20744#endif
20745}
20746
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020747#ifdef FEAT_FLOAT
20748/*
20749 * "tan()" function
20750 */
20751 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020752f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020753{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020754 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020755
20756 rettv->v_type = VAR_FLOAT;
20757 if (get_float_arg(argvars, &f) == OK)
20758 rettv->vval.v_float = tan(f);
20759 else
20760 rettv->vval.v_float = 0.0;
20761}
20762
20763/*
20764 * "tanh()" function
20765 */
20766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020767f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020768{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020769 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020770
20771 rettv->v_type = VAR_FLOAT;
20772 if (get_float_arg(argvars, &f) == OK)
20773 rettv->vval.v_float = tanh(f);
20774 else
20775 rettv->vval.v_float = 0.0;
20776}
20777#endif
20778
Bram Moolenaard52d9742005-08-21 22:20:28 +000020779/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020780 * "tolower(string)" function
20781 */
20782 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020783f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020784{
20785 char_u *p;
20786
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020787 p = vim_strsave(get_tv_string(&argvars[0]));
20788 rettv->v_type = VAR_STRING;
20789 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020790
20791 if (p != NULL)
20792 while (*p != NUL)
20793 {
20794#ifdef FEAT_MBYTE
20795 int l;
20796
20797 if (enc_utf8)
20798 {
20799 int c, lc;
20800
20801 c = utf_ptr2char(p);
20802 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020803 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020804 /* TODO: reallocate string when byte count changes. */
20805 if (utf_char2len(lc) == l)
20806 utf_char2bytes(lc, p);
20807 p += l;
20808 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020809 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020810 p += l; /* skip multi-byte character */
20811 else
20812#endif
20813 {
20814 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20815 ++p;
20816 }
20817 }
20818}
20819
20820/*
20821 * "toupper(string)" function
20822 */
20823 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020824f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020825{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020826 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020827 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020828}
20829
20830/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020831 * "tr(string, fromstr, tostr)" function
20832 */
20833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020834f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020835{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020836 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020837 char_u *fromstr;
20838 char_u *tostr;
20839 char_u *p;
20840#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020841 int inlen;
20842 int fromlen;
20843 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020844 int idx;
20845 char_u *cpstr;
20846 int cplen;
20847 int first = TRUE;
20848#endif
20849 char_u buf[NUMBUFLEN];
20850 char_u buf2[NUMBUFLEN];
20851 garray_T ga;
20852
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020853 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020854 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20855 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020856
20857 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020858 rettv->v_type = VAR_STRING;
20859 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020860 if (fromstr == NULL || tostr == NULL)
20861 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020862 ga_init2(&ga, (int)sizeof(char), 80);
20863
20864#ifdef FEAT_MBYTE
20865 if (!has_mbyte)
20866#endif
20867 /* not multi-byte: fromstr and tostr must be the same length */
20868 if (STRLEN(fromstr) != STRLEN(tostr))
20869 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020870#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020871error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020872#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020873 EMSG2(_(e_invarg2), fromstr);
20874 ga_clear(&ga);
20875 return;
20876 }
20877
20878 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020879 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020880 {
20881#ifdef FEAT_MBYTE
20882 if (has_mbyte)
20883 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020884 inlen = (*mb_ptr2len)(in_str);
20885 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020886 cplen = inlen;
20887 idx = 0;
20888 for (p = fromstr; *p != NUL; p += fromlen)
20889 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020890 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020891 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020892 {
20893 for (p = tostr; *p != NUL; p += tolen)
20894 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020895 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020896 if (idx-- == 0)
20897 {
20898 cplen = tolen;
20899 cpstr = p;
20900 break;
20901 }
20902 }
20903 if (*p == NUL) /* tostr is shorter than fromstr */
20904 goto error;
20905 break;
20906 }
20907 ++idx;
20908 }
20909
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020910 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020911 {
20912 /* Check that fromstr and tostr have the same number of
20913 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020914 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020915 first = FALSE;
20916 for (p = tostr; *p != NUL; p += tolen)
20917 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020918 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020919 --idx;
20920 }
20921 if (idx != 0)
20922 goto error;
20923 }
20924
Bram Moolenaarcde88542015-08-11 19:14:00 +020020925 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020926 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020927 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020928
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020929 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020930 }
20931 else
20932#endif
20933 {
20934 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020935 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020936 if (p != NULL)
20937 ga_append(&ga, tostr[p - fromstr]);
20938 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020939 ga_append(&ga, *in_str);
20940 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020941 }
20942 }
20943
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020944 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020945 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020946 ga_append(&ga, NUL);
20947
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020948 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020949}
20950
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020951#ifdef FEAT_FLOAT
20952/*
20953 * "trunc({float})" function
20954 */
20955 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020956f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020957{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020958 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020959
20960 rettv->v_type = VAR_FLOAT;
20961 if (get_float_arg(argvars, &f) == OK)
20962 /* trunc() is not in C90, use floor() or ceil() instead. */
20963 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20964 else
20965 rettv->vval.v_float = 0.0;
20966}
20967#endif
20968
Bram Moolenaar8299df92004-07-10 09:47:34 +000020969/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020970 * "type(expr)" function
20971 */
20972 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020973f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020974{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020975 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020976
20977 switch (argvars[0].v_type)
20978 {
20979 case VAR_NUMBER: n = 0; break;
20980 case VAR_STRING: n = 1; break;
20981 case VAR_FUNC: n = 2; break;
20982 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020983 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020984 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020985 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020986 if (argvars[0].vval.v_number == VVAL_FALSE
20987 || argvars[0].vval.v_number == VVAL_TRUE)
20988 n = 6;
20989 else
20990 n = 7;
20991 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020992 case VAR_JOB: n = 8; break;
20993 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020994 case VAR_UNKNOWN:
20995 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20996 n = -1;
20997 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020998 }
20999 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021000}
21001
21002/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021003 * "undofile(name)" function
21004 */
21005 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021006f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021007{
21008 rettv->v_type = VAR_STRING;
21009#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021010 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021011 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021012
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021013 if (*fname == NUL)
21014 {
21015 /* If there is no file name there will be no undo file. */
21016 rettv->vval.v_string = NULL;
21017 }
21018 else
21019 {
21020 char_u *ffname = FullName_save(fname, FALSE);
21021
21022 if (ffname != NULL)
21023 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
21024 vim_free(ffname);
21025 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021026 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021027#else
21028 rettv->vval.v_string = NULL;
21029#endif
21030}
21031
21032/*
Bram Moolenaara800b422010-06-27 01:15:55 +020021033 * "undotree()" function
21034 */
21035 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021036f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020021037{
21038 if (rettv_dict_alloc(rettv) == OK)
21039 {
21040 dict_T *dict = rettv->vval.v_dict;
21041 list_T *list;
21042
Bram Moolenaar730cde92010-06-27 05:18:54 +020021043 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021044 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021045 dict_add_nr_str(dict, "save_last",
21046 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021047 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
21048 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021049 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021050
21051 list = list_alloc();
21052 if (list != NULL)
21053 {
21054 u_eval_tree(curbuf->b_u_oldhead, list);
21055 dict_add_list(dict, "entries", list);
21056 }
21057 }
21058}
21059
21060/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000021061 * "values(dict)" function
21062 */
21063 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021064f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000021065{
21066 dict_list(argvars, rettv, 1);
21067}
21068
21069/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021070 * "virtcol(string)" function
21071 */
21072 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021073f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021074{
21075 colnr_T vcol = 0;
21076 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021077 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021078
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021079 fp = var2fpos(&argvars[0], FALSE, &fnum);
21080 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
21081 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021082 {
21083 getvvcol(curwin, fp, NULL, NULL, &vcol);
21084 ++vcol;
21085 }
21086
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021087 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021088}
21089
21090/*
21091 * "visualmode()" function
21092 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021093 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010021094f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021095{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021096 char_u str[2];
21097
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021098 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021099 str[0] = curbuf->b_visual_mode_eval;
21100 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021101 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021102
21103 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000021104 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021105 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021106}
21107
21108/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021109 * "wildmenumode()" function
21110 */
21111 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021112f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021113{
21114#ifdef FEAT_WILDMENU
21115 if (wild_menu_showing)
21116 rettv->vval.v_number = 1;
21117#endif
21118}
21119
21120/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021121 * "winbufnr(nr)" function
21122 */
21123 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021124f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021125{
21126 win_T *wp;
21127
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021128 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021129 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021130 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021131 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021132 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021133}
21134
21135/*
21136 * "wincol()" function
21137 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021138 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021139f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021140{
21141 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021142 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021143}
21144
21145/*
21146 * "winheight(nr)" function
21147 */
21148 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021149f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021150{
21151 win_T *wp;
21152
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021153 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021154 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021155 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021156 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021157 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021158}
21159
21160/*
21161 * "winline()" function
21162 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021163 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021164f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021165{
21166 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021167 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021168}
21169
21170/*
21171 * "winnr()" function
21172 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021173 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021174f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021175{
21176 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021177
Bram Moolenaar071d4272004-06-13 20:20:40 +000021178#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021179 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021180#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021181 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021182}
21183
21184/*
21185 * "winrestcmd()" function
21186 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021187 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021188f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021189{
21190#ifdef FEAT_WINDOWS
21191 win_T *wp;
21192 int winnr = 1;
21193 garray_T ga;
21194 char_u buf[50];
21195
21196 ga_init2(&ga, (int)sizeof(char), 70);
21197 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21198 {
21199 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21200 ga_concat(&ga, buf);
21201# ifdef FEAT_VERTSPLIT
21202 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21203 ga_concat(&ga, buf);
21204# endif
21205 ++winnr;
21206 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021207 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021208
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021209 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021210#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021211 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021212#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021213 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021214}
21215
21216/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021217 * "winrestview()" function
21218 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021219 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021220f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021221{
21222 dict_T *dict;
21223
21224 if (argvars[0].v_type != VAR_DICT
21225 || (dict = argvars[0].vval.v_dict) == NULL)
21226 EMSG(_(e_invarg));
21227 else
21228 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021229 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
21230 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
21231 if (dict_find(dict, (char_u *)"col", -1) != NULL)
21232 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021233#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021234 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
21235 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021236#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021237 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21238 {
21239 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
21240 curwin->w_set_curswant = FALSE;
21241 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021242
Bram Moolenaar82c25852014-05-28 16:47:16 +020021243 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
21244 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021245#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021246 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
21247 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021248#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021249 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
21250 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
21251 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
21252 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021253
21254 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021255 win_new_height(curwin, curwin->w_height);
21256# ifdef FEAT_VERTSPLIT
21257 win_new_width(curwin, W_WIDTH(curwin));
21258# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021259 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021260
Bram Moolenaarb851a962014-10-31 15:45:52 +010021261 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021262 curwin->w_topline = 1;
21263 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21264 curwin->w_topline = curbuf->b_ml.ml_line_count;
21265#ifdef FEAT_DIFF
21266 check_topfill(curwin, TRUE);
21267#endif
21268 }
21269}
21270
21271/*
21272 * "winsaveview()" function
21273 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021274 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021275f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021276{
21277 dict_T *dict;
21278
Bram Moolenaara800b422010-06-27 01:15:55 +020021279 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021280 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021281 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021282
21283 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21284 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21285#ifdef FEAT_VIRTUALEDIT
21286 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21287#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021288 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021289 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21290
21291 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21292#ifdef FEAT_DIFF
21293 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21294#endif
21295 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21296 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21297}
21298
21299/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021300 * "winwidth(nr)" function
21301 */
21302 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021303f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021304{
21305 win_T *wp;
21306
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021307 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021308 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021309 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021310 else
21311#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021312 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021313#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021314 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021315#endif
21316}
21317
Bram Moolenaar071d4272004-06-13 20:20:40 +000021318/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021319 * "wordcount()" function
21320 */
21321 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021322f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021323{
21324 if (rettv_dict_alloc(rettv) == FAIL)
21325 return;
21326 cursor_pos_info(rettv->vval.v_dict);
21327}
21328
21329/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021330 * Write list of strings to file
21331 */
21332 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021333write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021334{
21335 listitem_T *li;
21336 int c;
21337 int ret = OK;
21338 char_u *s;
21339
21340 for (li = list->lv_first; li != NULL; li = li->li_next)
21341 {
21342 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21343 {
21344 if (*s == '\n')
21345 c = putc(NUL, fd);
21346 else
21347 c = putc(*s, fd);
21348 if (c == EOF)
21349 {
21350 ret = FAIL;
21351 break;
21352 }
21353 }
21354 if (!binary || li->li_next != NULL)
21355 if (putc('\n', fd) == EOF)
21356 {
21357 ret = FAIL;
21358 break;
21359 }
21360 if (ret == FAIL)
21361 {
21362 EMSG(_(e_write));
21363 break;
21364 }
21365 }
21366 return ret;
21367}
21368
21369/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021370 * "writefile()" function
21371 */
21372 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021373f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021374{
21375 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021376 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021377 char_u *fname;
21378 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021379 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021380
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021381 if (check_restricted() || check_secure())
21382 return;
21383
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021384 if (argvars[0].v_type != VAR_LIST)
21385 {
21386 EMSG2(_(e_listarg), "writefile()");
21387 return;
21388 }
21389 if (argvars[0].vval.v_list == NULL)
21390 return;
21391
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021392 if (argvars[2].v_type != VAR_UNKNOWN)
21393 {
21394 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21395 binary = TRUE;
21396 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21397 append = TRUE;
21398 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021399
21400 /* Always open the file in binary mode, library functions have a mind of
21401 * their own about CR-LF conversion. */
21402 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021403 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21404 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021405 {
21406 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21407 ret = -1;
21408 }
21409 else
21410 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021411 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21412 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021413 fclose(fd);
21414 }
21415
21416 rettv->vval.v_number = ret;
21417}
21418
21419/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021420 * "xor(expr, expr)" function
21421 */
21422 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021423f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021424{
21425 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21426 ^ get_tv_number_chk(&argvars[1], NULL);
21427}
21428
21429
21430/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021431 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021432 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021433 */
21434 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021435var2fpos(
21436 typval_T *varp,
21437 int dollar_lnum, /* TRUE when $ is last line */
21438 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021439{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021440 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021441 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021442 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021443
Bram Moolenaara5525202006-03-02 22:52:09 +000021444 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021445 if (varp->v_type == VAR_LIST)
21446 {
21447 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021448 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021449 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021450 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021451
21452 l = varp->vval.v_list;
21453 if (l == NULL)
21454 return NULL;
21455
21456 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021457 pos.lnum = list_find_nr(l, 0L, &error);
21458 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021459 return NULL; /* invalid line number */
21460
21461 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021462 pos.col = list_find_nr(l, 1L, &error);
21463 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021464 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021465 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021466
21467 /* We accept "$" for the column number: last column. */
21468 li = list_find(l, 1L);
21469 if (li != NULL && li->li_tv.v_type == VAR_STRING
21470 && li->li_tv.vval.v_string != NULL
21471 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21472 pos.col = len + 1;
21473
Bram Moolenaara5525202006-03-02 22:52:09 +000021474 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021475 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021476 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021477 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021478
Bram Moolenaara5525202006-03-02 22:52:09 +000021479#ifdef FEAT_VIRTUALEDIT
21480 /* Get the virtual offset. Defaults to zero. */
21481 pos.coladd = list_find_nr(l, 2L, &error);
21482 if (error)
21483 pos.coladd = 0;
21484#endif
21485
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021486 return &pos;
21487 }
21488
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021489 name = get_tv_string_chk(varp);
21490 if (name == NULL)
21491 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021492 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021493 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021494 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21495 {
21496 if (VIsual_active)
21497 return &VIsual;
21498 return &curwin->w_cursor;
21499 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021500 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021501 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021502 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021503 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21504 return NULL;
21505 return pp;
21506 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021507
21508#ifdef FEAT_VIRTUALEDIT
21509 pos.coladd = 0;
21510#endif
21511
Bram Moolenaar477933c2007-07-17 14:32:23 +000021512 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021513 {
21514 pos.col = 0;
21515 if (name[1] == '0') /* "w0": first visible line */
21516 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021517 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021518 pos.lnum = curwin->w_topline;
21519 return &pos;
21520 }
21521 else if (name[1] == '$') /* "w$": last visible line */
21522 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021523 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021524 pos.lnum = curwin->w_botline - 1;
21525 return &pos;
21526 }
21527 }
21528 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021529 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021530 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021531 {
21532 pos.lnum = curbuf->b_ml.ml_line_count;
21533 pos.col = 0;
21534 }
21535 else
21536 {
21537 pos.lnum = curwin->w_cursor.lnum;
21538 pos.col = (colnr_T)STRLEN(ml_get_curline());
21539 }
21540 return &pos;
21541 }
21542 return NULL;
21543}
21544
21545/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021546 * Convert list in "arg" into a position and optional file number.
21547 * When "fnump" is NULL there is no file number, only 3 items.
21548 * Note that the column is passed on as-is, the caller may want to decrement
21549 * it to use 1 for the first column.
21550 * Return FAIL when conversion is not possible, doesn't check the position for
21551 * validity.
21552 */
21553 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021554list2fpos(
21555 typval_T *arg,
21556 pos_T *posp,
21557 int *fnump,
21558 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021559{
21560 list_T *l = arg->vval.v_list;
21561 long i = 0;
21562 long n;
21563
Bram Moolenaar493c1782014-05-28 14:34:46 +020021564 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21565 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021566 if (arg->v_type != VAR_LIST
21567 || l == NULL
21568 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021569 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021570 return FAIL;
21571
21572 if (fnump != NULL)
21573 {
21574 n = list_find_nr(l, i++, NULL); /* fnum */
21575 if (n < 0)
21576 return FAIL;
21577 if (n == 0)
21578 n = curbuf->b_fnum; /* current buffer */
21579 *fnump = n;
21580 }
21581
21582 n = list_find_nr(l, i++, NULL); /* lnum */
21583 if (n < 0)
21584 return FAIL;
21585 posp->lnum = n;
21586
21587 n = list_find_nr(l, i++, NULL); /* col */
21588 if (n < 0)
21589 return FAIL;
21590 posp->col = n;
21591
21592#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021593 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021594 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021595 posp->coladd = 0;
21596 else
21597 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021598#endif
21599
Bram Moolenaar493c1782014-05-28 14:34:46 +020021600 if (curswantp != NULL)
21601 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21602
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021603 return OK;
21604}
21605
21606/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021607 * Get the length of an environment variable name.
21608 * Advance "arg" to the first character after the name.
21609 * Return 0 for error.
21610 */
21611 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021612get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021613{
21614 char_u *p;
21615 int len;
21616
21617 for (p = *arg; vim_isIDc(*p); ++p)
21618 ;
21619 if (p == *arg) /* no name found */
21620 return 0;
21621
21622 len = (int)(p - *arg);
21623 *arg = p;
21624 return len;
21625}
21626
21627/*
21628 * Get the length of the name of a function or internal variable.
21629 * "arg" is advanced to the first non-white character after the name.
21630 * Return 0 if something is wrong.
21631 */
21632 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021633get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021634{
21635 char_u *p;
21636 int len;
21637
21638 /* Find the end of the name. */
21639 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021640 {
21641 if (*p == ':')
21642 {
21643 /* "s:" is start of "s:var", but "n:" is not and can be used in
21644 * slice "[n:]". Also "xx:" is not a namespace. */
21645 len = (int)(p - *arg);
21646 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21647 || len > 1)
21648 break;
21649 }
21650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021651 if (p == *arg) /* no name found */
21652 return 0;
21653
21654 len = (int)(p - *arg);
21655 *arg = skipwhite(p);
21656
21657 return len;
21658}
21659
21660/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021661 * Get the length of the name of a variable or function.
21662 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021663 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021664 * Return -1 if curly braces expansion failed.
21665 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021666 * If the name contains 'magic' {}'s, expand them and return the
21667 * expanded name in an allocated string via 'alias' - caller must free.
21668 */
21669 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021670get_name_len(
21671 char_u **arg,
21672 char_u **alias,
21673 int evaluate,
21674 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021675{
21676 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021677 char_u *p;
21678 char_u *expr_start;
21679 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021680
21681 *alias = NULL; /* default to no alias */
21682
21683 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21684 && (*arg)[2] == (int)KE_SNR)
21685 {
21686 /* hard coded <SNR>, already translated */
21687 *arg += 3;
21688 return get_id_len(arg) + 3;
21689 }
21690 len = eval_fname_script(*arg);
21691 if (len > 0)
21692 {
21693 /* literal "<SID>", "s:" or "<SNR>" */
21694 *arg += len;
21695 }
21696
Bram Moolenaar071d4272004-06-13 20:20:40 +000021697 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021698 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021699 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021700 p = find_name_end(*arg, &expr_start, &expr_end,
21701 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021702 if (expr_start != NULL)
21703 {
21704 char_u *temp_string;
21705
21706 if (!evaluate)
21707 {
21708 len += (int)(p - *arg);
21709 *arg = skipwhite(p);
21710 return len;
21711 }
21712
21713 /*
21714 * Include any <SID> etc in the expanded string:
21715 * Thus the -len here.
21716 */
21717 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21718 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021719 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021720 *alias = temp_string;
21721 *arg = skipwhite(p);
21722 return (int)STRLEN(temp_string);
21723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021724
21725 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021726 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021727 EMSG2(_(e_invexpr2), *arg);
21728
21729 return len;
21730}
21731
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021732/*
21733 * Find the end of a variable or function name, taking care of magic braces.
21734 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21735 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021736 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021737 * Return a pointer to just after the name. Equal to "arg" if there is no
21738 * valid name.
21739 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021740 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021741find_name_end(
21742 char_u *arg,
21743 char_u **expr_start,
21744 char_u **expr_end,
21745 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021746{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021747 int mb_nest = 0;
21748 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021749 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021750 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021751
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021752 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021753 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021754 *expr_start = NULL;
21755 *expr_end = NULL;
21756 }
21757
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021758 /* Quick check for valid starting character. */
21759 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21760 return arg;
21761
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021762 for (p = arg; *p != NUL
21763 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021764 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021765 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021766 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021767 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021768 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021769 if (*p == '\'')
21770 {
21771 /* skip over 'string' to avoid counting [ and ] inside it. */
21772 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21773 ;
21774 if (*p == NUL)
21775 break;
21776 }
21777 else if (*p == '"')
21778 {
21779 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21780 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21781 if (*p == '\\' && p[1] != NUL)
21782 ++p;
21783 if (*p == NUL)
21784 break;
21785 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021786 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21787 {
21788 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021789 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021790 len = (int)(p - arg);
21791 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021792 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021793 break;
21794 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021795
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021796 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021797 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021798 if (*p == '[')
21799 ++br_nest;
21800 else if (*p == ']')
21801 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021802 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021803
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021804 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021805 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021806 if (*p == '{')
21807 {
21808 mb_nest++;
21809 if (expr_start != NULL && *expr_start == NULL)
21810 *expr_start = p;
21811 }
21812 else if (*p == '}')
21813 {
21814 mb_nest--;
21815 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21816 *expr_end = p;
21817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021819 }
21820
21821 return p;
21822}
21823
21824/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021825 * Expands out the 'magic' {}'s in a variable/function name.
21826 * Note that this can call itself recursively, to deal with
21827 * constructs like foo{bar}{baz}{bam}
21828 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21829 * "in_start" ^
21830 * "expr_start" ^
21831 * "expr_end" ^
21832 * "in_end" ^
21833 *
21834 * Returns a new allocated string, which the caller must free.
21835 * Returns NULL for failure.
21836 */
21837 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021838make_expanded_name(
21839 char_u *in_start,
21840 char_u *expr_start,
21841 char_u *expr_end,
21842 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021843{
21844 char_u c1;
21845 char_u *retval = NULL;
21846 char_u *temp_result;
21847 char_u *nextcmd = NULL;
21848
21849 if (expr_end == NULL || in_end == NULL)
21850 return NULL;
21851 *expr_start = NUL;
21852 *expr_end = NUL;
21853 c1 = *in_end;
21854 *in_end = NUL;
21855
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021856 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021857 if (temp_result != NULL && nextcmd == NULL)
21858 {
21859 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21860 + (in_end - expr_end) + 1));
21861 if (retval != NULL)
21862 {
21863 STRCPY(retval, in_start);
21864 STRCAT(retval, temp_result);
21865 STRCAT(retval, expr_end + 1);
21866 }
21867 }
21868 vim_free(temp_result);
21869
21870 *in_end = c1; /* put char back for error messages */
21871 *expr_start = '{';
21872 *expr_end = '}';
21873
21874 if (retval != NULL)
21875 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021876 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021877 if (expr_start != NULL)
21878 {
21879 /* Further expansion! */
21880 temp_result = make_expanded_name(retval, expr_start,
21881 expr_end, temp_result);
21882 vim_free(retval);
21883 retval = temp_result;
21884 }
21885 }
21886
21887 return retval;
21888}
21889
21890/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021891 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021892 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021893 */
21894 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021895eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021896{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021897 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21898}
21899
21900/*
21901 * Return TRUE if character "c" can be used as the first character in a
21902 * variable or function name (excluding '{' and '}').
21903 */
21904 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021905eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021906{
21907 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021908}
21909
21910/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021911 * Set number v: variable to "val".
21912 */
21913 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021914set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021915{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021916 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021917}
21918
21919/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021920 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021921 */
21922 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021923get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021924{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021925 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021926}
21927
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021928/*
21929 * Get string v: variable value. Uses a static buffer, can only be used once.
21930 */
21931 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021932get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021933{
21934 return get_tv_string(&vimvars[idx].vv_tv);
21935}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021936
Bram Moolenaar071d4272004-06-13 20:20:40 +000021937/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021938 * Get List v: variable value. Caller must take care of reference count when
21939 * needed.
21940 */
21941 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021942get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021943{
21944 return vimvars[idx].vv_list;
21945}
21946
21947/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021948 * Set v:char to character "c".
21949 */
21950 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021951set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021952{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021953 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021954
21955#ifdef FEAT_MBYTE
21956 if (has_mbyte)
21957 buf[(*mb_char2bytes)(c, buf)] = NUL;
21958 else
21959#endif
21960 {
21961 buf[0] = c;
21962 buf[1] = NUL;
21963 }
21964 set_vim_var_string(VV_CHAR, buf, -1);
21965}
21966
21967/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021968 * Set v:count to "count" and v:count1 to "count1".
21969 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021970 */
21971 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021972set_vcount(
21973 long count,
21974 long count1,
21975 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021976{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021977 if (set_prevcount)
21978 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021979 vimvars[VV_COUNT].vv_nr = count;
21980 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021981}
21982
21983/*
21984 * Set string v: variable to a copy of "val".
21985 */
21986 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021987set_vim_var_string(
21988 int idx,
21989 char_u *val,
21990 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021991{
Bram Moolenaara542c682016-01-31 16:28:04 +010021992 clear_tv(&vimvars[idx].vv_di.di_tv);
21993 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021994 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021995 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021996 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021997 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021998 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021999 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022000}
22001
22002/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022003 * Set List v: variable to "val".
22004 */
22005 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022006set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000022007{
Bram Moolenaara542c682016-01-31 16:28:04 +010022008 clear_tv(&vimvars[idx].vv_di.di_tv);
22009 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000022010 vimvars[idx].vv_list = val;
22011 if (val != NULL)
22012 ++val->lv_refcount;
22013}
22014
22015/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020022016 * Set Dictionary v: variable to "val".
22017 */
22018 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022019set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020022020{
22021 int todo;
22022 hashitem_T *hi;
22023
Bram Moolenaara542c682016-01-31 16:28:04 +010022024 clear_tv(&vimvars[idx].vv_di.di_tv);
22025 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020022026 vimvars[idx].vv_dict = val;
22027 if (val != NULL)
22028 {
22029 ++val->dv_refcount;
22030
22031 /* Set readonly */
22032 todo = (int)val->dv_hashtab.ht_used;
22033 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
22034 {
22035 if (HASHITEM_EMPTY(hi))
22036 continue;
22037 --todo;
22038 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22039 }
22040 }
22041}
22042
22043/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022044 * Set v:register if needed.
22045 */
22046 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022047set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022048{
22049 char_u regname;
22050
22051 if (c == 0 || c == ' ')
22052 regname = '"';
22053 else
22054 regname = c;
22055 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000022056 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022057 set_vim_var_string(VV_REG, &regname, 1);
22058}
22059
22060/*
22061 * Get or set v:exception. If "oldval" == NULL, return the current value.
22062 * Otherwise, restore the value to "oldval" and return NULL.
22063 * Must always be called in pairs to save and restore v:exception! Does not
22064 * take care of memory allocations.
22065 */
22066 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022067v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022068{
22069 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022070 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022071
Bram Moolenaare9a41262005-01-15 22:18:47 +000022072 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022073 return NULL;
22074}
22075
22076/*
22077 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
22078 * Otherwise, restore the value to "oldval" and return NULL.
22079 * Must always be called in pairs to save and restore v:throwpoint! Does not
22080 * take care of memory allocations.
22081 */
22082 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022083v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022084{
22085 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022086 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022087
Bram Moolenaare9a41262005-01-15 22:18:47 +000022088 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022089 return NULL;
22090}
22091
22092#if defined(FEAT_AUTOCMD) || defined(PROTO)
22093/*
22094 * Set v:cmdarg.
22095 * If "eap" != NULL, use "eap" to generate the value and return the old value.
22096 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
22097 * Must always be called in pairs!
22098 */
22099 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022100set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022101{
22102 char_u *oldval;
22103 char_u *newval;
22104 unsigned len;
22105
Bram Moolenaare9a41262005-01-15 22:18:47 +000022106 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022107 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022108 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022109 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000022110 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022111 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022112 }
22113
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022114 if (eap->force_bin == FORCE_BIN)
22115 len = 6;
22116 else if (eap->force_bin == FORCE_NOBIN)
22117 len = 8;
22118 else
22119 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022120
22121 if (eap->read_edit)
22122 len += 7;
22123
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022124 if (eap->force_ff != 0)
22125 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
22126# ifdef FEAT_MBYTE
22127 if (eap->force_enc != 0)
22128 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022129 if (eap->bad_char != 0)
22130 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022131# endif
22132
22133 newval = alloc(len + 1);
22134 if (newval == NULL)
22135 return NULL;
22136
22137 if (eap->force_bin == FORCE_BIN)
22138 sprintf((char *)newval, " ++bin");
22139 else if (eap->force_bin == FORCE_NOBIN)
22140 sprintf((char *)newval, " ++nobin");
22141 else
22142 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022143
22144 if (eap->read_edit)
22145 STRCAT(newval, " ++edit");
22146
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022147 if (eap->force_ff != 0)
22148 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
22149 eap->cmd + eap->force_ff);
22150# ifdef FEAT_MBYTE
22151 if (eap->force_enc != 0)
22152 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
22153 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022154 if (eap->bad_char == BAD_KEEP)
22155 STRCPY(newval + STRLEN(newval), " ++bad=keep");
22156 else if (eap->bad_char == BAD_DROP)
22157 STRCPY(newval + STRLEN(newval), " ++bad=drop");
22158 else if (eap->bad_char != 0)
22159 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022160# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022161 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022162 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022163}
22164#endif
22165
22166/*
22167 * Get the value of internal variable "name".
22168 * Return OK or FAIL.
22169 */
22170 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022171get_var_tv(
22172 char_u *name,
22173 int len, /* length of "name" */
22174 typval_T *rettv, /* NULL when only checking existence */
22175 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
22176 int verbose, /* may give error message */
22177 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022178{
22179 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000022180 typval_T *tv = NULL;
22181 typval_T atv;
22182 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022183 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022184
22185 /* truncate the name, so that we can use strcmp() */
22186 cc = name[len];
22187 name[len] = NUL;
22188
22189 /*
22190 * Check for "b:changedtick".
22191 */
22192 if (STRCMP(name, "b:changedtick") == 0)
22193 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022194 atv.v_type = VAR_NUMBER;
22195 atv.vval.v_number = curbuf->b_changedtick;
22196 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022197 }
22198
22199 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022200 * Check for user-defined variables.
22201 */
22202 else
22203 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022204 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022205 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022206 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022207 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022208 if (dip != NULL)
22209 *dip = v;
22210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022211 }
22212
Bram Moolenaare9a41262005-01-15 22:18:47 +000022213 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022214 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022215 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022216 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022217 ret = FAIL;
22218 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022219 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022220 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022221
22222 name[len] = cc;
22223
22224 return ret;
22225}
22226
22227/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022228 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22229 * Also handle function call with Funcref variable: func(expr)
22230 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22231 */
22232 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022233handle_subscript(
22234 char_u **arg,
22235 typval_T *rettv,
22236 int evaluate, /* do more than finding the end */
22237 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022238{
22239 int ret = OK;
22240 dict_T *selfdict = NULL;
22241 char_u *s;
22242 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022243 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022244
22245 while (ret == OK
22246 && (**arg == '['
22247 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022248 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022249 && !vim_iswhite(*(*arg - 1)))
22250 {
22251 if (**arg == '(')
22252 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000022253 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022254 if (evaluate)
22255 {
22256 functv = *rettv;
22257 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022258
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022259 /* Invoke the function. Recursive! */
22260 s = functv.vval.v_string;
22261 }
22262 else
22263 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022264 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022265 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
22266 &len, evaluate, selfdict);
22267
22268 /* Clear the funcref afterwards, so that deleting it while
22269 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022270 if (evaluate)
22271 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022272
22273 /* Stop the expression evaluation when immediately aborting on
22274 * error, or when an interrupt occurred or an exception was thrown
22275 * but not caught. */
22276 if (aborting())
22277 {
22278 if (ret == OK)
22279 clear_tv(rettv);
22280 ret = FAIL;
22281 }
22282 dict_unref(selfdict);
22283 selfdict = NULL;
22284 }
22285 else /* **arg == '[' || **arg == '.' */
22286 {
22287 dict_unref(selfdict);
22288 if (rettv->v_type == VAR_DICT)
22289 {
22290 selfdict = rettv->vval.v_dict;
22291 if (selfdict != NULL)
22292 ++selfdict->dv_refcount;
22293 }
22294 else
22295 selfdict = NULL;
22296 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22297 {
22298 clear_tv(rettv);
22299 ret = FAIL;
22300 }
22301 }
22302 }
22303 dict_unref(selfdict);
22304 return ret;
22305}
22306
22307/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022308 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022309 * value).
22310 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022311 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022312alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022313{
Bram Moolenaar33570922005-01-25 22:26:29 +000022314 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022315}
22316
22317/*
22318 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022319 * The string "s" must have been allocated, it is consumed.
22320 * Return NULL for out of memory, the variable otherwise.
22321 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022322 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022323alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022324{
Bram Moolenaar33570922005-01-25 22:26:29 +000022325 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022326
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022327 rettv = alloc_tv();
22328 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022329 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022330 rettv->v_type = VAR_STRING;
22331 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022332 }
22333 else
22334 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022335 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022336}
22337
22338/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022339 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022340 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022341 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022342free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022343{
22344 if (varp != NULL)
22345 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022346 switch (varp->v_type)
22347 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022348 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022349 func_unref(varp->vval.v_string);
22350 /*FALLTHROUGH*/
22351 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022352 vim_free(varp->vval.v_string);
22353 break;
22354 case VAR_LIST:
22355 list_unref(varp->vval.v_list);
22356 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022357 case VAR_DICT:
22358 dict_unref(varp->vval.v_dict);
22359 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022360 case VAR_JOB:
22361#ifdef FEAT_JOB
22362 job_unref(varp->vval.v_job);
22363 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022364#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022365 case VAR_CHANNEL:
22366#ifdef FEAT_CHANNEL
22367 channel_unref(varp->vval.v_channel);
22368 break;
22369#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022370 case VAR_NUMBER:
22371 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022372 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022373 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022374 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022376 vim_free(varp);
22377 }
22378}
22379
22380/*
22381 * Free the memory for a variable value and set the value to NULL or 0.
22382 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022383 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022384clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022385{
22386 if (varp != NULL)
22387 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022388 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022389 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022390 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022391 func_unref(varp->vval.v_string);
22392 /*FALLTHROUGH*/
22393 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022394 vim_free(varp->vval.v_string);
22395 varp->vval.v_string = NULL;
22396 break;
22397 case VAR_LIST:
22398 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022399 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022400 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022401 case VAR_DICT:
22402 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022403 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022404 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022405 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022406 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022407 varp->vval.v_number = 0;
22408 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022409 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022410#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022411 varp->vval.v_float = 0.0;
22412 break;
22413#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022414 case VAR_JOB:
22415#ifdef FEAT_JOB
22416 job_unref(varp->vval.v_job);
22417 varp->vval.v_job = NULL;
22418#endif
22419 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022420 case VAR_CHANNEL:
22421#ifdef FEAT_CHANNEL
22422 channel_unref(varp->vval.v_channel);
22423 varp->vval.v_channel = NULL;
22424#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022425 case VAR_UNKNOWN:
22426 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022427 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022428 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022429 }
22430}
22431
22432/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022433 * Set the value of a variable to NULL without freeing items.
22434 */
22435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022436init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022437{
22438 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022439 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022440}
22441
22442/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022443 * Get the number value of a variable.
22444 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022445 * For incompatible types, return 0.
22446 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22447 * caller of incompatible types: it sets *denote to TRUE if "denote"
22448 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022449 */
22450 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022451get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022452{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022453 int error = FALSE;
22454
22455 return get_tv_number_chk(varp, &error); /* return 0L on error */
22456}
22457
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022458 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022459get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022460{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022461 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022462
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022463 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022464 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022465 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022466 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022467 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022468#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022469 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022470 break;
22471#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022472 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022473 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022474 break;
22475 case VAR_STRING:
22476 if (varp->vval.v_string != NULL)
22477 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022478 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022479 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022480 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022481 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022482 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022483 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022484 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022485 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022486 case VAR_SPECIAL:
22487 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22488 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022489 case VAR_JOB:
22490#ifdef FEAT_JOB
22491 EMSG(_("E910: Using a Job as a Number"));
22492 break;
22493#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022494 case VAR_CHANNEL:
22495#ifdef FEAT_CHANNEL
22496 EMSG(_("E913: Using a Channel as a Number"));
22497 break;
22498#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022499 case VAR_UNKNOWN:
22500 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022501 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022502 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022503 if (denote == NULL) /* useful for values that must be unsigned */
22504 n = -1;
22505 else
22506 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022507 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022508}
22509
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022510#ifdef FEAT_FLOAT
22511 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022512get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022513{
22514 switch (varp->v_type)
22515 {
22516 case VAR_NUMBER:
22517 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022518 case VAR_FLOAT:
22519 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022520 case VAR_FUNC:
22521 EMSG(_("E891: Using a Funcref as a Float"));
22522 break;
22523 case VAR_STRING:
22524 EMSG(_("E892: Using a String as a Float"));
22525 break;
22526 case VAR_LIST:
22527 EMSG(_("E893: Using a List as a Float"));
22528 break;
22529 case VAR_DICT:
22530 EMSG(_("E894: Using a Dictionary as a Float"));
22531 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022532 case VAR_SPECIAL:
22533 EMSG(_("E907: Using a special value as a Float"));
22534 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022535 case VAR_JOB:
22536# ifdef FEAT_JOB
22537 EMSG(_("E911: Using a Job as a Float"));
22538 break;
22539# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022540 case VAR_CHANNEL:
22541# ifdef FEAT_CHANNEL
22542 EMSG(_("E914: Using a Channel as a Float"));
22543 break;
22544# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022545 case VAR_UNKNOWN:
22546 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022547 break;
22548 }
22549 return 0;
22550}
22551#endif
22552
Bram Moolenaar071d4272004-06-13 20:20:40 +000022553/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022554 * Get the lnum from the first argument.
22555 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022556 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022557 */
22558 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022559get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022560{
Bram Moolenaar33570922005-01-25 22:26:29 +000022561 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022562 linenr_T lnum;
22563
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022564 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022565 if (lnum == 0) /* no valid number, try using line() */
22566 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022567 rettv.v_type = VAR_NUMBER;
22568 f_line(argvars, &rettv);
22569 lnum = rettv.vval.v_number;
22570 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022571 }
22572 return lnum;
22573}
22574
22575/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022576 * Get the lnum from the first argument.
22577 * Also accepts "$", then "buf" is used.
22578 * Returns 0 on error.
22579 */
22580 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022581get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022582{
22583 if (argvars[0].v_type == VAR_STRING
22584 && argvars[0].vval.v_string != NULL
22585 && argvars[0].vval.v_string[0] == '$'
22586 && buf != NULL)
22587 return buf->b_ml.ml_line_count;
22588 return get_tv_number_chk(&argvars[0], NULL);
22589}
22590
22591/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022592 * Get the string value of a variable.
22593 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022594 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22595 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022596 * If the String variable has never been set, return an empty string.
22597 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022598 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22599 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022600 */
22601 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022602get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022603{
22604 static char_u mybuf[NUMBUFLEN];
22605
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022606 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022607}
22608
22609 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022610get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022611{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022612 char_u *res = get_tv_string_buf_chk(varp, buf);
22613
22614 return res != NULL ? res : (char_u *)"";
22615}
22616
Bram Moolenaar7d647822014-04-05 21:28:56 +020022617/*
22618 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22619 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022620 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022621get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022622{
22623 static char_u mybuf[NUMBUFLEN];
22624
22625 return get_tv_string_buf_chk(varp, mybuf);
22626}
22627
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022628 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022629get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022630{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022631 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022632 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022633 case VAR_NUMBER:
22634 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22635 return buf;
22636 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022637 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022638 break;
22639 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022640 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022641 break;
22642 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022643 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022644 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022645 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022646#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022647 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022648 break;
22649#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022650 case VAR_STRING:
22651 if (varp->vval.v_string != NULL)
22652 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022653 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022654 case VAR_SPECIAL:
22655 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22656 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022657 case VAR_JOB:
22658#ifdef FEAT_JOB
22659 {
22660 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022661 char *status;
22662
22663 if (job == NULL)
22664 return (char_u *)"no process";
22665 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022666 : job->jv_status == JOB_ENDED ? "dead"
22667 : "run";
22668# ifdef UNIX
22669 vim_snprintf((char *)buf, NUMBUFLEN,
22670 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022671# elif defined(WIN32)
22672 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022673 "process %ld %s",
22674 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022675 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022676# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022677 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022678 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22679# endif
22680 return buf;
22681 }
22682#endif
22683 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022684 case VAR_CHANNEL:
22685#ifdef FEAT_CHANNEL
22686 {
22687 channel_T *channel = varp->vval.v_channel;
22688 char *status = channel_status(channel);
22689
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022690 if (channel == NULL)
22691 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22692 else
22693 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022694 "channel %d %s", channel->ch_id, status);
22695 return buf;
22696 }
22697#endif
22698 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022699 case VAR_UNKNOWN:
22700 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022701 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022702 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022703 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022704}
22705
22706/*
22707 * Find variable "name" in the list of variables.
22708 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022709 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022710 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022711 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022712 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022713 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022714find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022715{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022716 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022717 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022718
Bram Moolenaara7043832005-01-21 11:56:39 +000022719 ht = find_var_ht(name, &varname);
22720 if (htp != NULL)
22721 *htp = ht;
22722 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022723 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022724 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022725}
22726
22727/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022728 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022729 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022730 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022731 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022732find_var_in_ht(
22733 hashtab_T *ht,
22734 int htname,
22735 char_u *varname,
22736 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022737{
Bram Moolenaar33570922005-01-25 22:26:29 +000022738 hashitem_T *hi;
22739
22740 if (*varname == NUL)
22741 {
22742 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022743 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022744 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022745 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022746 case 'g': return &globvars_var;
22747 case 'v': return &vimvars_var;
22748 case 'b': return &curbuf->b_bufvar;
22749 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022750#ifdef FEAT_WINDOWS
22751 case 't': return &curtab->tp_winvar;
22752#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022753 case 'l': return current_funccal == NULL
22754 ? NULL : &current_funccal->l_vars_var;
22755 case 'a': return current_funccal == NULL
22756 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022757 }
22758 return NULL;
22759 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022760
22761 hi = hash_find(ht, varname);
22762 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022763 {
22764 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022765 * worked find the variable again. Don't auto-load a script if it was
22766 * loaded already, otherwise it would be loaded every time when
22767 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022768 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022769 {
22770 /* Note: script_autoload() may make "hi" invalid. It must either
22771 * be obtained again or not used. */
22772 if (!script_autoload(varname, FALSE) || aborting())
22773 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022774 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022775 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022776 if (HASHITEM_EMPTY(hi))
22777 return NULL;
22778 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022779 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022780}
22781
22782/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022783 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022784 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022785 * Set "varname" to the start of name without ':'.
22786 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022787 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022788find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022789{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022790 hashitem_T *hi;
22791
Bram Moolenaar73627d02015-08-11 15:46:09 +020022792 if (name[0] == NUL)
22793 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022794 if (name[1] != ':')
22795 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022796 /* The name must not start with a colon or #. */
22797 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022798 return NULL;
22799 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022800
22801 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022802 hi = hash_find(&compat_hashtab, name);
22803 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022804 return &compat_hashtab;
22805
Bram Moolenaar071d4272004-06-13 20:20:40 +000022806 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022807 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022808 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022809 }
22810 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022811 if (*name == 'g') /* global variable */
22812 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022813 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22814 */
22815 if (vim_strchr(name + 2, ':') != NULL
22816 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022817 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022818 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022819 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022820 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022821 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022822#ifdef FEAT_WINDOWS
22823 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022824 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022825#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022826 if (*name == 'v') /* v: variable */
22827 return &vimvarht;
22828 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022829 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022830 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022831 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022832 if (*name == 's' /* script variable */
22833 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22834 return &SCRIPT_VARS(current_SID);
22835 return NULL;
22836}
22837
22838/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022839 * Get function call environment based on bactrace debug level
22840 */
22841 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022842get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022843{
22844 int i;
22845 funccall_T *funccal;
22846 funccall_T *temp_funccal;
22847
22848 funccal = current_funccal;
22849 if (debug_backtrace_level > 0)
22850 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022851 for (i = 0; i < debug_backtrace_level; i++)
22852 {
22853 temp_funccal = funccal->caller;
22854 if (temp_funccal)
22855 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022856 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022857 /* backtrace level overflow. reset to max */
22858 debug_backtrace_level = i;
22859 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022860 }
22861 return funccal;
22862}
22863
22864/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022865 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022866 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022867 * Returns NULL when it doesn't exist.
22868 */
22869 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022870get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022871{
Bram Moolenaar33570922005-01-25 22:26:29 +000022872 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022873
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022874 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022875 if (v == NULL)
22876 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022877 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022878}
22879
22880/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022881 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022882 * sourcing this script and when executing functions defined in the script.
22883 */
22884 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022885new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022886{
Bram Moolenaara7043832005-01-21 11:56:39 +000022887 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022888 hashtab_T *ht;
22889 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022890
Bram Moolenaar071d4272004-06-13 20:20:40 +000022891 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22892 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022893 /* Re-allocating ga_data means that an ht_array pointing to
22894 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022895 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022896 for (i = 1; i <= ga_scripts.ga_len; ++i)
22897 {
22898 ht = &SCRIPT_VARS(i);
22899 if (ht->ht_mask == HT_INIT_SIZE - 1)
22900 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022901 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022902 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022903 }
22904
Bram Moolenaar071d4272004-06-13 20:20:40 +000022905 while (ga_scripts.ga_len < id)
22906 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022907 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022908 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022909 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022910 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022911 }
22912 }
22913}
22914
22915/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022916 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22917 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022918 */
22919 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022920init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022921{
Bram Moolenaar33570922005-01-25 22:26:29 +000022922 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022923 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022924 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022925 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022926 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022927 dict_var->di_tv.vval.v_dict = dict;
22928 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022929 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022930 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22931 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022932}
22933
22934/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022935 * Unreference a dictionary initialized by init_var_dict().
22936 */
22937 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022938unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022939{
22940 /* Now the dict needs to be freed if no one else is using it, go back to
22941 * normal reference counting. */
22942 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22943 dict_unref(dict);
22944}
22945
22946/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022947 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022948 * Frees all allocated variables and the value they contain.
22949 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022950 */
22951 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022952vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022953{
22954 vars_clear_ext(ht, TRUE);
22955}
22956
22957/*
22958 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22959 */
22960 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022961vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022962{
Bram Moolenaara7043832005-01-21 11:56:39 +000022963 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022964 hashitem_T *hi;
22965 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022966
Bram Moolenaar33570922005-01-25 22:26:29 +000022967 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022968 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022969 for (hi = ht->ht_array; todo > 0; ++hi)
22970 {
22971 if (!HASHITEM_EMPTY(hi))
22972 {
22973 --todo;
22974
Bram Moolenaar33570922005-01-25 22:26:29 +000022975 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022976 * ht_array might change then. hash_clear() takes care of it
22977 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022978 v = HI2DI(hi);
22979 if (free_val)
22980 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022981 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022982 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022983 }
22984 }
22985 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022986 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022987}
22988
Bram Moolenaara7043832005-01-21 11:56:39 +000022989/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022990 * Delete a variable from hashtab "ht" at item "hi".
22991 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022992 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022993 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022994delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022995{
Bram Moolenaar33570922005-01-25 22:26:29 +000022996 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022997
22998 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022999 clear_tv(&di->di_tv);
23000 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023001}
23002
23003/*
23004 * List the value of one internal variable.
23005 */
23006 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023007list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023008{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023009 char_u *tofree;
23010 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023011 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023012
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023013 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000023014 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023015 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023016 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023017}
23018
Bram Moolenaar071d4272004-06-13 20:20:40 +000023019 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023020list_one_var_a(
23021 char_u *prefix,
23022 char_u *name,
23023 int type,
23024 char_u *string,
23025 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023026{
Bram Moolenaar31859182007-08-14 20:41:13 +000023027 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
23028 msg_start();
23029 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023030 if (name != NULL) /* "a:" vars don't have a name stored */
23031 msg_puts(name);
23032 msg_putchar(' ');
23033 msg_advance(22);
23034 if (type == VAR_NUMBER)
23035 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023036 else if (type == VAR_FUNC)
23037 msg_putchar('*');
23038 else if (type == VAR_LIST)
23039 {
23040 msg_putchar('[');
23041 if (*string == '[')
23042 ++string;
23043 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000023044 else if (type == VAR_DICT)
23045 {
23046 msg_putchar('{');
23047 if (*string == '{')
23048 ++string;
23049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023050 else
23051 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023052
Bram Moolenaar071d4272004-06-13 20:20:40 +000023053 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023054
23055 if (type == VAR_FUNC)
23056 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023057 if (*first)
23058 {
23059 msg_clr_eos();
23060 *first = FALSE;
23061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023062}
23063
23064/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023065 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023066 * If the variable already exists, the value is updated.
23067 * Otherwise the variable is created.
23068 */
23069 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023070set_var(
23071 char_u *name,
23072 typval_T *tv,
23073 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023074{
Bram Moolenaar33570922005-01-25 22:26:29 +000023075 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023076 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023077 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023078
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023079 ht = find_var_ht(name, &varname);
23080 if (ht == NULL || *varname == NUL)
23081 {
23082 EMSG2(_(e_illvar), name);
23083 return;
23084 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020023085 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023086
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023087 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
23088 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023089
Bram Moolenaar33570922005-01-25 22:26:29 +000023090 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023091 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023092 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023093 if (var_check_ro(v->di_flags, name, FALSE)
23094 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000023095 return;
23096 if (v->di_tv.v_type != tv->v_type
23097 && !((v->di_tv.v_type == VAR_STRING
23098 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023099 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023100 || tv->v_type == VAR_NUMBER))
23101#ifdef FEAT_FLOAT
23102 && !((v->di_tv.v_type == VAR_NUMBER
23103 || v->di_tv.v_type == VAR_FLOAT)
23104 && (tv->v_type == VAR_NUMBER
23105 || tv->v_type == VAR_FLOAT))
23106#endif
23107 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023108 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000023109 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023110 return;
23111 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023112
23113 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023114 * Handle setting internal v: variables separately where needed to
23115 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000023116 */
23117 if (ht == &vimvarht)
23118 {
23119 if (v->di_tv.v_type == VAR_STRING)
23120 {
23121 vim_free(v->di_tv.vval.v_string);
23122 if (copy || tv->v_type != VAR_STRING)
23123 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
23124 else
23125 {
23126 /* Take over the string to avoid an extra alloc/free. */
23127 v->di_tv.vval.v_string = tv->vval.v_string;
23128 tv->vval.v_string = NULL;
23129 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023130 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023131 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023132 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023133 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023134 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023135 if (STRCMP(varname, "searchforward") == 0)
23136 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010023137#ifdef FEAT_SEARCH_EXTRA
23138 else if (STRCMP(varname, "hlsearch") == 0)
23139 {
23140 no_hlsearch = !v->di_tv.vval.v_number;
23141 redraw_all_later(SOME_VALID);
23142 }
23143#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023144 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023145 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023146 else if (v->di_tv.v_type != tv->v_type)
23147 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000023148 }
23149
23150 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023151 }
23152 else /* add a new variable */
23153 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000023154 /* Can't add "v:" variable. */
23155 if (ht == &vimvarht)
23156 {
23157 EMSG2(_(e_illvar), name);
23158 return;
23159 }
23160
Bram Moolenaar92124a32005-06-17 22:03:40 +000023161 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023162 if (!valid_varname(varname))
23163 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000023164
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023165 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23166 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000023167 if (v == NULL)
23168 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023169 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023170 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023171 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023172 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023173 return;
23174 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023175 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023176 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023177
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023178 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023179 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023180 else
23181 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023182 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023183 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023184 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023186}
23187
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023188/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023189 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023190 * Also give an error message.
23191 */
23192 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023193var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023194{
23195 if (flags & DI_FLAGS_RO)
23196 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023197 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023198 return TRUE;
23199 }
23200 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23201 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023202 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023203 return TRUE;
23204 }
23205 return FALSE;
23206}
23207
23208/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023209 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23210 * Also give an error message.
23211 */
23212 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023213var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023214{
23215 if (flags & DI_FLAGS_FIX)
23216 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023217 EMSG2(_("E795: Cannot delete variable %s"),
23218 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023219 return TRUE;
23220 }
23221 return FALSE;
23222}
23223
23224/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023225 * Check if a funcref is assigned to a valid variable name.
23226 * Return TRUE and give an error if not.
23227 */
23228 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023229var_check_func_name(
23230 char_u *name, /* points to start of variable name */
23231 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023232{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023233 /* Allow for w: b: s: and t:. */
23234 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023235 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23236 ? name[2] : name[0]))
23237 {
23238 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23239 name);
23240 return TRUE;
23241 }
23242 /* Don't allow hiding a function. When "v" is not NULL we might be
23243 * assigning another function to the same var, the type is checked
23244 * below. */
23245 if (new_var && function_exists(name))
23246 {
23247 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23248 name);
23249 return TRUE;
23250 }
23251 return FALSE;
23252}
23253
23254/*
23255 * Check if a variable name is valid.
23256 * Return FALSE and give an error if not.
23257 */
23258 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023259valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023260{
23261 char_u *p;
23262
23263 for (p = varname; *p != NUL; ++p)
23264 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23265 && *p != AUTOLOAD_CHAR)
23266 {
23267 EMSG2(_(e_illvar), varname);
23268 return FALSE;
23269 }
23270 return TRUE;
23271}
23272
23273/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023274 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023275 * Also give an error message, using "name" or _("name") when use_gettext is
23276 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023277 */
23278 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023279tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023280{
23281 if (lock & VAR_LOCKED)
23282 {
23283 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023284 name == NULL ? (char_u *)_("Unknown")
23285 : use_gettext ? (char_u *)_(name)
23286 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023287 return TRUE;
23288 }
23289 if (lock & VAR_FIXED)
23290 {
23291 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023292 name == NULL ? (char_u *)_("Unknown")
23293 : use_gettext ? (char_u *)_(name)
23294 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023295 return TRUE;
23296 }
23297 return FALSE;
23298}
23299
23300/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023301 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023302 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023303 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023304 * It is OK for "from" and "to" to point to the same item. This is used to
23305 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023306 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023307 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023308copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023309{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023310 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023311 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023312 switch (from->v_type)
23313 {
23314 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023315 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023316 to->vval.v_number = from->vval.v_number;
23317 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023318 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023319#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023320 to->vval.v_float = from->vval.v_float;
23321 break;
23322#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023323 case VAR_JOB:
Bram Moolenaarcb4b0122016-02-07 14:53:21 +010023324#ifdef FEAT_JOB
Bram Moolenaar835dc632016-02-07 14:27:38 +010023325 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023326 if (to->vval.v_job != NULL)
23327 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023328 break;
23329#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023330 case VAR_CHANNEL:
23331#ifdef FEAT_CHANNEL
23332 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023333 if (to->vval.v_channel != NULL)
23334 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023335 break;
23336#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023337 case VAR_STRING:
23338 case VAR_FUNC:
23339 if (from->vval.v_string == NULL)
23340 to->vval.v_string = NULL;
23341 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023342 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023343 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023344 if (from->v_type == VAR_FUNC)
23345 func_ref(to->vval.v_string);
23346 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023347 break;
23348 case VAR_LIST:
23349 if (from->vval.v_list == NULL)
23350 to->vval.v_list = NULL;
23351 else
23352 {
23353 to->vval.v_list = from->vval.v_list;
23354 ++to->vval.v_list->lv_refcount;
23355 }
23356 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023357 case VAR_DICT:
23358 if (from->vval.v_dict == NULL)
23359 to->vval.v_dict = NULL;
23360 else
23361 {
23362 to->vval.v_dict = from->vval.v_dict;
23363 ++to->vval.v_dict->dv_refcount;
23364 }
23365 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023366 case VAR_UNKNOWN:
23367 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023368 break;
23369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023370}
23371
23372/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023373 * Make a copy of an item.
23374 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023375 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23376 * reference to an already copied list/dict can be used.
23377 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023378 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023379 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023380item_copy(
23381 typval_T *from,
23382 typval_T *to,
23383 int deep,
23384 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023385{
23386 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023387 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023388
Bram Moolenaar33570922005-01-25 22:26:29 +000023389 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023390 {
23391 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023392 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023393 }
23394 ++recurse;
23395
23396 switch (from->v_type)
23397 {
23398 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023399 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023400 case VAR_STRING:
23401 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010023402 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023403 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023404 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023405 copy_tv(from, to);
23406 break;
23407 case VAR_LIST:
23408 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023409 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023410 if (from->vval.v_list == NULL)
23411 to->vval.v_list = NULL;
23412 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23413 {
23414 /* use the copy made earlier */
23415 to->vval.v_list = from->vval.v_list->lv_copylist;
23416 ++to->vval.v_list->lv_refcount;
23417 }
23418 else
23419 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23420 if (to->vval.v_list == NULL)
23421 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023422 break;
23423 case VAR_DICT:
23424 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023425 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023426 if (from->vval.v_dict == NULL)
23427 to->vval.v_dict = NULL;
23428 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23429 {
23430 /* use the copy made earlier */
23431 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23432 ++to->vval.v_dict->dv_refcount;
23433 }
23434 else
23435 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23436 if (to->vval.v_dict == NULL)
23437 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023438 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023439 case VAR_UNKNOWN:
23440 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023441 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023442 }
23443 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023444 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023445}
23446
23447/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023448 * ":echo expr1 ..." print each argument separated with a space, add a
23449 * newline at the end.
23450 * ":echon expr1 ..." print each argument plain.
23451 */
23452 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023453ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023454{
23455 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023456 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023457 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023458 char_u *p;
23459 int needclr = TRUE;
23460 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023461 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023462
23463 if (eap->skip)
23464 ++emsg_skip;
23465 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23466 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023467 /* If eval1() causes an error message the text from the command may
23468 * still need to be cleared. E.g., "echo 22,44". */
23469 need_clr_eos = needclr;
23470
Bram Moolenaar071d4272004-06-13 20:20:40 +000023471 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023472 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023473 {
23474 /*
23475 * Report the invalid expression unless the expression evaluation
23476 * has been cancelled due to an aborting error, an interrupt, or an
23477 * exception.
23478 */
23479 if (!aborting())
23480 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023481 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023482 break;
23483 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023484 need_clr_eos = FALSE;
23485
Bram Moolenaar071d4272004-06-13 20:20:40 +000023486 if (!eap->skip)
23487 {
23488 if (atstart)
23489 {
23490 atstart = FALSE;
23491 /* Call msg_start() after eval1(), evaluating the expression
23492 * may cause a message to appear. */
23493 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023494 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023495 /* Mark the saved text as finishing the line, so that what
23496 * follows is displayed on a new line when scrolling back
23497 * at the more prompt. */
23498 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023499 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023501 }
23502 else if (eap->cmdidx == CMD_echo)
23503 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023504 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023505 if (p != NULL)
23506 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023507 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023508 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023509 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023510 if (*p != TAB && needclr)
23511 {
23512 /* remove any text still there from the command */
23513 msg_clr_eos();
23514 needclr = FALSE;
23515 }
23516 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023517 }
23518 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023519 {
23520#ifdef FEAT_MBYTE
23521 if (has_mbyte)
23522 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023523 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023524
23525 (void)msg_outtrans_len_attr(p, i, echo_attr);
23526 p += i - 1;
23527 }
23528 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023529#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023530 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023532 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023533 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023534 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023535 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023536 arg = skipwhite(arg);
23537 }
23538 eap->nextcmd = check_nextcmd(arg);
23539
23540 if (eap->skip)
23541 --emsg_skip;
23542 else
23543 {
23544 /* remove text that may still be there from the command */
23545 if (needclr)
23546 msg_clr_eos();
23547 if (eap->cmdidx == CMD_echo)
23548 msg_end();
23549 }
23550}
23551
23552/*
23553 * ":echohl {name}".
23554 */
23555 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023556ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023557{
23558 int id;
23559
23560 id = syn_name2id(eap->arg);
23561 if (id == 0)
23562 echo_attr = 0;
23563 else
23564 echo_attr = syn_id2attr(id);
23565}
23566
23567/*
23568 * ":execute expr1 ..." execute the result of an expression.
23569 * ":echomsg expr1 ..." Print a message
23570 * ":echoerr expr1 ..." Print an error
23571 * Each gets spaces around each argument and a newline at the end for
23572 * echo commands
23573 */
23574 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023575ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023576{
23577 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023578 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023579 int ret = OK;
23580 char_u *p;
23581 garray_T ga;
23582 int len;
23583 int save_did_emsg;
23584
23585 ga_init2(&ga, 1, 80);
23586
23587 if (eap->skip)
23588 ++emsg_skip;
23589 while (*arg != NUL && *arg != '|' && *arg != '\n')
23590 {
23591 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023592 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023593 {
23594 /*
23595 * Report the invalid expression unless the expression evaluation
23596 * has been cancelled due to an aborting error, an interrupt, or an
23597 * exception.
23598 */
23599 if (!aborting())
23600 EMSG2(_(e_invexpr2), p);
23601 ret = FAIL;
23602 break;
23603 }
23604
23605 if (!eap->skip)
23606 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023607 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023608 len = (int)STRLEN(p);
23609 if (ga_grow(&ga, len + 2) == FAIL)
23610 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023611 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023612 ret = FAIL;
23613 break;
23614 }
23615 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023616 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023617 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023618 ga.ga_len += len;
23619 }
23620
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023621 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023622 arg = skipwhite(arg);
23623 }
23624
23625 if (ret != FAIL && ga.ga_data != NULL)
23626 {
23627 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023628 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023629 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023630 out_flush();
23631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023632 else if (eap->cmdidx == CMD_echoerr)
23633 {
23634 /* We don't want to abort following commands, restore did_emsg. */
23635 save_did_emsg = did_emsg;
23636 EMSG((char_u *)ga.ga_data);
23637 if (!force_abort)
23638 did_emsg = save_did_emsg;
23639 }
23640 else if (eap->cmdidx == CMD_execute)
23641 do_cmdline((char_u *)ga.ga_data,
23642 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23643 }
23644
23645 ga_clear(&ga);
23646
23647 if (eap->skip)
23648 --emsg_skip;
23649
23650 eap->nextcmd = check_nextcmd(arg);
23651}
23652
23653/*
23654 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23655 * "arg" points to the "&" or '+' when called, to "option" when returning.
23656 * Returns NULL when no option name found. Otherwise pointer to the char
23657 * after the option name.
23658 */
23659 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023660find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023661{
23662 char_u *p = *arg;
23663
23664 ++p;
23665 if (*p == 'g' && p[1] == ':')
23666 {
23667 *opt_flags = OPT_GLOBAL;
23668 p += 2;
23669 }
23670 else if (*p == 'l' && p[1] == ':')
23671 {
23672 *opt_flags = OPT_LOCAL;
23673 p += 2;
23674 }
23675 else
23676 *opt_flags = 0;
23677
23678 if (!ASCII_ISALPHA(*p))
23679 return NULL;
23680 *arg = p;
23681
23682 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23683 p += 4; /* termcap option */
23684 else
23685 while (ASCII_ISALPHA(*p))
23686 ++p;
23687 return p;
23688}
23689
23690/*
23691 * ":function"
23692 */
23693 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023694ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023695{
23696 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023697 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023698 int j;
23699 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023700 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023701 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023702 char_u *name = NULL;
23703 char_u *p;
23704 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023705 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023706 garray_T newargs;
23707 garray_T newlines;
23708 int varargs = FALSE;
23709 int mustend = FALSE;
23710 int flags = 0;
23711 ufunc_T *fp;
23712 int indent;
23713 int nesting;
23714 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023715 dictitem_T *v;
23716 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023717 static int func_nr = 0; /* number for nameless function */
23718 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023719 hashtab_T *ht;
23720 int todo;
23721 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023722 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023723
23724 /*
23725 * ":function" without argument: list functions.
23726 */
23727 if (ends_excmd(*eap->arg))
23728 {
23729 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023730 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023731 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023732 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023733 {
23734 if (!HASHITEM_EMPTY(hi))
23735 {
23736 --todo;
23737 fp = HI2UF(hi);
23738 if (!isdigit(*fp->uf_name))
23739 list_func_head(fp, FALSE);
23740 }
23741 }
23742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023743 eap->nextcmd = check_nextcmd(eap->arg);
23744 return;
23745 }
23746
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023747 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023748 * ":function /pat": list functions matching pattern.
23749 */
23750 if (*eap->arg == '/')
23751 {
23752 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23753 if (!eap->skip)
23754 {
23755 regmatch_T regmatch;
23756
23757 c = *p;
23758 *p = NUL;
23759 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23760 *p = c;
23761 if (regmatch.regprog != NULL)
23762 {
23763 regmatch.rm_ic = p_ic;
23764
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023765 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023766 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23767 {
23768 if (!HASHITEM_EMPTY(hi))
23769 {
23770 --todo;
23771 fp = HI2UF(hi);
23772 if (!isdigit(*fp->uf_name)
23773 && vim_regexec(&regmatch, fp->uf_name, 0))
23774 list_func_head(fp, FALSE);
23775 }
23776 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023777 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023778 }
23779 }
23780 if (*p == '/')
23781 ++p;
23782 eap->nextcmd = check_nextcmd(p);
23783 return;
23784 }
23785
23786 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023787 * Get the function name. There are these situations:
23788 * func normal function name
23789 * "name" == func, "fudi.fd_dict" == NULL
23790 * dict.func new dictionary entry
23791 * "name" == NULL, "fudi.fd_dict" set,
23792 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23793 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023794 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023795 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23796 * dict.func existing dict entry that's not a Funcref
23797 * "name" == NULL, "fudi.fd_dict" set,
23798 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023799 * s:func script-local function name
23800 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023801 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023802 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023803 name = trans_function_name(&p, eap->skip, 0, &fudi);
23804 paren = (vim_strchr(p, '(') != NULL);
23805 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023806 {
23807 /*
23808 * Return on an invalid expression in braces, unless the expression
23809 * evaluation has been cancelled due to an aborting error, an
23810 * interrupt, or an exception.
23811 */
23812 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023813 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023814 if (!eap->skip && fudi.fd_newkey != NULL)
23815 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023816 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023817 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023819 else
23820 eap->skip = TRUE;
23821 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023822
Bram Moolenaar071d4272004-06-13 20:20:40 +000023823 /* An error in a function call during evaluation of an expression in magic
23824 * braces should not cause the function not to be defined. */
23825 saved_did_emsg = did_emsg;
23826 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023827
23828 /*
23829 * ":function func" with only function name: list function.
23830 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023831 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023832 {
23833 if (!ends_excmd(*skipwhite(p)))
23834 {
23835 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023836 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023837 }
23838 eap->nextcmd = check_nextcmd(p);
23839 if (eap->nextcmd != NULL)
23840 *p = NUL;
23841 if (!eap->skip && !got_int)
23842 {
23843 fp = find_func(name);
23844 if (fp != NULL)
23845 {
23846 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023847 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023848 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023849 if (FUNCLINE(fp, j) == NULL)
23850 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023851 msg_putchar('\n');
23852 msg_outnum((long)(j + 1));
23853 if (j < 9)
23854 msg_putchar(' ');
23855 if (j < 99)
23856 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023857 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023858 out_flush(); /* show a line at a time */
23859 ui_breakcheck();
23860 }
23861 if (!got_int)
23862 {
23863 msg_putchar('\n');
23864 msg_puts((char_u *)" endfunction");
23865 }
23866 }
23867 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023868 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023869 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023870 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023871 }
23872
23873 /*
23874 * ":function name(arg1, arg2)" Define function.
23875 */
23876 p = skipwhite(p);
23877 if (*p != '(')
23878 {
23879 if (!eap->skip)
23880 {
23881 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023882 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023883 }
23884 /* attempt to continue by skipping some text */
23885 if (vim_strchr(p, '(') != NULL)
23886 p = vim_strchr(p, '(');
23887 }
23888 p = skipwhite(p + 1);
23889
23890 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23891 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23892
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023893 if (!eap->skip)
23894 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023895 /* Check the name of the function. Unless it's a dictionary function
23896 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023897 if (name != NULL)
23898 arg = name;
23899 else
23900 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023901 if (arg != NULL && (fudi.fd_di == NULL
23902 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023903 {
23904 if (*arg == K_SPECIAL)
23905 j = 3;
23906 else
23907 j = 0;
23908 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23909 : eval_isnamec(arg[j])))
23910 ++j;
23911 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023912 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023913 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023914 /* Disallow using the g: dict. */
23915 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23916 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023917 }
23918
Bram Moolenaar071d4272004-06-13 20:20:40 +000023919 /*
23920 * Isolate the arguments: "arg1, arg2, ...)"
23921 */
23922 while (*p != ')')
23923 {
23924 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23925 {
23926 varargs = TRUE;
23927 p += 3;
23928 mustend = TRUE;
23929 }
23930 else
23931 {
23932 arg = p;
23933 while (ASCII_ISALNUM(*p) || *p == '_')
23934 ++p;
23935 if (arg == p || isdigit(*arg)
23936 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23937 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23938 {
23939 if (!eap->skip)
23940 EMSG2(_("E125: Illegal argument: %s"), arg);
23941 break;
23942 }
23943 if (ga_grow(&newargs, 1) == FAIL)
23944 goto erret;
23945 c = *p;
23946 *p = NUL;
23947 arg = vim_strsave(arg);
23948 if (arg == NULL)
23949 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023950
23951 /* Check for duplicate argument name. */
23952 for (i = 0; i < newargs.ga_len; ++i)
23953 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23954 {
23955 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023956 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023957 goto erret;
23958 }
23959
Bram Moolenaar071d4272004-06-13 20:20:40 +000023960 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23961 *p = c;
23962 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023963 if (*p == ',')
23964 ++p;
23965 else
23966 mustend = TRUE;
23967 }
23968 p = skipwhite(p);
23969 if (mustend && *p != ')')
23970 {
23971 if (!eap->skip)
23972 EMSG2(_(e_invarg2), eap->arg);
23973 break;
23974 }
23975 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023976 if (*p != ')')
23977 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023978 ++p; /* skip the ')' */
23979
Bram Moolenaare9a41262005-01-15 22:18:47 +000023980 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023981 for (;;)
23982 {
23983 p = skipwhite(p);
23984 if (STRNCMP(p, "range", 5) == 0)
23985 {
23986 flags |= FC_RANGE;
23987 p += 5;
23988 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023989 else if (STRNCMP(p, "dict", 4) == 0)
23990 {
23991 flags |= FC_DICT;
23992 p += 4;
23993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023994 else if (STRNCMP(p, "abort", 5) == 0)
23995 {
23996 flags |= FC_ABORT;
23997 p += 5;
23998 }
23999 else
24000 break;
24001 }
24002
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024003 /* When there is a line break use what follows for the function body.
24004 * Makes 'exe "func Test()\n...\nendfunc"' work. */
24005 if (*p == '\n')
24006 line_arg = p + 1;
24007 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024008 EMSG(_(e_trailing));
24009
24010 /*
24011 * Read the body of the function, until ":endfunction" is found.
24012 */
24013 if (KeyTyped)
24014 {
24015 /* Check if the function already exists, don't let the user type the
24016 * whole function before telling him it doesn't work! For a script we
24017 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024018 if (!eap->skip && !eap->forceit)
24019 {
24020 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
24021 EMSG(_(e_funcdict));
24022 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024023 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024025
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024026 if (!eap->skip && did_emsg)
24027 goto erret;
24028
Bram Moolenaar071d4272004-06-13 20:20:40 +000024029 msg_putchar('\n'); /* don't overwrite the function name */
24030 cmdline_row = msg_row;
24031 }
24032
24033 indent = 2;
24034 nesting = 0;
24035 for (;;)
24036 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024037 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024038 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024039 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024040 saved_wait_return = FALSE;
24041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024042 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024043 sourcing_lnum_off = sourcing_lnum;
24044
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024045 if (line_arg != NULL)
24046 {
24047 /* Use eap->arg, split up in parts by line breaks. */
24048 theline = line_arg;
24049 p = vim_strchr(theline, '\n');
24050 if (p == NULL)
24051 line_arg += STRLEN(line_arg);
24052 else
24053 {
24054 *p = NUL;
24055 line_arg = p + 1;
24056 }
24057 }
24058 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024059 theline = getcmdline(':', 0L, indent);
24060 else
24061 theline = eap->getline(':', eap->cookie, indent);
24062 if (KeyTyped)
24063 lines_left = Rows - 1;
24064 if (theline == NULL)
24065 {
24066 EMSG(_("E126: Missing :endfunction"));
24067 goto erret;
24068 }
24069
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024070 /* Detect line continuation: sourcing_lnum increased more than one. */
24071 if (sourcing_lnum > sourcing_lnum_off + 1)
24072 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
24073 else
24074 sourcing_lnum_off = 0;
24075
Bram Moolenaar071d4272004-06-13 20:20:40 +000024076 if (skip_until != NULL)
24077 {
24078 /* between ":append" and "." and between ":python <<EOF" and "EOF"
24079 * don't check for ":endfunc". */
24080 if (STRCMP(theline, skip_until) == 0)
24081 {
24082 vim_free(skip_until);
24083 skip_until = NULL;
24084 }
24085 }
24086 else
24087 {
24088 /* skip ':' and blanks*/
24089 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
24090 ;
24091
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024092 /* Check for "endfunction". */
24093 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024094 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024095 if (line_arg == NULL)
24096 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024097 break;
24098 }
24099
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024100 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000024101 * at "end". */
24102 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
24103 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024104 else if (STRNCMP(p, "if", 2) == 0
24105 || STRNCMP(p, "wh", 2) == 0
24106 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000024107 || STRNCMP(p, "try", 3) == 0)
24108 indent += 2;
24109
24110 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024111 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024112 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024113 if (*p == '!')
24114 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024115 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010024116 vim_free(trans_function_name(&p, TRUE, 0, NULL));
24117 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000024118 {
Bram Moolenaaref923902014-12-13 21:00:55 +010024119 ++nesting;
24120 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024121 }
24122 }
24123
24124 /* Check for ":append" or ":insert". */
24125 p = skip_range(p, NULL);
24126 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
24127 || (p[0] == 'i'
24128 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
24129 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
24130 skip_until = vim_strsave((char_u *)".");
24131
24132 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
24133 arg = skipwhite(skiptowhite(p));
24134 if (arg[0] == '<' && arg[1] =='<'
24135 && ((p[0] == 'p' && p[1] == 'y'
24136 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
24137 || (p[0] == 'p' && p[1] == 'e'
24138 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
24139 || (p[0] == 't' && p[1] == 'c'
24140 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020024141 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
24142 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024143 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
24144 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024145 || (p[0] == 'm' && p[1] == 'z'
24146 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024147 ))
24148 {
24149 /* ":python <<" continues until a dot, like ":append" */
24150 p = skipwhite(arg + 2);
24151 if (*p == NUL)
24152 skip_until = vim_strsave((char_u *)".");
24153 else
24154 skip_until = vim_strsave(p);
24155 }
24156 }
24157
24158 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024159 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024160 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024161 if (line_arg == NULL)
24162 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024163 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024164 }
24165
24166 /* Copy the line to newly allocated memory. get_one_sourceline()
24167 * allocates 250 bytes per line, this saves 80% on average. The cost
24168 * is an extra alloc/free. */
24169 p = vim_strsave(theline);
24170 if (p != NULL)
24171 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024172 if (line_arg == NULL)
24173 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024174 theline = p;
24175 }
24176
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024177 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24178
24179 /* Add NULL lines for continuation lines, so that the line count is
24180 * equal to the index in the growarray. */
24181 while (sourcing_lnum_off-- > 0)
24182 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024183
24184 /* Check for end of eap->arg. */
24185 if (line_arg != NULL && *line_arg == NUL)
24186 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024187 }
24188
24189 /* Don't define the function when skipping commands or when an error was
24190 * detected. */
24191 if (eap->skip || did_emsg)
24192 goto erret;
24193
24194 /*
24195 * If there are no errors, add the function
24196 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024197 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024198 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024199 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024200 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024201 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024202 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024203 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024204 goto erret;
24205 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024206
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024207 fp = find_func(name);
24208 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024209 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024210 if (!eap->forceit)
24211 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024212 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024213 goto erret;
24214 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024215 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024216 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024217 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024218 name);
24219 goto erret;
24220 }
24221 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024222 ga_clear_strings(&(fp->uf_args));
24223 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024224 vim_free(name);
24225 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024227 }
24228 else
24229 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024230 char numbuf[20];
24231
24232 fp = NULL;
24233 if (fudi.fd_newkey == NULL && !eap->forceit)
24234 {
24235 EMSG(_(e_funcdict));
24236 goto erret;
24237 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024238 if (fudi.fd_di == NULL)
24239 {
24240 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024241 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024242 goto erret;
24243 }
24244 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024245 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024246 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024247
24248 /* Give the function a sequential number. Can only be used with a
24249 * Funcref! */
24250 vim_free(name);
24251 sprintf(numbuf, "%d", ++func_nr);
24252 name = vim_strsave((char_u *)numbuf);
24253 if (name == NULL)
24254 goto erret;
24255 }
24256
24257 if (fp == NULL)
24258 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024259 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024260 {
24261 int slen, plen;
24262 char_u *scriptname;
24263
24264 /* Check that the autoload name matches the script name. */
24265 j = FAIL;
24266 if (sourcing_name != NULL)
24267 {
24268 scriptname = autoload_name(name);
24269 if (scriptname != NULL)
24270 {
24271 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024272 plen = (int)STRLEN(p);
24273 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024274 if (slen > plen && fnamecmp(p,
24275 sourcing_name + slen - plen) == 0)
24276 j = OK;
24277 vim_free(scriptname);
24278 }
24279 }
24280 if (j == FAIL)
24281 {
24282 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24283 goto erret;
24284 }
24285 }
24286
24287 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024288 if (fp == NULL)
24289 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024290
24291 if (fudi.fd_dict != NULL)
24292 {
24293 if (fudi.fd_di == NULL)
24294 {
24295 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024296 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024297 if (fudi.fd_di == NULL)
24298 {
24299 vim_free(fp);
24300 goto erret;
24301 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024302 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24303 {
24304 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024305 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024306 goto erret;
24307 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024308 }
24309 else
24310 /* overwrite existing dict entry */
24311 clear_tv(&fudi.fd_di->di_tv);
24312 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024313 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024314 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024315 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024316
24317 /* behave like "dict" was used */
24318 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024319 }
24320
Bram Moolenaar071d4272004-06-13 20:20:40 +000024321 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024322 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024323 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24324 {
24325 vim_free(fp);
24326 goto erret;
24327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024328 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024329 fp->uf_args = newargs;
24330 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024331#ifdef FEAT_PROFILE
24332 fp->uf_tml_count = NULL;
24333 fp->uf_tml_total = NULL;
24334 fp->uf_tml_self = NULL;
24335 fp->uf_profiling = FALSE;
24336 if (prof_def_func())
24337 func_do_profile(fp);
24338#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024339 fp->uf_varargs = varargs;
24340 fp->uf_flags = flags;
24341 fp->uf_calls = 0;
24342 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024343 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024344
24345erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024346 ga_clear_strings(&newargs);
24347 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024348ret_free:
24349 vim_free(skip_until);
24350 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024351 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024352 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024353 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024354}
24355
24356/*
24357 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024358 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024359 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024360 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024361 * TFN_INT: internal function name OK
24362 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024363 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024364 * Advances "pp" to just after the function name (if no error).
24365 */
24366 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024367trans_function_name(
24368 char_u **pp,
24369 int skip, /* only find the end, don't evaluate */
24370 int flags,
24371 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024372{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024373 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024374 char_u *start;
24375 char_u *end;
24376 int lead;
24377 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024378 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024379 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024380
24381 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024382 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024383 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024384
24385 /* Check for hard coded <SNR>: already translated function ID (from a user
24386 * command). */
24387 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24388 && (*pp)[2] == (int)KE_SNR)
24389 {
24390 *pp += 3;
24391 len = get_id_len(pp) + 3;
24392 return vim_strnsave(start, len);
24393 }
24394
24395 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24396 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024397 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024398 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024399 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024400
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024401 /* Note that TFN_ flags use the same values as GLV_ flags. */
24402 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024403 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024404 if (end == start)
24405 {
24406 if (!skip)
24407 EMSG(_("E129: Function name required"));
24408 goto theend;
24409 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024410 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024411 {
24412 /*
24413 * Report an invalid expression in braces, unless the expression
24414 * evaluation has been cancelled due to an aborting error, an
24415 * interrupt, or an exception.
24416 */
24417 if (!aborting())
24418 {
24419 if (end != NULL)
24420 EMSG2(_(e_invarg2), start);
24421 }
24422 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024423 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024424 goto theend;
24425 }
24426
24427 if (lv.ll_tv != NULL)
24428 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024429 if (fdp != NULL)
24430 {
24431 fdp->fd_dict = lv.ll_dict;
24432 fdp->fd_newkey = lv.ll_newkey;
24433 lv.ll_newkey = NULL;
24434 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024435 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024436 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24437 {
24438 name = vim_strsave(lv.ll_tv->vval.v_string);
24439 *pp = end;
24440 }
24441 else
24442 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024443 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24444 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024445 EMSG(_(e_funcref));
24446 else
24447 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024448 name = NULL;
24449 }
24450 goto theend;
24451 }
24452
24453 if (lv.ll_name == NULL)
24454 {
24455 /* Error found, but continue after the function name. */
24456 *pp = end;
24457 goto theend;
24458 }
24459
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024460 /* Check if the name is a Funcref. If so, use the value. */
24461 if (lv.ll_exp_name != NULL)
24462 {
24463 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010024464 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024465 if (name == lv.ll_exp_name)
24466 name = NULL;
24467 }
24468 else
24469 {
24470 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010024471 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024472 if (name == *pp)
24473 name = NULL;
24474 }
24475 if (name != NULL)
24476 {
24477 name = vim_strsave(name);
24478 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024479 if (STRNCMP(name, "<SNR>", 5) == 0)
24480 {
24481 /* Change "<SNR>" to the byte sequence. */
24482 name[0] = K_SPECIAL;
24483 name[1] = KS_EXTRA;
24484 name[2] = (int)KE_SNR;
24485 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24486 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024487 goto theend;
24488 }
24489
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024490 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024491 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024492 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024493 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24494 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24495 {
24496 /* When there was "s:" already or the name expanded to get a
24497 * leading "s:" then remove it. */
24498 lv.ll_name += 2;
24499 len -= 2;
24500 lead = 2;
24501 }
24502 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024503 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024504 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024505 /* skip over "s:" and "g:" */
24506 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024507 lv.ll_name += 2;
24508 len = (int)(end - lv.ll_name);
24509 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024510
24511 /*
24512 * Copy the function name to allocated memory.
24513 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24514 * Accept <SNR>123_name() outside a script.
24515 */
24516 if (skip)
24517 lead = 0; /* do nothing */
24518 else if (lead > 0)
24519 {
24520 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024521 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24522 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024523 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024524 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024525 if (current_SID <= 0)
24526 {
24527 EMSG(_(e_usingsid));
24528 goto theend;
24529 }
24530 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24531 lead += (int)STRLEN(sid_buf);
24532 }
24533 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024534 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024535 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024536 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024537 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024538 goto theend;
24539 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024540 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024541 {
24542 char_u *cp = vim_strchr(lv.ll_name, ':');
24543
24544 if (cp != NULL && cp < end)
24545 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024546 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024547 goto theend;
24548 }
24549 }
24550
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024551 name = alloc((unsigned)(len + lead + 1));
24552 if (name != NULL)
24553 {
24554 if (lead > 0)
24555 {
24556 name[0] = K_SPECIAL;
24557 name[1] = KS_EXTRA;
24558 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024559 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024560 STRCPY(name + 3, sid_buf);
24561 }
24562 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024563 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024564 }
24565 *pp = end;
24566
24567theend:
24568 clear_lval(&lv);
24569 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024570}
24571
24572/*
24573 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24574 * Return 2 if "p" starts with "s:".
24575 * Return 0 otherwise.
24576 */
24577 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024578eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024579{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024580 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24581 * the standard library function. */
24582 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24583 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024584 return 5;
24585 if (p[0] == 's' && p[1] == ':')
24586 return 2;
24587 return 0;
24588}
24589
24590/*
24591 * Return TRUE if "p" starts with "<SID>" or "s:".
24592 * Only works if eval_fname_script() returned non-zero for "p"!
24593 */
24594 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024595eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024596{
24597 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24598}
24599
24600/*
24601 * List the head of the function: "name(arg1, arg2)".
24602 */
24603 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024604list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024605{
24606 int j;
24607
24608 msg_start();
24609 if (indent)
24610 MSG_PUTS(" ");
24611 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024612 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024613 {
24614 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024615 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024616 }
24617 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024618 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024619 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024620 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024621 {
24622 if (j)
24623 MSG_PUTS(", ");
24624 msg_puts(FUNCARG(fp, j));
24625 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024626 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024627 {
24628 if (j)
24629 MSG_PUTS(", ");
24630 MSG_PUTS("...");
24631 }
24632 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024633 if (fp->uf_flags & FC_ABORT)
24634 MSG_PUTS(" abort");
24635 if (fp->uf_flags & FC_RANGE)
24636 MSG_PUTS(" range");
24637 if (fp->uf_flags & FC_DICT)
24638 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024639 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024640 if (p_verbose > 0)
24641 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024642}
24643
24644/*
24645 * Find a function by name, return pointer to it in ufuncs.
24646 * Return NULL for unknown function.
24647 */
24648 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024649find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024650{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024651 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024652
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024653 hi = hash_find(&func_hashtab, name);
24654 if (!HASHITEM_EMPTY(hi))
24655 return HI2UF(hi);
24656 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024657}
24658
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024659#if defined(EXITFREE) || defined(PROTO)
24660 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024661free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024662{
24663 hashitem_T *hi;
24664
24665 /* Need to start all over every time, because func_free() may change the
24666 * hash table. */
24667 while (func_hashtab.ht_used > 0)
24668 for (hi = func_hashtab.ht_array; ; ++hi)
24669 if (!HASHITEM_EMPTY(hi))
24670 {
24671 func_free(HI2UF(hi));
24672 break;
24673 }
24674}
24675#endif
24676
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024677 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024678translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024679{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024680 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024681 return find_internal_func(name) >= 0;
24682 return find_func(name) != NULL;
24683}
24684
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024685/*
24686 * Return TRUE if a function "name" exists.
24687 */
24688 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024689function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024690{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024691 char_u *nm = name;
24692 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024693 int n = FALSE;
24694
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024695 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
24696 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024697 nm = skipwhite(nm);
24698
24699 /* Only accept "funcname", "funcname ", "funcname (..." and
24700 * "funcname(...", not "funcname!...". */
24701 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024702 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024703 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024704 return n;
24705}
24706
Bram Moolenaara1544c02013-05-30 12:35:52 +020024707 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024708get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024709{
24710 char_u *nm = name;
24711 char_u *p;
24712
24713 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
24714
24715 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024716 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024717 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024718
Bram Moolenaara1544c02013-05-30 12:35:52 +020024719 vim_free(p);
24720 return NULL;
24721}
24722
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024723/*
24724 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024725 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24726 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024727 */
24728 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024729builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024730{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024731 char_u *p;
24732
24733 if (!ASCII_ISLOWER(name[0]))
24734 return FALSE;
24735 p = vim_strchr(name, AUTOLOAD_CHAR);
24736 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024737}
24738
Bram Moolenaar05159a02005-02-26 23:04:13 +000024739#if defined(FEAT_PROFILE) || defined(PROTO)
24740/*
24741 * Start profiling function "fp".
24742 */
24743 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024744func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024745{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024746 int len = fp->uf_lines.ga_len;
24747
24748 if (len == 0)
24749 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024750 fp->uf_tm_count = 0;
24751 profile_zero(&fp->uf_tm_self);
24752 profile_zero(&fp->uf_tm_total);
24753 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024754 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024755 if (fp->uf_tml_total == NULL)
24756 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024757 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024758 if (fp->uf_tml_self == NULL)
24759 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024760 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024761 fp->uf_tml_idx = -1;
24762 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24763 || fp->uf_tml_self == NULL)
24764 return; /* out of memory */
24765
24766 fp->uf_profiling = TRUE;
24767}
24768
24769/*
24770 * Dump the profiling results for all functions in file "fd".
24771 */
24772 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024773func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024774{
24775 hashitem_T *hi;
24776 int todo;
24777 ufunc_T *fp;
24778 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024779 ufunc_T **sorttab;
24780 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024781
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024782 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024783 if (todo == 0)
24784 return; /* nothing to dump */
24785
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024786 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024787
Bram Moolenaar05159a02005-02-26 23:04:13 +000024788 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24789 {
24790 if (!HASHITEM_EMPTY(hi))
24791 {
24792 --todo;
24793 fp = HI2UF(hi);
24794 if (fp->uf_profiling)
24795 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024796 if (sorttab != NULL)
24797 sorttab[st_len++] = fp;
24798
Bram Moolenaar05159a02005-02-26 23:04:13 +000024799 if (fp->uf_name[0] == K_SPECIAL)
24800 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24801 else
24802 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24803 if (fp->uf_tm_count == 1)
24804 fprintf(fd, "Called 1 time\n");
24805 else
24806 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24807 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24808 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24809 fprintf(fd, "\n");
24810 fprintf(fd, "count total (s) self (s)\n");
24811
24812 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24813 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024814 if (FUNCLINE(fp, i) == NULL)
24815 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024816 prof_func_line(fd, fp->uf_tml_count[i],
24817 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024818 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24819 }
24820 fprintf(fd, "\n");
24821 }
24822 }
24823 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024824
24825 if (sorttab != NULL && st_len > 0)
24826 {
24827 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24828 prof_total_cmp);
24829 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24830 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24831 prof_self_cmp);
24832 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24833 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024834
24835 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024836}
Bram Moolenaar73830342005-02-28 22:48:19 +000024837
24838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024839prof_sort_list(
24840 FILE *fd,
24841 ufunc_T **sorttab,
24842 int st_len,
24843 char *title,
24844 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024845{
24846 int i;
24847 ufunc_T *fp;
24848
24849 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24850 fprintf(fd, "count total (s) self (s) function\n");
24851 for (i = 0; i < 20 && i < st_len; ++i)
24852 {
24853 fp = sorttab[i];
24854 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24855 prefer_self);
24856 if (fp->uf_name[0] == K_SPECIAL)
24857 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24858 else
24859 fprintf(fd, " %s()\n", fp->uf_name);
24860 }
24861 fprintf(fd, "\n");
24862}
24863
24864/*
24865 * Print the count and times for one function or function line.
24866 */
24867 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024868prof_func_line(
24869 FILE *fd,
24870 int count,
24871 proftime_T *total,
24872 proftime_T *self,
24873 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024874{
24875 if (count > 0)
24876 {
24877 fprintf(fd, "%5d ", count);
24878 if (prefer_self && profile_equal(total, self))
24879 fprintf(fd, " ");
24880 else
24881 fprintf(fd, "%s ", profile_msg(total));
24882 if (!prefer_self && profile_equal(total, self))
24883 fprintf(fd, " ");
24884 else
24885 fprintf(fd, "%s ", profile_msg(self));
24886 }
24887 else
24888 fprintf(fd, " ");
24889}
24890
24891/*
24892 * Compare function for total time sorting.
24893 */
24894 static int
24895#ifdef __BORLANDC__
24896_RTLENTRYF
24897#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024898prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024899{
24900 ufunc_T *p1, *p2;
24901
24902 p1 = *(ufunc_T **)s1;
24903 p2 = *(ufunc_T **)s2;
24904 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24905}
24906
24907/*
24908 * Compare function for self time sorting.
24909 */
24910 static int
24911#ifdef __BORLANDC__
24912_RTLENTRYF
24913#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024914prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024915{
24916 ufunc_T *p1, *p2;
24917
24918 p1 = *(ufunc_T **)s1;
24919 p2 = *(ufunc_T **)s2;
24920 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24921}
24922
Bram Moolenaar05159a02005-02-26 23:04:13 +000024923#endif
24924
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024925/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024926 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024927 * Return TRUE if a package was loaded.
24928 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024929 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024930script_autoload(
24931 char_u *name,
24932 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024933{
24934 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024935 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024936 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024937 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024938
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024939 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024940 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024941 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024942 return FALSE;
24943
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024944 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024945
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024946 /* Find the name in the list of previously loaded package names. Skip
24947 * "autoload/", it's always the same. */
24948 for (i = 0; i < ga_loaded.ga_len; ++i)
24949 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24950 break;
24951 if (!reload && i < ga_loaded.ga_len)
24952 ret = FALSE; /* was loaded already */
24953 else
24954 {
24955 /* Remember the name if it wasn't loaded already. */
24956 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24957 {
24958 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24959 tofree = NULL;
24960 }
24961
24962 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000024963 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024964 ret = TRUE;
24965 }
24966
24967 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024968 return ret;
24969}
24970
24971/*
24972 * Return the autoload script name for a function or variable name.
24973 * Returns NULL when out of memory.
24974 */
24975 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024976autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024977{
24978 char_u *p;
24979 char_u *scriptname;
24980
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024981 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024982 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24983 if (scriptname == NULL)
24984 return FALSE;
24985 STRCPY(scriptname, "autoload/");
24986 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024987 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024988 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024989 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024990 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024991 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024992}
24993
Bram Moolenaar071d4272004-06-13 20:20:40 +000024994#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24995
24996/*
24997 * Function given to ExpandGeneric() to obtain the list of user defined
24998 * function names.
24999 */
25000 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025001get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025002{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025003 static long_u done;
25004 static hashitem_T *hi;
25005 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025006
25007 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025008 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025009 done = 0;
25010 hi = func_hashtab.ht_array;
25011 }
25012 if (done < func_hashtab.ht_used)
25013 {
25014 if (done++ > 0)
25015 ++hi;
25016 while (HASHITEM_EMPTY(hi))
25017 ++hi;
25018 fp = HI2UF(hi);
25019
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025020 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010025021 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025022
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025023 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
25024 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025025
25026 cat_func_name(IObuff, fp);
25027 if (xp->xp_context != EXPAND_USER_FUNC)
25028 {
25029 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025030 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025031 STRCAT(IObuff, ")");
25032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025033 return IObuff;
25034 }
25035 return NULL;
25036}
25037
25038#endif /* FEAT_CMDL_COMPL */
25039
25040/*
25041 * Copy the function name of "fp" to buffer "buf".
25042 * "buf" must be able to hold the function name plus three bytes.
25043 * Takes care of script-local function names.
25044 */
25045 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025046cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025047{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025048 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025049 {
25050 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025051 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025052 }
25053 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025054 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025055}
25056
25057/*
25058 * ":delfunction {name}"
25059 */
25060 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025061ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025062{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025063 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025064 char_u *p;
25065 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000025066 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025067
25068 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025069 name = trans_function_name(&p, eap->skip, 0, &fudi);
25070 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025071 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025072 {
25073 if (fudi.fd_dict != NULL && !eap->skip)
25074 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000025075 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025077 if (!ends_excmd(*skipwhite(p)))
25078 {
25079 vim_free(name);
25080 EMSG(_(e_trailing));
25081 return;
25082 }
25083 eap->nextcmd = check_nextcmd(p);
25084 if (eap->nextcmd != NULL)
25085 *p = NUL;
25086
25087 if (!eap->skip)
25088 fp = find_func(name);
25089 vim_free(name);
25090
25091 if (!eap->skip)
25092 {
25093 if (fp == NULL)
25094 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025095 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025096 return;
25097 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025098 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025099 {
25100 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
25101 return;
25102 }
25103
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025104 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025105 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025106 /* Delete the dict item that refers to the function, it will
25107 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000025108 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025109 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025110 else
25111 func_free(fp);
25112 }
25113}
25114
25115/*
25116 * Free a function and remove it from the list of functions.
25117 */
25118 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025119func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025120{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025121 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025122
25123 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025124 ga_clear_strings(&(fp->uf_args));
25125 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025126#ifdef FEAT_PROFILE
25127 vim_free(fp->uf_tml_count);
25128 vim_free(fp->uf_tml_total);
25129 vim_free(fp->uf_tml_self);
25130#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025131
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025132 /* remove the function from the function hashtable */
25133 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
25134 if (HASHITEM_EMPTY(hi))
25135 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025136 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025137 hash_remove(&func_hashtab, hi);
25138
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025139 vim_free(fp);
25140}
25141
25142/*
25143 * Unreference a Function: decrement the reference count and free it when it
25144 * becomes zero. Only for numbered functions.
25145 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025146 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025147func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025148{
25149 ufunc_T *fp;
25150
25151 if (name != NULL && isdigit(*name))
25152 {
25153 fp = find_func(name);
25154 if (fp == NULL)
25155 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025156 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025157 {
25158 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025159 * when "uf_calls" becomes zero. */
25160 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025161 func_free(fp);
25162 }
25163 }
25164}
25165
25166/*
25167 * Count a reference to a Function.
25168 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025169 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025170func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025171{
25172 ufunc_T *fp;
25173
25174 if (name != NULL && isdigit(*name))
25175 {
25176 fp = find_func(name);
25177 if (fp == NULL)
25178 EMSG2(_(e_intern2), "func_ref()");
25179 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025180 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025181 }
25182}
25183
25184/*
25185 * Call a user function.
25186 */
25187 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025188call_user_func(
25189 ufunc_T *fp, /* pointer to function */
25190 int argcount, /* nr of args */
25191 typval_T *argvars, /* arguments */
25192 typval_T *rettv, /* return value */
25193 linenr_T firstline, /* first line of range */
25194 linenr_T lastline, /* last line of range */
25195 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025196{
Bram Moolenaar33570922005-01-25 22:26:29 +000025197 char_u *save_sourcing_name;
25198 linenr_T save_sourcing_lnum;
25199 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025200 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025201 int save_did_emsg;
25202 static int depth = 0;
25203 dictitem_T *v;
25204 int fixvar_idx = 0; /* index in fixvar[] */
25205 int i;
25206 int ai;
25207 char_u numbuf[NUMBUFLEN];
25208 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025209 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025210#ifdef FEAT_PROFILE
25211 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025212 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025214
25215 /* If depth of calling is getting too high, don't execute the function */
25216 if (depth >= p_mfd)
25217 {
25218 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025219 rettv->v_type = VAR_NUMBER;
25220 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025221 return;
25222 }
25223 ++depth;
25224
25225 line_breakcheck(); /* check for CTRL-C hit */
25226
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025227 fc = (funccall_T *)alloc(sizeof(funccall_T));
25228 fc->caller = current_funccal;
25229 current_funccal = fc;
25230 fc->func = fp;
25231 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025232 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025233 fc->linenr = 0;
25234 fc->returned = FALSE;
25235 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025236 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025237 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25238 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025239
Bram Moolenaar33570922005-01-25 22:26:29 +000025240 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025241 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025242 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25243 * each argument variable and saves a lot of time.
25244 */
25245 /*
25246 * Init l: variables.
25247 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025248 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025249 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025250 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025251 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25252 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025253 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025254 name = v->di_key;
25255 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025256 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025257 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025258 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025259 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025260 v->di_tv.vval.v_dict = selfdict;
25261 ++selfdict->dv_refcount;
25262 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025263
Bram Moolenaar33570922005-01-25 22:26:29 +000025264 /*
25265 * Init a: variables.
25266 * Set a:0 to "argcount".
25267 * Set a:000 to a list with room for the "..." arguments.
25268 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025269 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025270 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025271 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025272 /* Use "name" to avoid a warning from some compiler that checks the
25273 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025274 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025275 name = v->di_key;
25276 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025277 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025278 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025279 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025280 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025281 v->di_tv.vval.v_list = &fc->l_varlist;
25282 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25283 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25284 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025285
25286 /*
25287 * Set a:firstline to "firstline" and a:lastline to "lastline".
25288 * Set a:name to named arguments.
25289 * Set a:N to the "..." arguments.
25290 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025291 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025292 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025293 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025294 (varnumber_T)lastline);
25295 for (i = 0; i < argcount; ++i)
25296 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025297 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025298 if (ai < 0)
25299 /* named argument a:name */
25300 name = FUNCARG(fp, i);
25301 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025302 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025303 /* "..." argument a:1, a:2, etc. */
25304 sprintf((char *)numbuf, "%d", ai + 1);
25305 name = numbuf;
25306 }
25307 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25308 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025309 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025310 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25311 }
25312 else
25313 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025314 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25315 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025316 if (v == NULL)
25317 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025318 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025319 }
25320 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025321 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025322
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025323 /* Note: the values are copied directly to avoid alloc/free.
25324 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025325 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025326 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025327
25328 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25329 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025330 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25331 fc->l_listitems[ai].li_tv = argvars[i];
25332 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025333 }
25334 }
25335
Bram Moolenaar071d4272004-06-13 20:20:40 +000025336 /* Don't redraw while executing the function. */
25337 ++RedrawingDisabled;
25338 save_sourcing_name = sourcing_name;
25339 save_sourcing_lnum = sourcing_lnum;
25340 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025341 /* need space for function name + ("function " + 3) or "[number]" */
25342 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25343 + STRLEN(fp->uf_name) + 20;
25344 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025345 if (sourcing_name != NULL)
25346 {
25347 if (save_sourcing_name != NULL
25348 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025349 sprintf((char *)sourcing_name, "%s[%d]..",
25350 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025351 else
25352 STRCPY(sourcing_name, "function ");
25353 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25354
25355 if (p_verbose >= 12)
25356 {
25357 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025358 verbose_enter_scroll();
25359
Bram Moolenaar555b2802005-05-19 21:08:39 +000025360 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025361 if (p_verbose >= 14)
25362 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025363 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025364 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025365 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025366 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025367
25368 msg_puts((char_u *)"(");
25369 for (i = 0; i < argcount; ++i)
25370 {
25371 if (i > 0)
25372 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025373 if (argvars[i].v_type == VAR_NUMBER)
25374 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025375 else
25376 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025377 /* Do not want errors such as E724 here. */
25378 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025379 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025380 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025381 if (s != NULL)
25382 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025383 if (vim_strsize(s) > MSG_BUF_CLEN)
25384 {
25385 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25386 s = buf;
25387 }
25388 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025389 vim_free(tofree);
25390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025391 }
25392 }
25393 msg_puts((char_u *)")");
25394 }
25395 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025396
25397 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025398 --no_wait_return;
25399 }
25400 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025401#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025402 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025403 {
25404 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25405 func_do_profile(fp);
25406 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025407 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025408 {
25409 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025410 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025411 profile_zero(&fp->uf_tm_children);
25412 }
25413 script_prof_save(&wait_start);
25414 }
25415#endif
25416
Bram Moolenaar071d4272004-06-13 20:20:40 +000025417 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025418 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025419 save_did_emsg = did_emsg;
25420 did_emsg = FALSE;
25421
25422 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025423 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025424 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25425
25426 --RedrawingDisabled;
25427
25428 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025429 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025430 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025431 clear_tv(rettv);
25432 rettv->v_type = VAR_NUMBER;
25433 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025434 }
25435
Bram Moolenaar05159a02005-02-26 23:04:13 +000025436#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025437 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025438 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025439 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025440 profile_end(&call_start);
25441 profile_sub_wait(&wait_start, &call_start);
25442 profile_add(&fp->uf_tm_total, &call_start);
25443 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025444 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025445 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025446 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25447 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025448 }
25449 }
25450#endif
25451
Bram Moolenaar071d4272004-06-13 20:20:40 +000025452 /* when being verbose, mention the return value */
25453 if (p_verbose >= 12)
25454 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025455 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025456 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025457
Bram Moolenaar071d4272004-06-13 20:20:40 +000025458 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025459 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025460 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025461 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025462 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025463 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025464 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025465 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025466 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025467 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025468 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025469
Bram Moolenaar555b2802005-05-19 21:08:39 +000025470 /* The value may be very long. Skip the middle part, so that we
25471 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025472 * truncate it at the end. Don't want errors such as E724 here. */
25473 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025474 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025475 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025476 if (s != NULL)
25477 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025478 if (vim_strsize(s) > MSG_BUF_CLEN)
25479 {
25480 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25481 s = buf;
25482 }
25483 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025484 vim_free(tofree);
25485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025486 }
25487 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025488
25489 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025490 --no_wait_return;
25491 }
25492
25493 vim_free(sourcing_name);
25494 sourcing_name = save_sourcing_name;
25495 sourcing_lnum = save_sourcing_lnum;
25496 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025497#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025498 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025499 script_prof_restore(&wait_start);
25500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025501
25502 if (p_verbose >= 12 && sourcing_name != NULL)
25503 {
25504 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025505 verbose_enter_scroll();
25506
Bram Moolenaar555b2802005-05-19 21:08:39 +000025507 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025508 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025509
25510 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025511 --no_wait_return;
25512 }
25513
25514 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025515 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025516 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025517
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025518 /* If the a:000 list and the l: and a: dicts are not referenced we can
25519 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025520 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25521 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25522 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25523 {
25524 free_funccal(fc, FALSE);
25525 }
25526 else
25527 {
25528 hashitem_T *hi;
25529 listitem_T *li;
25530 int todo;
25531
25532 /* "fc" is still in use. This can happen when returning "a:000" or
25533 * assigning "l:" to a global variable.
25534 * Link "fc" in the list for garbage collection later. */
25535 fc->caller = previous_funccal;
25536 previous_funccal = fc;
25537
25538 /* Make a copy of the a: variables, since we didn't do that above. */
25539 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25540 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25541 {
25542 if (!HASHITEM_EMPTY(hi))
25543 {
25544 --todo;
25545 v = HI2DI(hi);
25546 copy_tv(&v->di_tv, &v->di_tv);
25547 }
25548 }
25549
25550 /* Make a copy of the a:000 items, since we didn't do that above. */
25551 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25552 copy_tv(&li->li_tv, &li->li_tv);
25553 }
25554}
25555
25556/*
25557 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025558 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025559 */
25560 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025561can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025562{
25563 return (fc->l_varlist.lv_copyID != copyID
25564 && fc->l_vars.dv_copyID != copyID
25565 && fc->l_avars.dv_copyID != copyID);
25566}
25567
25568/*
25569 * Free "fc" and what it contains.
25570 */
25571 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025572free_funccal(
25573 funccall_T *fc,
25574 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025575{
25576 listitem_T *li;
25577
25578 /* The a: variables typevals may not have been allocated, only free the
25579 * allocated variables. */
25580 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25581
25582 /* free all l: variables */
25583 vars_clear(&fc->l_vars.dv_hashtab);
25584
25585 /* Free the a:000 variables if they were allocated. */
25586 if (free_val)
25587 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25588 clear_tv(&li->li_tv);
25589
25590 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025591}
25592
25593/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025594 * Add a number variable "name" to dict "dp" with value "nr".
25595 */
25596 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025597add_nr_var(
25598 dict_T *dp,
25599 dictitem_T *v,
25600 char *name,
25601 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025602{
25603 STRCPY(v->di_key, name);
25604 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25605 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25606 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025607 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025608 v->di_tv.vval.v_number = nr;
25609}
25610
25611/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025612 * ":return [expr]"
25613 */
25614 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025615ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025616{
25617 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025618 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025619 int returning = FALSE;
25620
25621 if (current_funccal == NULL)
25622 {
25623 EMSG(_("E133: :return not inside a function"));
25624 return;
25625 }
25626
25627 if (eap->skip)
25628 ++emsg_skip;
25629
25630 eap->nextcmd = NULL;
25631 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025632 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025633 {
25634 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025635 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025636 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025637 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025638 }
25639 /* It's safer to return also on error. */
25640 else if (!eap->skip)
25641 {
25642 /*
25643 * Return unless the expression evaluation has been cancelled due to an
25644 * aborting error, an interrupt, or an exception.
25645 */
25646 if (!aborting())
25647 returning = do_return(eap, FALSE, TRUE, NULL);
25648 }
25649
25650 /* When skipping or the return gets pending, advance to the next command
25651 * in this line (!returning). Otherwise, ignore the rest of the line.
25652 * Following lines will be ignored by get_func_line(). */
25653 if (returning)
25654 eap->nextcmd = NULL;
25655 else if (eap->nextcmd == NULL) /* no argument */
25656 eap->nextcmd = check_nextcmd(arg);
25657
25658 if (eap->skip)
25659 --emsg_skip;
25660}
25661
25662/*
25663 * Return from a function. Possibly makes the return pending. Also called
25664 * for a pending return at the ":endtry" or after returning from an extra
25665 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025666 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025667 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025668 * FALSE when the return gets pending.
25669 */
25670 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025671do_return(
25672 exarg_T *eap,
25673 int reanimate,
25674 int is_cmd,
25675 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025676{
25677 int idx;
25678 struct condstack *cstack = eap->cstack;
25679
25680 if (reanimate)
25681 /* Undo the return. */
25682 current_funccal->returned = FALSE;
25683
25684 /*
25685 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25686 * not in its finally clause (which then is to be executed next) is found.
25687 * In this case, make the ":return" pending for execution at the ":endtry".
25688 * Otherwise, return normally.
25689 */
25690 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25691 if (idx >= 0)
25692 {
25693 cstack->cs_pending[idx] = CSTP_RETURN;
25694
25695 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025696 /* A pending return again gets pending. "rettv" points to an
25697 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025698 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025699 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025700 else
25701 {
25702 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025703 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025704 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025705 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025706
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025707 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025708 {
25709 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025710 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025711 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025712 else
25713 EMSG(_(e_outofmem));
25714 }
25715 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025716 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025717
25718 if (reanimate)
25719 {
25720 /* The pending return value could be overwritten by a ":return"
25721 * without argument in a finally clause; reset the default
25722 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025723 current_funccal->rettv->v_type = VAR_NUMBER;
25724 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025725 }
25726 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025727 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025728 }
25729 else
25730 {
25731 current_funccal->returned = TRUE;
25732
25733 /* If the return is carried out now, store the return value. For
25734 * a return immediately after reanimation, the value is already
25735 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025736 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025737 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025738 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025739 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025740 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025741 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025742 }
25743 }
25744
25745 return idx < 0;
25746}
25747
25748/*
25749 * Free the variable with a pending return value.
25750 */
25751 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025752discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025753{
Bram Moolenaar33570922005-01-25 22:26:29 +000025754 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025755}
25756
25757/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025758 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025759 * is an allocated string. Used by report_pending() for verbose messages.
25760 */
25761 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025762get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025763{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025764 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025765 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025766 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025767
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025768 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025769 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025770 if (s == NULL)
25771 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025772
25773 STRCPY(IObuff, ":return ");
25774 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25775 if (STRLEN(s) + 8 >= IOSIZE)
25776 STRCPY(IObuff + IOSIZE - 4, "...");
25777 vim_free(tofree);
25778 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025779}
25780
25781/*
25782 * Get next function line.
25783 * Called by do_cmdline() to get the next line.
25784 * Returns allocated string, or NULL for end of function.
25785 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025786 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025787get_func_line(
25788 int c UNUSED,
25789 void *cookie,
25790 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025791{
Bram Moolenaar33570922005-01-25 22:26:29 +000025792 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025793 ufunc_T *fp = fcp->func;
25794 char_u *retval;
25795 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025796
25797 /* If breakpoints have been added/deleted need to check for it. */
25798 if (fcp->dbg_tick != debug_tick)
25799 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025800 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025801 sourcing_lnum);
25802 fcp->dbg_tick = debug_tick;
25803 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025804#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025805 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025806 func_line_end(cookie);
25807#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025808
Bram Moolenaar05159a02005-02-26 23:04:13 +000025809 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025810 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25811 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025812 retval = NULL;
25813 else
25814 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025815 /* Skip NULL lines (continuation lines). */
25816 while (fcp->linenr < gap->ga_len
25817 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25818 ++fcp->linenr;
25819 if (fcp->linenr >= gap->ga_len)
25820 retval = NULL;
25821 else
25822 {
25823 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25824 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025825#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025826 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025827 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025828#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025830 }
25831
25832 /* Did we encounter a breakpoint? */
25833 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25834 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025835 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025836 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025837 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025838 sourcing_lnum);
25839 fcp->dbg_tick = debug_tick;
25840 }
25841
25842 return retval;
25843}
25844
Bram Moolenaar05159a02005-02-26 23:04:13 +000025845#if defined(FEAT_PROFILE) || defined(PROTO)
25846/*
25847 * Called when starting to read a function line.
25848 * "sourcing_lnum" must be correct!
25849 * When skipping lines it may not actually be executed, but we won't find out
25850 * until later and we need to store the time now.
25851 */
25852 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025853func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025854{
25855 funccall_T *fcp = (funccall_T *)cookie;
25856 ufunc_T *fp = fcp->func;
25857
25858 if (fp->uf_profiling && sourcing_lnum >= 1
25859 && sourcing_lnum <= fp->uf_lines.ga_len)
25860 {
25861 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025862 /* Skip continuation lines. */
25863 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25864 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025865 fp->uf_tml_execed = FALSE;
25866 profile_start(&fp->uf_tml_start);
25867 profile_zero(&fp->uf_tml_children);
25868 profile_get_wait(&fp->uf_tml_wait);
25869 }
25870}
25871
25872/*
25873 * Called when actually executing a function line.
25874 */
25875 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025876func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025877{
25878 funccall_T *fcp = (funccall_T *)cookie;
25879 ufunc_T *fp = fcp->func;
25880
25881 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25882 fp->uf_tml_execed = TRUE;
25883}
25884
25885/*
25886 * Called when done with a function line.
25887 */
25888 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025889func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025890{
25891 funccall_T *fcp = (funccall_T *)cookie;
25892 ufunc_T *fp = fcp->func;
25893
25894 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25895 {
25896 if (fp->uf_tml_execed)
25897 {
25898 ++fp->uf_tml_count[fp->uf_tml_idx];
25899 profile_end(&fp->uf_tml_start);
25900 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025901 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025902 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25903 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025904 }
25905 fp->uf_tml_idx = -1;
25906 }
25907}
25908#endif
25909
Bram Moolenaar071d4272004-06-13 20:20:40 +000025910/*
25911 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025912 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025913 */
25914 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025915func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025916{
Bram Moolenaar33570922005-01-25 22:26:29 +000025917 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025918
25919 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25920 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025921 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025922 || fcp->returned);
25923}
25924
25925/*
25926 * return TRUE if cookie indicates a function which "abort"s on errors.
25927 */
25928 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025929func_has_abort(
25930 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025931{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025932 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025933}
25934
25935#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25936typedef enum
25937{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025938 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25939 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25940 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025941} var_flavour_T;
25942
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025943static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025944
25945 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025946var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025947{
25948 char_u *p = varname;
25949
25950 if (ASCII_ISUPPER(*p))
25951 {
25952 while (*(++p))
25953 if (ASCII_ISLOWER(*p))
25954 return VAR_FLAVOUR_SESSION;
25955 return VAR_FLAVOUR_VIMINFO;
25956 }
25957 else
25958 return VAR_FLAVOUR_DEFAULT;
25959}
25960#endif
25961
25962#if defined(FEAT_VIMINFO) || defined(PROTO)
25963/*
25964 * Restore global vars that start with a capital from the viminfo file
25965 */
25966 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025967read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025968{
25969 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025970 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025971 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025972 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025973
25974 if (!writing && (find_viminfo_parameter('!') != NULL))
25975 {
25976 tab = vim_strchr(virp->vir_line + 1, '\t');
25977 if (tab != NULL)
25978 {
25979 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025980 switch (*tab)
25981 {
25982 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025983#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025984 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025985#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025986 case 'D': type = VAR_DICT; break;
25987 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025988 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025990
25991 tab = vim_strchr(tab, '\t');
25992 if (tab != NULL)
25993 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025994 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025995 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025996 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025997 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025998#ifdef FEAT_FLOAT
25999 else if (type == VAR_FLOAT)
26000 (void)string2float(tab + 1, &tv.vval.v_float);
26001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026002 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026003 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026004 if (type == VAR_DICT || type == VAR_LIST)
26005 {
26006 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
26007
26008 if (etv == NULL)
26009 /* Failed to parse back the dict or list, use it as a
26010 * string. */
26011 tv.v_type = VAR_STRING;
26012 else
26013 {
26014 vim_free(tv.vval.v_string);
26015 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010026016 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026017 }
26018 }
26019
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026020 /* when in a function use global variables */
26021 save_funccal = current_funccal;
26022 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026023 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026024 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026025
26026 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026027 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026028 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
26029 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026030 }
26031 }
26032 }
26033
26034 return viminfo_readline(virp);
26035}
26036
26037/*
26038 * Write global vars that start with a capital to the viminfo file
26039 */
26040 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026041write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026042{
Bram Moolenaar33570922005-01-25 22:26:29 +000026043 hashitem_T *hi;
26044 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026045 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010026046 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026047 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026048 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026049 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026050
26051 if (find_viminfo_parameter('!') == NULL)
26052 return;
26053
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020026054 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000026055
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026056 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026057 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026058 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026059 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026060 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026061 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026062 this_var = HI2DI(hi);
26063 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026064 {
Bram Moolenaar33570922005-01-25 22:26:29 +000026065 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000026066 {
26067 case VAR_STRING: s = "STR"; break;
26068 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026069 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026070 case VAR_DICT: s = "DIC"; break;
26071 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026072 case VAR_SPECIAL: s = "XPL"; break;
26073
26074 case VAR_UNKNOWN:
26075 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010026076 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010026077 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010026078 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000026079 }
Bram Moolenaar33570922005-01-25 22:26:29 +000026080 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026081 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026082 if (p != NULL)
26083 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000026084 vim_free(tofree);
26085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026086 }
26087 }
26088}
26089#endif
26090
26091#if defined(FEAT_SESSION) || defined(PROTO)
26092 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026093store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026094{
Bram Moolenaar33570922005-01-25 22:26:29 +000026095 hashitem_T *hi;
26096 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026097 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026098 char_u *p, *t;
26099
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026100 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026101 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026102 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026103 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026104 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026105 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026106 this_var = HI2DI(hi);
26107 if ((this_var->di_tv.v_type == VAR_NUMBER
26108 || this_var->di_tv.v_type == VAR_STRING)
26109 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026110 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026111 /* Escape special characters with a backslash. Turn a LF and
26112 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000026113 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000026114 (char_u *)"\\\"\n\r");
26115 if (p == NULL) /* out of memory */
26116 break;
26117 for (t = p; *t != NUL; ++t)
26118 if (*t == '\n')
26119 *t = 'n';
26120 else if (*t == '\r')
26121 *t = 'r';
26122 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000026123 this_var->di_key,
26124 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26125 : ' ',
26126 p,
26127 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26128 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000026129 || put_eol(fd) == FAIL)
26130 {
26131 vim_free(p);
26132 return FAIL;
26133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026134 vim_free(p);
26135 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026136#ifdef FEAT_FLOAT
26137 else if (this_var->di_tv.v_type == VAR_FLOAT
26138 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
26139 {
26140 float_T f = this_var->di_tv.vval.v_float;
26141 int sign = ' ';
26142
26143 if (f < 0)
26144 {
26145 f = -f;
26146 sign = '-';
26147 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010026148 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026149 this_var->di_key, sign, f) < 0)
26150 || put_eol(fd) == FAIL)
26151 return FAIL;
26152 }
26153#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026154 }
26155 }
26156 return OK;
26157}
26158#endif
26159
Bram Moolenaar661b1822005-07-28 22:36:45 +000026160/*
26161 * Display script name where an item was last set.
26162 * Should only be invoked when 'verbose' is non-zero.
26163 */
26164 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026165last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000026166{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026167 char_u *p;
26168
Bram Moolenaar661b1822005-07-28 22:36:45 +000026169 if (scriptID != 0)
26170 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026171 p = home_replace_save(NULL, get_scriptname(scriptID));
26172 if (p != NULL)
26173 {
26174 verbose_enter();
26175 MSG_PUTS(_("\n\tLast set from "));
26176 MSG_PUTS(p);
26177 vim_free(p);
26178 verbose_leave();
26179 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026180 }
26181}
26182
Bram Moolenaard812df62008-11-09 12:46:09 +000026183/*
26184 * List v:oldfiles in a nice way.
26185 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026186 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026187ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026188{
26189 list_T *l = vimvars[VV_OLDFILES].vv_list;
26190 listitem_T *li;
26191 int nr = 0;
26192
26193 if (l == NULL)
26194 msg((char_u *)_("No old files"));
26195 else
26196 {
26197 msg_start();
26198 msg_scroll = TRUE;
26199 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26200 {
26201 msg_outnum((long)++nr);
26202 MSG_PUTS(": ");
26203 msg_outtrans(get_tv_string(&li->li_tv));
26204 msg_putchar('\n');
26205 out_flush(); /* output one line at a time */
26206 ui_breakcheck();
26207 }
26208 /* Assume "got_int" was set to truncate the listing. */
26209 got_int = FALSE;
26210
26211#ifdef FEAT_BROWSE_CMD
26212 if (cmdmod.browse)
26213 {
26214 quit_more = FALSE;
26215 nr = prompt_for_number(FALSE);
26216 msg_starthere();
26217 if (nr > 0)
26218 {
26219 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26220 (long)nr);
26221
26222 if (p != NULL)
26223 {
26224 p = expand_env_save(p);
26225 eap->arg = p;
26226 eap->cmdidx = CMD_edit;
26227 cmdmod.browse = FALSE;
26228 do_exedit(eap, NULL);
26229 vim_free(p);
26230 }
26231 }
26232 }
26233#endif
26234 }
26235}
26236
Bram Moolenaar53744302015-07-17 17:38:22 +020026237/* reset v:option_new, v:option_old and v:option_type */
26238 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026239reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026240{
26241 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26242 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26243 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26244}
26245
26246
Bram Moolenaar071d4272004-06-13 20:20:40 +000026247#endif /* FEAT_EVAL */
26248
Bram Moolenaar071d4272004-06-13 20:20:40 +000026249
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026250#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026251
26252#ifdef WIN3264
26253/*
26254 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26255 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026256static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26257static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26258static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026259
26260/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026261 * Get the short path (8.3) for the filename in "fnamep".
26262 * Only works for a valid file name.
26263 * When the path gets longer "fnamep" is changed and the allocated buffer
26264 * is put in "bufp".
26265 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26266 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026267 */
26268 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026269get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026270{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026271 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026272 char_u *newbuf;
26273
26274 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026275 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026276 if (l > len - 1)
26277 {
26278 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026279 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026280 newbuf = vim_strnsave(*fnamep, l);
26281 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026282 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026283
26284 vim_free(*bufp);
26285 *fnamep = *bufp = newbuf;
26286
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026287 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026288 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026289 }
26290
26291 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026292 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026293}
26294
26295/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026296 * Get the short path (8.3) for the filename in "fname". The converted
26297 * path is returned in "bufp".
26298 *
26299 * Some of the directories specified in "fname" may not exist. This function
26300 * will shorten the existing directories at the beginning of the path and then
26301 * append the remaining non-existing path.
26302 *
26303 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026304 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026305 * bufp - Pointer to an allocated buffer for the filename.
26306 * fnamelen - Length of the filename pointed to by fname
26307 *
26308 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026309 */
26310 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026311shortpath_for_invalid_fname(
26312 char_u **fname,
26313 char_u **bufp,
26314 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026315{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026316 char_u *short_fname, *save_fname, *pbuf_unused;
26317 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026318 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026319 int old_len, len;
26320 int new_len, sfx_len;
26321 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026322
26323 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026324 old_len = *fnamelen;
26325 save_fname = vim_strnsave(*fname, old_len);
26326 pbuf_unused = NULL;
26327 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026328
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026329 endp = save_fname + old_len - 1; /* Find the end of the copy */
26330 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026331
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026332 /*
26333 * Try shortening the supplied path till it succeeds by removing one
26334 * directory at a time from the tail of the path.
26335 */
26336 len = 0;
26337 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026338 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026339 /* go back one path-separator */
26340 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26341 --endp;
26342 if (endp <= save_fname)
26343 break; /* processed the complete path */
26344
26345 /*
26346 * Replace the path separator with a NUL and try to shorten the
26347 * resulting path.
26348 */
26349 ch = *endp;
26350 *endp = 0;
26351 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026352 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026353 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26354 {
26355 retval = FAIL;
26356 goto theend;
26357 }
26358 *endp = ch; /* preserve the string */
26359
26360 if (len > 0)
26361 break; /* successfully shortened the path */
26362
26363 /* failed to shorten the path. Skip the path separator */
26364 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026365 }
26366
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026367 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026368 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026369 /*
26370 * Succeeded in shortening the path. Now concatenate the shortened
26371 * path with the remaining path at the tail.
26372 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026373
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026374 /* Compute the length of the new path. */
26375 sfx_len = (int)(save_endp - endp) + 1;
26376 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026377
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026378 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026379 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026380 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026381 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026382 /* There is not enough space in the currently allocated string,
26383 * copy it to a buffer big enough. */
26384 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026385 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026386 {
26387 retval = FAIL;
26388 goto theend;
26389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026390 }
26391 else
26392 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026393 /* Transfer short_fname to the main buffer (it's big enough),
26394 * unless get_short_pathname() did its work in-place. */
26395 *fname = *bufp = save_fname;
26396 if (short_fname != save_fname)
26397 vim_strncpy(save_fname, short_fname, len);
26398 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026399 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026400
26401 /* concat the not-shortened part of the path */
26402 vim_strncpy(*fname + len, endp, sfx_len);
26403 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026404 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026405
26406theend:
26407 vim_free(pbuf_unused);
26408 vim_free(save_fname);
26409
26410 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026411}
26412
26413/*
26414 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026415 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026416 */
26417 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026418shortpath_for_partial(
26419 char_u **fnamep,
26420 char_u **bufp,
26421 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026422{
26423 int sepcount, len, tflen;
26424 char_u *p;
26425 char_u *pbuf, *tfname;
26426 int hasTilde;
26427
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026428 /* Count up the path separators from the RHS.. so we know which part
26429 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026430 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026431 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026432 if (vim_ispathsep(*p))
26433 ++sepcount;
26434
26435 /* Need full path first (use expand_env() to remove a "~/") */
26436 hasTilde = (**fnamep == '~');
26437 if (hasTilde)
26438 pbuf = tfname = expand_env_save(*fnamep);
26439 else
26440 pbuf = tfname = FullName_save(*fnamep, FALSE);
26441
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026442 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026443
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026444 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26445 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026446
26447 if (len == 0)
26448 {
26449 /* Don't have a valid filename, so shorten the rest of the
26450 * path if we can. This CAN give us invalid 8.3 filenames, but
26451 * there's not a lot of point in guessing what it might be.
26452 */
26453 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026454 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26455 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026456 }
26457
26458 /* Count the paths backward to find the beginning of the desired string. */
26459 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026460 {
26461#ifdef FEAT_MBYTE
26462 if (has_mbyte)
26463 p -= mb_head_off(tfname, p);
26464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026465 if (vim_ispathsep(*p))
26466 {
26467 if (sepcount == 0 || (hasTilde && sepcount == 1))
26468 break;
26469 else
26470 sepcount --;
26471 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026473 if (hasTilde)
26474 {
26475 --p;
26476 if (p >= tfname)
26477 *p = '~';
26478 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026479 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026480 }
26481 else
26482 ++p;
26483
26484 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26485 vim_free(*bufp);
26486 *fnamelen = (int)STRLEN(p);
26487 *bufp = pbuf;
26488 *fnamep = p;
26489
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026490 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026491}
26492#endif /* WIN3264 */
26493
26494/*
26495 * Adjust a filename, according to a string of modifiers.
26496 * *fnamep must be NUL terminated when called. When returning, the length is
26497 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026498 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026499 * When there is an error, *fnamep is set to NULL.
26500 */
26501 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026502modify_fname(
26503 char_u *src, /* string with modifiers */
26504 int *usedlen, /* characters after src that are used */
26505 char_u **fnamep, /* file name so far */
26506 char_u **bufp, /* buffer for allocated file name or NULL */
26507 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026508{
26509 int valid = 0;
26510 char_u *tail;
26511 char_u *s, *p, *pbuf;
26512 char_u dirname[MAXPATHL];
26513 int c;
26514 int has_fullname = 0;
26515#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026516 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026517 int has_shortname = 0;
26518#endif
26519
26520repeat:
26521 /* ":p" - full path/file_name */
26522 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26523 {
26524 has_fullname = 1;
26525
26526 valid |= VALID_PATH;
26527 *usedlen += 2;
26528
26529 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26530 if ((*fnamep)[0] == '~'
26531#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26532 && ((*fnamep)[1] == '/'
26533# ifdef BACKSLASH_IN_FILENAME
26534 || (*fnamep)[1] == '\\'
26535# endif
26536 || (*fnamep)[1] == NUL)
26537
26538#endif
26539 )
26540 {
26541 *fnamep = expand_env_save(*fnamep);
26542 vim_free(*bufp); /* free any allocated file name */
26543 *bufp = *fnamep;
26544 if (*fnamep == NULL)
26545 return -1;
26546 }
26547
26548 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026549 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026550 {
26551 if (vim_ispathsep(*p)
26552 && p[1] == '.'
26553 && (p[2] == NUL
26554 || vim_ispathsep(p[2])
26555 || (p[2] == '.'
26556 && (p[3] == NUL || vim_ispathsep(p[3])))))
26557 break;
26558 }
26559
26560 /* FullName_save() is slow, don't use it when not needed. */
26561 if (*p != NUL || !vim_isAbsName(*fnamep))
26562 {
26563 *fnamep = FullName_save(*fnamep, *p != NUL);
26564 vim_free(*bufp); /* free any allocated file name */
26565 *bufp = *fnamep;
26566 if (*fnamep == NULL)
26567 return -1;
26568 }
26569
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026570#ifdef WIN3264
26571# if _WIN32_WINNT >= 0x0500
26572 if (vim_strchr(*fnamep, '~') != NULL)
26573 {
26574 /* Expand 8.3 filename to full path. Needed to make sure the same
26575 * file does not have two different names.
26576 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26577 p = alloc(_MAX_PATH + 1);
26578 if (p != NULL)
26579 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026580 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026581 {
26582 vim_free(*bufp);
26583 *bufp = *fnamep = p;
26584 }
26585 else
26586 vim_free(p);
26587 }
26588 }
26589# endif
26590#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026591 /* Append a path separator to a directory. */
26592 if (mch_isdir(*fnamep))
26593 {
26594 /* Make room for one or two extra characters. */
26595 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26596 vim_free(*bufp); /* free any allocated file name */
26597 *bufp = *fnamep;
26598 if (*fnamep == NULL)
26599 return -1;
26600 add_pathsep(*fnamep);
26601 }
26602 }
26603
26604 /* ":." - path relative to the current directory */
26605 /* ":~" - path relative to the home directory */
26606 /* ":8" - shortname path - postponed till after */
26607 while (src[*usedlen] == ':'
26608 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26609 {
26610 *usedlen += 2;
26611 if (c == '8')
26612 {
26613#ifdef WIN3264
26614 has_shortname = 1; /* Postpone this. */
26615#endif
26616 continue;
26617 }
26618 pbuf = NULL;
26619 /* Need full path first (use expand_env() to remove a "~/") */
26620 if (!has_fullname)
26621 {
26622 if (c == '.' && **fnamep == '~')
26623 p = pbuf = expand_env_save(*fnamep);
26624 else
26625 p = pbuf = FullName_save(*fnamep, FALSE);
26626 }
26627 else
26628 p = *fnamep;
26629
26630 has_fullname = 0;
26631
26632 if (p != NULL)
26633 {
26634 if (c == '.')
26635 {
26636 mch_dirname(dirname, MAXPATHL);
26637 s = shorten_fname(p, dirname);
26638 if (s != NULL)
26639 {
26640 *fnamep = s;
26641 if (pbuf != NULL)
26642 {
26643 vim_free(*bufp); /* free any allocated file name */
26644 *bufp = pbuf;
26645 pbuf = NULL;
26646 }
26647 }
26648 }
26649 else
26650 {
26651 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26652 /* Only replace it when it starts with '~' */
26653 if (*dirname == '~')
26654 {
26655 s = vim_strsave(dirname);
26656 if (s != NULL)
26657 {
26658 *fnamep = s;
26659 vim_free(*bufp);
26660 *bufp = s;
26661 }
26662 }
26663 }
26664 vim_free(pbuf);
26665 }
26666 }
26667
26668 tail = gettail(*fnamep);
26669 *fnamelen = (int)STRLEN(*fnamep);
26670
26671 /* ":h" - head, remove "/file_name", can be repeated */
26672 /* Don't remove the first "/" or "c:\" */
26673 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26674 {
26675 valid |= VALID_HEAD;
26676 *usedlen += 2;
26677 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026678 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026679 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026680 *fnamelen = (int)(tail - *fnamep);
26681#ifdef VMS
26682 if (*fnamelen > 0)
26683 *fnamelen += 1; /* the path separator is part of the path */
26684#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026685 if (*fnamelen == 0)
26686 {
26687 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26688 p = vim_strsave((char_u *)".");
26689 if (p == NULL)
26690 return -1;
26691 vim_free(*bufp);
26692 *bufp = *fnamep = tail = p;
26693 *fnamelen = 1;
26694 }
26695 else
26696 {
26697 while (tail > s && !after_pathsep(s, tail))
26698 mb_ptr_back(*fnamep, tail);
26699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026700 }
26701
26702 /* ":8" - shortname */
26703 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26704 {
26705 *usedlen += 2;
26706#ifdef WIN3264
26707 has_shortname = 1;
26708#endif
26709 }
26710
26711#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026712 /*
26713 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026714 */
26715 if (has_shortname)
26716 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026717 /* Copy the string if it is shortened by :h and when it wasn't copied
26718 * yet, because we are going to change it in place. Avoids changing
26719 * the buffer name for "%:8". */
26720 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026721 {
26722 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026723 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026724 return -1;
26725 vim_free(*bufp);
26726 *bufp = *fnamep = p;
26727 }
26728
26729 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026730 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026731 if (!has_fullname && !vim_isAbsName(*fnamep))
26732 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026733 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026734 return -1;
26735 }
26736 else
26737 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026738 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026739
Bram Moolenaardc935552011-08-17 15:23:23 +020026740 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026741 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026742 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026743 return -1;
26744
26745 if (l == 0)
26746 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026747 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026748 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026749 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026750 return -1;
26751 }
26752 *fnamelen = l;
26753 }
26754 }
26755#endif /* WIN3264 */
26756
26757 /* ":t" - tail, just the basename */
26758 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26759 {
26760 *usedlen += 2;
26761 *fnamelen -= (int)(tail - *fnamep);
26762 *fnamep = tail;
26763 }
26764
26765 /* ":e" - extension, can be repeated */
26766 /* ":r" - root, without extension, can be repeated */
26767 while (src[*usedlen] == ':'
26768 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26769 {
26770 /* find a '.' in the tail:
26771 * - for second :e: before the current fname
26772 * - otherwise: The last '.'
26773 */
26774 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26775 s = *fnamep - 2;
26776 else
26777 s = *fnamep + *fnamelen - 1;
26778 for ( ; s > tail; --s)
26779 if (s[0] == '.')
26780 break;
26781 if (src[*usedlen + 1] == 'e') /* :e */
26782 {
26783 if (s > tail)
26784 {
26785 *fnamelen += (int)(*fnamep - (s + 1));
26786 *fnamep = s + 1;
26787#ifdef VMS
26788 /* cut version from the extension */
26789 s = *fnamep + *fnamelen - 1;
26790 for ( ; s > *fnamep; --s)
26791 if (s[0] == ';')
26792 break;
26793 if (s > *fnamep)
26794 *fnamelen = s - *fnamep;
26795#endif
26796 }
26797 else if (*fnamep <= tail)
26798 *fnamelen = 0;
26799 }
26800 else /* :r */
26801 {
26802 if (s > tail) /* remove one extension */
26803 *fnamelen = (int)(s - *fnamep);
26804 }
26805 *usedlen += 2;
26806 }
26807
26808 /* ":s?pat?foo?" - substitute */
26809 /* ":gs?pat?foo?" - global substitute */
26810 if (src[*usedlen] == ':'
26811 && (src[*usedlen + 1] == 's'
26812 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26813 {
26814 char_u *str;
26815 char_u *pat;
26816 char_u *sub;
26817 int sep;
26818 char_u *flags;
26819 int didit = FALSE;
26820
26821 flags = (char_u *)"";
26822 s = src + *usedlen + 2;
26823 if (src[*usedlen + 1] == 'g')
26824 {
26825 flags = (char_u *)"g";
26826 ++s;
26827 }
26828
26829 sep = *s++;
26830 if (sep)
26831 {
26832 /* find end of pattern */
26833 p = vim_strchr(s, sep);
26834 if (p != NULL)
26835 {
26836 pat = vim_strnsave(s, (int)(p - s));
26837 if (pat != NULL)
26838 {
26839 s = p + 1;
26840 /* find end of substitution */
26841 p = vim_strchr(s, sep);
26842 if (p != NULL)
26843 {
26844 sub = vim_strnsave(s, (int)(p - s));
26845 str = vim_strnsave(*fnamep, *fnamelen);
26846 if (sub != NULL && str != NULL)
26847 {
26848 *usedlen = (int)(p + 1 - src);
26849 s = do_string_sub(str, pat, sub, flags);
26850 if (s != NULL)
26851 {
26852 *fnamep = s;
26853 *fnamelen = (int)STRLEN(s);
26854 vim_free(*bufp);
26855 *bufp = s;
26856 didit = TRUE;
26857 }
26858 }
26859 vim_free(sub);
26860 vim_free(str);
26861 }
26862 vim_free(pat);
26863 }
26864 }
26865 /* after using ":s", repeat all the modifiers */
26866 if (didit)
26867 goto repeat;
26868 }
26869 }
26870
Bram Moolenaar26df0922014-02-23 23:39:13 +010026871 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26872 {
26873 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
26874 if (p == NULL)
26875 return -1;
26876 vim_free(*bufp);
26877 *bufp = *fnamep = p;
26878 *fnamelen = (int)STRLEN(p);
26879 *usedlen += 2;
26880 }
26881
Bram Moolenaar071d4272004-06-13 20:20:40 +000026882 return valid;
26883}
26884
26885/*
26886 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26887 * "flags" can be "g" to do a global substitute.
26888 * Returns an allocated string, NULL for error.
26889 */
26890 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026891do_string_sub(
26892 char_u *str,
26893 char_u *pat,
26894 char_u *sub,
26895 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026896{
26897 int sublen;
26898 regmatch_T regmatch;
26899 int i;
26900 int do_all;
26901 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026902 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026903 garray_T ga;
26904 char_u *ret;
26905 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026906 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026907
26908 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26909 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026910 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026911
26912 ga_init2(&ga, 1, 200);
26913
26914 do_all = (flags[0] == 'g');
26915
26916 regmatch.rm_ic = p_ic;
26917 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26918 if (regmatch.regprog != NULL)
26919 {
26920 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026921 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026922 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26923 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026924 /* Skip empty match except for first match. */
26925 if (regmatch.startp[0] == regmatch.endp[0])
26926 {
26927 if (zero_width == regmatch.startp[0])
26928 {
26929 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026930 i = MB_PTR2LEN(tail);
26931 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26932 (size_t)i);
26933 ga.ga_len += i;
26934 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026935 continue;
26936 }
26937 zero_width = regmatch.startp[0];
26938 }
26939
Bram Moolenaar071d4272004-06-13 20:20:40 +000026940 /*
26941 * Get some space for a temporary buffer to do the substitution
26942 * into. It will contain:
26943 * - The text up to where the match is.
26944 * - The substituted text.
26945 * - The text after the match.
26946 */
26947 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026948 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026949 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26950 {
26951 ga_clear(&ga);
26952 break;
26953 }
26954
26955 /* copy the text up to where the match is */
26956 i = (int)(regmatch.startp[0] - tail);
26957 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26958 /* add the substituted text */
26959 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26960 + ga.ga_len + i, TRUE, TRUE, FALSE);
26961 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026962 tail = regmatch.endp[0];
26963 if (*tail == NUL)
26964 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026965 if (!do_all)
26966 break;
26967 }
26968
26969 if (ga.ga_data != NULL)
26970 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26971
Bram Moolenaar473de612013-06-08 18:19:48 +020026972 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026973 }
26974
26975 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26976 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026977 if (p_cpo == empty_option)
26978 p_cpo = save_cpo;
26979 else
26980 /* Darn, evaluating {sub} expression changed the value. */
26981 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026982
26983 return ret;
26984}
26985
26986#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */