blob: d922e6aef943bd6dd1d71e9784df5b12abe41957 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar071d4272004-06-13 20:20:40 +000019#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
Bram Moolenaar314f11d2010-08-09 22:07:08 +020023#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#ifdef MACOS
28# include <time.h> /* for time_t */
29#endif
30
Bram Moolenaar33570922005-01-25 22:26:29 +000031#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000032
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000033#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
34 be freed. */
35
Bram Moolenaar071d4272004-06-13 20:20:40 +000036/*
Bram Moolenaar33570922005-01-25 22:26:29 +000037 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
38 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000039 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
40 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
41 * HI2DI() converts a hashitem pointer to a dictitem pointer.
42 */
Bram Moolenaar33570922005-01-25 22:26:29 +000043static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000044#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000045#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000046#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000047
48/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000049 * Structure returned by get_lval() and used by set_var_lval().
50 * For a plain name:
51 * "name" points to the variable name.
52 * "exp_name" is NULL.
53 * "tv" is NULL
54 * For a magic braces name:
55 * "name" points to the expanded variable name.
56 * "exp_name" is non-NULL, to be freed later.
57 * "tv" is NULL
58 * For an index in a list:
59 * "name" points to the (expanded) variable name.
60 * "exp_name" NULL or non-NULL, to be freed later.
61 * "tv" points to the (first) list item value
62 * "li" points to the (first) list item
63 * "range", "n1", "n2" and "empty2" indicate what items are used.
64 * For an existing Dict item:
65 * "name" points to the (expanded) variable name.
66 * "exp_name" NULL or non-NULL, to be freed later.
67 * "tv" points to the dict item value
68 * "newkey" is NULL
69 * For a non-existing Dict item:
70 * "name" points to the (expanded) variable name.
71 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000072 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000073 * "newkey" is the key for the new item.
74 */
75typedef struct lval_S
76{
77 char_u *ll_name; /* start of variable name (can be NULL) */
78 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000079 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000080 isn't NULL it's the Dict to which to add
81 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 listitem_T *ll_li; /* The list item or NULL. */
83 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000084 int ll_range; /* TRUE when a [i:j] range was used */
85 long ll_n1; /* First index for list */
86 long ll_n2; /* Second index for list range */
87 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000088 dict_T *ll_dict; /* The Dictionary or NULL */
89 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000090 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000091} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000099static char *e_listreq = N_("E714: List required");
100static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000101static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000102static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
103static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
104static char *e_funcdict = N_("E717: Dictionary entry already exists");
105static char *e_funcref = N_("E718: Funcref required");
106static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
107static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000108static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000109static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200110#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200111static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200112#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000113
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +0100114#define NAMESPACE_CHAR (char_u *)"abglstvw"
115
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200116static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000117#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000120 * Old Vim variables such as "v:version" are also available without the "v:".
121 * Also in functions. We need a special hashtable for them.
122 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000123static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124
125/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000126 * When recursively copying lists and dicts we need to remember which ones we
127 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000128 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 */
130static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131#define COPYID_INC 2
132#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000133
Bram Moolenaar8502c702014-06-17 12:51:16 +0200134/* Abort conversion to string after a recursion error. */
135static int did_echo_string_emsg = FALSE;
136
Bram Moolenaard9fba312005-06-26 22:34:35 +0000137/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000138 * Array to hold the hashtab with variables local to each sourced script.
139 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000141typedef struct
142{
143 dictitem_T sv_var;
144 dict_T sv_dict;
145} scriptvar_T;
146
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200147static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
148#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
149#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150
151static int echo_attr = 0; /* attributes used for ":echo" */
152
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000153/* Values for trans_function_name() argument: */
154#define TFN_INT 1 /* internal function name OK */
155#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100156#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
157
158/* Values for get_lval() flags argument: */
159#define GLV_QUIET TFN_QUIET /* no error messages */
160#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000161
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162/*
163 * Structure to hold info for a user function.
164 */
165typedef struct ufunc ufunc_T;
166
167struct ufunc
168{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000169 int uf_varargs; /* variable nr of arguments */
170 int uf_flags;
171 int uf_calls; /* nr of active calls */
172 garray_T uf_args; /* arguments */
173 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000174#ifdef FEAT_PROFILE
175 int uf_profiling; /* TRUE when func is being profiled */
176 /* profiling the function as a whole */
177 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000178 proftime_T uf_tm_total; /* time spent in function + children */
179 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 proftime_T uf_tm_children; /* time spent in children this call */
181 /* profiling the function per line */
182 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000183 proftime_T *uf_tml_total; /* time spent in a line + children */
184 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000185 proftime_T uf_tml_start; /* start time for current line */
186 proftime_T uf_tml_children; /* time spent in children for this line */
187 proftime_T uf_tml_wait; /* start wait time for current line */
188 int uf_tml_idx; /* index of line being timed; -1 if none */
189 int uf_tml_execed; /* line being timed was executed */
190#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000191 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 int uf_refcount; /* for numbered function: reference count */
194 char_u uf_name[1]; /* name of function (actually longer); can
195 start with <SNR>123_ (<SNR> is K_SPECIAL
196 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197};
198
199/* function flags */
200#define FC_ABORT 1 /* abort function on error */
201#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000202#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
204/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000205 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000207static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000209/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000210static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
211
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000212/* list heads for garbage collection */
213static dict_T *first_dict = NULL; /* list of all dicts */
214static list_T *first_list = NULL; /* list of all lists */
215
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000216/* From user function to hashitem and back. */
217static ufunc_T dumuf;
218#define UF2HIKEY(fp) ((fp)->uf_name)
219#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
220#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
221
222#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
223#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224
Bram Moolenaar33570922005-01-25 22:26:29 +0000225#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
226#define VAR_SHORT_LEN 20 /* short variable name length */
227#define FIXVAR_CNT 12 /* number of fixed variables */
228
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000230typedef struct funccall_S funccall_T;
231
232struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233{
234 ufunc_T *func; /* function being called */
235 int linenr; /* next line to be executed */
236 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000237 struct /* fixed variables for arguments */
238 {
239 dictitem_T var; /* variable (without room for name) */
240 char_u room[VAR_SHORT_LEN]; /* room for the name */
241 } fixvar[FIXVAR_CNT];
242 dict_T l_vars; /* l: local function variables */
243 dictitem_T l_vars_var; /* variable for l: scope */
244 dict_T l_avars; /* a: argument variables */
245 dictitem_T l_avars_var; /* variable for a: scope */
246 list_T l_varlist; /* list for a:000 */
247 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
248 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 linenr_T breakpoint; /* next line with breakpoint or zero */
250 int dbg_tick; /* debug_tick when breakpoint was set */
251 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000252#ifdef FEAT_PROFILE
253 proftime_T prof_child; /* time spent in a child */
254#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000255 funccall_T *caller; /* calling function or NULL */
256};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257
258/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000259 * Info used by a ":for" loop.
260 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000261typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262{
263 int fi_semicolon; /* TRUE if ending in '; var]' */
264 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 listwatch_T fi_lw; /* keep an eye on the item used. */
266 list_T *fi_list; /* list being used */
267} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000268
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000269/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000270 * Struct used by trans_function_name()
271 */
272typedef struct
273{
Bram Moolenaar33570922005-01-25 22:26:29 +0000274 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000275 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dictitem_T *fd_di; /* Dictionary item used */
277} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000278
Bram Moolenaara7043832005-01-21 11:56:39 +0000279
280/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000281 * Array to hold the value of v: variables.
282 * The value is in a dictitem, so that it can also be used in the v: scope.
283 * The reason to use this table anyway is for very quick access to the
284 * variables with the VV_ defines.
285 */
286#include "version.h"
287
288/* values for vv_flags: */
289#define VV_COMPAT 1 /* compatible, also used without "v:" */
290#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000291#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000292
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000293#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
295static struct vimvar
296{
297 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000298 dictitem_T vv_di; /* value and name for key */
299 char vv_filler[16]; /* space for LONGEST name below!!! */
300 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
301} vimvars[VV_LEN] =
302{
303 /*
304 * The order here must match the VV_ defines in vim.h!
305 * Initializing a union does not work, leave tv.vval empty to get zero's.
306 */
307 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
308 {VV_NAME("count1", VAR_NUMBER), VV_RO},
309 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
310 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
311 {VV_NAME("warningmsg", VAR_STRING), 0},
312 {VV_NAME("statusmsg", VAR_STRING), 0},
313 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
314 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
315 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
316 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
317 {VV_NAME("termresponse", VAR_STRING), VV_RO},
318 {VV_NAME("fname", VAR_STRING), VV_RO},
319 {VV_NAME("lang", VAR_STRING), VV_RO},
320 {VV_NAME("lc_time", VAR_STRING), VV_RO},
321 {VV_NAME("ctype", VAR_STRING), VV_RO},
322 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
323 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
324 {VV_NAME("fname_in", VAR_STRING), VV_RO},
325 {VV_NAME("fname_out", VAR_STRING), VV_RO},
326 {VV_NAME("fname_new", VAR_STRING), VV_RO},
327 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
328 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
329 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
330 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
331 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
332 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("progname", VAR_STRING), VV_RO},
334 {VV_NAME("servername", VAR_STRING), VV_RO},
335 {VV_NAME("dying", VAR_NUMBER), VV_RO},
336 {VV_NAME("exception", VAR_STRING), VV_RO},
337 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
338 {VV_NAME("register", VAR_STRING), VV_RO},
339 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
340 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000341 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
342 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000343 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000344 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
345 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000346 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
347 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000351 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000352 {VV_NAME("swapname", VAR_STRING), VV_RO},
353 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000354 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200355 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000356 {VV_NAME("mouse_win", VAR_NUMBER), 0},
357 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
358 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000359 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000360 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100361 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000362 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200363 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200364 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200365 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200366 {VV_NAME("option_new", VAR_STRING), VV_RO},
367 {VV_NAME("option_old", VAR_STRING), VV_RO},
368 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100369 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100370 {VV_NAME("false", VAR_SPECIAL), VV_RO},
371 {VV_NAME("true", VAR_SPECIAL), VV_RO},
372 {VV_NAME("null", VAR_SPECIAL), VV_RO},
373 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000374};
375
376/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000377#define vv_type vv_di.di_tv.v_type
378#define vv_nr vv_di.di_tv.vval.v_number
379#define vv_float vv_di.di_tv.vval.v_float
380#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000381#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200382#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000383#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000384
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200385static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000386#define vimvarht vimvardict.dv_hashtab
387
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100388static void prepare_vimvar(int idx, typval_T *save_tv);
389static void restore_vimvar(int idx, typval_T *save_tv);
390static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
391static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
392static char_u *skip_var_one(char_u *arg);
393static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
394static void list_glob_vars(int *first);
395static void list_buf_vars(int *first);
396static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000397#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100398static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000399#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100400static void list_vim_vars(int *first);
401static void list_script_vars(int *first);
402static void list_func_vars(int *first);
403static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
404static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
405static int check_changedtick(char_u *arg);
406static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
407static void clear_lval(lval_T *lp);
408static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
409static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
410static void list_fix_watch(list_T *l, listitem_T *item);
411static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
412static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
413static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
414static void item_lock(typval_T *tv, int deep, int lock);
415static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000416
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100417static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
418static int eval1(char_u **arg, typval_T *rettv, int evaluate);
419static int eval2(char_u **arg, typval_T *rettv, int evaluate);
420static int eval3(char_u **arg, typval_T *rettv, int evaluate);
421static int eval4(char_u **arg, typval_T *rettv, int evaluate);
422static int eval5(char_u **arg, typval_T *rettv, int evaluate);
423static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
424static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000425
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100426static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
427static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
428static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
429static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
430static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
431static long list_len(list_T *l);
432static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
433static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
434static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
435static long list_find_nr(list_T *l, long idx, int *errorp);
436static long list_idx_of_item(list_T *l, listitem_T *item);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100437static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
438static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
439static list_T *list_copy(list_T *orig, int deep, int copyID);
440static char_u *list2string(typval_T *tv, int copyID);
441static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap);
442static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo, int copyID);
443static int free_unref_items(int copyID);
444static dictitem_T *dictitem_copy(dictitem_T *org);
445static void dictitem_remove(dict_T *dict, dictitem_T *item);
446static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
447static long dict_len(dict_T *d);
448static char_u *dict2string(typval_T *tv, int copyID);
449static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
450static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
451static char_u *tv2string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
452static char_u *string_quote(char_u *str, int function);
453static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
454static int find_internal_func(char_u *name);
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100455static char_u *deref_func_name(char_u *name, int *lenp, partial_T **partial, int no_autoload);
456static int get_func_tv(char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, partial_T *partial, dict_T *selfdict);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100457static void emsg_funcname(char *ermsg, char_u *name);
458static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000459
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000460#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100461static void f_abs(typval_T *argvars, typval_T *rettv);
462static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000463#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100464static void f_add(typval_T *argvars, typval_T *rettv);
465static void f_alloc_fail(typval_T *argvars, typval_T *rettv);
466static void f_and(typval_T *argvars, typval_T *rettv);
467static void f_append(typval_T *argvars, typval_T *rettv);
468static void f_argc(typval_T *argvars, typval_T *rettv);
469static void f_argidx(typval_T *argvars, typval_T *rettv);
470static void f_arglistid(typval_T *argvars, typval_T *rettv);
471static void f_argv(typval_T *argvars, typval_T *rettv);
472static void f_assert_equal(typval_T *argvars, typval_T *rettv);
473static void f_assert_exception(typval_T *argvars, typval_T *rettv);
474static void f_assert_fails(typval_T *argvars, typval_T *rettv);
475static void f_assert_false(typval_T *argvars, typval_T *rettv);
476static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000477#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100478static void f_asin(typval_T *argvars, typval_T *rettv);
479static void f_atan(typval_T *argvars, typval_T *rettv);
480static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000481#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100482static void f_browse(typval_T *argvars, typval_T *rettv);
483static void f_browsedir(typval_T *argvars, typval_T *rettv);
484static void f_bufexists(typval_T *argvars, typval_T *rettv);
485static void f_buflisted(typval_T *argvars, typval_T *rettv);
486static void f_bufloaded(typval_T *argvars, typval_T *rettv);
487static void f_bufname(typval_T *argvars, typval_T *rettv);
488static void f_bufnr(typval_T *argvars, typval_T *rettv);
489static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
490static void f_byte2line(typval_T *argvars, typval_T *rettv);
491static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
492static void f_byteidx(typval_T *argvars, typval_T *rettv);
493static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
494static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000495#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100496static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000497#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100498#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100499static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100500static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
501static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100502static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100503static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100504static void f_ch_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100505static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100506static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
507static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100508static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100509static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100510static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
511static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100512static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100513static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100514#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100515static void f_changenr(typval_T *argvars, typval_T *rettv);
516static void f_char2nr(typval_T *argvars, typval_T *rettv);
517static void f_cindent(typval_T *argvars, typval_T *rettv);
518static void f_clearmatches(typval_T *argvars, typval_T *rettv);
519static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000520#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100521static void f_complete(typval_T *argvars, typval_T *rettv);
522static void f_complete_add(typval_T *argvars, typval_T *rettv);
523static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000524#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100525static void f_confirm(typval_T *argvars, typval_T *rettv);
526static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000527#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100528static void f_cos(typval_T *argvars, typval_T *rettv);
529static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000530#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100531static void f_count(typval_T *argvars, typval_T *rettv);
532static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
533static void f_cursor(typval_T *argsvars, typval_T *rettv);
534static void f_deepcopy(typval_T *argvars, typval_T *rettv);
535static void f_delete(typval_T *argvars, typval_T *rettv);
536static void f_did_filetype(typval_T *argvars, typval_T *rettv);
537static void f_diff_filler(typval_T *argvars, typval_T *rettv);
538static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +0100539static void f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100540static void f_empty(typval_T *argvars, typval_T *rettv);
541static void f_escape(typval_T *argvars, typval_T *rettv);
542static void f_eval(typval_T *argvars, typval_T *rettv);
543static void f_eventhandler(typval_T *argvars, typval_T *rettv);
544static void f_executable(typval_T *argvars, typval_T *rettv);
545static void f_exepath(typval_T *argvars, typval_T *rettv);
546static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200547#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100548static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200549#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100550static void f_expand(typval_T *argvars, typval_T *rettv);
551static void f_extend(typval_T *argvars, typval_T *rettv);
552static void f_feedkeys(typval_T *argvars, typval_T *rettv);
553static void f_filereadable(typval_T *argvars, typval_T *rettv);
554static void f_filewritable(typval_T *argvars, typval_T *rettv);
555static void f_filter(typval_T *argvars, typval_T *rettv);
556static void f_finddir(typval_T *argvars, typval_T *rettv);
557static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000558#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100559static void f_float2nr(typval_T *argvars, typval_T *rettv);
560static void f_floor(typval_T *argvars, typval_T *rettv);
561static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000562#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100563static void f_fnameescape(typval_T *argvars, typval_T *rettv);
564static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
565static void f_foldclosed(typval_T *argvars, typval_T *rettv);
566static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
567static void f_foldlevel(typval_T *argvars, typval_T *rettv);
568static void f_foldtext(typval_T *argvars, typval_T *rettv);
569static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
570static void f_foreground(typval_T *argvars, typval_T *rettv);
571static void f_function(typval_T *argvars, typval_T *rettv);
572static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
573static void f_get(typval_T *argvars, typval_T *rettv);
574static void f_getbufline(typval_T *argvars, typval_T *rettv);
575static void f_getbufvar(typval_T *argvars, typval_T *rettv);
576static void f_getchar(typval_T *argvars, typval_T *rettv);
577static void f_getcharmod(typval_T *argvars, typval_T *rettv);
578static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
579static void f_getcmdline(typval_T *argvars, typval_T *rettv);
580static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
581static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
582static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
583static void f_getcwd(typval_T *argvars, typval_T *rettv);
584static void f_getfontname(typval_T *argvars, typval_T *rettv);
585static void f_getfperm(typval_T *argvars, typval_T *rettv);
586static void f_getfsize(typval_T *argvars, typval_T *rettv);
587static void f_getftime(typval_T *argvars, typval_T *rettv);
588static void f_getftype(typval_T *argvars, typval_T *rettv);
589static void f_getline(typval_T *argvars, typval_T *rettv);
590static void f_getmatches(typval_T *argvars, typval_T *rettv);
591static void f_getpid(typval_T *argvars, typval_T *rettv);
592static void f_getcurpos(typval_T *argvars, typval_T *rettv);
593static void f_getpos(typval_T *argvars, typval_T *rettv);
594static void f_getqflist(typval_T *argvars, typval_T *rettv);
595static void f_getreg(typval_T *argvars, typval_T *rettv);
596static void f_getregtype(typval_T *argvars, typval_T *rettv);
597static void f_gettabvar(typval_T *argvars, typval_T *rettv);
598static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
599static void f_getwinposx(typval_T *argvars, typval_T *rettv);
600static void f_getwinposy(typval_T *argvars, typval_T *rettv);
601static void f_getwinvar(typval_T *argvars, typval_T *rettv);
602static void f_glob(typval_T *argvars, typval_T *rettv);
603static void f_globpath(typval_T *argvars, typval_T *rettv);
604static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
605static void f_has(typval_T *argvars, typval_T *rettv);
606static void f_has_key(typval_T *argvars, typval_T *rettv);
607static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
608static void f_hasmapto(typval_T *argvars, typval_T *rettv);
609static void f_histadd(typval_T *argvars, typval_T *rettv);
610static void f_histdel(typval_T *argvars, typval_T *rettv);
611static void f_histget(typval_T *argvars, typval_T *rettv);
612static void f_histnr(typval_T *argvars, typval_T *rettv);
613static void f_hlID(typval_T *argvars, typval_T *rettv);
614static void f_hlexists(typval_T *argvars, typval_T *rettv);
615static void f_hostname(typval_T *argvars, typval_T *rettv);
616static void f_iconv(typval_T *argvars, typval_T *rettv);
617static void f_indent(typval_T *argvars, typval_T *rettv);
618static void f_index(typval_T *argvars, typval_T *rettv);
619static void f_input(typval_T *argvars, typval_T *rettv);
620static void f_inputdialog(typval_T *argvars, typval_T *rettv);
621static void f_inputlist(typval_T *argvars, typval_T *rettv);
622static void f_inputrestore(typval_T *argvars, typval_T *rettv);
623static void f_inputsave(typval_T *argvars, typval_T *rettv);
624static void f_inputsecret(typval_T *argvars, typval_T *rettv);
625static void f_insert(typval_T *argvars, typval_T *rettv);
626static void f_invert(typval_T *argvars, typval_T *rettv);
627static void f_isdirectory(typval_T *argvars, typval_T *rettv);
628static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100629#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
630static void f_isnan(typval_T *argvars, typval_T *rettv);
631#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100632static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100633#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100634static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8950a562016-03-12 15:22:55 +0100635static void f_job_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar65edff82016-02-21 16:40:11 +0100636static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100637static void f_job_start(typval_T *argvars, typval_T *rettv);
638static void f_job_stop(typval_T *argvars, typval_T *rettv);
639static void f_job_status(typval_T *argvars, typval_T *rettv);
640#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100641static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100642static void f_js_decode(typval_T *argvars, typval_T *rettv);
643static void f_js_encode(typval_T *argvars, typval_T *rettv);
644static void f_json_decode(typval_T *argvars, typval_T *rettv);
645static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100646static void f_keys(typval_T *argvars, typval_T *rettv);
647static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
648static void f_len(typval_T *argvars, typval_T *rettv);
649static void f_libcall(typval_T *argvars, typval_T *rettv);
650static void f_libcallnr(typval_T *argvars, typval_T *rettv);
651static void f_line(typval_T *argvars, typval_T *rettv);
652static void f_line2byte(typval_T *argvars, typval_T *rettv);
653static void f_lispindent(typval_T *argvars, typval_T *rettv);
654static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000655#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100656static void f_log(typval_T *argvars, typval_T *rettv);
657static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000658#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200659#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100660static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200661#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100662static void f_map(typval_T *argvars, typval_T *rettv);
663static void f_maparg(typval_T *argvars, typval_T *rettv);
664static void f_mapcheck(typval_T *argvars, typval_T *rettv);
665static void f_match(typval_T *argvars, typval_T *rettv);
666static void f_matchadd(typval_T *argvars, typval_T *rettv);
667static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
668static void f_matcharg(typval_T *argvars, typval_T *rettv);
669static void f_matchdelete(typval_T *argvars, typval_T *rettv);
670static void f_matchend(typval_T *argvars, typval_T *rettv);
671static void f_matchlist(typval_T *argvars, typval_T *rettv);
672static void f_matchstr(typval_T *argvars, typval_T *rettv);
673static void f_max(typval_T *argvars, typval_T *rettv);
674static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000675#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100676static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000677#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100678static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100679#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100680static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100681#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100682static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
683static void f_nr2char(typval_T *argvars, typval_T *rettv);
684static void f_or(typval_T *argvars, typval_T *rettv);
685static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100686#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100687static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100688#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000689#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100690static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000691#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100692static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
693static void f_printf(typval_T *argvars, typval_T *rettv);
694static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200695#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100696static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200697#endif
698#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100699static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200700#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100701static void f_range(typval_T *argvars, typval_T *rettv);
702static void f_readfile(typval_T *argvars, typval_T *rettv);
703static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100704#ifdef FEAT_FLOAT
705static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
706#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100707static void f_reltimestr(typval_T *argvars, typval_T *rettv);
708static void f_remote_expr(typval_T *argvars, typval_T *rettv);
709static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
710static void f_remote_peek(typval_T *argvars, typval_T *rettv);
711static void f_remote_read(typval_T *argvars, typval_T *rettv);
712static void f_remote_send(typval_T *argvars, typval_T *rettv);
713static void f_remove(typval_T *argvars, typval_T *rettv);
714static void f_rename(typval_T *argvars, typval_T *rettv);
715static void f_repeat(typval_T *argvars, typval_T *rettv);
716static void f_resolve(typval_T *argvars, typval_T *rettv);
717static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000718#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100719static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000720#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100721static void f_screenattr(typval_T *argvars, typval_T *rettv);
722static void f_screenchar(typval_T *argvars, typval_T *rettv);
723static void f_screencol(typval_T *argvars, typval_T *rettv);
724static void f_screenrow(typval_T *argvars, typval_T *rettv);
725static void f_search(typval_T *argvars, typval_T *rettv);
726static void f_searchdecl(typval_T *argvars, typval_T *rettv);
727static void f_searchpair(typval_T *argvars, typval_T *rettv);
728static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
729static void f_searchpos(typval_T *argvars, typval_T *rettv);
730static void f_server2client(typval_T *argvars, typval_T *rettv);
731static void f_serverlist(typval_T *argvars, typval_T *rettv);
732static void f_setbufvar(typval_T *argvars, typval_T *rettv);
733static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
734static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100735static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100736static void f_setline(typval_T *argvars, typval_T *rettv);
737static void f_setloclist(typval_T *argvars, typval_T *rettv);
738static void f_setmatches(typval_T *argvars, typval_T *rettv);
739static void f_setpos(typval_T *argvars, typval_T *rettv);
740static void f_setqflist(typval_T *argvars, typval_T *rettv);
741static void f_setreg(typval_T *argvars, typval_T *rettv);
742static void f_settabvar(typval_T *argvars, typval_T *rettv);
743static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
744static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100745#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100746static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100747#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100748static void f_shellescape(typval_T *argvars, typval_T *rettv);
749static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
750static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000751#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100752static void f_sin(typval_T *argvars, typval_T *rettv);
753static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000754#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100755static void f_sort(typval_T *argvars, typval_T *rettv);
756static void f_soundfold(typval_T *argvars, typval_T *rettv);
757static void f_spellbadword(typval_T *argvars, typval_T *rettv);
758static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
759static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000760#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100761static void f_sqrt(typval_T *argvars, typval_T *rettv);
762static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000763#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100764static void f_str2nr(typval_T *argvars, typval_T *rettv);
765static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000766#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100767static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000768#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100769static void f_stridx(typval_T *argvars, typval_T *rettv);
770static void f_string(typval_T *argvars, typval_T *rettv);
771static void f_strlen(typval_T *argvars, typval_T *rettv);
772static void f_strpart(typval_T *argvars, typval_T *rettv);
773static void f_strridx(typval_T *argvars, typval_T *rettv);
774static void f_strtrans(typval_T *argvars, typval_T *rettv);
775static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
776static void f_strwidth(typval_T *argvars, typval_T *rettv);
777static void f_submatch(typval_T *argvars, typval_T *rettv);
778static void f_substitute(typval_T *argvars, typval_T *rettv);
779static void f_synID(typval_T *argvars, typval_T *rettv);
780static void f_synIDattr(typval_T *argvars, typval_T *rettv);
781static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
782static void f_synstack(typval_T *argvars, typval_T *rettv);
783static void f_synconcealed(typval_T *argvars, typval_T *rettv);
784static void f_system(typval_T *argvars, typval_T *rettv);
785static void f_systemlist(typval_T *argvars, typval_T *rettv);
786static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
787static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
788static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
789static void f_taglist(typval_T *argvars, typval_T *rettv);
790static void f_tagfiles(typval_T *argvars, typval_T *rettv);
791static void f_tempname(typval_T *argvars, typval_T *rettv);
792static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200793#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100794static void f_tan(typval_T *argvars, typval_T *rettv);
795static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200796#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +0100797#ifdef FEAT_TIMERS
798static void f_timer_start(typval_T *argvars, typval_T *rettv);
799static void f_timer_stop(typval_T *argvars, typval_T *rettv);
800#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100801static void f_tolower(typval_T *argvars, typval_T *rettv);
802static void f_toupper(typval_T *argvars, typval_T *rettv);
803static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000804#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100805static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000806#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100807static void f_type(typval_T *argvars, typval_T *rettv);
808static void f_undofile(typval_T *argvars, typval_T *rettv);
809static void f_undotree(typval_T *argvars, typval_T *rettv);
810static void f_uniq(typval_T *argvars, typval_T *rettv);
811static void f_values(typval_T *argvars, typval_T *rettv);
812static void f_virtcol(typval_T *argvars, typval_T *rettv);
813static void f_visualmode(typval_T *argvars, typval_T *rettv);
814static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +0100815static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar86edef62016-03-13 18:07:30 +0100816static void f_win_getid(typval_T *argvars, typval_T *rettv);
817static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
818static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
819static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100820static void f_winbufnr(typval_T *argvars, typval_T *rettv);
821static void f_wincol(typval_T *argvars, typval_T *rettv);
822static void f_winheight(typval_T *argvars, typval_T *rettv);
823static void f_winline(typval_T *argvars, typval_T *rettv);
824static void f_winnr(typval_T *argvars, typval_T *rettv);
825static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
826static void f_winrestview(typval_T *argvars, typval_T *rettv);
827static void f_winsaveview(typval_T *argvars, typval_T *rettv);
828static void f_winwidth(typval_T *argvars, typval_T *rettv);
829static void f_writefile(typval_T *argvars, typval_T *rettv);
830static void f_wordcount(typval_T *argvars, typval_T *rettv);
831static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000832
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100833static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
834static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
835static int get_env_len(char_u **arg);
836static int get_id_len(char_u **arg);
837static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
838static 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 +0000839#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
840#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
841 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100842static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
843static int eval_isnamec(int c);
844static int eval_isnamec1(int c);
845static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
846static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100847static typval_T *alloc_string_tv(char_u *string);
848static void init_tv(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100849#ifdef FEAT_FLOAT
850static float_T get_tv_float(typval_T *varp);
851#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100852static linenr_T get_tv_lnum(typval_T *argvars);
853static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100854static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
855static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
856static hashtab_T *find_var_ht(char_u *name, char_u **varname);
857static funccall_T *get_funccal(void);
858static void vars_clear_ext(hashtab_T *ht, int free_val);
859static void delete_var(hashtab_T *ht, hashitem_T *hi);
860static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
861static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
862static void set_var(char_u *name, typval_T *varp, int copy);
863static int var_check_ro(int flags, char_u *name, int use_gettext);
864static int var_check_fixed(int flags, char_u *name, int use_gettext);
865static int var_check_func_name(char_u *name, int new_var);
866static int valid_varname(char_u *varname);
867static int tv_check_lock(int lock, char_u *name, int use_gettext);
868static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
869static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar65639032016-03-16 21:40:30 +0100870static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd, partial_T **partial);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100871static int eval_fname_script(char_u *p);
872static int eval_fname_sid(char_u *p);
873static void list_func_head(ufunc_T *fp, int indent);
874static ufunc_T *find_func(char_u *name);
875static int function_exists(char_u *name);
876static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000877#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100878static void func_do_profile(ufunc_T *fp);
879static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
880static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000881static int
882# ifdef __BORLANDC__
883 _RTLENTRYF
884# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100885 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000886static int
887# ifdef __BORLANDC__
888 _RTLENTRYF
889# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100890 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000891#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100892static int script_autoload(char_u *name, int reload);
893static char_u *autoload_name(char_u *name);
894static void cat_func_name(char_u *buf, ufunc_T *fp);
895static void func_free(ufunc_T *fp);
896static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
897static int can_free_funccal(funccall_T *fc, int copyID) ;
898static void free_funccal(funccall_T *fc, int free_val);
899static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
900static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
901static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
902static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
903static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
904static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
905static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
906static int write_list(FILE *fd, list_T *list, int binary);
907static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000908
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200909
910#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100911static int compare_func_name(const void *s1, const void *s2);
912static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200913#endif
914
Bram Moolenaar33570922005-01-25 22:26:29 +0000915/*
916 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000917 */
918 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100919eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000920{
Bram Moolenaar33570922005-01-25 22:26:29 +0000921 int i;
922 struct vimvar *p;
923
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200924 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
925 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200926 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000927 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000928 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000929
930 for (i = 0; i < VV_LEN; ++i)
931 {
932 p = &vimvars[i];
933 STRCPY(p->vv_di.di_key, p->vv_name);
934 if (p->vv_flags & VV_RO)
935 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
936 else if (p->vv_flags & VV_RO_SBX)
937 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
938 else
939 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000940
941 /* add to v: scope dict, unless the value is not always available */
942 if (p->vv_type != VAR_UNKNOWN)
943 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000944 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000945 /* add to compat scope dict */
946 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000947 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100948 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
949
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000950 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100951 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200952 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100953 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100954
955 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
956 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
957 set_vim_var_nr(VV_NONE, VVAL_NONE);
958 set_vim_var_nr(VV_NULL, VVAL_NULL);
959
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200960 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200961
962#ifdef EBCDIC
963 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100964 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200965 */
966 sortFunctions();
967#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000968}
969
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000970#if defined(EXITFREE) || defined(PROTO)
971 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100972eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000973{
974 int i;
975 struct vimvar *p;
976
977 for (i = 0; i < VV_LEN; ++i)
978 {
979 p = &vimvars[i];
980 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000981 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000982 vim_free(p->vv_str);
983 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000984 }
985 else if (p->vv_di.di_tv.v_type == VAR_LIST)
986 {
987 list_unref(p->vv_list);
988 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000989 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000990 }
991 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000992 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000993 hash_clear(&compat_hashtab);
994
Bram Moolenaard9fba312005-06-26 22:34:35 +0000995 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100996# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200997 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100998# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000999
1000 /* global variables */
1001 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +00001002
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001003 /* autoloaded script names */
1004 ga_clear_strings(&ga_loaded);
1005
Bram Moolenaarcca74132013-09-25 21:00:28 +02001006 /* Script-local variables. First clear all the variables and in a second
1007 * loop free the scriptvar_T, because a variable in one script might hold
1008 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001009 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001010 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001011 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001012 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001013 ga_clear(&ga_scripts);
1014
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001015 /* unreferenced lists and dicts */
1016 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001017
1018 /* functions */
1019 free_all_functions();
1020 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001021}
1022#endif
1023
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001024/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 * Return the name of the executed function.
1026 */
1027 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001028func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001030 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031}
1032
1033/*
1034 * Return the address holding the next breakpoint line for a funccall cookie.
1035 */
1036 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001037func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038{
Bram Moolenaar33570922005-01-25 22:26:29 +00001039 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040}
1041
1042/*
1043 * Return the address holding the debug tick for a funccall cookie.
1044 */
1045 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001046func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047{
Bram Moolenaar33570922005-01-25 22:26:29 +00001048 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049}
1050
1051/*
1052 * Return the nesting level for a funccall cookie.
1053 */
1054 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001055func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056{
Bram Moolenaar33570922005-01-25 22:26:29 +00001057 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058}
1059
1060/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001061funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001063/* pointer to list of previously used funccal, still around because some
1064 * item in it is still being used. */
1065funccall_T *previous_funccal = NULL;
1066
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067/*
1068 * Return TRUE when a function was ended by a ":return" command.
1069 */
1070 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001071current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072{
1073 return current_funccal->returned;
1074}
1075
1076
1077/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 * Set an internal variable to a string value. Creates the variable if it does
1079 * not already exist.
1080 */
1081 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001082set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001084 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001085 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086
1087 val = vim_strsave(value);
1088 if (val != NULL)
1089 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001090 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001091 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001093 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001094 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 }
1096 }
1097}
1098
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001099static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001100static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001101static char_u *redir_endp = NULL;
1102static char_u *redir_varname = NULL;
1103
1104/*
1105 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001106 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001107 * Returns OK if successfully completed the setup. FAIL otherwise.
1108 */
1109 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001110var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111{
1112 int save_emsg;
1113 int err;
1114 typval_T tv;
1115
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001116 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001117 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001118 {
1119 EMSG(_(e_invarg));
1120 return FAIL;
1121 }
1122
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001123 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001124 redir_varname = vim_strsave(name);
1125 if (redir_varname == NULL)
1126 return FAIL;
1127
1128 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1129 if (redir_lval == NULL)
1130 {
1131 var_redir_stop();
1132 return FAIL;
1133 }
1134
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001135 /* The output is stored in growarray "redir_ga" until redirection ends. */
1136 ga_init2(&redir_ga, (int)sizeof(char), 500);
1137
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001139 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001140 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001141 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1142 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001143 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001144 if (redir_endp != NULL && *redir_endp != NUL)
1145 /* Trailing characters are present after the variable name */
1146 EMSG(_(e_trailing));
1147 else
1148 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001149 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001150 var_redir_stop();
1151 return FAIL;
1152 }
1153
1154 /* check if we can write to the variable: set it to or append an empty
1155 * string */
1156 save_emsg = did_emsg;
1157 did_emsg = FALSE;
1158 tv.v_type = VAR_STRING;
1159 tv.vval.v_string = (char_u *)"";
1160 if (append)
1161 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1162 else
1163 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001164 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001165 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001166 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001167 if (err)
1168 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001169 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001170 var_redir_stop();
1171 return FAIL;
1172 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001173
1174 return OK;
1175}
1176
1177/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001178 * Append "value[value_len]" to the variable set by var_redir_start().
1179 * The actual appending is postponed until redirection ends, because the value
1180 * appended may in fact be the string we write to, changing it may cause freed
1181 * memory to be used:
1182 * :redir => foo
1183 * :let foo
1184 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001185 */
1186 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001187var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001188{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001189 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001190
1191 if (redir_lval == NULL)
1192 return;
1193
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001194 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001195 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001196 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001197 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001198
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001199 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001200 {
1201 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001202 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001203 }
1204 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001205 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001206}
1207
1208/*
1209 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001210 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001211 */
1212 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001213var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001214{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001215 typval_T tv;
1216
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001217 if (redir_lval != NULL)
1218 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001219 /* If there was no error: assign the text to the variable. */
1220 if (redir_endp != NULL)
1221 {
1222 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1223 tv.v_type = VAR_STRING;
1224 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001225 /* Call get_lval() again, if it's inside a Dict or List it may
1226 * have changed. */
1227 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001228 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001229 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1230 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1231 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001232 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001233
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001234 /* free the collected output */
1235 vim_free(redir_ga.ga_data);
1236 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001237
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001238 vim_free(redir_lval);
1239 redir_lval = NULL;
1240 }
1241 vim_free(redir_varname);
1242 redir_varname = NULL;
1243}
1244
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245# if defined(FEAT_MBYTE) || defined(PROTO)
1246 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001247eval_charconvert(
1248 char_u *enc_from,
1249 char_u *enc_to,
1250 char_u *fname_from,
1251 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252{
1253 int err = FALSE;
1254
1255 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1256 set_vim_var_string(VV_CC_TO, enc_to, -1);
1257 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1258 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1259 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1260 err = TRUE;
1261 set_vim_var_string(VV_CC_FROM, NULL, -1);
1262 set_vim_var_string(VV_CC_TO, NULL, -1);
1263 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1264 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1265
1266 if (err)
1267 return FAIL;
1268 return OK;
1269}
1270# endif
1271
1272# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1273 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001274eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275{
1276 int err = FALSE;
1277
1278 set_vim_var_string(VV_FNAME_IN, fname, -1);
1279 set_vim_var_string(VV_CMDARG, args, -1);
1280 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1281 err = TRUE;
1282 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1283 set_vim_var_string(VV_CMDARG, NULL, -1);
1284
1285 if (err)
1286 {
1287 mch_remove(fname);
1288 return FAIL;
1289 }
1290 return OK;
1291}
1292# endif
1293
1294# if defined(FEAT_DIFF) || defined(PROTO)
1295 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001296eval_diff(
1297 char_u *origfile,
1298 char_u *newfile,
1299 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300{
1301 int err = FALSE;
1302
1303 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1304 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1305 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1306 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1307 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1308 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1309 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1310}
1311
1312 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001313eval_patch(
1314 char_u *origfile,
1315 char_u *difffile,
1316 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317{
1318 int err;
1319
1320 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1321 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1322 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1323 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1324 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1325 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1326 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1327}
1328# endif
1329
1330/*
1331 * Top level evaluation function, returning a boolean.
1332 * Sets "error" to TRUE if there was an error.
1333 * Return TRUE or FALSE.
1334 */
1335 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001336eval_to_bool(
1337 char_u *arg,
1338 int *error,
1339 char_u **nextcmd,
1340 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341{
Bram Moolenaar33570922005-01-25 22:26:29 +00001342 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 int retval = FALSE;
1344
1345 if (skip)
1346 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001347 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 else
1350 {
1351 *error = FALSE;
1352 if (!skip)
1353 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001354 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001355 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 }
1357 }
1358 if (skip)
1359 --emsg_skip;
1360
1361 return retval;
1362}
1363
1364/*
1365 * Top level evaluation function, returning a string. If "skip" is TRUE,
1366 * only parsing to "nextcmd" is done, without reporting errors. Return
1367 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1368 */
1369 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001370eval_to_string_skip(
1371 char_u *arg,
1372 char_u **nextcmd,
1373 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374{
Bram Moolenaar33570922005-01-25 22:26:29 +00001375 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 char_u *retval;
1377
1378 if (skip)
1379 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001380 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 retval = NULL;
1382 else
1383 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001384 retval = vim_strsave(get_tv_string(&tv));
1385 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 }
1387 if (skip)
1388 --emsg_skip;
1389
1390 return retval;
1391}
1392
1393/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001394 * Skip over an expression at "*pp".
1395 * Return FAIL for an error, OK otherwise.
1396 */
1397 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001398skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001399{
Bram Moolenaar33570922005-01-25 22:26:29 +00001400 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001401
1402 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001403 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001404}
1405
1406/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001408 * When "convert" is TRUE convert a List into a sequence of lines and convert
1409 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 * Return pointer to allocated memory, or NULL for failure.
1411 */
1412 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001413eval_to_string(
1414 char_u *arg,
1415 char_u **nextcmd,
1416 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417{
Bram Moolenaar33570922005-01-25 22:26:29 +00001418 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001420 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001421#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001422 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001425 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 retval = NULL;
1427 else
1428 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001429 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001430 {
1431 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001432 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001433 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001434 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001435 if (tv.vval.v_list->lv_len > 0)
1436 ga_append(&ga, NL);
1437 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001438 ga_append(&ga, NUL);
1439 retval = (char_u *)ga.ga_data;
1440 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001441#ifdef FEAT_FLOAT
1442 else if (convert && tv.v_type == VAR_FLOAT)
1443 {
1444 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1445 retval = vim_strsave(numbuf);
1446 }
1447#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001448 else
1449 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001450 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 }
1452
1453 return retval;
1454}
1455
1456/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001457 * Call eval_to_string() without using current local variables and using
1458 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 */
1460 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001461eval_to_string_safe(
1462 char_u *arg,
1463 char_u **nextcmd,
1464 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465{
1466 char_u *retval;
1467 void *save_funccalp;
1468
1469 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001470 if (use_sandbox)
1471 ++sandbox;
1472 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001473 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001474 if (use_sandbox)
1475 --sandbox;
1476 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 restore_funccal(save_funccalp);
1478 return retval;
1479}
1480
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481/*
1482 * Top level evaluation function, returning a number.
1483 * Evaluates "expr" silently.
1484 * Returns -1 for an error.
1485 */
1486 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001487eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488{
Bram Moolenaar33570922005-01-25 22:26:29 +00001489 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001491 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492
1493 ++emsg_off;
1494
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001495 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 retval = -1;
1497 else
1498 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001499 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001500 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 }
1502 --emsg_off;
1503
1504 return retval;
1505}
1506
Bram Moolenaara40058a2005-07-11 22:42:07 +00001507/*
1508 * Prepare v: variable "idx" to be used.
1509 * Save the current typeval in "save_tv".
1510 * When not used yet add the variable to the v: hashtable.
1511 */
1512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001513prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001514{
1515 *save_tv = vimvars[idx].vv_tv;
1516 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1517 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1518}
1519
1520/*
1521 * Restore v: variable "idx" to typeval "save_tv".
1522 * When no longer defined, remove the variable from the v: hashtable.
1523 */
1524 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001525restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001526{
1527 hashitem_T *hi;
1528
Bram Moolenaara40058a2005-07-11 22:42:07 +00001529 vimvars[idx].vv_tv = *save_tv;
1530 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1531 {
1532 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1533 if (HASHITEM_EMPTY(hi))
1534 EMSG2(_(e_intern2), "restore_vimvar()");
1535 else
1536 hash_remove(&vimvarht, hi);
1537 }
1538}
1539
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001540#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001541/*
1542 * Evaluate an expression to a list with suggestions.
1543 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001544 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001545 */
1546 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001547eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001548{
1549 typval_T save_val;
1550 typval_T rettv;
1551 list_T *list = NULL;
1552 char_u *p = skipwhite(expr);
1553
1554 /* Set "v:val" to the bad word. */
1555 prepare_vimvar(VV_VAL, &save_val);
1556 vimvars[VV_VAL].vv_type = VAR_STRING;
1557 vimvars[VV_VAL].vv_str = badword;
1558 if (p_verbose == 0)
1559 ++emsg_off;
1560
1561 if (eval1(&p, &rettv, TRUE) == OK)
1562 {
1563 if (rettv.v_type != VAR_LIST)
1564 clear_tv(&rettv);
1565 else
1566 list = rettv.vval.v_list;
1567 }
1568
1569 if (p_verbose == 0)
1570 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001571 restore_vimvar(VV_VAL, &save_val);
1572
1573 return list;
1574}
1575
1576/*
1577 * "list" is supposed to contain two items: a word and a number. Return the
1578 * word in "pp" and the number as the return value.
1579 * Return -1 if anything isn't right.
1580 * Used to get the good word and score from the eval_spell_expr() result.
1581 */
1582 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001583get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001584{
1585 listitem_T *li;
1586
1587 li = list->lv_first;
1588 if (li == NULL)
1589 return -1;
1590 *pp = get_tv_string(&li->li_tv);
1591
1592 li = li->li_next;
1593 if (li == NULL)
1594 return -1;
1595 return get_tv_number(&li->li_tv);
1596}
1597#endif
1598
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001599/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001600 * Top level evaluation function.
1601 * Returns an allocated typval_T with the result.
1602 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001603 */
1604 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001605eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001606{
1607 typval_T *tv;
1608
1609 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001610 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001611 {
1612 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001613 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001614 }
1615
1616 return tv;
1617}
1618
1619
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001621 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001622 * Uses argv[argc] for the function arguments. Only Number and String
1623 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001624 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001626 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001627call_vim_function(
1628 char_u *func,
1629 int argc,
1630 char_u **argv,
1631 int safe, /* use the sandbox */
1632 int str_arg_only, /* all arguments are strings */
1633 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634{
Bram Moolenaar33570922005-01-25 22:26:29 +00001635 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 long n;
1637 int len;
1638 int i;
1639 int doesrange;
1640 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001641 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001643 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001645 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646
1647 for (i = 0; i < argc; i++)
1648 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001649 /* Pass a NULL or empty argument as an empty string */
1650 if (argv[i] == NULL || *argv[i] == NUL)
1651 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001652 argvars[i].v_type = VAR_STRING;
1653 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001654 continue;
1655 }
1656
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001657 if (str_arg_only)
1658 len = 0;
1659 else
1660 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001661 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 if (len != 0 && len == (int)STRLEN(argv[i]))
1663 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001664 argvars[i].v_type = VAR_NUMBER;
1665 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 }
1667 else
1668 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001669 argvars[i].v_type = VAR_STRING;
1670 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 }
1672 }
1673
1674 if (safe)
1675 {
1676 save_funccalp = save_funccal();
1677 ++sandbox;
1678 }
1679
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001680 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1681 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001683 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 if (safe)
1685 {
1686 --sandbox;
1687 restore_funccal(save_funccalp);
1688 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001689 vim_free(argvars);
1690
1691 if (ret == FAIL)
1692 clear_tv(rettv);
1693
1694 return ret;
1695}
1696
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001697/*
1698 * Call vimL function "func" and return the result as a number.
1699 * Returns -1 when calling the function fails.
1700 * Uses argv[argc] for the function arguments.
1701 */
1702 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001703call_func_retnr(
1704 char_u *func,
1705 int argc,
1706 char_u **argv,
1707 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001708{
1709 typval_T rettv;
1710 long retval;
1711
1712 /* All arguments are passed as strings, no conversion to number. */
1713 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1714 return -1;
1715
1716 retval = get_tv_number_chk(&rettv, NULL);
1717 clear_tv(&rettv);
1718 return retval;
1719}
1720
1721#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1722 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1723
Bram Moolenaar4f688582007-07-24 12:34:30 +00001724# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001725/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001726 * Call vimL function "func" and return the result as a string.
1727 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001728 * Uses argv[argc] for the function arguments.
1729 */
1730 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001731call_func_retstr(
1732 char_u *func,
1733 int argc,
1734 char_u **argv,
1735 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001736{
1737 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001738 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001739
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001740 /* All arguments are passed as strings, no conversion to number. */
1741 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001742 return NULL;
1743
1744 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001745 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 return retval;
1747}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001748# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001749
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001750/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001751 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001752 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001753 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001754 */
1755 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001756call_func_retlist(
1757 char_u *func,
1758 int argc,
1759 char_u **argv,
1760 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001761{
1762 typval_T rettv;
1763
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001764 /* All arguments are passed as strings, no conversion to number. */
1765 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001766 return NULL;
1767
1768 if (rettv.v_type != VAR_LIST)
1769 {
1770 clear_tv(&rettv);
1771 return NULL;
1772 }
1773
1774 return rettv.vval.v_list;
1775}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776#endif
1777
1778/*
1779 * Save the current function call pointer, and set it to NULL.
1780 * Used when executing autocommands and for ":source".
1781 */
1782 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001783save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001785 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 current_funccal = NULL;
1788 return (void *)fc;
1789}
1790
1791 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001792restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001794 funccall_T *fc = (funccall_T *)vfc;
1795
1796 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797}
1798
Bram Moolenaar05159a02005-02-26 23:04:13 +00001799#if defined(FEAT_PROFILE) || defined(PROTO)
1800/*
1801 * Prepare profiling for entering a child or something else that is not
1802 * counted for the script/function itself.
1803 * Should always be called in pair with prof_child_exit().
1804 */
1805 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001806prof_child_enter(
1807 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001808{
1809 funccall_T *fc = current_funccal;
1810
1811 if (fc != NULL && fc->func->uf_profiling)
1812 profile_start(&fc->prof_child);
1813 script_prof_save(tm);
1814}
1815
1816/*
1817 * Take care of time spent in a child.
1818 * Should always be called after prof_child_enter().
1819 */
1820 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001821prof_child_exit(
1822 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001823{
1824 funccall_T *fc = current_funccal;
1825
1826 if (fc != NULL && fc->func->uf_profiling)
1827 {
1828 profile_end(&fc->prof_child);
1829 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1830 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1831 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1832 }
1833 script_prof_restore(tm);
1834}
1835#endif
1836
1837
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838#ifdef FEAT_FOLDING
1839/*
1840 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1841 * it in "*cp". Doesn't give error messages.
1842 */
1843 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001844eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845{
Bram Moolenaar33570922005-01-25 22:26:29 +00001846 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 int retval;
1848 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001849 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1850 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851
1852 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001853 if (use_sandbox)
1854 ++sandbox;
1855 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001857 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 retval = 0;
1859 else
1860 {
1861 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001862 if (tv.v_type == VAR_NUMBER)
1863 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001864 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 retval = 0;
1866 else
1867 {
1868 /* If the result is a string, check if there is a non-digit before
1869 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001870 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 if (!VIM_ISDIGIT(*s) && *s != '-')
1872 *cp = *s++;
1873 retval = atol((char *)s);
1874 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001875 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 }
1877 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001878 if (use_sandbox)
1879 --sandbox;
1880 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881
1882 return retval;
1883}
1884#endif
1885
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001887 * ":let" list all variable values
1888 * ":let var1 var2" list variable values
1889 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001890 * ":let var += expr" assignment command.
1891 * ":let var -= expr" assignment command.
1892 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001893 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 */
1895 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001896ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897{
1898 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001899 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001900 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001902 int var_count = 0;
1903 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001904 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001905 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001906 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907
Bram Moolenaardb552d602006-03-23 22:59:57 +00001908 argend = skip_var_list(arg, &var_count, &semicolon);
1909 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001910 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001911 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1912 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001913 expr = skipwhite(argend);
1914 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1915 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001917 /*
1918 * ":let" without "=": list variables
1919 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001920 if (*arg == '[')
1921 EMSG(_(e_invarg));
1922 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001923 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001924 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001925 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001926 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001927 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001928 list_glob_vars(&first);
1929 list_buf_vars(&first);
1930 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001931#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001932 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001933#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001934 list_script_vars(&first);
1935 list_func_vars(&first);
1936 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 eap->nextcmd = check_nextcmd(arg);
1939 }
1940 else
1941 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001942 op[0] = '=';
1943 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001944 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001945 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001946 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1947 op[0] = *expr; /* +=, -= or .= */
1948 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001949 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001950 else
1951 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001952
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 if (eap->skip)
1954 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001955 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 if (eap->skip)
1957 {
1958 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001959 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 --emsg_skip;
1961 }
1962 else if (i != FAIL)
1963 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001964 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001965 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001966 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 }
1968 }
1969}
1970
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001971/*
1972 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1973 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001974 * When "nextchars" is not NULL it points to a string with characters that
1975 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1976 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001977 * Returns OK or FAIL;
1978 */
1979 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001980ex_let_vars(
1981 char_u *arg_start,
1982 typval_T *tv,
1983 int copy, /* copy values from "tv", don't move */
1984 int semicolon, /* from skip_var_list() */
1985 int var_count, /* from skip_var_list() */
1986 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001987{
1988 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001989 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001990 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001991 listitem_T *item;
1992 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001993
1994 if (*arg != '[')
1995 {
1996 /*
1997 * ":let var = expr" or ":for var in list"
1998 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001999 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002000 return FAIL;
2001 return OK;
2002 }
2003
2004 /*
2005 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2006 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002007 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002008 {
2009 EMSG(_(e_listreq));
2010 return FAIL;
2011 }
2012
2013 i = list_len(l);
2014 if (semicolon == 0 && var_count < i)
2015 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002016 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002017 return FAIL;
2018 }
2019 if (var_count - semicolon > i)
2020 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002021 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002022 return FAIL;
2023 }
2024
2025 item = l->lv_first;
2026 while (*arg != ']')
2027 {
2028 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002029 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002030 item = item->li_next;
2031 if (arg == NULL)
2032 return FAIL;
2033
2034 arg = skipwhite(arg);
2035 if (*arg == ';')
2036 {
2037 /* Put the rest of the list (may be empty) in the var after ';'.
2038 * Create a new list for this. */
2039 l = list_alloc();
2040 if (l == NULL)
2041 return FAIL;
2042 while (item != NULL)
2043 {
2044 list_append_tv(l, &item->li_tv);
2045 item = item->li_next;
2046 }
2047
2048 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002049 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002050 ltv.vval.v_list = l;
2051 l->lv_refcount = 1;
2052
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002053 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2054 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002055 clear_tv(&ltv);
2056 if (arg == NULL)
2057 return FAIL;
2058 break;
2059 }
2060 else if (*arg != ',' && *arg != ']')
2061 {
2062 EMSG2(_(e_intern2), "ex_let_vars()");
2063 return FAIL;
2064 }
2065 }
2066
2067 return OK;
2068}
2069
2070/*
2071 * Skip over assignable variable "var" or list of variables "[var, var]".
2072 * Used for ":let varvar = expr" and ":for varvar in expr".
2073 * For "[var, var]" increment "*var_count" for each variable.
2074 * for "[var, var; var]" set "semicolon".
2075 * Return NULL for an error.
2076 */
2077 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002078skip_var_list(
2079 char_u *arg,
2080 int *var_count,
2081 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002082{
2083 char_u *p, *s;
2084
2085 if (*arg == '[')
2086 {
2087 /* "[var, var]": find the matching ']'. */
2088 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002089 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002090 {
2091 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2092 s = skip_var_one(p);
2093 if (s == p)
2094 {
2095 EMSG2(_(e_invarg2), p);
2096 return NULL;
2097 }
2098 ++*var_count;
2099
2100 p = skipwhite(s);
2101 if (*p == ']')
2102 break;
2103 else if (*p == ';')
2104 {
2105 if (*semicolon == 1)
2106 {
2107 EMSG(_("Double ; in list of variables"));
2108 return NULL;
2109 }
2110 *semicolon = 1;
2111 }
2112 else if (*p != ',')
2113 {
2114 EMSG2(_(e_invarg2), p);
2115 return NULL;
2116 }
2117 }
2118 return p + 1;
2119 }
2120 else
2121 return skip_var_one(arg);
2122}
2123
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002124/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002125 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002126 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002127 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002128 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002129skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002130{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002131 if (*arg == '@' && arg[1] != NUL)
2132 return arg + 2;
2133 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2134 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002135}
2136
Bram Moolenaara7043832005-01-21 11:56:39 +00002137/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002138 * List variables for hashtab "ht" with prefix "prefix".
2139 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002140 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002142list_hashtable_vars(
2143 hashtab_T *ht,
2144 char_u *prefix,
2145 int empty,
2146 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002147{
Bram Moolenaar33570922005-01-25 22:26:29 +00002148 hashitem_T *hi;
2149 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002150 int todo;
2151
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002152 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002153 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2154 {
2155 if (!HASHITEM_EMPTY(hi))
2156 {
2157 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002158 di = HI2DI(hi);
2159 if (empty || di->di_tv.v_type != VAR_STRING
2160 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002161 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002162 }
2163 }
2164}
2165
2166/*
2167 * List global variables.
2168 */
2169 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002170list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002171{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002172 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002173}
2174
2175/*
2176 * List buffer variables.
2177 */
2178 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002179list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002180{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002181 char_u numbuf[NUMBUFLEN];
2182
Bram Moolenaar429fa852013-04-15 12:27:36 +02002183 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002184 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002185
2186 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002187 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2188 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002189}
2190
2191/*
2192 * List window variables.
2193 */
2194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002195list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002196{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002197 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002198 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002199}
2200
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002201#ifdef FEAT_WINDOWS
2202/*
2203 * List tab page variables.
2204 */
2205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002206list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002207{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002208 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002209 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002210}
2211#endif
2212
Bram Moolenaara7043832005-01-21 11:56:39 +00002213/*
2214 * List Vim variables.
2215 */
2216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002217list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002218{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002219 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002220}
2221
2222/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002223 * List script-local variables, if there is a script.
2224 */
2225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002226list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002227{
2228 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002229 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2230 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002231}
2232
2233/*
2234 * List function variables, if there is a function.
2235 */
2236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002237list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002238{
2239 if (current_funccal != NULL)
2240 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002241 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002242}
2243
2244/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245 * List variables in "arg".
2246 */
2247 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002248list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249{
2250 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002251 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002252 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002253 char_u *name_start;
2254 char_u *arg_subsc;
2255 char_u *tofree;
2256 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002257
2258 while (!ends_excmd(*arg) && !got_int)
2259 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002260 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002261 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002262 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002263 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2264 {
2265 emsg_severe = TRUE;
2266 EMSG(_(e_trailing));
2267 break;
2268 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002269 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002270 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002271 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 /* get_name_len() takes care of expanding curly braces */
2273 name_start = name = arg;
2274 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2275 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002276 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002277 /* This is mainly to keep test 49 working: when expanding
2278 * curly braces fails overrule the exception error message. */
2279 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002280 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002281 emsg_severe = TRUE;
2282 EMSG2(_(e_invarg2), arg);
2283 break;
2284 }
2285 error = TRUE;
2286 }
2287 else
2288 {
2289 if (tofree != NULL)
2290 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002291 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002292 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002293 else
2294 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002295 /* handle d.key, l[idx], f(expr) */
2296 arg_subsc = arg;
2297 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002298 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002299 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002300 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002301 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002302 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002303 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002304 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002305 case 'g': list_glob_vars(first); break;
2306 case 'b': list_buf_vars(first); break;
2307 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002308#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002309 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002310#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002311 case 'v': list_vim_vars(first); break;
2312 case 's': list_script_vars(first); break;
2313 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002314 default:
2315 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002316 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002317 }
2318 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002319 {
2320 char_u numbuf[NUMBUFLEN];
2321 char_u *tf;
2322 int c;
2323 char_u *s;
2324
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002325 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002326 c = *arg;
2327 *arg = NUL;
2328 list_one_var_a((char_u *)"",
2329 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002330 tv.v_type,
2331 s == NULL ? (char_u *)"" : s,
2332 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002333 *arg = c;
2334 vim_free(tf);
2335 }
2336 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002337 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002338 }
2339 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002340
2341 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002342 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002343
2344 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002345 }
2346
2347 return arg;
2348}
2349
2350/*
2351 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2352 * Returns a pointer to the char just after the var name.
2353 * Returns NULL if there is an error.
2354 */
2355 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002356ex_let_one(
2357 char_u *arg, /* points to variable name */
2358 typval_T *tv, /* value to assign to variable */
2359 int copy, /* copy value from "tv" */
2360 char_u *endchars, /* valid chars after variable name or NULL */
2361 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002362{
2363 int c1;
2364 char_u *name;
2365 char_u *p;
2366 char_u *arg_end = NULL;
2367 int len;
2368 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002369 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002370
2371 /*
2372 * ":let $VAR = expr": Set environment variable.
2373 */
2374 if (*arg == '$')
2375 {
2376 /* Find the end of the name. */
2377 ++arg;
2378 name = arg;
2379 len = get_env_len(&arg);
2380 if (len == 0)
2381 EMSG2(_(e_invarg2), name - 1);
2382 else
2383 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002384 if (op != NULL && (*op == '+' || *op == '-'))
2385 EMSG2(_(e_letwrong), op);
2386 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002387 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002388 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002389 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002390 {
2391 c1 = name[len];
2392 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002393 p = get_tv_string_chk(tv);
2394 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002395 {
2396 int mustfree = FALSE;
2397 char_u *s = vim_getenv(name, &mustfree);
2398
2399 if (s != NULL)
2400 {
2401 p = tofree = concat_str(s, p);
2402 if (mustfree)
2403 vim_free(s);
2404 }
2405 }
2406 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002407 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002408 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002409 if (STRICMP(name, "HOME") == 0)
2410 init_homedir();
2411 else if (didset_vim && STRICMP(name, "VIM") == 0)
2412 didset_vim = FALSE;
2413 else if (didset_vimruntime
2414 && STRICMP(name, "VIMRUNTIME") == 0)
2415 didset_vimruntime = FALSE;
2416 arg_end = arg;
2417 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002418 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002419 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002420 }
2421 }
2422 }
2423
2424 /*
2425 * ":let &option = expr": Set option value.
2426 * ":let &l:option = expr": Set local option value.
2427 * ":let &g:option = expr": Set global option value.
2428 */
2429 else if (*arg == '&')
2430 {
2431 /* Find the end of the name. */
2432 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002433 if (p == NULL || (endchars != NULL
2434 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002435 EMSG(_(e_letunexp));
2436 else
2437 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002438 long n;
2439 int opt_type;
2440 long numval;
2441 char_u *stringval = NULL;
2442 char_u *s;
2443
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002444 c1 = *p;
2445 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002446
2447 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002448 s = get_tv_string_chk(tv); /* != NULL if number or string */
2449 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002450 {
2451 opt_type = get_option_value(arg, &numval,
2452 &stringval, opt_flags);
2453 if ((opt_type == 1 && *op == '.')
2454 || (opt_type == 0 && *op != '.'))
2455 EMSG2(_(e_letwrong), op);
2456 else
2457 {
2458 if (opt_type == 1) /* number */
2459 {
2460 if (*op == '+')
2461 n = numval + n;
2462 else
2463 n = numval - n;
2464 }
2465 else if (opt_type == 0 && stringval != NULL) /* string */
2466 {
2467 s = concat_str(stringval, s);
2468 vim_free(stringval);
2469 stringval = s;
2470 }
2471 }
2472 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002473 if (s != NULL)
2474 {
2475 set_option_value(arg, n, s, opt_flags);
2476 arg_end = p;
2477 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002478 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002479 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002480 }
2481 }
2482
2483 /*
2484 * ":let @r = expr": Set register contents.
2485 */
2486 else if (*arg == '@')
2487 {
2488 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002489 if (op != NULL && (*op == '+' || *op == '-'))
2490 EMSG2(_(e_letwrong), op);
2491 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002492 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002493 EMSG(_(e_letunexp));
2494 else
2495 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002496 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002497 char_u *s;
2498
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002499 p = get_tv_string_chk(tv);
2500 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002501 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002502 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002503 if (s != NULL)
2504 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002505 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002506 vim_free(s);
2507 }
2508 }
2509 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002510 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002511 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002512 arg_end = arg + 1;
2513 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002514 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002515 }
2516 }
2517
2518 /*
2519 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002521 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002522 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002523 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002524 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002525
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002526 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002527 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002528 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002529 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2530 EMSG(_(e_letunexp));
2531 else
2532 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002533 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002534 arg_end = p;
2535 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002536 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002538 }
2539
2540 else
2541 EMSG2(_(e_invarg2), arg);
2542
2543 return arg_end;
2544}
2545
2546/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002547 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2548 */
2549 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002550check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002551{
2552 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2553 {
2554 EMSG2(_(e_readonlyvar), arg);
2555 return TRUE;
2556 }
2557 return FALSE;
2558}
2559
2560/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561 * Get an lval: variable, Dict item or List item that can be assigned a value
2562 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2563 * "name.key", "name.key[expr]" etc.
2564 * Indexing only works if "name" is an existing List or Dictionary.
2565 * "name" points to the start of the name.
2566 * If "rettv" is not NULL it points to the value to be assigned.
2567 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2568 * wrong; must end in space or cmd separator.
2569 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002570 * flags:
2571 * GLV_QUIET: do not give error messages
2572 * GLV_NO_AUTOLOAD: do not use script autoloading
2573 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002574 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002575 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002576 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002577 */
2578 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002579get_lval(
2580 char_u *name,
2581 typval_T *rettv,
2582 lval_T *lp,
2583 int unlet,
2584 int skip,
2585 int flags, /* GLV_ values */
2586 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002587{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002588 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002589 char_u *expr_start, *expr_end;
2590 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002591 dictitem_T *v;
2592 typval_T var1;
2593 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002594 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002595 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002596 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002597 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002598 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002599 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002600
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002602 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603
2604 if (skip)
2605 {
2606 /* When skipping just find the end of the name. */
2607 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002608 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 }
2610
2611 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002612 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002613 if (expr_start != NULL)
2614 {
2615 /* Don't expand the name when we already know there is an error. */
2616 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2617 && *p != '[' && *p != '.')
2618 {
2619 EMSG(_(e_trailing));
2620 return NULL;
2621 }
2622
2623 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2624 if (lp->ll_exp_name == NULL)
2625 {
2626 /* Report an invalid expression in braces, unless the
2627 * expression evaluation has been cancelled due to an
2628 * aborting error, an interrupt, or an exception. */
2629 if (!aborting() && !quiet)
2630 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002631 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002632 EMSG2(_(e_invarg2), name);
2633 return NULL;
2634 }
2635 }
2636 lp->ll_name = lp->ll_exp_name;
2637 }
2638 else
2639 lp->ll_name = name;
2640
2641 /* Without [idx] or .key we are done. */
2642 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2643 return p;
2644
2645 cc = *p;
2646 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002647 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 if (v == NULL && !quiet)
2649 EMSG2(_(e_undefvar), lp->ll_name);
2650 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002651 if (v == NULL)
2652 return NULL;
2653
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 /*
2655 * Loop until no more [idx] or .key is following.
2656 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002657 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002659 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2661 && !(lp->ll_tv->v_type == VAR_DICT
2662 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002663 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 if (!quiet)
2665 EMSG(_("E689: Can only index a List or Dictionary"));
2666 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002667 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002669 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002670 if (!quiet)
2671 EMSG(_("E708: [:] must come last"));
2672 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002673 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002674
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 len = -1;
2676 if (*p == '.')
2677 {
2678 key = p + 1;
2679 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2680 ;
2681 if (len == 0)
2682 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002683 if (!quiet)
2684 EMSG(_(e_emptykey));
2685 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002686 }
2687 p = key + len;
2688 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002689 else
2690 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002692 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 if (*p == ':')
2694 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002695 else
2696 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 empty1 = FALSE;
2698 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002700 if (get_tv_string_chk(&var1) == NULL)
2701 {
2702 /* not a number or string */
2703 clear_tv(&var1);
2704 return NULL;
2705 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 }
2707
2708 /* Optionally get the second index [ :expr]. */
2709 if (*p == ':')
2710 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002714 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002715 if (!empty1)
2716 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002717 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002718 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 if (rettv != NULL && (rettv->v_type != VAR_LIST
2720 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 if (!quiet)
2723 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 if (!empty1)
2725 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002726 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727 }
2728 p = skipwhite(p + 1);
2729 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002730 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002731 else
2732 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002733 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002734 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2735 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002736 if (!empty1)
2737 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002738 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002739 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002740 if (get_tv_string_chk(&var2) == NULL)
2741 {
2742 /* not a number or string */
2743 if (!empty1)
2744 clear_tv(&var1);
2745 clear_tv(&var2);
2746 return NULL;
2747 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002748 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002750 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002751 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002753
Bram Moolenaar8c711452005-01-14 21:53:12 +00002754 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002755 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002756 if (!quiet)
2757 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002758 if (!empty1)
2759 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002760 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002761 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002762 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002763 }
2764
2765 /* Skip to past ']'. */
2766 ++p;
2767 }
2768
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002770 {
2771 if (len == -1)
2772 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002774 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002775 if (*key == NUL)
2776 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002777 if (!quiet)
2778 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002779 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002780 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002781 }
2782 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002783 lp->ll_list = NULL;
2784 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002785 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002786
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002787 /* When assigning to a scope dictionary check that a function and
2788 * variable name is valid (only variable name unless it is l: or
2789 * g: dictionary). Disallow overwriting a builtin function. */
2790 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002791 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002792 int prevval;
2793 int wrong;
2794
2795 if (len != -1)
2796 {
2797 prevval = key[len];
2798 key[len] = NUL;
2799 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002800 else
2801 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002802 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2803 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002804 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002805 || !valid_varname(key);
2806 if (len != -1)
2807 key[len] = prevval;
2808 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002809 return NULL;
2810 }
2811
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002812 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002813 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002814 /* Can't add "v:" variable. */
2815 if (lp->ll_dict == &vimvardict)
2816 {
2817 EMSG2(_(e_illvar), name);
2818 return NULL;
2819 }
2820
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002821 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002823 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002825 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002826 if (len == -1)
2827 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002828 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002829 }
2830 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002831 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002832 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002833 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002834 if (len == -1)
2835 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002837 p = NULL;
2838 break;
2839 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002840 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002841 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002842 return NULL;
2843
Bram Moolenaar8c711452005-01-14 21:53:12 +00002844 if (len == -1)
2845 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002846 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002847 }
2848 else
2849 {
2850 /*
2851 * Get the number and item for the only or first index of the List.
2852 */
2853 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002854 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002855 else
2856 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002857 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002858 clear_tv(&var1);
2859 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002860 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002861 lp->ll_list = lp->ll_tv->vval.v_list;
2862 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2863 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002864 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002865 if (lp->ll_n1 < 0)
2866 {
2867 lp->ll_n1 = 0;
2868 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2869 }
2870 }
2871 if (lp->ll_li == NULL)
2872 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002873 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002874 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002875 if (!quiet)
2876 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002878 }
2879
2880 /*
2881 * May need to find the item or absolute index for the second
2882 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883 * When no index given: "lp->ll_empty2" is TRUE.
2884 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002885 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002887 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002888 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002889 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002890 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002891 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002893 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002894 {
2895 if (!quiet)
2896 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002897 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002898 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002899 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002900 }
2901
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002902 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2903 if (lp->ll_n1 < 0)
2904 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2905 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002906 {
2907 if (!quiet)
2908 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002909 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002910 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002911 }
2912
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002914 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002915 }
2916
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002917 return p;
2918}
2919
2920/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002921 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002922 */
2923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002924clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925{
2926 vim_free(lp->ll_exp_name);
2927 vim_free(lp->ll_newkey);
2928}
2929
2930/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002931 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002932 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002933 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002934 */
2935 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002936set_var_lval(
2937 lval_T *lp,
2938 char_u *endp,
2939 typval_T *rettv,
2940 int copy,
2941 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002942{
2943 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002944 listitem_T *ri;
2945 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002946
2947 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002948 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002949 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002950 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002951 cc = *endp;
2952 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002953 if (op != NULL && *op != '=')
2954 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002955 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002956
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002957 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002958 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002959 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002960 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002961 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002962 if ((di == NULL
2963 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2964 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2965 FALSE)))
2966 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002967 set_var(lp->ll_name, &tv, FALSE);
2968 clear_tv(&tv);
2969 }
2970 }
2971 else
2972 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002973 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002974 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002975 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002976 else if (tv_check_lock(lp->ll_newkey == NULL
2977 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002978 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002979 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002980 else if (lp->ll_range)
2981 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002982 listitem_T *ll_li = lp->ll_li;
2983 int ll_n1 = lp->ll_n1;
2984
2985 /*
2986 * Check whether any of the list items is locked
2987 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002988 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002989 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002990 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002991 return;
2992 ri = ri->li_next;
2993 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2994 break;
2995 ll_li = ll_li->li_next;
2996 ++ll_n1;
2997 }
2998
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002999 /*
3000 * Assign the List values to the list items.
3001 */
3002 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003003 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003004 if (op != NULL && *op != '=')
3005 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3006 else
3007 {
3008 clear_tv(&lp->ll_li->li_tv);
3009 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3010 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003011 ri = ri->li_next;
3012 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3013 break;
3014 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003015 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003016 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003017 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003018 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003019 ri = NULL;
3020 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003021 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003022 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003023 lp->ll_li = lp->ll_li->li_next;
3024 ++lp->ll_n1;
3025 }
3026 if (ri != NULL)
3027 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003028 else if (lp->ll_empty2
3029 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003030 : lp->ll_n1 != lp->ll_n2)
3031 EMSG(_("E711: List value has not enough items"));
3032 }
3033 else
3034 {
3035 /*
3036 * Assign to a List or Dictionary item.
3037 */
3038 if (lp->ll_newkey != NULL)
3039 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003040 if (op != NULL && *op != '=')
3041 {
3042 EMSG2(_(e_letwrong), op);
3043 return;
3044 }
3045
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003046 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003047 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003048 if (di == NULL)
3049 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003050 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3051 {
3052 vim_free(di);
3053 return;
3054 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003055 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003056 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003057 else if (op != NULL && *op != '=')
3058 {
3059 tv_op(lp->ll_tv, rettv, op);
3060 return;
3061 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003062 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003063 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003064
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003065 /*
3066 * Assign the value to the variable or list item.
3067 */
3068 if (copy)
3069 copy_tv(rettv, lp->ll_tv);
3070 else
3071 {
3072 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003073 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003074 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003075 }
3076 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003077}
3078
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003079/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003080 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3081 * Returns OK or FAIL.
3082 */
3083 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003084tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003085{
3086 long n;
3087 char_u numbuf[NUMBUFLEN];
3088 char_u *s;
3089
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003090 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3091 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3092 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003093 {
3094 switch (tv1->v_type)
3095 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003096 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003097 case VAR_DICT:
3098 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003099 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003100 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003101 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003102 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003103 break;
3104
3105 case VAR_LIST:
3106 if (*op != '+' || tv2->v_type != VAR_LIST)
3107 break;
3108 /* List += List */
3109 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3110 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3111 return OK;
3112
3113 case VAR_NUMBER:
3114 case VAR_STRING:
3115 if (tv2->v_type == VAR_LIST)
3116 break;
3117 if (*op == '+' || *op == '-')
3118 {
3119 /* nr += nr or nr -= nr*/
3120 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003121#ifdef FEAT_FLOAT
3122 if (tv2->v_type == VAR_FLOAT)
3123 {
3124 float_T f = n;
3125
3126 if (*op == '+')
3127 f += tv2->vval.v_float;
3128 else
3129 f -= tv2->vval.v_float;
3130 clear_tv(tv1);
3131 tv1->v_type = VAR_FLOAT;
3132 tv1->vval.v_float = f;
3133 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003134 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003135#endif
3136 {
3137 if (*op == '+')
3138 n += get_tv_number(tv2);
3139 else
3140 n -= get_tv_number(tv2);
3141 clear_tv(tv1);
3142 tv1->v_type = VAR_NUMBER;
3143 tv1->vval.v_number = n;
3144 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003145 }
3146 else
3147 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003148 if (tv2->v_type == VAR_FLOAT)
3149 break;
3150
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003151 /* str .= str */
3152 s = get_tv_string(tv1);
3153 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3154 clear_tv(tv1);
3155 tv1->v_type = VAR_STRING;
3156 tv1->vval.v_string = s;
3157 }
3158 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003159
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003160 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003161#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003162 {
3163 float_T f;
3164
3165 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3166 && tv2->v_type != VAR_NUMBER
3167 && tv2->v_type != VAR_STRING))
3168 break;
3169 if (tv2->v_type == VAR_FLOAT)
3170 f = tv2->vval.v_float;
3171 else
3172 f = get_tv_number(tv2);
3173 if (*op == '+')
3174 tv1->vval.v_float += f;
3175 else
3176 tv1->vval.v_float -= f;
3177 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003178#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003179 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003180 }
3181 }
3182
3183 EMSG2(_(e_letwrong), op);
3184 return FAIL;
3185}
3186
3187/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003188 * Add a watcher to a list.
3189 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003190 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003191list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003192{
3193 lw->lw_next = l->lv_watch;
3194 l->lv_watch = lw;
3195}
3196
3197/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003198 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003199 * No warning when it isn't found...
3200 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003201 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003202list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003203{
Bram Moolenaar33570922005-01-25 22:26:29 +00003204 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003205
3206 lwp = &l->lv_watch;
3207 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3208 {
3209 if (lw == lwrem)
3210 {
3211 *lwp = lw->lw_next;
3212 break;
3213 }
3214 lwp = &lw->lw_next;
3215 }
3216}
3217
3218/*
3219 * Just before removing an item from a list: advance watchers to the next
3220 * item.
3221 */
3222 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003223list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003224{
Bram Moolenaar33570922005-01-25 22:26:29 +00003225 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003226
3227 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3228 if (lw->lw_item == item)
3229 lw->lw_item = item->li_next;
3230}
3231
3232/*
3233 * Evaluate the expression used in a ":for var in expr" command.
3234 * "arg" points to "var".
3235 * Set "*errp" to TRUE for an error, FALSE otherwise;
3236 * Return a pointer that holds the info. Null when there is an error.
3237 */
3238 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003239eval_for_line(
3240 char_u *arg,
3241 int *errp,
3242 char_u **nextcmdp,
3243 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003244{
Bram Moolenaar33570922005-01-25 22:26:29 +00003245 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003246 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003247 typval_T tv;
3248 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003249
3250 *errp = TRUE; /* default: there is an error */
3251
Bram Moolenaar33570922005-01-25 22:26:29 +00003252 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003253 if (fi == NULL)
3254 return NULL;
3255
3256 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3257 if (expr == NULL)
3258 return fi;
3259
3260 expr = skipwhite(expr);
3261 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3262 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003263 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003264 return fi;
3265 }
3266
3267 if (skip)
3268 ++emsg_skip;
3269 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3270 {
3271 *errp = FALSE;
3272 if (!skip)
3273 {
3274 l = tv.vval.v_list;
3275 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003276 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003277 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003278 clear_tv(&tv);
3279 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003280 else
3281 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003282 /* No need to increment the refcount, it's already set for the
3283 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003284 fi->fi_list = l;
3285 list_add_watch(l, &fi->fi_lw);
3286 fi->fi_lw.lw_item = l->lv_first;
3287 }
3288 }
3289 }
3290 if (skip)
3291 --emsg_skip;
3292
3293 return fi;
3294}
3295
3296/*
3297 * Use the first item in a ":for" list. Advance to the next.
3298 * Assign the values to the variable (list). "arg" points to the first one.
3299 * Return TRUE when a valid item was found, FALSE when at end of list or
3300 * something wrong.
3301 */
3302 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003303next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003304{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003305 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003306 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003307 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003308
3309 item = fi->fi_lw.lw_item;
3310 if (item == NULL)
3311 result = FALSE;
3312 else
3313 {
3314 fi->fi_lw.lw_item = item->li_next;
3315 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3316 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3317 }
3318 return result;
3319}
3320
3321/*
3322 * Free the structure used to store info used by ":for".
3323 */
3324 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003325free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003326{
Bram Moolenaar33570922005-01-25 22:26:29 +00003327 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003328
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003329 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003330 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003331 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003332 list_unref(fi->fi_list);
3333 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003334 vim_free(fi);
3335}
3336
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3338
3339 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003340set_context_for_expression(
3341 expand_T *xp,
3342 char_u *arg,
3343 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344{
3345 int got_eq = FALSE;
3346 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003347 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003349 if (cmdidx == CMD_let)
3350 {
3351 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003352 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003353 {
3354 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003355 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003356 {
3357 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003358 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003359 if (vim_iswhite(*p))
3360 break;
3361 }
3362 return;
3363 }
3364 }
3365 else
3366 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3367 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 while ((xp->xp_pattern = vim_strpbrk(arg,
3369 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3370 {
3371 c = *xp->xp_pattern;
3372 if (c == '&')
3373 {
3374 c = xp->xp_pattern[1];
3375 if (c == '&')
3376 {
3377 ++xp->xp_pattern;
3378 xp->xp_context = cmdidx != CMD_let || got_eq
3379 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3380 }
3381 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003382 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003384 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3385 xp->xp_pattern += 2;
3386
3387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 }
3389 else if (c == '$')
3390 {
3391 /* environment variable */
3392 xp->xp_context = EXPAND_ENV_VARS;
3393 }
3394 else if (c == '=')
3395 {
3396 got_eq = TRUE;
3397 xp->xp_context = EXPAND_EXPRESSION;
3398 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003399 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 && xp->xp_context == EXPAND_FUNCTIONS
3401 && vim_strchr(xp->xp_pattern, '(') == NULL)
3402 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003403 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 break;
3405 }
3406 else if (cmdidx != CMD_let || got_eq)
3407 {
3408 if (c == '"') /* string */
3409 {
3410 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3411 if (c == '\\' && xp->xp_pattern[1] != NUL)
3412 ++xp->xp_pattern;
3413 xp->xp_context = EXPAND_NOTHING;
3414 }
3415 else if (c == '\'') /* literal string */
3416 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003417 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3419 /* skip */ ;
3420 xp->xp_context = EXPAND_NOTHING;
3421 }
3422 else if (c == '|')
3423 {
3424 if (xp->xp_pattern[1] == '|')
3425 {
3426 ++xp->xp_pattern;
3427 xp->xp_context = EXPAND_EXPRESSION;
3428 }
3429 else
3430 xp->xp_context = EXPAND_COMMANDS;
3431 }
3432 else
3433 xp->xp_context = EXPAND_EXPRESSION;
3434 }
3435 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003436 /* Doesn't look like something valid, expand as an expression
3437 * anyway. */
3438 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 arg = xp->xp_pattern;
3440 if (*arg != NUL)
3441 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3442 /* skip */ ;
3443 }
3444 xp->xp_pattern = arg;
3445}
3446
3447#endif /* FEAT_CMDL_COMPL */
3448
3449/*
3450 * ":1,25call func(arg1, arg2)" function call.
3451 */
3452 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003453ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454{
3455 char_u *arg = eap->arg;
3456 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003458 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003460 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 linenr_T lnum;
3462 int doesrange;
3463 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003464 funcdict_T fudi;
Bram Moolenaar9e63f612016-03-17 23:13:28 +01003465 partial_T *partial = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003467 if (eap->skip)
3468 {
3469 /* trans_function_name() doesn't work well when skipping, use eval0()
3470 * instead to skip to any following command, e.g. for:
3471 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003472 ++emsg_skip;
3473 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3474 clear_tv(&rettv);
3475 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003476 return;
3477 }
3478
Bram Moolenaar65639032016-03-16 21:40:30 +01003479 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003480 if (fudi.fd_newkey != NULL)
3481 {
3482 /* Still need to give an error message for missing key. */
3483 EMSG2(_(e_dictkey), fudi.fd_newkey);
3484 vim_free(fudi.fd_newkey);
3485 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003486 if (tofree == NULL)
3487 return;
3488
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003489 /* Increase refcount on dictionary, it could get deleted when evaluating
3490 * the arguments. */
3491 if (fudi.fd_dict != NULL)
3492 ++fudi.fd_dict->dv_refcount;
3493
Bram Moolenaar65639032016-03-16 21:40:30 +01003494 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3495 * contents. For VAR_PARTIAL get its partial, unless we already have one
3496 * from trans_function_name(). */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003497 len = (int)STRLEN(tofree);
Bram Moolenaar65639032016-03-16 21:40:30 +01003498 name = deref_func_name(tofree, &len,
3499 partial != NULL ? NULL : &partial, FALSE);
3500
Bram Moolenaar532c7802005-01-27 14:44:31 +00003501 /* Skip white space to allow ":call func ()". Not good, but required for
3502 * backward compatibility. */
3503 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003504 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505
3506 if (*startarg != '(')
3507 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003508 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 goto end;
3510 }
3511
3512 /*
3513 * When skipping, evaluate the function once, to find the end of the
3514 * arguments.
3515 * When the function takes a range, this is discovered after the first
3516 * call, and the loop is broken.
3517 */
3518 if (eap->skip)
3519 {
3520 ++emsg_skip;
3521 lnum = eap->line2; /* do it once, also with an invalid range */
3522 }
3523 else
3524 lnum = eap->line1;
3525 for ( ; lnum <= eap->line2; ++lnum)
3526 {
3527 if (!eap->skip && eap->addr_count > 0)
3528 {
3529 curwin->w_cursor.lnum = lnum;
3530 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003531#ifdef FEAT_VIRTUALEDIT
3532 curwin->w_cursor.coladd = 0;
3533#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 }
3535 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003536 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003537 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003538 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 {
3540 failed = TRUE;
3541 break;
3542 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003543
3544 /* Handle a function returning a Funcref, Dictionary or List. */
3545 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3546 {
3547 failed = TRUE;
3548 break;
3549 }
3550
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003551 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 if (doesrange || eap->skip)
3553 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003554
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003556 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003557 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003558 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 if (aborting())
3560 break;
3561 }
3562 if (eap->skip)
3563 --emsg_skip;
3564
3565 if (!failed)
3566 {
3567 /* Check for trailing illegal characters and a following command. */
3568 if (!ends_excmd(*arg))
3569 {
3570 emsg_severe = TRUE;
3571 EMSG(_(e_trailing));
3572 }
3573 else
3574 eap->nextcmd = check_nextcmd(arg);
3575 }
3576
3577end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003578 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003579 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580}
3581
3582/*
3583 * ":unlet[!] var1 ... " command.
3584 */
3585 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003586ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003588 ex_unletlock(eap, eap->arg, 0);
3589}
3590
3591/*
3592 * ":lockvar" and ":unlockvar" commands
3593 */
3594 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003595ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003596{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003598 int deep = 2;
3599
3600 if (eap->forceit)
3601 deep = -1;
3602 else if (vim_isdigit(*arg))
3603 {
3604 deep = getdigits(&arg);
3605 arg = skipwhite(arg);
3606 }
3607
3608 ex_unletlock(eap, arg, deep);
3609}
3610
3611/*
3612 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3613 */
3614 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003615ex_unletlock(
3616 exarg_T *eap,
3617 char_u *argstart,
3618 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003619{
3620 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003623 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624
3625 do
3626 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003627 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003628 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003629 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003630 if (lv.ll_name == NULL)
3631 error = TRUE; /* error but continue parsing */
3632 if (name_end == NULL || (!vim_iswhite(*name_end)
3633 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003635 if (name_end != NULL)
3636 {
3637 emsg_severe = TRUE;
3638 EMSG(_(e_trailing));
3639 }
3640 if (!(eap->skip || error))
3641 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 break;
3643 }
3644
3645 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003646 {
3647 if (eap->cmdidx == CMD_unlet)
3648 {
3649 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3650 error = TRUE;
3651 }
3652 else
3653 {
3654 if (do_lock_var(&lv, name_end, deep,
3655 eap->cmdidx == CMD_lockvar) == FAIL)
3656 error = TRUE;
3657 }
3658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003660 if (!eap->skip)
3661 clear_lval(&lv);
3662
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 arg = skipwhite(name_end);
3664 } while (!ends_excmd(*arg));
3665
3666 eap->nextcmd = check_nextcmd(arg);
3667}
3668
Bram Moolenaar8c711452005-01-14 21:53:12 +00003669 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003670do_unlet_var(
3671 lval_T *lp,
3672 char_u *name_end,
3673 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003674{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003675 int ret = OK;
3676 int cc;
3677
3678 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003679 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003680 cc = *name_end;
3681 *name_end = NUL;
3682
3683 /* Normal name or expanded name. */
3684 if (check_changedtick(lp->ll_name))
3685 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003686 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003687 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003688 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003689 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003690 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003691 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003692 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003693 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003694 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003695 else if (lp->ll_range)
3696 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003697 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003698 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003699 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003700
3701 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3702 {
3703 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003704 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003705 return FAIL;
3706 ll_li = li;
3707 ++ll_n1;
3708 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003709
3710 /* Delete a range of List items. */
3711 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3712 {
3713 li = lp->ll_li->li_next;
3714 listitem_remove(lp->ll_list, lp->ll_li);
3715 lp->ll_li = li;
3716 ++lp->ll_n1;
3717 }
3718 }
3719 else
3720 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003721 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003722 /* unlet a List item. */
3723 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003724 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003725 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003726 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003727 }
3728
3729 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003730}
3731
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732/*
3733 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003734 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 */
3736 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003737do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738{
Bram Moolenaar33570922005-01-25 22:26:29 +00003739 hashtab_T *ht;
3740 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003741 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003742 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003743 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744
Bram Moolenaar33570922005-01-25 22:26:29 +00003745 ht = find_var_ht(name, &varname);
3746 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003748 if (ht == &globvarht)
3749 d = &globvardict;
3750 else if (current_funccal != NULL
3751 && ht == &current_funccal->l_vars.dv_hashtab)
3752 d = &current_funccal->l_vars;
3753 else if (ht == &compat_hashtab)
3754 d = &vimvardict;
3755 else
3756 {
3757 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3758 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3759 }
3760 if (d == NULL)
3761 {
3762 EMSG2(_(e_intern2), "do_unlet()");
3763 return FAIL;
3764 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003765 hi = hash_find(ht, varname);
3766 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003767 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003768 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003769 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003770 || var_check_ro(di->di_flags, name, FALSE)
3771 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003772 return FAIL;
3773
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003774 delete_var(ht, hi);
3775 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003778 if (forceit)
3779 return OK;
3780 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 return FAIL;
3782}
3783
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003784/*
3785 * Lock or unlock variable indicated by "lp".
3786 * "deep" is the levels to go (-1 for unlimited);
3787 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3788 */
3789 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003790do_lock_var(
3791 lval_T *lp,
3792 char_u *name_end,
3793 int deep,
3794 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003795{
3796 int ret = OK;
3797 int cc;
3798 dictitem_T *di;
3799
3800 if (deep == 0) /* nothing to do */
3801 return OK;
3802
3803 if (lp->ll_tv == NULL)
3804 {
3805 cc = *name_end;
3806 *name_end = NUL;
3807
3808 /* Normal name or expanded name. */
3809 if (check_changedtick(lp->ll_name))
3810 ret = FAIL;
3811 else
3812 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003813 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003814 if (di == NULL)
3815 ret = FAIL;
3816 else
3817 {
3818 if (lock)
3819 di->di_flags |= DI_FLAGS_LOCK;
3820 else
3821 di->di_flags &= ~DI_FLAGS_LOCK;
3822 item_lock(&di->di_tv, deep, lock);
3823 }
3824 }
3825 *name_end = cc;
3826 }
3827 else if (lp->ll_range)
3828 {
3829 listitem_T *li = lp->ll_li;
3830
3831 /* (un)lock a range of List items. */
3832 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3833 {
3834 item_lock(&li->li_tv, deep, lock);
3835 li = li->li_next;
3836 ++lp->ll_n1;
3837 }
3838 }
3839 else if (lp->ll_list != NULL)
3840 /* (un)lock a List item. */
3841 item_lock(&lp->ll_li->li_tv, deep, lock);
3842 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003843 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003844 item_lock(&lp->ll_di->di_tv, deep, lock);
3845
3846 return ret;
3847}
3848
3849/*
3850 * Lock or unlock an item. "deep" is nr of levels to go.
3851 */
3852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003853item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003854{
3855 static int recurse = 0;
3856 list_T *l;
3857 listitem_T *li;
3858 dict_T *d;
3859 hashitem_T *hi;
3860 int todo;
3861
3862 if (recurse >= DICT_MAXNEST)
3863 {
3864 EMSG(_("E743: variable nested too deep for (un)lock"));
3865 return;
3866 }
3867 if (deep == 0)
3868 return;
3869 ++recurse;
3870
3871 /* lock/unlock the item itself */
3872 if (lock)
3873 tv->v_lock |= VAR_LOCKED;
3874 else
3875 tv->v_lock &= ~VAR_LOCKED;
3876
3877 switch (tv->v_type)
3878 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003879 case VAR_UNKNOWN:
3880 case VAR_NUMBER:
3881 case VAR_STRING:
3882 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003883 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003884 case VAR_FLOAT:
3885 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003886 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003887 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003888 break;
3889
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003890 case VAR_LIST:
3891 if ((l = tv->vval.v_list) != NULL)
3892 {
3893 if (lock)
3894 l->lv_lock |= VAR_LOCKED;
3895 else
3896 l->lv_lock &= ~VAR_LOCKED;
3897 if (deep < 0 || deep > 1)
3898 /* recursive: lock/unlock the items the List contains */
3899 for (li = l->lv_first; li != NULL; li = li->li_next)
3900 item_lock(&li->li_tv, deep - 1, lock);
3901 }
3902 break;
3903 case VAR_DICT:
3904 if ((d = tv->vval.v_dict) != NULL)
3905 {
3906 if (lock)
3907 d->dv_lock |= VAR_LOCKED;
3908 else
3909 d->dv_lock &= ~VAR_LOCKED;
3910 if (deep < 0 || deep > 1)
3911 {
3912 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003913 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003914 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3915 {
3916 if (!HASHITEM_EMPTY(hi))
3917 {
3918 --todo;
3919 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3920 }
3921 }
3922 }
3923 }
3924 }
3925 --recurse;
3926}
3927
Bram Moolenaara40058a2005-07-11 22:42:07 +00003928/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003929 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3930 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003931 */
3932 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003933tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003934{
3935 return (tv->v_lock & VAR_LOCKED)
3936 || (tv->v_type == VAR_LIST
3937 && tv->vval.v_list != NULL
3938 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3939 || (tv->v_type == VAR_DICT
3940 && tv->vval.v_dict != NULL
3941 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3942}
3943
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3945/*
3946 * Delete all "menutrans_" variables.
3947 */
3948 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003949del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950{
Bram Moolenaar33570922005-01-25 22:26:29 +00003951 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003952 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953
Bram Moolenaar33570922005-01-25 22:26:29 +00003954 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003955 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003956 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003957 {
3958 if (!HASHITEM_EMPTY(hi))
3959 {
3960 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003961 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3962 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003963 }
3964 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003965 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966}
3967#endif
3968
3969#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3970
3971/*
3972 * Local string buffer for the next two functions to store a variable name
3973 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3974 * get_user_var_name().
3975 */
3976
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003977static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978
3979static char_u *varnamebuf = NULL;
3980static int varnamebuflen = 0;
3981
3982/*
3983 * Function to concatenate a prefix and a variable name.
3984 */
3985 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003986cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987{
3988 int len;
3989
3990 len = (int)STRLEN(name) + 3;
3991 if (len > varnamebuflen)
3992 {
3993 vim_free(varnamebuf);
3994 len += 10; /* some additional space */
3995 varnamebuf = alloc(len);
3996 if (varnamebuf == NULL)
3997 {
3998 varnamebuflen = 0;
3999 return NULL;
4000 }
4001 varnamebuflen = len;
4002 }
4003 *varnamebuf = prefix;
4004 varnamebuf[1] = ':';
4005 STRCPY(varnamebuf + 2, name);
4006 return varnamebuf;
4007}
4008
4009/*
4010 * Function given to ExpandGeneric() to obtain the list of user defined
4011 * (global/buffer/window/built-in) variable names.
4012 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004014get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004016 static long_u gdone;
4017 static long_u bdone;
4018 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004019#ifdef FEAT_WINDOWS
4020 static long_u tdone;
4021#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004022 static int vidx;
4023 static hashitem_T *hi;
4024 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025
4026 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004027 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004028 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004029#ifdef FEAT_WINDOWS
4030 tdone = 0;
4031#endif
4032 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004033
4034 /* Global variables */
4035 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004037 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004038 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004039 else
4040 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004041 while (HASHITEM_EMPTY(hi))
4042 ++hi;
4043 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4044 return cat_prefix_varname('g', hi->hi_key);
4045 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004047
4048 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004049 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004050 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004052 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004053 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004054 else
4055 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004056 while (HASHITEM_EMPTY(hi))
4057 ++hi;
4058 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004060 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004062 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 return (char_u *)"b:changedtick";
4064 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004065
4066 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004067 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004068 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004070 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004071 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004072 else
4073 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004074 while (HASHITEM_EMPTY(hi))
4075 ++hi;
4076 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004078
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004079#ifdef FEAT_WINDOWS
4080 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004081 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004082 if (tdone < ht->ht_used)
4083 {
4084 if (tdone++ == 0)
4085 hi = ht->ht_array;
4086 else
4087 ++hi;
4088 while (HASHITEM_EMPTY(hi))
4089 ++hi;
4090 return cat_prefix_varname('t', hi->hi_key);
4091 }
4092#endif
4093
Bram Moolenaar33570922005-01-25 22:26:29 +00004094 /* v: variables */
4095 if (vidx < VV_LEN)
4096 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097
4098 vim_free(varnamebuf);
4099 varnamebuf = NULL;
4100 varnamebuflen = 0;
4101 return NULL;
4102}
4103
4104#endif /* FEAT_CMDL_COMPL */
4105
4106/*
4107 * types for expressions.
4108 */
4109typedef enum
4110{
4111 TYPE_UNKNOWN = 0
4112 , TYPE_EQUAL /* == */
4113 , TYPE_NEQUAL /* != */
4114 , TYPE_GREATER /* > */
4115 , TYPE_GEQUAL /* >= */
4116 , TYPE_SMALLER /* < */
4117 , TYPE_SEQUAL /* <= */
4118 , TYPE_MATCH /* =~ */
4119 , TYPE_NOMATCH /* !~ */
4120} exptype_T;
4121
4122/*
4123 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004124 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4126 */
4127
4128/*
4129 * Handle zero level expression.
4130 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004131 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004132 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 * Return OK or FAIL.
4134 */
4135 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004136eval0(
4137 char_u *arg,
4138 typval_T *rettv,
4139 char_u **nextcmd,
4140 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141{
4142 int ret;
4143 char_u *p;
4144
4145 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004146 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 if (ret == FAIL || !ends_excmd(*p))
4148 {
4149 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004150 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 /*
4152 * Report the invalid expression unless the expression evaluation has
4153 * been cancelled due to an aborting error, an interrupt, or an
4154 * exception.
4155 */
4156 if (!aborting())
4157 EMSG2(_(e_invexpr2), arg);
4158 ret = FAIL;
4159 }
4160 if (nextcmd != NULL)
4161 *nextcmd = check_nextcmd(p);
4162
4163 return ret;
4164}
4165
4166/*
4167 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004168 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 *
4170 * "arg" must point to the first non-white of the expression.
4171 * "arg" is advanced to the next non-white after the recognized expression.
4172 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004173 * Note: "rettv.v_lock" is not set.
4174 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 * Return OK or FAIL.
4176 */
4177 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004178eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179{
4180 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004181 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182
4183 /*
4184 * Get the first variable.
4185 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004186 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 return FAIL;
4188
4189 if ((*arg)[0] == '?')
4190 {
4191 result = FALSE;
4192 if (evaluate)
4193 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004194 int error = FALSE;
4195
4196 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004198 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004199 if (error)
4200 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 }
4202
4203 /*
4204 * Get the second variable.
4205 */
4206 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004207 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 return FAIL;
4209
4210 /*
4211 * Check for the ":".
4212 */
4213 if ((*arg)[0] != ':')
4214 {
4215 EMSG(_("E109: Missing ':' after '?'"));
4216 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004217 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 return FAIL;
4219 }
4220
4221 /*
4222 * Get the third variable.
4223 */
4224 *arg = skipwhite(*arg + 1);
4225 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4226 {
4227 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004228 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 return FAIL;
4230 }
4231 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004232 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 }
4234
4235 return OK;
4236}
4237
4238/*
4239 * Handle first level expression:
4240 * expr2 || expr2 || expr2 logical OR
4241 *
4242 * "arg" must point to the first non-white of the expression.
4243 * "arg" is advanced to the next non-white after the recognized expression.
4244 *
4245 * Return OK or FAIL.
4246 */
4247 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004248eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249{
Bram Moolenaar33570922005-01-25 22:26:29 +00004250 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 long result;
4252 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004253 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254
4255 /*
4256 * Get the first variable.
4257 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004258 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 return FAIL;
4260
4261 /*
4262 * Repeat until there is no following "||".
4263 */
4264 first = TRUE;
4265 result = FALSE;
4266 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4267 {
4268 if (evaluate && first)
4269 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004270 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004272 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004273 if (error)
4274 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 first = FALSE;
4276 }
4277
4278 /*
4279 * Get the second variable.
4280 */
4281 *arg = skipwhite(*arg + 2);
4282 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4283 return FAIL;
4284
4285 /*
4286 * Compute the result.
4287 */
4288 if (evaluate && !result)
4289 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004290 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004292 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004293 if (error)
4294 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 }
4296 if (evaluate)
4297 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004298 rettv->v_type = VAR_NUMBER;
4299 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 }
4301 }
4302
4303 return OK;
4304}
4305
4306/*
4307 * Handle second level expression:
4308 * expr3 && expr3 && expr3 logical AND
4309 *
4310 * "arg" must point to the first non-white of the expression.
4311 * "arg" is advanced to the next non-white after the recognized expression.
4312 *
4313 * Return OK or FAIL.
4314 */
4315 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004316eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317{
Bram Moolenaar33570922005-01-25 22:26:29 +00004318 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 long result;
4320 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004321 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322
4323 /*
4324 * Get the first variable.
4325 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004326 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 return FAIL;
4328
4329 /*
4330 * Repeat until there is no following "&&".
4331 */
4332 first = TRUE;
4333 result = TRUE;
4334 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4335 {
4336 if (evaluate && first)
4337 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004338 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004340 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004341 if (error)
4342 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 first = FALSE;
4344 }
4345
4346 /*
4347 * Get the second variable.
4348 */
4349 *arg = skipwhite(*arg + 2);
4350 if (eval4(arg, &var2, evaluate && result) == FAIL)
4351 return FAIL;
4352
4353 /*
4354 * Compute the result.
4355 */
4356 if (evaluate && result)
4357 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004358 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004360 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004361 if (error)
4362 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 }
4364 if (evaluate)
4365 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004366 rettv->v_type = VAR_NUMBER;
4367 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 }
4369 }
4370
4371 return OK;
4372}
4373
4374/*
4375 * Handle third level expression:
4376 * var1 == var2
4377 * var1 =~ var2
4378 * var1 != var2
4379 * var1 !~ var2
4380 * var1 > var2
4381 * var1 >= var2
4382 * var1 < var2
4383 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004384 * var1 is var2
4385 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 *
4387 * "arg" must point to the first non-white of the expression.
4388 * "arg" is advanced to the next non-white after the recognized expression.
4389 *
4390 * Return OK or FAIL.
4391 */
4392 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004393eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394{
Bram Moolenaar33570922005-01-25 22:26:29 +00004395 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 char_u *p;
4397 int i;
4398 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004399 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 int len = 2;
4401 long n1, n2;
4402 char_u *s1, *s2;
4403 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4404 regmatch_T regmatch;
4405 int ic;
4406 char_u *save_cpo;
4407
4408 /*
4409 * Get the first variable.
4410 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004411 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 return FAIL;
4413
4414 p = *arg;
4415 switch (p[0])
4416 {
4417 case '=': if (p[1] == '=')
4418 type = TYPE_EQUAL;
4419 else if (p[1] == '~')
4420 type = TYPE_MATCH;
4421 break;
4422 case '!': if (p[1] == '=')
4423 type = TYPE_NEQUAL;
4424 else if (p[1] == '~')
4425 type = TYPE_NOMATCH;
4426 break;
4427 case '>': if (p[1] != '=')
4428 {
4429 type = TYPE_GREATER;
4430 len = 1;
4431 }
4432 else
4433 type = TYPE_GEQUAL;
4434 break;
4435 case '<': if (p[1] != '=')
4436 {
4437 type = TYPE_SMALLER;
4438 len = 1;
4439 }
4440 else
4441 type = TYPE_SEQUAL;
4442 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004443 case 'i': if (p[1] == 's')
4444 {
4445 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4446 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004447 i = p[len];
4448 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004449 {
4450 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4451 type_is = TRUE;
4452 }
4453 }
4454 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 }
4456
4457 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004458 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 */
4460 if (type != TYPE_UNKNOWN)
4461 {
4462 /* extra question mark appended: ignore case */
4463 if (p[len] == '?')
4464 {
4465 ic = TRUE;
4466 ++len;
4467 }
4468 /* extra '#' appended: match case */
4469 else if (p[len] == '#')
4470 {
4471 ic = FALSE;
4472 ++len;
4473 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004474 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 else
4476 ic = p_ic;
4477
4478 /*
4479 * Get the second variable.
4480 */
4481 *arg = skipwhite(p + len);
4482 if (eval5(arg, &var2, evaluate) == FAIL)
4483 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004484 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 return FAIL;
4486 }
4487
4488 if (evaluate)
4489 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004490 if (type_is && rettv->v_type != var2.v_type)
4491 {
4492 /* For "is" a different type always means FALSE, for "notis"
4493 * it means TRUE. */
4494 n1 = (type == TYPE_NEQUAL);
4495 }
4496 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4497 {
4498 if (type_is)
4499 {
4500 n1 = (rettv->v_type == var2.v_type
4501 && rettv->vval.v_list == var2.vval.v_list);
4502 if (type == TYPE_NEQUAL)
4503 n1 = !n1;
4504 }
4505 else if (rettv->v_type != var2.v_type
4506 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4507 {
4508 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004509 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004510 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004511 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004512 clear_tv(rettv);
4513 clear_tv(&var2);
4514 return FAIL;
4515 }
4516 else
4517 {
4518 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004519 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4520 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004521 if (type == TYPE_NEQUAL)
4522 n1 = !n1;
4523 }
4524 }
4525
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004526 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4527 {
4528 if (type_is)
4529 {
4530 n1 = (rettv->v_type == var2.v_type
4531 && rettv->vval.v_dict == var2.vval.v_dict);
4532 if (type == TYPE_NEQUAL)
4533 n1 = !n1;
4534 }
4535 else if (rettv->v_type != var2.v_type
4536 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4537 {
4538 if (rettv->v_type != var2.v_type)
4539 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4540 else
4541 EMSG(_("E736: Invalid operation for Dictionary"));
4542 clear_tv(rettv);
4543 clear_tv(&var2);
4544 return FAIL;
4545 }
4546 else
4547 {
4548 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004549 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4550 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004551 if (type == TYPE_NEQUAL)
4552 n1 = !n1;
4553 }
4554 }
4555
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004556 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4557 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004558 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004559 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004560 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004561 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004562 clear_tv(rettv);
4563 clear_tv(&var2);
4564 return FAIL;
4565 }
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004566 n1 = tv_equal(rettv, &var2, FALSE, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004567 if (type == TYPE_NEQUAL)
4568 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004569 }
4570
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004571#ifdef FEAT_FLOAT
4572 /*
4573 * If one of the two variables is a float, compare as a float.
4574 * When using "=~" or "!~", always compare as string.
4575 */
4576 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4577 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4578 {
4579 float_T f1, f2;
4580
4581 if (rettv->v_type == VAR_FLOAT)
4582 f1 = rettv->vval.v_float;
4583 else
4584 f1 = get_tv_number(rettv);
4585 if (var2.v_type == VAR_FLOAT)
4586 f2 = var2.vval.v_float;
4587 else
4588 f2 = get_tv_number(&var2);
4589 n1 = FALSE;
4590 switch (type)
4591 {
4592 case TYPE_EQUAL: n1 = (f1 == f2); break;
4593 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4594 case TYPE_GREATER: n1 = (f1 > f2); break;
4595 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4596 case TYPE_SMALLER: n1 = (f1 < f2); break;
4597 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4598 case TYPE_UNKNOWN:
4599 case TYPE_MATCH:
4600 case TYPE_NOMATCH: break; /* avoid gcc warning */
4601 }
4602 }
4603#endif
4604
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 /*
4606 * If one of the two variables is a number, compare as a number.
4607 * When using "=~" or "!~", always compare as string.
4608 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004609 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4611 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004612 n1 = get_tv_number(rettv);
4613 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 switch (type)
4615 {
4616 case TYPE_EQUAL: n1 = (n1 == n2); break;
4617 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4618 case TYPE_GREATER: n1 = (n1 > n2); break;
4619 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4620 case TYPE_SMALLER: n1 = (n1 < n2); break;
4621 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4622 case TYPE_UNKNOWN:
4623 case TYPE_MATCH:
4624 case TYPE_NOMATCH: break; /* avoid gcc warning */
4625 }
4626 }
4627 else
4628 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004629 s1 = get_tv_string_buf(rettv, buf1);
4630 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4632 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4633 else
4634 i = 0;
4635 n1 = FALSE;
4636 switch (type)
4637 {
4638 case TYPE_EQUAL: n1 = (i == 0); break;
4639 case TYPE_NEQUAL: n1 = (i != 0); break;
4640 case TYPE_GREATER: n1 = (i > 0); break;
4641 case TYPE_GEQUAL: n1 = (i >= 0); break;
4642 case TYPE_SMALLER: n1 = (i < 0); break;
4643 case TYPE_SEQUAL: n1 = (i <= 0); break;
4644
4645 case TYPE_MATCH:
4646 case TYPE_NOMATCH:
4647 /* avoid 'l' flag in 'cpoptions' */
4648 save_cpo = p_cpo;
4649 p_cpo = (char_u *)"";
4650 regmatch.regprog = vim_regcomp(s2,
4651 RE_MAGIC + RE_STRING);
4652 regmatch.rm_ic = ic;
4653 if (regmatch.regprog != NULL)
4654 {
4655 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004656 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 if (type == TYPE_NOMATCH)
4658 n1 = !n1;
4659 }
4660 p_cpo = save_cpo;
4661 break;
4662
4663 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4664 }
4665 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004666 clear_tv(rettv);
4667 clear_tv(&var2);
4668 rettv->v_type = VAR_NUMBER;
4669 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 }
4671 }
4672
4673 return OK;
4674}
4675
4676/*
4677 * Handle fourth level expression:
4678 * + number addition
4679 * - number subtraction
4680 * . string concatenation
4681 *
4682 * "arg" must point to the first non-white of the expression.
4683 * "arg" is advanced to the next non-white after the recognized expression.
4684 *
4685 * Return OK or FAIL.
4686 */
4687 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004688eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689{
Bram Moolenaar33570922005-01-25 22:26:29 +00004690 typval_T var2;
4691 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 int op;
4693 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004694#ifdef FEAT_FLOAT
4695 float_T f1 = 0, f2 = 0;
4696#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 char_u *s1, *s2;
4698 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4699 char_u *p;
4700
4701 /*
4702 * Get the first variable.
4703 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004704 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 return FAIL;
4706
4707 /*
4708 * Repeat computing, until no '+', '-' or '.' is following.
4709 */
4710 for (;;)
4711 {
4712 op = **arg;
4713 if (op != '+' && op != '-' && op != '.')
4714 break;
4715
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004716 if ((op != '+' || rettv->v_type != VAR_LIST)
4717#ifdef FEAT_FLOAT
4718 && (op == '.' || rettv->v_type != VAR_FLOAT)
4719#endif
4720 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004721 {
4722 /* For "list + ...", an illegal use of the first operand as
4723 * a number cannot be determined before evaluating the 2nd
4724 * operand: if this is also a list, all is ok.
4725 * For "something . ...", "something - ..." or "non-list + ...",
4726 * we know that the first operand needs to be a string or number
4727 * without evaluating the 2nd operand. So check before to avoid
4728 * side effects after an error. */
4729 if (evaluate && get_tv_string_chk(rettv) == NULL)
4730 {
4731 clear_tv(rettv);
4732 return FAIL;
4733 }
4734 }
4735
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 /*
4737 * Get the second variable.
4738 */
4739 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004740 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004742 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 return FAIL;
4744 }
4745
4746 if (evaluate)
4747 {
4748 /*
4749 * Compute the result.
4750 */
4751 if (op == '.')
4752 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004753 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4754 s2 = get_tv_string_buf_chk(&var2, buf2);
4755 if (s2 == NULL) /* type error ? */
4756 {
4757 clear_tv(rettv);
4758 clear_tv(&var2);
4759 return FAIL;
4760 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004761 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004762 clear_tv(rettv);
4763 rettv->v_type = VAR_STRING;
4764 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004766 else if (op == '+' && rettv->v_type == VAR_LIST
4767 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004768 {
4769 /* concatenate Lists */
4770 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4771 &var3) == FAIL)
4772 {
4773 clear_tv(rettv);
4774 clear_tv(&var2);
4775 return FAIL;
4776 }
4777 clear_tv(rettv);
4778 *rettv = var3;
4779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 else
4781 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004782 int error = FALSE;
4783
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004784#ifdef FEAT_FLOAT
4785 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004786 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004787 f1 = rettv->vval.v_float;
4788 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004789 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004790 else
4791#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004792 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004793 n1 = get_tv_number_chk(rettv, &error);
4794 if (error)
4795 {
4796 /* This can only happen for "list + non-list". For
4797 * "non-list + ..." or "something - ...", we returned
4798 * before evaluating the 2nd operand. */
4799 clear_tv(rettv);
4800 return FAIL;
4801 }
4802#ifdef FEAT_FLOAT
4803 if (var2.v_type == VAR_FLOAT)
4804 f1 = n1;
4805#endif
4806 }
4807#ifdef FEAT_FLOAT
4808 if (var2.v_type == VAR_FLOAT)
4809 {
4810 f2 = var2.vval.v_float;
4811 n2 = 0;
4812 }
4813 else
4814#endif
4815 {
4816 n2 = get_tv_number_chk(&var2, &error);
4817 if (error)
4818 {
4819 clear_tv(rettv);
4820 clear_tv(&var2);
4821 return FAIL;
4822 }
4823#ifdef FEAT_FLOAT
4824 if (rettv->v_type == VAR_FLOAT)
4825 f2 = n2;
4826#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004827 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004828 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004829
4830#ifdef FEAT_FLOAT
4831 /* If there is a float on either side the result is a float. */
4832 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4833 {
4834 if (op == '+')
4835 f1 = f1 + f2;
4836 else
4837 f1 = f1 - f2;
4838 rettv->v_type = VAR_FLOAT;
4839 rettv->vval.v_float = f1;
4840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004842#endif
4843 {
4844 if (op == '+')
4845 n1 = n1 + n2;
4846 else
4847 n1 = n1 - n2;
4848 rettv->v_type = VAR_NUMBER;
4849 rettv->vval.v_number = n1;
4850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004852 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 }
4854 }
4855 return OK;
4856}
4857
4858/*
4859 * Handle fifth level expression:
4860 * * number multiplication
4861 * / number division
4862 * % number modulo
4863 *
4864 * "arg" must point to the first non-white of the expression.
4865 * "arg" is advanced to the next non-white after the recognized expression.
4866 *
4867 * Return OK or FAIL.
4868 */
4869 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004870eval6(
4871 char_u **arg,
4872 typval_T *rettv,
4873 int evaluate,
4874 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875{
Bram Moolenaar33570922005-01-25 22:26:29 +00004876 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 int op;
4878 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004879#ifdef FEAT_FLOAT
4880 int use_float = FALSE;
4881 float_T f1 = 0, f2;
4882#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004883 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884
4885 /*
4886 * Get the first variable.
4887 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004888 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 return FAIL;
4890
4891 /*
4892 * Repeat computing, until no '*', '/' or '%' is following.
4893 */
4894 for (;;)
4895 {
4896 op = **arg;
4897 if (op != '*' && op != '/' && op != '%')
4898 break;
4899
4900 if (evaluate)
4901 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004902#ifdef FEAT_FLOAT
4903 if (rettv->v_type == VAR_FLOAT)
4904 {
4905 f1 = rettv->vval.v_float;
4906 use_float = TRUE;
4907 n1 = 0;
4908 }
4909 else
4910#endif
4911 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004912 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004913 if (error)
4914 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 }
4916 else
4917 n1 = 0;
4918
4919 /*
4920 * Get the second variable.
4921 */
4922 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004923 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 return FAIL;
4925
4926 if (evaluate)
4927 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004928#ifdef FEAT_FLOAT
4929 if (var2.v_type == VAR_FLOAT)
4930 {
4931 if (!use_float)
4932 {
4933 f1 = n1;
4934 use_float = TRUE;
4935 }
4936 f2 = var2.vval.v_float;
4937 n2 = 0;
4938 }
4939 else
4940#endif
4941 {
4942 n2 = get_tv_number_chk(&var2, &error);
4943 clear_tv(&var2);
4944 if (error)
4945 return FAIL;
4946#ifdef FEAT_FLOAT
4947 if (use_float)
4948 f2 = n2;
4949#endif
4950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951
4952 /*
4953 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004954 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004956#ifdef FEAT_FLOAT
4957 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004959 if (op == '*')
4960 f1 = f1 * f2;
4961 else if (op == '/')
4962 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004963# ifdef VMS
4964 /* VMS crashes on divide by zero, work around it */
4965 if (f2 == 0.0)
4966 {
4967 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004968 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004969 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004970 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004971 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004972 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004973 }
4974 else
4975 f1 = f1 / f2;
4976# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004977 /* We rely on the floating point library to handle divide
4978 * by zero to result in "inf" and not a crash. */
4979 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004980# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004983 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004984 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004985 return FAIL;
4986 }
4987 rettv->v_type = VAR_FLOAT;
4988 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 }
4990 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004991#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004993 if (op == '*')
4994 n1 = n1 * n2;
4995 else if (op == '/')
4996 {
4997 if (n2 == 0) /* give an error message? */
4998 {
4999 if (n1 == 0)
5000 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5001 else if (n1 < 0)
5002 n1 = -0x7fffffffL;
5003 else
5004 n1 = 0x7fffffffL;
5005 }
5006 else
5007 n1 = n1 / n2;
5008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005010 {
5011 if (n2 == 0) /* give an error message? */
5012 n1 = 0;
5013 else
5014 n1 = n1 % n2;
5015 }
5016 rettv->v_type = VAR_NUMBER;
5017 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 }
5020 }
5021
5022 return OK;
5023}
5024
5025/*
5026 * Handle sixth level expression:
5027 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005028 * "string" string constant
5029 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 * &option-name option value
5031 * @r register contents
5032 * identifier variable value
5033 * function() function call
5034 * $VAR environment variable
5035 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005036 * [expr, expr] List
5037 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 *
5039 * Also handle:
5040 * ! in front logical NOT
5041 * - in front unary minus
5042 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005043 * trailing [] subscript in String or List
5044 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 *
5046 * "arg" must point to the first non-white of the expression.
5047 * "arg" is advanced to the next non-white after the recognized expression.
5048 *
5049 * Return OK or FAIL.
5050 */
5051 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005052eval7(
5053 char_u **arg,
5054 typval_T *rettv,
5055 int evaluate,
5056 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 long n;
5059 int len;
5060 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 char_u *start_leader, *end_leader;
5062 int ret = OK;
5063 char_u *alias;
5064
5065 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005066 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005067 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005069 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070
5071 /*
5072 * Skip '!' and '-' characters. They are handled later.
5073 */
5074 start_leader = *arg;
5075 while (**arg == '!' || **arg == '-' || **arg == '+')
5076 *arg = skipwhite(*arg + 1);
5077 end_leader = *arg;
5078
5079 switch (**arg)
5080 {
5081 /*
5082 * Number constant.
5083 */
5084 case '0':
5085 case '1':
5086 case '2':
5087 case '3':
5088 case '4':
5089 case '5':
5090 case '6':
5091 case '7':
5092 case '8':
5093 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005094 {
5095#ifdef FEAT_FLOAT
5096 char_u *p = skipdigits(*arg + 1);
5097 int get_float = FALSE;
5098
5099 /* We accept a float when the format matches
5100 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005101 * strict to avoid backwards compatibility problems.
5102 * Don't look for a float after the "." operator, so that
5103 * ":let vers = 1.2.3" doesn't fail. */
5104 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005106 get_float = TRUE;
5107 p = skipdigits(p + 2);
5108 if (*p == 'e' || *p == 'E')
5109 {
5110 ++p;
5111 if (*p == '-' || *p == '+')
5112 ++p;
5113 if (!vim_isdigit(*p))
5114 get_float = FALSE;
5115 else
5116 p = skipdigits(p + 1);
5117 }
5118 if (ASCII_ISALPHA(*p) || *p == '.')
5119 get_float = FALSE;
5120 }
5121 if (get_float)
5122 {
5123 float_T f;
5124
5125 *arg += string2float(*arg, &f);
5126 if (evaluate)
5127 {
5128 rettv->v_type = VAR_FLOAT;
5129 rettv->vval.v_float = f;
5130 }
5131 }
5132 else
5133#endif
5134 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005135 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005136 *arg += len;
5137 if (evaluate)
5138 {
5139 rettv->v_type = VAR_NUMBER;
5140 rettv->vval.v_number = n;
5141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 }
5143 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145
5146 /*
5147 * String constant: "string".
5148 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005149 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 break;
5151
5152 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005153 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005155 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005156 break;
5157
5158 /*
5159 * List: [expr, expr]
5160 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005161 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 break;
5163
5164 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005165 * Dictionary: {key: val, key: val}
5166 */
5167 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5168 break;
5169
5170 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005171 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005173 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 break;
5175
5176 /*
5177 * Environment variable: $VAR.
5178 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005179 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 break;
5181
5182 /*
5183 * Register contents: @r.
5184 */
5185 case '@': ++*arg;
5186 if (evaluate)
5187 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005188 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005189 rettv->vval.v_string = get_reg_contents(**arg,
5190 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 }
5192 if (**arg != NUL)
5193 ++*arg;
5194 break;
5195
5196 /*
5197 * nested expression: (expression).
5198 */
5199 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005200 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 if (**arg == ')')
5202 ++*arg;
5203 else if (ret == OK)
5204 {
5205 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005206 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 ret = FAIL;
5208 }
5209 break;
5210
Bram Moolenaar8c711452005-01-14 21:53:12 +00005211 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 break;
5213 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005214
5215 if (ret == NOTDONE)
5216 {
5217 /*
5218 * Must be a variable or function name.
5219 * Can also be a curly-braces kind of name: {expr}.
5220 */
5221 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005222 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005223 if (alias != NULL)
5224 s = alias;
5225
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005226 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005227 ret = FAIL;
5228 else
5229 {
5230 if (**arg == '(') /* recursive! */
5231 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005232 partial_T *partial;
5233
Bram Moolenaar8c711452005-01-14 21:53:12 +00005234 /* If "s" is the name of a variable of type VAR_FUNC
5235 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005236 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005237
5238 /* Invoke the function. */
5239 ret = get_func_tv(s, len, rettv, arg,
5240 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005241 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005242
5243 /* If evaluate is FALSE rettv->v_type was not set in
5244 * get_func_tv, but it's needed in handle_subscript() to parse
5245 * what follows. So set it here. */
5246 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5247 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005248 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005249 rettv->v_type = VAR_FUNC;
5250 }
5251
Bram Moolenaar8c711452005-01-14 21:53:12 +00005252 /* Stop the expression evaluation when immediately
5253 * aborting on error, or when an interrupt occurred or
5254 * an exception was thrown but not caught. */
5255 if (aborting())
5256 {
5257 if (ret == OK)
5258 clear_tv(rettv);
5259 ret = FAIL;
5260 }
5261 }
5262 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005263 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005264 else
5265 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005266 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005267 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005268 }
5269
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 *arg = skipwhite(*arg);
5271
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005272 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5273 * expr(expr). */
5274 if (ret == OK)
5275 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276
5277 /*
5278 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5279 */
5280 if (ret == OK && evaluate && end_leader > start_leader)
5281 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005282 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005283 int val = 0;
5284#ifdef FEAT_FLOAT
5285 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005286
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005287 if (rettv->v_type == VAR_FLOAT)
5288 f = rettv->vval.v_float;
5289 else
5290#endif
5291 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005292 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005294 clear_tv(rettv);
5295 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005297 else
5298 {
5299 while (end_leader > start_leader)
5300 {
5301 --end_leader;
5302 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005303 {
5304#ifdef FEAT_FLOAT
5305 if (rettv->v_type == VAR_FLOAT)
5306 f = !f;
5307 else
5308#endif
5309 val = !val;
5310 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005311 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005312 {
5313#ifdef FEAT_FLOAT
5314 if (rettv->v_type == VAR_FLOAT)
5315 f = -f;
5316 else
5317#endif
5318 val = -val;
5319 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005320 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005321#ifdef FEAT_FLOAT
5322 if (rettv->v_type == VAR_FLOAT)
5323 {
5324 clear_tv(rettv);
5325 rettv->vval.v_float = f;
5326 }
5327 else
5328#endif
5329 {
5330 clear_tv(rettv);
5331 rettv->v_type = VAR_NUMBER;
5332 rettv->vval.v_number = val;
5333 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 }
5336
5337 return ret;
5338}
5339
5340/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005341 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5342 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005343 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5344 */
5345 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005346eval_index(
5347 char_u **arg,
5348 typval_T *rettv,
5349 int evaluate,
5350 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005351{
5352 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005353 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005355 long len = -1;
5356 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005357 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005358 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005359
Bram Moolenaara03f2332016-02-06 18:09:59 +01005360 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005361 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005362 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005363 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005364 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:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005485 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005486 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005487 case VAR_SPECIAL:
5488 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005489 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005490 break; /* not evaluating, skipping over subscript */
5491
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005492 case VAR_NUMBER:
5493 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005494 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005495 len = (long)STRLEN(s);
5496 if (range)
5497 {
5498 /* The resulting variable is a substring. If the indexes
5499 * are out of range the result is empty. */
5500 if (n1 < 0)
5501 {
5502 n1 = len + n1;
5503 if (n1 < 0)
5504 n1 = 0;
5505 }
5506 if (n2 < 0)
5507 n2 = len + n2;
5508 else if (n2 >= len)
5509 n2 = len;
5510 if (n1 >= len || n2 < 0 || n1 > n2)
5511 s = NULL;
5512 else
5513 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5514 }
5515 else
5516 {
5517 /* The resulting variable is a string of a single
5518 * character. If the index is too big or negative the
5519 * result is empty. */
5520 if (n1 >= len || n1 < 0)
5521 s = NULL;
5522 else
5523 s = vim_strnsave(s + n1, 1);
5524 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005525 clear_tv(rettv);
5526 rettv->v_type = VAR_STRING;
5527 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005528 break;
5529
5530 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005531 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005532 if (n1 < 0)
5533 n1 = len + n1;
5534 if (!empty1 && (n1 < 0 || n1 >= len))
5535 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005536 /* For a range we allow invalid values and return an empty
5537 * list. A list index out of range is an error. */
5538 if (!range)
5539 {
5540 if (verbose)
5541 EMSGN(_(e_listidx), n1);
5542 return FAIL;
5543 }
5544 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005545 }
5546 if (range)
5547 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005548 list_T *l;
5549 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005550
5551 if (n2 < 0)
5552 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005553 else if (n2 >= len)
5554 n2 = len - 1;
5555 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005556 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005557 l = list_alloc();
5558 if (l == NULL)
5559 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005560 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005561 n1 <= n2; ++n1)
5562 {
5563 if (list_append_tv(l, &item->li_tv) == FAIL)
5564 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005565 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005566 return FAIL;
5567 }
5568 item = item->li_next;
5569 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005570 clear_tv(rettv);
5571 rettv->v_type = VAR_LIST;
5572 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005573 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005574 }
5575 else
5576 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005577 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005578 clear_tv(rettv);
5579 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005580 }
5581 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005582
5583 case VAR_DICT:
5584 if (range)
5585 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005586 if (verbose)
5587 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005588 if (len == -1)
5589 clear_tv(&var1);
5590 return FAIL;
5591 }
5592 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005593 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005594
5595 if (len == -1)
5596 {
5597 key = get_tv_string(&var1);
5598 if (*key == NUL)
5599 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005600 if (verbose)
5601 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005602 clear_tv(&var1);
5603 return FAIL;
5604 }
5605 }
5606
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005607 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005608
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005609 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005610 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005611 if (len == -1)
5612 clear_tv(&var1);
5613 if (item == NULL)
5614 return FAIL;
5615
5616 copy_tv(&item->di_tv, &var1);
5617 clear_tv(rettv);
5618 *rettv = var1;
5619 }
5620 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005621 }
5622 }
5623
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005624 return OK;
5625}
5626
5627/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 * Get an option value.
5629 * "arg" points to the '&' or '+' before the option name.
5630 * "arg" is advanced to character after the option name.
5631 * Return OK or FAIL.
5632 */
5633 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005634get_option_tv(
5635 char_u **arg,
5636 typval_T *rettv, /* when NULL, only check if option exists */
5637 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638{
5639 char_u *option_end;
5640 long numval;
5641 char_u *stringval;
5642 int opt_type;
5643 int c;
5644 int working = (**arg == '+'); /* has("+option") */
5645 int ret = OK;
5646 int opt_flags;
5647
5648 /*
5649 * Isolate the option name and find its value.
5650 */
5651 option_end = find_option_end(arg, &opt_flags);
5652 if (option_end == NULL)
5653 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005654 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 EMSG2(_("E112: Option name missing: %s"), *arg);
5656 return FAIL;
5657 }
5658
5659 if (!evaluate)
5660 {
5661 *arg = option_end;
5662 return OK;
5663 }
5664
5665 c = *option_end;
5666 *option_end = NUL;
5667 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005668 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669
5670 if (opt_type == -3) /* invalid name */
5671 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005672 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 EMSG2(_("E113: Unknown option: %s"), *arg);
5674 ret = FAIL;
5675 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005676 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 {
5678 if (opt_type == -2) /* hidden string option */
5679 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005680 rettv->v_type = VAR_STRING;
5681 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 }
5683 else if (opt_type == -1) /* hidden number option */
5684 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005685 rettv->v_type = VAR_NUMBER;
5686 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 }
5688 else if (opt_type == 1) /* number option */
5689 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005690 rettv->v_type = VAR_NUMBER;
5691 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 }
5693 else /* string option */
5694 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005695 rettv->v_type = VAR_STRING;
5696 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 }
5698 }
5699 else if (working && (opt_type == -2 || opt_type == -1))
5700 ret = FAIL;
5701
5702 *option_end = c; /* put back for error messages */
5703 *arg = option_end;
5704
5705 return ret;
5706}
5707
5708/*
5709 * Allocate a variable for a string constant.
5710 * Return OK or FAIL.
5711 */
5712 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005713get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714{
5715 char_u *p;
5716 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 int extra = 0;
5718
5719 /*
5720 * Find the end of the string, skipping backslashed characters.
5721 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005722 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 {
5724 if (*p == '\\' && p[1] != NUL)
5725 {
5726 ++p;
5727 /* A "\<x>" form occupies at least 4 characters, and produces up
5728 * to 6 characters: reserve space for 2 extra */
5729 if (*p == '<')
5730 extra += 2;
5731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 }
5733
5734 if (*p != '"')
5735 {
5736 EMSG2(_("E114: Missing quote: %s"), *arg);
5737 return FAIL;
5738 }
5739
5740 /* If only parsing, set *arg and return here */
5741 if (!evaluate)
5742 {
5743 *arg = p + 1;
5744 return OK;
5745 }
5746
5747 /*
5748 * Copy the string into allocated memory, handling backslashed
5749 * characters.
5750 */
5751 name = alloc((unsigned)(p - *arg + extra));
5752 if (name == NULL)
5753 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005754 rettv->v_type = VAR_STRING;
5755 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756
Bram Moolenaar8c711452005-01-14 21:53:12 +00005757 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 {
5759 if (*p == '\\')
5760 {
5761 switch (*++p)
5762 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005763 case 'b': *name++ = BS; ++p; break;
5764 case 'e': *name++ = ESC; ++p; break;
5765 case 'f': *name++ = FF; ++p; break;
5766 case 'n': *name++ = NL; ++p; break;
5767 case 'r': *name++ = CAR; ++p; break;
5768 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769
5770 case 'X': /* hex: "\x1", "\x12" */
5771 case 'x':
5772 case 'u': /* Unicode: "\u0023" */
5773 case 'U':
5774 if (vim_isxdigit(p[1]))
5775 {
5776 int n, nr;
5777 int c = toupper(*p);
5778
5779 if (c == 'X')
5780 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005781 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005783 else
5784 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 nr = 0;
5786 while (--n >= 0 && vim_isxdigit(p[1]))
5787 {
5788 ++p;
5789 nr = (nr << 4) + hex2nr(*p);
5790 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005791 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792#ifdef FEAT_MBYTE
5793 /* For "\u" store the number according to
5794 * 'encoding'. */
5795 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005796 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 else
5798#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005799 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 break;
5802
5803 /* octal: "\1", "\12", "\123" */
5804 case '0':
5805 case '1':
5806 case '2':
5807 case '3':
5808 case '4':
5809 case '5':
5810 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005811 case '7': *name = *p++ - '0';
5812 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005814 *name = (*name << 3) + *p++ - '0';
5815 if (*p >= '0' && *p <= '7')
5816 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005818 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 break;
5820
5821 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005822 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 if (extra != 0)
5824 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005825 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 break;
5827 }
5828 /* FALLTHROUGH */
5829
Bram Moolenaar8c711452005-01-14 21:53:12 +00005830 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 break;
5832 }
5833 }
5834 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005835 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005838 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 *arg = p + 1;
5840
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 return OK;
5842}
5843
5844/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005845 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 * Return OK or FAIL.
5847 */
5848 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005849get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850{
5851 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005852 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005853 int reduce = 0;
5854
5855 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005856 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005857 */
5858 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5859 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005860 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005861 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005862 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005863 break;
5864 ++reduce;
5865 ++p;
5866 }
5867 }
5868
Bram Moolenaar8c711452005-01-14 21:53:12 +00005869 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005870 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005871 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005872 return FAIL;
5873 }
5874
Bram Moolenaar8c711452005-01-14 21:53:12 +00005875 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005876 if (!evaluate)
5877 {
5878 *arg = p + 1;
5879 return OK;
5880 }
5881
5882 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005883 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005884 */
5885 str = alloc((unsigned)((p - *arg) - reduce));
5886 if (str == NULL)
5887 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005888 rettv->v_type = VAR_STRING;
5889 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005890
Bram Moolenaar8c711452005-01-14 21:53:12 +00005891 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005892 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005893 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005894 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005895 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005896 break;
5897 ++p;
5898 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005899 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005900 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005901 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005902 *arg = p + 1;
5903
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005904 return OK;
5905}
5906
5907/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005908 * Allocate a variable for a List and fill it from "*arg".
5909 * Return OK or FAIL.
5910 */
5911 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005912get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005913{
Bram Moolenaar33570922005-01-25 22:26:29 +00005914 list_T *l = NULL;
5915 typval_T tv;
5916 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005917
5918 if (evaluate)
5919 {
5920 l = list_alloc();
5921 if (l == NULL)
5922 return FAIL;
5923 }
5924
5925 *arg = skipwhite(*arg + 1);
5926 while (**arg != ']' && **arg != NUL)
5927 {
5928 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5929 goto failret;
5930 if (evaluate)
5931 {
5932 item = listitem_alloc();
5933 if (item != NULL)
5934 {
5935 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005936 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005937 list_append(l, item);
5938 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005939 else
5940 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005941 }
5942
5943 if (**arg == ']')
5944 break;
5945 if (**arg != ',')
5946 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005947 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005948 goto failret;
5949 }
5950 *arg = skipwhite(*arg + 1);
5951 }
5952
5953 if (**arg != ']')
5954 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005955 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005956failret:
5957 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005958 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005959 return FAIL;
5960 }
5961
5962 *arg = skipwhite(*arg + 1);
5963 if (evaluate)
5964 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005965 rettv->v_type = VAR_LIST;
5966 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005967 ++l->lv_refcount;
5968 }
5969
5970 return OK;
5971}
5972
5973/*
5974 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005975 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005976 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005977 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005978list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005979{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005980 list_T *l;
5981
5982 l = (list_T *)alloc_clear(sizeof(list_T));
5983 if (l != NULL)
5984 {
5985 /* Prepend the list to the list of lists for garbage collection. */
5986 if (first_list != NULL)
5987 first_list->lv_used_prev = l;
5988 l->lv_used_prev = NULL;
5989 l->lv_used_next = first_list;
5990 first_list = l;
5991 }
5992 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005993}
5994
5995/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005996 * Allocate an empty list for a return value.
5997 * Returns OK or FAIL.
5998 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005999 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006000rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006001{
6002 list_T *l = list_alloc();
6003
6004 if (l == NULL)
6005 return FAIL;
6006
6007 rettv->vval.v_list = l;
6008 rettv->v_type = VAR_LIST;
6009 ++l->lv_refcount;
6010 return OK;
6011}
6012
6013/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006014 * Unreference a list: decrement the reference count and free it when it
6015 * becomes zero.
6016 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006017 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006018list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006019{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006020 if (l != NULL && --l->lv_refcount <= 0)
6021 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006022}
6023
6024/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006025 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006026 * Ignores the reference count.
6027 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006028 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006029list_free(
6030 list_T *l,
6031 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006032{
Bram Moolenaar33570922005-01-25 22:26:29 +00006033 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006034
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006035 /* Remove the list from the list of lists for garbage collection. */
6036 if (l->lv_used_prev == NULL)
6037 first_list = l->lv_used_next;
6038 else
6039 l->lv_used_prev->lv_used_next = l->lv_used_next;
6040 if (l->lv_used_next != NULL)
6041 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6042
Bram Moolenaard9fba312005-06-26 22:34:35 +00006043 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006044 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006045 /* Remove the item before deleting it. */
6046 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006047 if (recurse || (item->li_tv.v_type != VAR_LIST
6048 && item->li_tv.v_type != VAR_DICT))
6049 clear_tv(&item->li_tv);
6050 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006051 }
6052 vim_free(l);
6053}
6054
6055/*
6056 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006057 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006058 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006059 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006060listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006061{
Bram Moolenaar33570922005-01-25 22:26:29 +00006062 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006063}
6064
6065/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006066 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006067 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006068 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006069listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006070{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006071 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006072 vim_free(item);
6073}
6074
6075/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006076 * Remove a list item from a List and free it. Also clears the value.
6077 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006078 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006079listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006080{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006081 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006082 listitem_free(item);
6083}
6084
6085/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006086 * Get the number of items in a list.
6087 */
6088 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006089list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006090{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006091 if (l == NULL)
6092 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006093 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006094}
6095
6096/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006097 * Return TRUE when two lists have exactly the same values.
6098 */
6099 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006100list_equal(
6101 list_T *l1,
6102 list_T *l2,
6103 int ic, /* ignore case for strings */
6104 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006105{
Bram Moolenaar33570922005-01-25 22:26:29 +00006106 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006107
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006108 if (l1 == NULL || l2 == NULL)
6109 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006110 if (l1 == l2)
6111 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006112 if (list_len(l1) != list_len(l2))
6113 return FALSE;
6114
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006115 for (item1 = l1->lv_first, item2 = l2->lv_first;
6116 item1 != NULL && item2 != NULL;
6117 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006118 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006119 return FALSE;
6120 return item1 == NULL && item2 == NULL;
6121}
6122
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006123/*
6124 * Return the dictitem that an entry in a hashtable points to.
6125 */
6126 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006127dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006128{
6129 return HI2DI(hi);
6130}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006131
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006132/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006133 * Return TRUE when two dictionaries have exactly the same key/values.
6134 */
6135 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006136dict_equal(
6137 dict_T *d1,
6138 dict_T *d2,
6139 int ic, /* ignore case for strings */
6140 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006141{
Bram Moolenaar33570922005-01-25 22:26:29 +00006142 hashitem_T *hi;
6143 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006144 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006145
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006146 if (d1 == NULL || d2 == NULL)
6147 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006148 if (d1 == d2)
6149 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006150 if (dict_len(d1) != dict_len(d2))
6151 return FALSE;
6152
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006153 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006154 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006155 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006156 if (!HASHITEM_EMPTY(hi))
6157 {
6158 item2 = dict_find(d2, hi->hi_key, -1);
6159 if (item2 == NULL)
6160 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006161 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006162 return FALSE;
6163 --todo;
6164 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006165 }
6166 return TRUE;
6167}
6168
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006169static int tv_equal_recurse_limit;
6170
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006171/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006172 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006173 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006174 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006175 */
6176 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006177tv_equal(
6178 typval_T *tv1,
6179 typval_T *tv2,
6180 int ic, /* ignore case */
6181 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006182{
6183 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006184 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006185 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006186 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006187
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006188 /* For VAR_FUNC and VAR_PARTIAL only compare the function name. */
6189 if ((tv1->v_type == VAR_FUNC
6190 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
6191 && (tv2->v_type == VAR_FUNC
6192 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
6193 {
6194 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
6195 : tv1->vval.v_partial->pt_name;
6196 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
6197 : tv2->vval.v_partial->pt_name;
6198 return (s1 != NULL && s2 != NULL && STRCMP(s1, s2) == 0);
6199 }
6200
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006201 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006202 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006203
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006204 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006205 * recursiveness to a limit. We guess they are equal then.
6206 * A fixed limit has the problem of still taking an awful long time.
6207 * Reduce the limit every time running into it. That should work fine for
6208 * deeply linked structures that are not recursively linked and catch
6209 * recursiveness quickly. */
6210 if (!recursive)
6211 tv_equal_recurse_limit = 1000;
6212 if (recursive_cnt >= tv_equal_recurse_limit)
6213 {
6214 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006215 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006216 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006217
6218 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006219 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006220 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006221 ++recursive_cnt;
6222 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6223 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006224 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006225
6226 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006227 ++recursive_cnt;
6228 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6229 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006230 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006231
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006232 case VAR_NUMBER:
6233 return tv1->vval.v_number == tv2->vval.v_number;
6234
6235 case VAR_STRING:
6236 s1 = get_tv_string_buf(tv1, buf1);
6237 s2 = get_tv_string_buf(tv2, buf2);
6238 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006239
6240 case VAR_SPECIAL:
6241 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006242
6243 case VAR_FLOAT:
6244#ifdef FEAT_FLOAT
6245 return tv1->vval.v_float == tv2->vval.v_float;
6246#endif
6247 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006248#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006249 return tv1->vval.v_job == tv2->vval.v_job;
6250#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006251 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006252#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006253 return tv1->vval.v_channel == tv2->vval.v_channel;
6254#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006255 case VAR_FUNC:
6256 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006257 case VAR_UNKNOWN:
6258 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006259 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006260
Bram Moolenaara03f2332016-02-06 18:09:59 +01006261 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6262 * does not equal anything, not even itself. */
6263 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006264}
6265
6266/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006267 * Locate item with index "n" in list "l" and return it.
6268 * A negative index is counted from the end; -1 is the last item.
6269 * Returns NULL when "n" is out of range.
6270 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006271 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006272list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006273{
Bram Moolenaar33570922005-01-25 22:26:29 +00006274 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006275 long idx;
6276
6277 if (l == NULL)
6278 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006279
6280 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006281 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006282 n = l->lv_len + n;
6283
6284 /* Check for index out of range. */
6285 if (n < 0 || n >= l->lv_len)
6286 return NULL;
6287
6288 /* When there is a cached index may start search from there. */
6289 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006290 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006291 if (n < l->lv_idx / 2)
6292 {
6293 /* closest to the start of the list */
6294 item = l->lv_first;
6295 idx = 0;
6296 }
6297 else if (n > (l->lv_idx + l->lv_len) / 2)
6298 {
6299 /* closest to the end of the list */
6300 item = l->lv_last;
6301 idx = l->lv_len - 1;
6302 }
6303 else
6304 {
6305 /* closest to the cached index */
6306 item = l->lv_idx_item;
6307 idx = l->lv_idx;
6308 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006309 }
6310 else
6311 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006312 if (n < l->lv_len / 2)
6313 {
6314 /* closest to the start of the list */
6315 item = l->lv_first;
6316 idx = 0;
6317 }
6318 else
6319 {
6320 /* closest to the end of the list */
6321 item = l->lv_last;
6322 idx = l->lv_len - 1;
6323 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006324 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006325
6326 while (n > idx)
6327 {
6328 /* search forward */
6329 item = item->li_next;
6330 ++idx;
6331 }
6332 while (n < idx)
6333 {
6334 /* search backward */
6335 item = item->li_prev;
6336 --idx;
6337 }
6338
6339 /* cache the used index */
6340 l->lv_idx = idx;
6341 l->lv_idx_item = item;
6342
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006343 return item;
6344}
6345
6346/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006347 * Get list item "l[idx]" as a number.
6348 */
6349 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006350list_find_nr(
6351 list_T *l,
6352 long idx,
6353 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006354{
6355 listitem_T *li;
6356
6357 li = list_find(l, idx);
6358 if (li == NULL)
6359 {
6360 if (errorp != NULL)
6361 *errorp = TRUE;
6362 return -1L;
6363 }
6364 return get_tv_number_chk(&li->li_tv, errorp);
6365}
6366
6367/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006368 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6369 */
6370 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006371list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006372{
6373 listitem_T *li;
6374
6375 li = list_find(l, idx - 1);
6376 if (li == NULL)
6377 {
6378 EMSGN(_(e_listidx), idx);
6379 return NULL;
6380 }
6381 return get_tv_string(&li->li_tv);
6382}
6383
6384/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006385 * Locate "item" list "l" and return its index.
6386 * Returns -1 when "item" is not in the list.
6387 */
6388 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006389list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006390{
6391 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006392 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006393
6394 if (l == NULL)
6395 return -1;
6396 idx = 0;
6397 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6398 ++idx;
6399 if (li == NULL)
6400 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006401 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006402}
6403
6404/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006405 * Append item "item" to the end of list "l".
6406 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006407 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006408list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006409{
6410 if (l->lv_last == NULL)
6411 {
6412 /* empty list */
6413 l->lv_first = item;
6414 l->lv_last = item;
6415 item->li_prev = NULL;
6416 }
6417 else
6418 {
6419 l->lv_last->li_next = item;
6420 item->li_prev = l->lv_last;
6421 l->lv_last = item;
6422 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006423 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006424 item->li_next = NULL;
6425}
6426
6427/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006428 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006429 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006430 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006431 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006432list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006433{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006434 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006435
Bram Moolenaar05159a02005-02-26 23:04:13 +00006436 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006437 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006438 copy_tv(tv, &li->li_tv);
6439 list_append(l, li);
6440 return OK;
6441}
6442
6443/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006444 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006445 * Return FAIL when out of memory.
6446 */
6447 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006448list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006449{
6450 listitem_T *li = listitem_alloc();
6451
6452 if (li == NULL)
6453 return FAIL;
6454 li->li_tv.v_type = VAR_DICT;
6455 li->li_tv.v_lock = 0;
6456 li->li_tv.vval.v_dict = dict;
6457 list_append(list, li);
6458 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006459 return OK;
6460}
6461
6462/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006463 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006464 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006465 * Returns FAIL when out of memory.
6466 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006467 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006468list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006469{
6470 listitem_T *li = listitem_alloc();
6471
6472 if (li == NULL)
6473 return FAIL;
6474 list_append(l, li);
6475 li->li_tv.v_type = VAR_STRING;
6476 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006477 if (str == NULL)
6478 li->li_tv.vval.v_string = NULL;
6479 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006480 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006481 return FAIL;
6482 return OK;
6483}
6484
6485/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006486 * Append "n" to list "l".
6487 * Returns FAIL when out of memory.
6488 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006489 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006490list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006491{
6492 listitem_T *li;
6493
6494 li = listitem_alloc();
6495 if (li == NULL)
6496 return FAIL;
6497 li->li_tv.v_type = VAR_NUMBER;
6498 li->li_tv.v_lock = 0;
6499 li->li_tv.vval.v_number = n;
6500 list_append(l, li);
6501 return OK;
6502}
6503
6504/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006505 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006506 * If "item" is NULL append at the end.
6507 * Return FAIL when out of memory.
6508 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006509 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006510list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006511{
Bram Moolenaar33570922005-01-25 22:26:29 +00006512 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006513
6514 if (ni == NULL)
6515 return FAIL;
6516 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006517 list_insert(l, ni, item);
6518 return OK;
6519}
6520
6521 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006522list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006523{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006524 if (item == NULL)
6525 /* Append new item at end of list. */
6526 list_append(l, ni);
6527 else
6528 {
6529 /* Insert new item before existing item. */
6530 ni->li_prev = item->li_prev;
6531 ni->li_next = item;
6532 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006533 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006534 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006535 ++l->lv_idx;
6536 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006537 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006538 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006539 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006540 l->lv_idx_item = NULL;
6541 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006542 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006543 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006544 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006545}
6546
6547/*
6548 * Extend "l1" with "l2".
6549 * If "bef" is NULL append at the end, otherwise insert before this item.
6550 * Returns FAIL when out of memory.
6551 */
6552 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006553list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006554{
Bram Moolenaar33570922005-01-25 22:26:29 +00006555 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006556 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006557
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006558 /* We also quit the loop when we have inserted the original item count of
6559 * the list, avoid a hang when we extend a list with itself. */
6560 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006561 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6562 return FAIL;
6563 return OK;
6564}
6565
6566/*
6567 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6568 * Return FAIL when out of memory.
6569 */
6570 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006571list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006572{
Bram Moolenaar33570922005-01-25 22:26:29 +00006573 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006574
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006575 if (l1 == NULL || l2 == NULL)
6576 return FAIL;
6577
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006578 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006579 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006580 if (l == NULL)
6581 return FAIL;
6582 tv->v_type = VAR_LIST;
6583 tv->vval.v_list = l;
6584
6585 /* append all items from the second list */
6586 return list_extend(l, l2, NULL);
6587}
6588
6589/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006590 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006591 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006592 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006593 * Returns NULL when out of memory.
6594 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006595 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006596list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006597{
Bram Moolenaar33570922005-01-25 22:26:29 +00006598 list_T *copy;
6599 listitem_T *item;
6600 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006601
6602 if (orig == NULL)
6603 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006604
6605 copy = list_alloc();
6606 if (copy != NULL)
6607 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006608 if (copyID != 0)
6609 {
6610 /* Do this before adding the items, because one of the items may
6611 * refer back to this list. */
6612 orig->lv_copyID = copyID;
6613 orig->lv_copylist = copy;
6614 }
6615 for (item = orig->lv_first; item != NULL && !got_int;
6616 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006617 {
6618 ni = listitem_alloc();
6619 if (ni == NULL)
6620 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006621 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006622 {
6623 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6624 {
6625 vim_free(ni);
6626 break;
6627 }
6628 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006629 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006630 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006631 list_append(copy, ni);
6632 }
6633 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006634 if (item != NULL)
6635 {
6636 list_unref(copy);
6637 copy = NULL;
6638 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006639 }
6640
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006641 return copy;
6642}
6643
6644/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006645 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006646 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006647 * This used to be called list_remove, but that conflicts with a Sun header
6648 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006649 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006650 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006651vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006652{
Bram Moolenaar33570922005-01-25 22:26:29 +00006653 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006654
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006655 /* notify watchers */
6656 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006657 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006658 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006659 list_fix_watch(l, ip);
6660 if (ip == item2)
6661 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006662 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006663
6664 if (item2->li_next == NULL)
6665 l->lv_last = item->li_prev;
6666 else
6667 item2->li_next->li_prev = item->li_prev;
6668 if (item->li_prev == NULL)
6669 l->lv_first = item2->li_next;
6670 else
6671 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006672 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006673}
6674
6675/*
6676 * Return an allocated string with the string representation of a list.
6677 * May return NULL.
6678 */
6679 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006680list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006681{
6682 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006683
6684 if (tv->vval.v_list == NULL)
6685 return NULL;
6686 ga_init2(&ga, (int)sizeof(char), 80);
6687 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006688 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006689 {
6690 vim_free(ga.ga_data);
6691 return NULL;
6692 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006693 ga_append(&ga, ']');
6694 ga_append(&ga, NUL);
6695 return (char_u *)ga.ga_data;
6696}
6697
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006698typedef struct join_S {
6699 char_u *s;
6700 char_u *tofree;
6701} join_T;
6702
6703 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006704list_join_inner(
6705 garray_T *gap, /* to store the result in */
6706 list_T *l,
6707 char_u *sep,
6708 int echo_style,
6709 int copyID,
6710 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006711{
6712 int i;
6713 join_T *p;
6714 int len;
6715 int sumlen = 0;
6716 int first = TRUE;
6717 char_u *tofree;
6718 char_u numbuf[NUMBUFLEN];
6719 listitem_T *item;
6720 char_u *s;
6721
6722 /* Stringify each item in the list. */
6723 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6724 {
6725 if (echo_style)
6726 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6727 else
6728 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6729 if (s == NULL)
6730 return FAIL;
6731
6732 len = (int)STRLEN(s);
6733 sumlen += len;
6734
Bram Moolenaarcde88542015-08-11 19:14:00 +02006735 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006736 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6737 if (tofree != NULL || s != numbuf)
6738 {
6739 p->s = s;
6740 p->tofree = tofree;
6741 }
6742 else
6743 {
6744 p->s = vim_strnsave(s, len);
6745 p->tofree = p->s;
6746 }
6747
6748 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006749 if (did_echo_string_emsg) /* recursion error, bail out */
6750 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006751 }
6752
6753 /* Allocate result buffer with its total size, avoid re-allocation and
6754 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6755 if (join_gap->ga_len >= 2)
6756 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6757 if (ga_grow(gap, sumlen + 2) == FAIL)
6758 return FAIL;
6759
6760 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6761 {
6762 if (first)
6763 first = FALSE;
6764 else
6765 ga_concat(gap, sep);
6766 p = ((join_T *)join_gap->ga_data) + i;
6767
6768 if (p->s != NULL)
6769 ga_concat(gap, p->s);
6770 line_breakcheck();
6771 }
6772
6773 return OK;
6774}
6775
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006776/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006777 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006778 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006779 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006780 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006781 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006782list_join(
6783 garray_T *gap,
6784 list_T *l,
6785 char_u *sep,
6786 int echo_style,
6787 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006788{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006789 garray_T join_ga;
6790 int retval;
6791 join_T *p;
6792 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006793
Bram Moolenaard39a7512015-04-16 22:51:22 +02006794 if (l->lv_len < 1)
6795 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006796 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6797 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6798
6799 /* Dispose each item in join_ga. */
6800 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006801 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006802 p = (join_T *)join_ga.ga_data;
6803 for (i = 0; i < join_ga.ga_len; ++i)
6804 {
6805 vim_free(p->tofree);
6806 ++p;
6807 }
6808 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006809 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006810
6811 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006812}
6813
6814/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006815 * Return the next (unique) copy ID.
6816 * Used for serializing nested structures.
6817 */
6818 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006819get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006820{
6821 current_copyID += COPYID_INC;
6822 return current_copyID;
6823}
6824
6825/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006826 * Garbage collection for lists and dictionaries.
6827 *
6828 * We use reference counts to be able to free most items right away when they
6829 * are no longer used. But for composite items it's possible that it becomes
6830 * unused while the reference count is > 0: When there is a recursive
6831 * reference. Example:
6832 * :let l = [1, 2, 3]
6833 * :let d = {9: l}
6834 * :let l[1] = d
6835 *
6836 * Since this is quite unusual we handle this with garbage collection: every
6837 * once in a while find out which lists and dicts are not referenced from any
6838 * variable.
6839 *
6840 * Here is a good reference text about garbage collection (refers to Python
6841 * but it applies to all reference-counting mechanisms):
6842 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006843 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006844
6845/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006846 * Do garbage collection for lists and dicts.
6847 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006848 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006849 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006850garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006851{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006852 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006853 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006854 buf_T *buf;
6855 win_T *wp;
6856 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006857 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006858 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006859 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006860#ifdef FEAT_WINDOWS
6861 tabpage_T *tp;
6862#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006863
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006864 /* Only do this once. */
6865 want_garbage_collect = FALSE;
6866 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006867 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006868
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006869 /* We advance by two because we add one for items referenced through
6870 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006871 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006872
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006873 /*
6874 * 1. Go through all accessible variables and mark all lists and dicts
6875 * with copyID.
6876 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006877
6878 /* Don't free variables in the previous_funccal list unless they are only
6879 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006880 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006881 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6882 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006883 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6884 NULL);
6885 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6886 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006887 }
6888
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006889 /* script-local variables */
6890 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006891 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006892
6893 /* buffer-local variables */
6894 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006895 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6896 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006897
6898 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006899 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006900 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6901 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006902#ifdef FEAT_AUTOCMD
6903 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006904 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6905 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006906#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006907
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006908#ifdef FEAT_WINDOWS
6909 /* tabpage-local variables */
6910 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006911 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6912 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006913#endif
6914
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006915 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006916 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006917
6918 /* function-local variables */
6919 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6920 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006921 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6922 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006923 }
6924
Bram Moolenaard812df62008-11-09 12:46:09 +00006925 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006926 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006927
Bram Moolenaar1dced572012-04-05 16:54:08 +02006928#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006929 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006930#endif
6931
Bram Moolenaardb913952012-06-29 12:54:53 +02006932#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006933 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006934#endif
6935
6936#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006937 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006938#endif
6939
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006940#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006941 abort = abort || set_ref_in_channel(copyID);
6942#endif
6943
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006944 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006945 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006946 /*
6947 * 2. Free lists and dictionaries that are not referenced.
6948 */
6949 did_free = free_unref_items(copyID);
6950
6951 /*
6952 * 3. Check if any funccal can be freed now.
6953 */
6954 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006955 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006956 if (can_free_funccal(*pfc, copyID))
6957 {
6958 fc = *pfc;
6959 *pfc = fc->caller;
6960 free_funccal(fc, TRUE);
6961 did_free = TRUE;
6962 did_free_funccal = TRUE;
6963 }
6964 else
6965 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006966 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006967 if (did_free_funccal)
6968 /* When a funccal was freed some more items might be garbage
6969 * collected, so run again. */
6970 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006971 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006972 else if (p_verbose > 0)
6973 {
6974 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6975 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006976
6977 return did_free;
6978}
6979
6980/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01006981 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006982 */
6983 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006984free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006985{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006986 dict_T *dd, *dd_next;
6987 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006988 int did_free = FALSE;
6989
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006990 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006991 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006992 */
6993 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006994 {
6995 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006996 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006997 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006998 /* Free the Dictionary and ordinary items it contains, but don't
6999 * recurse into Lists and Dictionaries, they will be in the list
7000 * of dicts or list of lists. */
7001 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007002 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007003 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007004 dd = dd_next;
7005 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007006
7007 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007008 * Go through the list of lists and free items without the copyID.
7009 * But don't free a list that has a watcher (used in a for loop), these
7010 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007011 */
7012 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007013 {
7014 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007015 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7016 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007017 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007018 /* Free the List and ordinary items it contains, but don't recurse
7019 * into Lists and Dictionaries, they will be in the list of dicts
7020 * or list of lists. */
7021 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007022 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007023 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007024 ll = ll_next;
7025 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007026
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007027 return did_free;
7028}
7029
7030/*
7031 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007032 * "list_stack" is used to add lists to be marked. Can be NULL.
7033 *
7034 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007035 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007036 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007037set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007038{
7039 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007040 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007041 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007042 hashtab_T *cur_ht;
7043 ht_stack_T *ht_stack = NULL;
7044 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007045
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007046 cur_ht = ht;
7047 for (;;)
7048 {
7049 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007050 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007051 /* Mark each item in the hashtab. If the item contains a hashtab
7052 * it is added to ht_stack, if it contains a list it is added to
7053 * list_stack. */
7054 todo = (int)cur_ht->ht_used;
7055 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7056 if (!HASHITEM_EMPTY(hi))
7057 {
7058 --todo;
7059 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7060 &ht_stack, list_stack);
7061 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007062 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007063
7064 if (ht_stack == NULL)
7065 break;
7066
7067 /* take an item from the stack */
7068 cur_ht = ht_stack->ht;
7069 tempitem = ht_stack;
7070 ht_stack = ht_stack->prev;
7071 free(tempitem);
7072 }
7073
7074 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007075}
7076
7077/*
7078 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007079 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7080 *
7081 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007082 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007083 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007084set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007085{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007086 listitem_T *li;
7087 int abort = FALSE;
7088 list_T *cur_l;
7089 list_stack_T *list_stack = NULL;
7090 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007091
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007092 cur_l = l;
7093 for (;;)
7094 {
7095 if (!abort)
7096 /* Mark each item in the list. If the item contains a hashtab
7097 * it is added to ht_stack, if it contains a list it is added to
7098 * list_stack. */
7099 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7100 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7101 ht_stack, &list_stack);
7102 if (list_stack == NULL)
7103 break;
7104
7105 /* take an item from the stack */
7106 cur_l = list_stack->list;
7107 tempitem = list_stack;
7108 list_stack = list_stack->prev;
7109 free(tempitem);
7110 }
7111
7112 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007113}
7114
7115/*
7116 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007117 * "list_stack" is used to add lists to be marked. Can be NULL.
7118 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7119 *
7120 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007121 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007122 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007123set_ref_in_item(
7124 typval_T *tv,
7125 int copyID,
7126 ht_stack_T **ht_stack,
7127 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007128{
7129 dict_T *dd;
7130 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007131 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007132
Bram Moolenaara03f2332016-02-06 18:09:59 +01007133 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007134 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007135 dd = tv->vval.v_dict;
7136 if (dd != NULL && dd->dv_copyID != copyID)
7137 {
7138 /* Didn't see this dict yet. */
7139 dd->dv_copyID = copyID;
7140 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007141 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007142 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7143 }
7144 else
7145 {
7146 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7147 if (newitem == NULL)
7148 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007149 else
7150 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007151 newitem->ht = &dd->dv_hashtab;
7152 newitem->prev = *ht_stack;
7153 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007154 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007155 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007156 }
7157 }
7158 else if (tv->v_type == VAR_LIST)
7159 {
7160 ll = tv->vval.v_list;
7161 if (ll != NULL && ll->lv_copyID != copyID)
7162 {
7163 /* Didn't see this list yet. */
7164 ll->lv_copyID = copyID;
7165 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007166 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007167 abort = set_ref_in_list(ll, copyID, ht_stack);
7168 }
7169 else
7170 {
7171 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007172 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007173 if (newitem == NULL)
7174 abort = TRUE;
7175 else
7176 {
7177 newitem->list = ll;
7178 newitem->prev = *list_stack;
7179 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007180 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007181 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007182 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007183 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007184 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007185}
7186
7187/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007188 * Allocate an empty header for a dictionary.
7189 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007190 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007191dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007192{
Bram Moolenaar33570922005-01-25 22:26:29 +00007193 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007194
Bram Moolenaar33570922005-01-25 22:26:29 +00007195 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007196 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007197 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007198 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007199 if (first_dict != NULL)
7200 first_dict->dv_used_prev = d;
7201 d->dv_used_next = first_dict;
7202 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007203 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007204
Bram Moolenaar33570922005-01-25 22:26:29 +00007205 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007206 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007207 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007208 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007209 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007210 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007211 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007212}
7213
7214/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007215 * Allocate an empty dict for a return value.
7216 * Returns OK or FAIL.
7217 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007218 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007219rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007220{
7221 dict_T *d = dict_alloc();
7222
7223 if (d == NULL)
7224 return FAIL;
7225
7226 rettv->vval.v_dict = d;
7227 rettv->v_type = VAR_DICT;
7228 ++d->dv_refcount;
7229 return OK;
7230}
7231
7232
7233/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007234 * Unreference a Dictionary: decrement the reference count and free it when it
7235 * becomes zero.
7236 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007237 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007238dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007239{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007240 if (d != NULL && --d->dv_refcount <= 0)
7241 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007242}
7243
7244/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007245 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007246 * Ignores the reference count.
7247 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007248 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007249dict_free(
7250 dict_T *d,
7251 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007252{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007253 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007254 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007255 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007256
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007257 /* Remove the dict from the list of dicts for garbage collection. */
7258 if (d->dv_used_prev == NULL)
7259 first_dict = d->dv_used_next;
7260 else
7261 d->dv_used_prev->dv_used_next = d->dv_used_next;
7262 if (d->dv_used_next != NULL)
7263 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7264
7265 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007266 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007267 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007268 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007269 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007270 if (!HASHITEM_EMPTY(hi))
7271 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007272 /* Remove the item before deleting it, just in case there is
7273 * something recursive causing trouble. */
7274 di = HI2DI(hi);
7275 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007276 if (recurse || (di->di_tv.v_type != VAR_LIST
7277 && di->di_tv.v_type != VAR_DICT))
7278 clear_tv(&di->di_tv);
7279 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007280 --todo;
7281 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007282 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007283 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007284 vim_free(d);
7285}
7286
7287/*
7288 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007289 * The "key" is copied to the new item.
7290 * Note that the value of the item "di_tv" still needs to be initialized!
7291 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007292 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007293 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007294dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007295{
Bram Moolenaar33570922005-01-25 22:26:29 +00007296 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007297
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007298 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007299 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007300 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007301 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007302 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007303 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007304 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007305}
7306
7307/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007308 * Make a copy of a Dictionary item.
7309 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007310 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007311dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007312{
Bram Moolenaar33570922005-01-25 22:26:29 +00007313 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007314
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007315 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7316 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007317 if (di != NULL)
7318 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007319 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007320 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007321 copy_tv(&org->di_tv, &di->di_tv);
7322 }
7323 return di;
7324}
7325
7326/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007327 * Remove item "item" from Dictionary "dict" and free it.
7328 */
7329 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007330dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007331{
Bram Moolenaar33570922005-01-25 22:26:29 +00007332 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007333
Bram Moolenaar33570922005-01-25 22:26:29 +00007334 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007335 if (HASHITEM_EMPTY(hi))
7336 EMSG2(_(e_intern2), "dictitem_remove()");
7337 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007338 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007339 dictitem_free(item);
7340}
7341
7342/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007343 * Free a dict item. Also clears the value.
7344 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007345 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007346dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007347{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007348 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007349 if (item->di_flags & DI_FLAGS_ALLOC)
7350 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007351}
7352
7353/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007354 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7355 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007356 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007357 * Returns NULL when out of memory.
7358 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007359 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007360dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007361{
Bram Moolenaar33570922005-01-25 22:26:29 +00007362 dict_T *copy;
7363 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007364 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007365 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007366
7367 if (orig == NULL)
7368 return NULL;
7369
7370 copy = dict_alloc();
7371 if (copy != NULL)
7372 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007373 if (copyID != 0)
7374 {
7375 orig->dv_copyID = copyID;
7376 orig->dv_copydict = copy;
7377 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007378 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007379 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007380 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007381 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007382 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007383 --todo;
7384
7385 di = dictitem_alloc(hi->hi_key);
7386 if (di == NULL)
7387 break;
7388 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007389 {
7390 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7391 copyID) == FAIL)
7392 {
7393 vim_free(di);
7394 break;
7395 }
7396 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007397 else
7398 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7399 if (dict_add(copy, di) == FAIL)
7400 {
7401 dictitem_free(di);
7402 break;
7403 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007404 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007405 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007406
Bram Moolenaare9a41262005-01-15 22:18:47 +00007407 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007408 if (todo > 0)
7409 {
7410 dict_unref(copy);
7411 copy = NULL;
7412 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007413 }
7414
7415 return copy;
7416}
7417
7418/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007419 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007420 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007421 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007422 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007423dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007424{
Bram Moolenaar33570922005-01-25 22:26:29 +00007425 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007426}
7427
Bram Moolenaar8c711452005-01-14 21:53:12 +00007428/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007429 * Add a number or string entry to dictionary "d".
7430 * When "str" is NULL use number "nr", otherwise use "str".
7431 * Returns FAIL when out of memory and when key already exists.
7432 */
7433 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007434dict_add_nr_str(
7435 dict_T *d,
7436 char *key,
7437 long nr,
7438 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007439{
7440 dictitem_T *item;
7441
7442 item = dictitem_alloc((char_u *)key);
7443 if (item == NULL)
7444 return FAIL;
7445 item->di_tv.v_lock = 0;
7446 if (str == NULL)
7447 {
7448 item->di_tv.v_type = VAR_NUMBER;
7449 item->di_tv.vval.v_number = nr;
7450 }
7451 else
7452 {
7453 item->di_tv.v_type = VAR_STRING;
7454 item->di_tv.vval.v_string = vim_strsave(str);
7455 }
7456 if (dict_add(d, item) == FAIL)
7457 {
7458 dictitem_free(item);
7459 return FAIL;
7460 }
7461 return OK;
7462}
7463
7464/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007465 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007466 * Returns FAIL when out of memory and when key already exists.
7467 */
7468 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007469dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007470{
7471 dictitem_T *item;
7472
7473 item = dictitem_alloc((char_u *)key);
7474 if (item == NULL)
7475 return FAIL;
7476 item->di_tv.v_lock = 0;
7477 item->di_tv.v_type = VAR_LIST;
7478 item->di_tv.vval.v_list = list;
7479 if (dict_add(d, item) == FAIL)
7480 {
7481 dictitem_free(item);
7482 return FAIL;
7483 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007484 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007485 return OK;
7486}
7487
7488/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007489 * Get the number of items in a Dictionary.
7490 */
7491 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007492dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007493{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007494 if (d == NULL)
7495 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007496 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007497}
7498
7499/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007500 * Find item "key[len]" in Dictionary "d".
7501 * If "len" is negative use strlen(key).
7502 * Returns NULL when not found.
7503 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007504 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007505dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007506{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007507#define AKEYLEN 200
7508 char_u buf[AKEYLEN];
7509 char_u *akey;
7510 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007511 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007512
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007513 if (len < 0)
7514 akey = key;
7515 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007516 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007517 tofree = akey = vim_strnsave(key, len);
7518 if (akey == NULL)
7519 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007520 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007521 else
7522 {
7523 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007524 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007525 akey = buf;
7526 }
7527
Bram Moolenaar33570922005-01-25 22:26:29 +00007528 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007529 vim_free(tofree);
7530 if (HASHITEM_EMPTY(hi))
7531 return NULL;
7532 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007533}
7534
7535/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007536 * Get a string item from a dictionary.
7537 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007538 * Returns NULL if the entry doesn't exist or out of memory.
7539 */
7540 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007541get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007542{
7543 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007544 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007545
7546 di = dict_find(d, key, -1);
7547 if (di == NULL)
7548 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007549 s = get_tv_string(&di->di_tv);
7550 if (save && s != NULL)
7551 s = vim_strsave(s);
7552 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007553}
7554
7555/*
7556 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007557 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007558 */
7559 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007560get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007561{
7562 dictitem_T *di;
7563
7564 di = dict_find(d, key, -1);
7565 if (di == NULL)
7566 return 0;
7567 return get_tv_number(&di->di_tv);
7568}
7569
7570/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007571 * Return an allocated string with the string representation of a Dictionary.
7572 * May return NULL.
7573 */
7574 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007575dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007576{
7577 garray_T ga;
7578 int first = TRUE;
7579 char_u *tofree;
7580 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007581 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007582 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007583 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007584 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007585
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007586 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007587 return NULL;
7588 ga_init2(&ga, (int)sizeof(char), 80);
7589 ga_append(&ga, '{');
7590
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007591 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007592 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007593 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007594 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007595 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007596 --todo;
7597
7598 if (first)
7599 first = FALSE;
7600 else
7601 ga_concat(&ga, (char_u *)", ");
7602
7603 tofree = string_quote(hi->hi_key, FALSE);
7604 if (tofree != NULL)
7605 {
7606 ga_concat(&ga, tofree);
7607 vim_free(tofree);
7608 }
7609 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007610 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007611 if (s != NULL)
7612 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007613 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007614 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007615 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007616 line_breakcheck();
7617
Bram Moolenaar8c711452005-01-14 21:53:12 +00007618 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007619 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007620 if (todo > 0)
7621 {
7622 vim_free(ga.ga_data);
7623 return NULL;
7624 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007625
7626 ga_append(&ga, '}');
7627 ga_append(&ga, NUL);
7628 return (char_u *)ga.ga_data;
7629}
7630
7631/*
7632 * Allocate a variable for a Dictionary and fill it from "*arg".
7633 * Return OK or FAIL. Returns NOTDONE for {expr}.
7634 */
7635 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007636get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007637{
Bram Moolenaar33570922005-01-25 22:26:29 +00007638 dict_T *d = NULL;
7639 typval_T tvkey;
7640 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007641 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007642 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007643 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007644 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007645
7646 /*
7647 * First check if it's not a curly-braces thing: {expr}.
7648 * Must do this without evaluating, otherwise a function may be called
7649 * twice. Unfortunately this means we need to call eval1() twice for the
7650 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007651 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007652 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007653 if (*start != '}')
7654 {
7655 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7656 return FAIL;
7657 if (*start == '}')
7658 return NOTDONE;
7659 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007660
7661 if (evaluate)
7662 {
7663 d = dict_alloc();
7664 if (d == NULL)
7665 return FAIL;
7666 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007667 tvkey.v_type = VAR_UNKNOWN;
7668 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007669
7670 *arg = skipwhite(*arg + 1);
7671 while (**arg != '}' && **arg != NUL)
7672 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007673 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007674 goto failret;
7675 if (**arg != ':')
7676 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007677 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007678 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007679 goto failret;
7680 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007681 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007682 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007683 key = get_tv_string_buf_chk(&tvkey, buf);
7684 if (key == NULL || *key == NUL)
7685 {
7686 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7687 if (key != NULL)
7688 EMSG(_(e_emptykey));
7689 clear_tv(&tvkey);
7690 goto failret;
7691 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007692 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007693
7694 *arg = skipwhite(*arg + 1);
7695 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7696 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007697 if (evaluate)
7698 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007699 goto failret;
7700 }
7701 if (evaluate)
7702 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007703 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007704 if (item != NULL)
7705 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007706 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007707 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007708 clear_tv(&tv);
7709 goto failret;
7710 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007711 item = dictitem_alloc(key);
7712 clear_tv(&tvkey);
7713 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007714 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007715 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007716 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007717 if (dict_add(d, item) == FAIL)
7718 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007719 }
7720 }
7721
7722 if (**arg == '}')
7723 break;
7724 if (**arg != ',')
7725 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007726 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007727 goto failret;
7728 }
7729 *arg = skipwhite(*arg + 1);
7730 }
7731
7732 if (**arg != '}')
7733 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007734 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007735failret:
7736 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007737 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007738 return FAIL;
7739 }
7740
7741 *arg = skipwhite(*arg + 1);
7742 if (evaluate)
7743 {
7744 rettv->v_type = VAR_DICT;
7745 rettv->vval.v_dict = d;
7746 ++d->dv_refcount;
7747 }
7748
7749 return OK;
7750}
7751
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007752#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01007753#endif
7754
Bram Moolenaar17a13432016-01-24 14:22:10 +01007755 static char *
7756get_var_special_name(int nr)
7757{
7758 switch (nr)
7759 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007760 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01007761 case VVAL_TRUE: return "v:true";
7762 case VVAL_NONE: return "v:none";
7763 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007764 }
7765 EMSG2(_(e_intern2), "get_var_special_name()");
7766 return "42";
7767}
7768
Bram Moolenaar8c711452005-01-14 21:53:12 +00007769/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007770 * Return a string with the string representation of a variable.
7771 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007772 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007773 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007774 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007775 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007776 */
7777 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007778echo_string(
7779 typval_T *tv,
7780 char_u **tofree,
7781 char_u *numbuf,
7782 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007783{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007784 static int recurse = 0;
7785 char_u *r = NULL;
7786
Bram Moolenaar33570922005-01-25 22:26:29 +00007787 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007788 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007789 if (!did_echo_string_emsg)
7790 {
7791 /* Only give this message once for a recursive call to avoid
7792 * flooding the user with errors. And stop iterating over lists
7793 * and dicts. */
7794 did_echo_string_emsg = TRUE;
7795 EMSG(_("E724: variable nested too deep for displaying"));
7796 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007797 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007798 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007799 }
7800 ++recurse;
7801
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007802 switch (tv->v_type)
7803 {
7804 case VAR_FUNC:
7805 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007806 r = tv->vval.v_string;
7807 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007808
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007809 case VAR_PARTIAL:
7810 *tofree = NULL;
7811 /* TODO: arguments */
7812 r = tv->vval.v_partial == NULL ? NULL : tv->vval.v_partial->pt_name;
7813 break;
7814
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007815 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007816 if (tv->vval.v_list == NULL)
7817 {
7818 *tofree = NULL;
7819 r = NULL;
7820 }
7821 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7822 {
7823 *tofree = NULL;
7824 r = (char_u *)"[...]";
7825 }
7826 else
7827 {
7828 tv->vval.v_list->lv_copyID = copyID;
7829 *tofree = list2string(tv, copyID);
7830 r = *tofree;
7831 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007832 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007833
Bram Moolenaar8c711452005-01-14 21:53:12 +00007834 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007835 if (tv->vval.v_dict == NULL)
7836 {
7837 *tofree = NULL;
7838 r = NULL;
7839 }
7840 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7841 {
7842 *tofree = NULL;
7843 r = (char_u *)"{...}";
7844 }
7845 else
7846 {
7847 tv->vval.v_dict->dv_copyID = copyID;
7848 *tofree = dict2string(tv, copyID);
7849 r = *tofree;
7850 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007851 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007852
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007853 case VAR_STRING:
7854 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007855 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007856 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007857 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007858 *tofree = NULL;
7859 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007860 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007861
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007862 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007863#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007864 *tofree = NULL;
7865 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7866 r = numbuf;
7867 break;
7868#endif
7869
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007870 case VAR_SPECIAL:
7871 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007872 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007873 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007874 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007875
Bram Moolenaar8502c702014-06-17 12:51:16 +02007876 if (--recurse == 0)
7877 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007878 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007879}
7880
7881/*
7882 * Return a string with the string representation of a variable.
7883 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7884 * "numbuf" is used for a number.
7885 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007886 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007887 */
7888 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007889tv2string(
7890 typval_T *tv,
7891 char_u **tofree,
7892 char_u *numbuf,
7893 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007894{
7895 switch (tv->v_type)
7896 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007897 case VAR_FUNC:
7898 *tofree = string_quote(tv->vval.v_string, TRUE);
7899 return *tofree;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007900 case VAR_PARTIAL:
Bram Moolenaar5c291542016-03-19 20:05:45 +01007901 {
7902 partial_T *pt = tv->vval.v_partial;
7903 char_u *fname = string_quote(pt == NULL ? NULL
7904 : pt->pt_name, FALSE);
7905 garray_T ga;
7906 int i;
7907 char_u *tf;
7908
7909 ga_init2(&ga, 1, 100);
7910 ga_concat(&ga, (char_u *)"function(");
7911 if (fname != NULL)
7912 {
7913 ga_concat(&ga, fname);
7914 vim_free(fname);
7915 }
7916 if (pt != NULL && pt->pt_argc > 0)
7917 {
7918 ga_concat(&ga, (char_u *)", [");
7919 for (i = 0; i < pt->pt_argc; ++i)
7920 {
7921 if (i > 0)
7922 ga_concat(&ga, (char_u *)", ");
7923 ga_concat(&ga,
7924 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
7925 vim_free(tf);
7926 }
7927 ga_concat(&ga, (char_u *)"]");
7928 }
7929 if (pt != NULL && pt->pt_dict != NULL)
7930 {
7931 typval_T dtv;
7932
7933 ga_concat(&ga, (char_u *)", ");
7934 dtv.v_type = VAR_DICT;
7935 dtv.vval.v_dict = pt->pt_dict;
7936 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
7937 vim_free(tf);
7938 }
7939 ga_concat(&ga, (char_u *)")");
7940
7941 *tofree = ga.ga_data;
7942 return *tofree;
7943 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007944 case VAR_STRING:
7945 *tofree = string_quote(tv->vval.v_string, FALSE);
7946 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007947 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01007948#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007949 *tofree = NULL;
7950 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7951 return numbuf;
7952#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007953 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007954 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007955 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007956 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007957 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007958 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007959 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007960 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007961 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007962 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007963}
7964
7965/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007966 * Return string "str" in ' quotes, doubling ' characters.
7967 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007968 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007969 */
7970 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007971string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007972{
Bram Moolenaar33570922005-01-25 22:26:29 +00007973 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007974 char_u *p, *r, *s;
7975
Bram Moolenaar33570922005-01-25 22:26:29 +00007976 len = (function ? 13 : 3);
7977 if (str != NULL)
7978 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007979 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007980 for (p = str; *p != NUL; mb_ptr_adv(p))
7981 if (*p == '\'')
7982 ++len;
7983 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007984 s = r = alloc(len);
7985 if (r != NULL)
7986 {
7987 if (function)
7988 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007989 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007990 r += 10;
7991 }
7992 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007993 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007994 if (str != NULL)
7995 for (p = str; *p != NUL; )
7996 {
7997 if (*p == '\'')
7998 *r++ = '\'';
7999 MB_COPY_CHAR(p, r);
8000 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008001 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008002 if (function)
8003 *r++ = ')';
8004 *r++ = NUL;
8005 }
8006 return s;
8007}
8008
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008009#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008010/*
8011 * Convert the string "text" to a floating point number.
8012 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8013 * this always uses a decimal point.
8014 * Returns the length of the text that was consumed.
8015 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008016 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008017string2float(
8018 char_u *text,
8019 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008020{
8021 char *s = (char *)text;
8022 float_T f;
8023
8024 f = strtod(s, &s);
8025 *value = f;
8026 return (int)((char_u *)s - text);
8027}
8028#endif
8029
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008030/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 * Get the value of an environment variable.
8032 * "arg" is pointing to the '$'. It is advanced to after the name.
8033 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008034 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 */
8036 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008037get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038{
8039 char_u *string = NULL;
8040 int len;
8041 int cc;
8042 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008043 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044
8045 ++*arg;
8046 name = *arg;
8047 len = get_env_len(arg);
8048 if (evaluate)
8049 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008050 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008051 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008052
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008053 cc = name[len];
8054 name[len] = NUL;
8055 /* first try vim_getenv(), fast for normal environment vars */
8056 string = vim_getenv(name, &mustfree);
8057 if (string != NULL && *string != NUL)
8058 {
8059 if (!mustfree)
8060 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008062 else
8063 {
8064 if (mustfree)
8065 vim_free(string);
8066
8067 /* next try expanding things like $VIM and ${HOME} */
8068 string = expand_env_save(name - 1);
8069 if (string != NULL && *string == '$')
8070 {
8071 vim_free(string);
8072 string = NULL;
8073 }
8074 }
8075 name[len] = cc;
8076
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008077 rettv->v_type = VAR_STRING;
8078 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 }
8080
8081 return OK;
8082}
8083
8084/*
8085 * Array with names and number of arguments of all internal functions
8086 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8087 */
8088static struct fst
8089{
8090 char *f_name; /* function name */
8091 char f_min_argc; /* minimal number of arguments */
8092 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008093 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008094 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095} functions[] =
8096{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008097#ifdef FEAT_FLOAT
8098 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008099 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008100#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008101 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008102 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008103 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 {"append", 2, 2, f_append},
8105 {"argc", 0, 0, f_argc},
8106 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008107 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008108 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008109#ifdef FEAT_FLOAT
8110 {"asin", 1, 1, f_asin}, /* WJMc */
8111#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008112 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008113 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008114 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008115 {"assert_false", 1, 2, f_assert_false},
8116 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008117#ifdef FEAT_FLOAT
8118 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008119 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008122 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 {"bufexists", 1, 1, f_bufexists},
8124 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8125 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8126 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8127 {"buflisted", 1, 1, f_buflisted},
8128 {"bufloaded", 1, 1, f_bufloaded},
8129 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008130 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 {"bufwinnr", 1, 1, f_bufwinnr},
8132 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008133 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008134 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008135 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008136#ifdef FEAT_FLOAT
8137 {"ceil", 1, 1, f_ceil},
8138#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008139#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008140 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008141 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8142 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008143 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008144 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar03602ec2016-03-20 20:57:45 +01008145 {"ch_info", 1, 1, f_ch_info},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008146 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008147 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008148 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008149 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008150 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008151 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8152 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008153 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008154 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008155#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008156 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008157 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008159 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008161#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008162 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008163 {"complete_add", 1, 1, f_complete_add},
8164 {"complete_check", 0, 0, f_complete_check},
8165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008167 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008168#ifdef FEAT_FLOAT
8169 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008170 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008171#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008172 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008174 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008175 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008176 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008178 {"diff_filler", 1, 1, f_diff_filler},
8179 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008180 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008181 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008183 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 {"eventhandler", 0, 0, f_eventhandler},
8185 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008186 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008188#ifdef FEAT_FLOAT
8189 {"exp", 1, 1, f_exp},
8190#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008191 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008192 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008193 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8195 {"filereadable", 1, 1, f_filereadable},
8196 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008197 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008198 {"finddir", 1, 3, f_finddir},
8199 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008200#ifdef FEAT_FLOAT
8201 {"float2nr", 1, 1, f_float2nr},
8202 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008203 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008204#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008205 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 {"fnamemodify", 2, 2, f_fnamemodify},
8207 {"foldclosed", 1, 1, f_foldclosed},
8208 {"foldclosedend", 1, 1, f_foldclosedend},
8209 {"foldlevel", 1, 1, f_foldlevel},
8210 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008211 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008213 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008214 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008215 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008216 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008217 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 {"getchar", 0, 1, f_getchar},
8219 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008220 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 {"getcmdline", 0, 0, f_getcmdline},
8222 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008223 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008224 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008225 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008226 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008227 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008228 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 {"getfsize", 1, 1, f_getfsize},
8230 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008231 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008232 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008233 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008234 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008235 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008236 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008237 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008238 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008240 {"gettabvar", 2, 3, f_gettabvar},
8241 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 {"getwinposx", 0, 0, f_getwinposx},
8243 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008244 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008245 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008246 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008247 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008249 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008250 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008251 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8253 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8254 {"histadd", 2, 2, f_histadd},
8255 {"histdel", 1, 2, f_histdel},
8256 {"histget", 1, 2, f_histget},
8257 {"histnr", 1, 1, f_histnr},
8258 {"hlID", 1, 1, f_hlID},
8259 {"hlexists", 1, 1, f_hlexists},
8260 {"hostname", 0, 0, f_hostname},
8261 {"iconv", 3, 3, f_iconv},
8262 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008263 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008264 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008266 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 {"inputrestore", 0, 0, f_inputrestore},
8268 {"inputsave", 0, 0, f_inputsave},
8269 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008270 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008271 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008273 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008274#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8275 {"isnan", 1, 1, f_isnan},
8276#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008277 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008278#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008279 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008280 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008281 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008282 {"job_start", 1, 2, f_job_start},
8283 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008284 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008285#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008286 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008287 {"js_decode", 1, 1, f_js_decode},
8288 {"js_encode", 1, 1, f_js_encode},
8289 {"json_decode", 1, 1, f_json_decode},
8290 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008291 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008293 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 {"libcall", 3, 3, f_libcall},
8295 {"libcallnr", 3, 3, f_libcallnr},
8296 {"line", 1, 1, f_line},
8297 {"line2byte", 1, 1, f_line2byte},
8298 {"lispindent", 1, 1, f_lispindent},
8299 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008300#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008301 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008302 {"log10", 1, 1, f_log10},
8303#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008304#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008305 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008306#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008307 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008308 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008309 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008310 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008311 {"matchadd", 2, 5, f_matchadd},
8312 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008313 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008314 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008315 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008316 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008317 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008318 {"max", 1, 1, f_max},
8319 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008320#ifdef vim_mkdir
8321 {"mkdir", 1, 3, f_mkdir},
8322#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008323 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008324#ifdef FEAT_MZSCHEME
8325 {"mzeval", 1, 1, f_mzeval},
8326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008328 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008329 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008330 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008331#ifdef FEAT_PERL
8332 {"perleval", 1, 1, f_perleval},
8333#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008334#ifdef FEAT_FLOAT
8335 {"pow", 2, 2, f_pow},
8336#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008338 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008339 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008340#ifdef FEAT_PYTHON3
8341 {"py3eval", 1, 1, f_py3eval},
8342#endif
8343#ifdef FEAT_PYTHON
8344 {"pyeval", 1, 1, f_pyeval},
8345#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008346 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008347 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008348 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008349#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008350 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008351#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008352 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 {"remote_expr", 2, 3, f_remote_expr},
8354 {"remote_foreground", 1, 1, f_remote_foreground},
8355 {"remote_peek", 1, 2, f_remote_peek},
8356 {"remote_read", 1, 1, f_remote_read},
8357 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008358 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008360 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008362 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008363#ifdef FEAT_FLOAT
8364 {"round", 1, 1, f_round},
8365#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008366 {"screenattr", 2, 2, f_screenattr},
8367 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008368 {"screencol", 0, 0, f_screencol},
8369 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008370 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008371 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008372 {"searchpair", 3, 7, f_searchpair},
8373 {"searchpairpos", 3, 7, f_searchpairpos},
8374 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 {"server2client", 2, 2, f_server2client},
8376 {"serverlist", 0, 0, f_serverlist},
8377 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008378 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008380 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008382 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008383 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008384 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008385 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008387 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008388 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008390#ifdef FEAT_CRYPT
8391 {"sha256", 1, 1, f_sha256},
8392#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008393 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008394 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008396#ifdef FEAT_FLOAT
8397 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008398 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008399#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008400 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008401 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008402 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008403 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008404 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008405#ifdef FEAT_FLOAT
8406 {"sqrt", 1, 1, f_sqrt},
8407 {"str2float", 1, 1, f_str2float},
8408#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008409 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008410 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008411 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412#ifdef HAVE_STRFTIME
8413 {"strftime", 1, 2, f_strftime},
8414#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008415 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008416 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 {"strlen", 1, 1, f_strlen},
8418 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008419 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008421 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008422 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 {"substitute", 4, 4, f_substitute},
8424 {"synID", 3, 3, f_synID},
8425 {"synIDattr", 2, 3, f_synIDattr},
8426 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008427 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008428 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008429 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008430 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008431 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008432 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008433 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008434 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008435 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008436#ifdef FEAT_FLOAT
8437 {"tan", 1, 1, f_tan},
8438 {"tanh", 1, 1, f_tanh},
8439#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008441 {"test", 1, 1, f_test},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008442#ifdef FEAT_TIMERS
8443 {"timer_start", 2, 3, f_timer_start},
8444 {"timer_stop", 1, 1, f_timer_stop},
8445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 {"tolower", 1, 1, f_tolower},
8447 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008448 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008449#ifdef FEAT_FLOAT
8450 {"trunc", 1, 1, f_trunc},
8451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008453 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008454 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008455 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008456 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 {"virtcol", 1, 1, f_virtcol},
8458 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008459 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008460 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008461 {"win_getid", 0, 2, f_win_getid},
8462 {"win_gotoid", 1, 1, f_win_gotoid},
8463 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8464 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 {"winbufnr", 1, 1, f_winbufnr},
8466 {"wincol", 0, 0, f_wincol},
8467 {"winheight", 1, 1, f_winheight},
8468 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008469 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008471 {"winrestview", 1, 1, f_winrestview},
8472 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008474 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008475 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008476 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477};
8478
8479#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8480
8481/*
8482 * Function given to ExpandGeneric() to obtain the list of internal
8483 * or user defined function names.
8484 */
8485 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008486get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487{
8488 static int intidx = -1;
8489 char_u *name;
8490
8491 if (idx == 0)
8492 intidx = -1;
8493 if (intidx < 0)
8494 {
8495 name = get_user_func_name(xp, idx);
8496 if (name != NULL)
8497 return name;
8498 }
8499 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8500 {
8501 STRCPY(IObuff, functions[intidx].f_name);
8502 STRCAT(IObuff, "(");
8503 if (functions[intidx].f_max_argc == 0)
8504 STRCAT(IObuff, ")");
8505 return IObuff;
8506 }
8507
8508 return NULL;
8509}
8510
8511/*
8512 * Function given to ExpandGeneric() to obtain the list of internal or
8513 * user defined variable or function names.
8514 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008516get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517{
8518 static int intidx = -1;
8519 char_u *name;
8520
8521 if (idx == 0)
8522 intidx = -1;
8523 if (intidx < 0)
8524 {
8525 name = get_function_name(xp, idx);
8526 if (name != NULL)
8527 return name;
8528 }
8529 return get_user_var_name(xp, ++intidx);
8530}
8531
8532#endif /* FEAT_CMDL_COMPL */
8533
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008534#if defined(EBCDIC) || defined(PROTO)
8535/*
8536 * Compare struct fst by function name.
8537 */
8538 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008539compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008540{
8541 struct fst *p1 = (struct fst *)s1;
8542 struct fst *p2 = (struct fst *)s2;
8543
8544 return STRCMP(p1->f_name, p2->f_name);
8545}
8546
8547/*
8548 * Sort the function table by function name.
8549 * The sorting of the table above is ASCII dependant.
8550 * On machines using EBCDIC we have to sort it.
8551 */
8552 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008553sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008554{
8555 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8556
8557 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8558}
8559#endif
8560
8561
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562/*
8563 * Find internal function in table above.
8564 * Return index, or -1 if not found
8565 */
8566 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008567find_internal_func(
8568 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569{
8570 int first = 0;
8571 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8572 int cmp;
8573 int x;
8574
8575 /*
8576 * Find the function name in the table. Binary search.
8577 */
8578 while (first <= last)
8579 {
8580 x = first + ((unsigned)(last - first) >> 1);
8581 cmp = STRCMP(name, functions[x].f_name);
8582 if (cmp < 0)
8583 last = x - 1;
8584 else if (cmp > 0)
8585 first = x + 1;
8586 else
8587 return x;
8588 }
8589 return -1;
8590}
8591
8592/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008593 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8594 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01008595 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
8596 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008597 */
8598 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01008599deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008600{
Bram Moolenaar33570922005-01-25 22:26:29 +00008601 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008602 int cc;
8603
Bram Moolenaar65639032016-03-16 21:40:30 +01008604 if (partialp != NULL)
8605 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008606
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008607 cc = name[*lenp];
8608 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008609 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008610 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008611 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008612 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008613 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008614 {
8615 *lenp = 0;
8616 return (char_u *)""; /* just in case */
8617 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008618 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008619 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008620 }
8621
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008622 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
8623 {
Bram Moolenaar65639032016-03-16 21:40:30 +01008624 partial_T *pt = v->di_tv.vval.v_partial;
8625
8626 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008627 {
8628 *lenp = 0;
8629 return (char_u *)""; /* just in case */
8630 }
Bram Moolenaar65639032016-03-16 21:40:30 +01008631 if (partialp != NULL)
8632 *partialp = pt;
8633 *lenp = (int)STRLEN(pt->pt_name);
8634 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008635 }
8636
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008637 return name;
8638}
8639
8640/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 * Allocate a variable for the result of a function.
8642 * Return OK or FAIL.
8643 */
8644 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008645get_func_tv(
8646 char_u *name, /* name of the function */
8647 int len, /* length of "name" */
8648 typval_T *rettv,
8649 char_u **arg, /* argument, pointing to the '(' */
8650 linenr_T firstline, /* first line of range */
8651 linenr_T lastline, /* last line of range */
8652 int *doesrange, /* return: function handled range */
8653 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008654 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008655 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656{
8657 char_u *argp;
8658 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008659 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660 int argcount = 0; /* number of arguments found */
8661
8662 /*
8663 * Get the arguments.
8664 */
8665 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008666 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667 {
8668 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8669 if (*argp == ')' || *argp == ',' || *argp == NUL)
8670 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8672 {
8673 ret = FAIL;
8674 break;
8675 }
8676 ++argcount;
8677 if (*argp != ',')
8678 break;
8679 }
8680 if (*argp == ')')
8681 ++argp;
8682 else
8683 ret = FAIL;
8684
8685 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008686 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008687 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008689 {
8690 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008691 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008692 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008693 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695
8696 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008697 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698
8699 *arg = skipwhite(argp);
8700 return ret;
8701}
8702
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008703#define ERROR_UNKNOWN 0
8704#define ERROR_TOOMANY 1
8705#define ERROR_TOOFEW 2
8706#define ERROR_SCRIPT 3
8707#define ERROR_DICT 4
8708#define ERROR_NONE 5
8709#define ERROR_OTHER 6
8710#define FLEN_FIXED 40
8711
8712/*
8713 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8714 * Change <SNR>123_name() to K_SNR 123_name().
8715 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
8716 * (slow).
8717 */
8718 static char_u *
8719fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
8720{
8721 int llen;
8722 char_u *fname;
8723 int i;
8724
8725 llen = eval_fname_script(name);
8726 if (llen > 0)
8727 {
8728 fname_buf[0] = K_SPECIAL;
8729 fname_buf[1] = KS_EXTRA;
8730 fname_buf[2] = (int)KE_SNR;
8731 i = 3;
8732 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8733 {
8734 if (current_SID <= 0)
8735 *error = ERROR_SCRIPT;
8736 else
8737 {
8738 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8739 i = (int)STRLEN(fname_buf);
8740 }
8741 }
8742 if (i + STRLEN(name + llen) < FLEN_FIXED)
8743 {
8744 STRCPY(fname_buf + i, name + llen);
8745 fname = fname_buf;
8746 }
8747 else
8748 {
8749 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8750 if (fname == NULL)
8751 *error = ERROR_OTHER;
8752 else
8753 {
8754 *tofree = fname;
8755 mch_memmove(fname, fname_buf, (size_t)i);
8756 STRCPY(fname + i, name + llen);
8757 }
8758 }
8759 }
8760 else
8761 fname = name;
8762 return fname;
8763}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764
8765/*
8766 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008767 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008768 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008770 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008771call_func(
8772 char_u *funcname, /* name of the function */
8773 int len, /* length of "name" */
8774 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008775 int argcount_in, /* number of "argvars" */
8776 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008777 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008778 linenr_T firstline, /* first line of range */
8779 linenr_T lastline, /* last line of range */
8780 int *doesrange, /* return: function handled range */
8781 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008782 partial_T *partial, /* optional, can be NULL */
8783 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784{
8785 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 int error = ERROR_NONE;
8787 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008790 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008792 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008793 int argcount = argcount_in;
8794 typval_T *argvars = argvars_in;
8795 dict_T *selfdict = selfdict_in;
8796 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
8797 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008798
8799 /* Make a copy of the name, if it comes from a funcref variable it could
8800 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008801 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008802 if (name == NULL)
8803 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008805 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806
8807 *doesrange = FALSE;
8808
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008809 if (partial != NULL)
8810 {
8811 if (partial->pt_dict != NULL)
8812 {
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008813 /* When the function has a partial with a dict and there is a dict
8814 * argument, use the dict argument. That is backwards compatible.
8815 */
8816 if (selfdict_in == NULL)
8817 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008818 }
8819 if (error == ERROR_NONE && partial->pt_argc > 0)
8820 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008821 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
8822 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
8823 for (i = 0; i < argcount_in; ++i)
8824 argv[i + argv_clear] = argvars_in[i];
8825 argvars = argv;
8826 argcount = partial->pt_argc + argcount_in;
8827 }
8828 }
8829
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830
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 Moolenaar1735bc92016-03-14 23:05:14 +01008976 while (argv_clear > 0)
8977 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008978 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008979 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980
8981 return ret;
8982}
8983
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008984/*
8985 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008986 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008987 */
8988 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008989emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008990{
8991 char_u *p;
8992
8993 if (*name == K_SPECIAL)
8994 p = concat_str((char_u *)"<SNR>", name + 3);
8995 else
8996 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008997 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008998 if (p != name)
8999 vim_free(p);
9000}
9001
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009002/*
9003 * Return TRUE for a non-zero Number and a non-empty String.
9004 */
9005 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009006non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009007{
9008 return ((argvars[0].v_type == VAR_NUMBER
9009 && argvars[0].vval.v_number != 0)
9010 || (argvars[0].v_type == VAR_STRING
9011 && argvars[0].vval.v_string != NULL
9012 && *argvars[0].vval.v_string != NUL));
9013}
9014
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015/*********************************************
9016 * Implementation of the built-in functions
9017 */
9018
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009019#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009020static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009021
9022/*
9023 * Get the float value of "argvars[0]" into "f".
9024 * Returns FAIL when the argument is not a Number or Float.
9025 */
9026 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009027get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009028{
9029 if (argvars[0].v_type == VAR_FLOAT)
9030 {
9031 *f = argvars[0].vval.v_float;
9032 return OK;
9033 }
9034 if (argvars[0].v_type == VAR_NUMBER)
9035 {
9036 *f = (float_T)argvars[0].vval.v_number;
9037 return OK;
9038 }
9039 EMSG(_("E808: Number or Float required"));
9040 return FAIL;
9041}
9042
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009043/*
9044 * "abs(expr)" function
9045 */
9046 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009047f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009048{
9049 if (argvars[0].v_type == VAR_FLOAT)
9050 {
9051 rettv->v_type = VAR_FLOAT;
9052 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9053 }
9054 else
9055 {
9056 varnumber_T n;
9057 int error = FALSE;
9058
9059 n = get_tv_number_chk(&argvars[0], &error);
9060 if (error)
9061 rettv->vval.v_number = -1;
9062 else if (n > 0)
9063 rettv->vval.v_number = n;
9064 else
9065 rettv->vval.v_number = -n;
9066 }
9067}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009068
9069/*
9070 * "acos()" function
9071 */
9072 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009073f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009074{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009075 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009076
9077 rettv->v_type = VAR_FLOAT;
9078 if (get_float_arg(argvars, &f) == OK)
9079 rettv->vval.v_float = acos(f);
9080 else
9081 rettv->vval.v_float = 0.0;
9082}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009083#endif
9084
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009086 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087 */
9088 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009089f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090{
Bram Moolenaar33570922005-01-25 22:26:29 +00009091 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009093 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009094 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009096 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009097 && !tv_check_lock(l->lv_lock,
9098 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009099 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009100 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009101 }
9102 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009103 EMSG(_(e_listreq));
9104}
9105
9106/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009107 * "alloc_fail(id, countdown, repeat)" function
9108 */
9109 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009110f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009111{
9112 if (argvars[0].v_type != VAR_NUMBER
9113 || argvars[0].vval.v_number <= 0
9114 || argvars[1].v_type != VAR_NUMBER
9115 || argvars[1].vval.v_number < 0
9116 || argvars[2].v_type != VAR_NUMBER)
9117 EMSG(_(e_invarg));
9118 else
9119 {
9120 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009121 if (alloc_fail_id >= aid_last)
9122 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009123 alloc_fail_countdown = argvars[1].vval.v_number;
9124 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009125 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009126 }
9127}
9128
9129/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009130 * "and(expr, expr)" function
9131 */
9132 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009133f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009134{
9135 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9136 & get_tv_number_chk(&argvars[1], NULL);
9137}
9138
9139/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009140 * "append(lnum, string/list)" function
9141 */
9142 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009143f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009144{
9145 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009146 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009147 list_T *l = NULL;
9148 listitem_T *li = NULL;
9149 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009150 long added = 0;
9151
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009152 /* When coming here from Insert mode, sync undo, so that this can be
9153 * undone separately from what was previously inserted. */
9154 if (u_sync_once == 2)
9155 {
9156 u_sync_once = 1; /* notify that u_sync() was called */
9157 u_sync(TRUE);
9158 }
9159
Bram Moolenaar0d660222005-01-07 21:51:51 +00009160 lnum = get_tv_lnum(argvars);
9161 if (lnum >= 0
9162 && lnum <= curbuf->b_ml.ml_line_count
9163 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009164 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009165 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009166 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009167 l = argvars[1].vval.v_list;
9168 if (l == NULL)
9169 return;
9170 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009171 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009172 for (;;)
9173 {
9174 if (l == NULL)
9175 tv = &argvars[1]; /* append a string */
9176 else if (li == NULL)
9177 break; /* end of list */
9178 else
9179 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009180 line = get_tv_string_chk(tv);
9181 if (line == NULL) /* type error */
9182 {
9183 rettv->vval.v_number = 1; /* Failed */
9184 break;
9185 }
9186 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009187 ++added;
9188 if (l == NULL)
9189 break;
9190 li = li->li_next;
9191 }
9192
9193 appended_lines_mark(lnum, added);
9194 if (curwin->w_cursor.lnum > lnum)
9195 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009197 else
9198 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199}
9200
9201/*
9202 * "argc()" function
9203 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009205f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009207 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208}
9209
9210/*
9211 * "argidx()" function
9212 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009214f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009216 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217}
9218
9219/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009220 * "arglistid()" function
9221 */
9222 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009223f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009224{
9225 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009226
9227 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009228 wp = find_tabwin(&argvars[0], &argvars[1]);
9229 if (wp != NULL)
9230 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009231}
9232
9233/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234 * "argv(nr)" function
9235 */
9236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009237f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238{
9239 int idx;
9240
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009241 if (argvars[0].v_type != VAR_UNKNOWN)
9242 {
9243 idx = get_tv_number_chk(&argvars[0], NULL);
9244 if (idx >= 0 && idx < ARGCOUNT)
9245 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9246 else
9247 rettv->vval.v_string = NULL;
9248 rettv->v_type = VAR_STRING;
9249 }
9250 else if (rettv_list_alloc(rettv) == OK)
9251 for (idx = 0; idx < ARGCOUNT; ++idx)
9252 list_append_string(rettv->vval.v_list,
9253 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254}
9255
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009256static void prepare_assert_error(garray_T*gap);
9257static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9258static void assert_error(garray_T *gap);
9259static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009260
9261/*
9262 * Prepare "gap" for an assert error and add the sourcing position.
9263 */
9264 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009265prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009266{
9267 char buf[NUMBUFLEN];
9268
9269 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009270 if (sourcing_name != NULL)
9271 {
9272 ga_concat(gap, sourcing_name);
9273 if (sourcing_lnum > 0)
9274 ga_concat(gap, (char_u *)" ");
9275 }
9276 if (sourcing_lnum > 0)
9277 {
9278 sprintf(buf, "line %ld", (long)sourcing_lnum);
9279 ga_concat(gap, (char_u *)buf);
9280 }
9281 if (sourcing_name != NULL || sourcing_lnum > 0)
9282 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009283}
9284
9285/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009286 * Append "str" to "gap", escaping unprintable characters.
9287 * Changes NL to \n, CR to \r, etc.
9288 */
9289 static void
9290ga_concat_esc(garray_T *gap, char_u *str)
9291{
9292 char_u *p;
9293 char_u buf[NUMBUFLEN];
9294
Bram Moolenaarf1551962016-03-15 12:55:58 +01009295 if (str == NULL)
9296 {
9297 ga_concat(gap, (char_u *)"NULL");
9298 return;
9299 }
9300
Bram Moolenaar23689172016-02-15 22:37:37 +01009301 for (p = str; *p != NUL; ++p)
9302 switch (*p)
9303 {
9304 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9305 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9306 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9307 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9308 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9309 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9310 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9311 default:
9312 if (*p < ' ')
9313 {
9314 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9315 ga_concat(gap, buf);
9316 }
9317 else
9318 ga_append(gap, *p);
9319 break;
9320 }
9321}
9322
9323/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009324 * Fill "gap" with information about an assert error.
9325 */
9326 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009327fill_assert_error(
9328 garray_T *gap,
9329 typval_T *opt_msg_tv,
9330 char_u *exp_str,
9331 typval_T *exp_tv,
9332 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009333{
9334 char_u numbuf[NUMBUFLEN];
9335 char_u *tofree;
9336
9337 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9338 {
9339 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9340 vim_free(tofree);
9341 }
9342 else
9343 {
9344 ga_concat(gap, (char_u *)"Expected ");
9345 if (exp_str == NULL)
9346 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009347 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009348 vim_free(tofree);
9349 }
9350 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009351 ga_concat_esc(gap, exp_str);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009352 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009353 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009354 vim_free(tofree);
9355 }
9356}
Bram Moolenaar43345542015-11-29 17:35:35 +01009357
9358/*
9359 * Add an assert error to v:errors.
9360 */
9361 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009362assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009363{
9364 struct vimvar *vp = &vimvars[VV_ERRORS];
9365
9366 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9367 /* Make sure v:errors is a list. */
9368 set_vim_var_list(VV_ERRORS, list_alloc());
9369 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9370}
9371
Bram Moolenaar43345542015-11-29 17:35:35 +01009372/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009373 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009374 */
9375 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009376f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009377{
9378 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009379
9380 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9381 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009382 prepare_assert_error(&ga);
9383 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9384 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009385 ga_clear(&ga);
9386 }
9387}
9388
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009389/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009390 * "assert_exception(string[, msg])" function
9391 */
9392 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009393f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009394{
9395 garray_T ga;
9396 char *error;
9397
9398 error = (char *)get_tv_string_chk(&argvars[0]);
9399 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9400 {
9401 prepare_assert_error(&ga);
9402 ga_concat(&ga, (char_u *)"v:exception is not set");
9403 assert_error(&ga);
9404 ga_clear(&ga);
9405 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009406 else if (error != NULL
9407 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009408 {
9409 prepare_assert_error(&ga);
9410 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9411 &vimvars[VV_EXCEPTION].vv_tv);
9412 assert_error(&ga);
9413 ga_clear(&ga);
9414 }
9415}
9416
9417/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009418 * "assert_fails(cmd [, error])" function
9419 */
9420 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009421f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009422{
9423 char_u *cmd = get_tv_string_chk(&argvars[0]);
9424 garray_T ga;
9425
9426 called_emsg = FALSE;
9427 suppress_errthrow = TRUE;
9428 emsg_silent = TRUE;
9429 do_cmdline_cmd(cmd);
9430 if (!called_emsg)
9431 {
9432 prepare_assert_error(&ga);
9433 ga_concat(&ga, (char_u *)"command did not fail: ");
9434 ga_concat(&ga, cmd);
9435 assert_error(&ga);
9436 ga_clear(&ga);
9437 }
9438 else if (argvars[1].v_type != VAR_UNKNOWN)
9439 {
9440 char_u buf[NUMBUFLEN];
9441 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9442
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009443 if (error == NULL
9444 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009445 {
9446 prepare_assert_error(&ga);
9447 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9448 &vimvars[VV_ERRMSG].vv_tv);
9449 assert_error(&ga);
9450 ga_clear(&ga);
9451 }
9452 }
9453
9454 called_emsg = FALSE;
9455 suppress_errthrow = FALSE;
9456 emsg_silent = FALSE;
9457 emsg_on_display = FALSE;
9458 set_vim_var_string(VV_ERRMSG, NULL, 0);
9459}
9460
9461/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009462 * Common for assert_true() and assert_false().
9463 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009464 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009465assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009466{
9467 int error = FALSE;
9468 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009469
Bram Moolenaar37127922016-02-06 20:29:28 +01009470 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009471 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009472 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009473 if (argvars[0].v_type != VAR_NUMBER
9474 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9475 || error)
9476 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009477 prepare_assert_error(&ga);
9478 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009479 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009480 NULL, &argvars[0]);
9481 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009482 ga_clear(&ga);
9483 }
9484}
9485
9486/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009487 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009488 */
9489 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009490f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009491{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009492 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009493}
9494
9495/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009496 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009497 */
9498 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009499f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009500{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009501 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009502}
9503
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009504#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009505/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009506 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009507 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009508 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009509f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009510{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009511 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009512
9513 rettv->v_type = VAR_FLOAT;
9514 if (get_float_arg(argvars, &f) == OK)
9515 rettv->vval.v_float = asin(f);
9516 else
9517 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009518}
9519
9520/*
9521 * "atan()" function
9522 */
9523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009524f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009525{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009526 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009527
9528 rettv->v_type = VAR_FLOAT;
9529 if (get_float_arg(argvars, &f) == OK)
9530 rettv->vval.v_float = atan(f);
9531 else
9532 rettv->vval.v_float = 0.0;
9533}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009534
9535/*
9536 * "atan2()" function
9537 */
9538 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009539f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009540{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009541 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009542
9543 rettv->v_type = VAR_FLOAT;
9544 if (get_float_arg(argvars, &fx) == OK
9545 && get_float_arg(&argvars[1], &fy) == OK)
9546 rettv->vval.v_float = atan2(fx, fy);
9547 else
9548 rettv->vval.v_float = 0.0;
9549}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009550#endif
9551
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552/*
9553 * "browse(save, title, initdir, default)" function
9554 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009556f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557{
9558#ifdef FEAT_BROWSE
9559 int save;
9560 char_u *title;
9561 char_u *initdir;
9562 char_u *defname;
9563 char_u buf[NUMBUFLEN];
9564 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009565 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009567 save = get_tv_number_chk(&argvars[0], &error);
9568 title = get_tv_string_chk(&argvars[1]);
9569 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9570 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009572 if (error || title == NULL || initdir == NULL || defname == NULL)
9573 rettv->vval.v_string = NULL;
9574 else
9575 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009576 do_browse(save ? BROWSE_SAVE : 0,
9577 title, defname, NULL, initdir, NULL, curbuf);
9578#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009579 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009580#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009581 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009582}
9583
9584/*
9585 * "browsedir(title, initdir)" function
9586 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009587 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009588f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009589{
9590#ifdef FEAT_BROWSE
9591 char_u *title;
9592 char_u *initdir;
9593 char_u buf[NUMBUFLEN];
9594
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009595 title = get_tv_string_chk(&argvars[0]);
9596 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009597
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009598 if (title == NULL || initdir == NULL)
9599 rettv->vval.v_string = NULL;
9600 else
9601 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009602 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009604 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009606 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607}
9608
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009609static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009610
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611/*
9612 * Find a buffer by number or exact name.
9613 */
9614 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009615find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616{
9617 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009619 if (avar->v_type == VAR_NUMBER)
9620 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009621 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009623 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009624 if (buf == NULL)
9625 {
9626 /* No full path name match, try a match with a URL or a "nofile"
9627 * buffer, these don't use the full path. */
9628 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9629 if (buf->b_fname != NULL
9630 && (path_with_url(buf->b_fname)
9631#ifdef FEAT_QUICKFIX
9632 || bt_nofile(buf)
9633#endif
9634 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009635 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009636 break;
9637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638 }
9639 return buf;
9640}
9641
9642/*
9643 * "bufexists(expr)" function
9644 */
9645 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009646f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009648 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649}
9650
9651/*
9652 * "buflisted(expr)" function
9653 */
9654 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009655f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656{
9657 buf_T *buf;
9658
9659 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009660 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661}
9662
9663/*
9664 * "bufloaded(expr)" function
9665 */
9666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009667f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668{
9669 buf_T *buf;
9670
9671 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009672 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673}
9674
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01009675 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +01009676buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678 int save_magic;
9679 char_u *save_cpo;
9680 buf_T *buf;
9681
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9683 save_magic = p_magic;
9684 p_magic = TRUE;
9685 save_cpo = p_cpo;
9686 p_cpo = (char_u *)"";
9687
9688 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009689 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690
9691 p_magic = save_magic;
9692 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +01009693 return buf;
9694}
9695
9696/*
9697 * Get buffer by number or pattern.
9698 */
9699 static buf_T *
9700get_buf_tv(typval_T *tv, int curtab_only)
9701{
9702 char_u *name = tv->vval.v_string;
9703 buf_T *buf;
9704
9705 if (tv->v_type == VAR_NUMBER)
9706 return buflist_findnr((int)tv->vval.v_number);
9707 if (tv->v_type != VAR_STRING)
9708 return NULL;
9709 if (name == NULL || *name == NUL)
9710 return curbuf;
9711 if (name[0] == '$' && name[1] == NUL)
9712 return lastbuf;
9713
9714 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715
9716 /* If not found, try expanding the name, like done for bufexists(). */
9717 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009718 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719
9720 return buf;
9721}
9722
9723/*
9724 * "bufname(expr)" function
9725 */
9726 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009727f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728{
9729 buf_T *buf;
9730
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009731 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009733 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009734 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009736 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009738 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739 --emsg_off;
9740}
9741
9742/*
9743 * "bufnr(expr)" function
9744 */
9745 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009746f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747{
9748 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009749 int error = FALSE;
9750 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009752 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009754 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009755 --emsg_off;
9756
9757 /* If the buffer isn't found and the second argument is not zero create a
9758 * new buffer. */
9759 if (buf == NULL
9760 && argvars[1].v_type != VAR_UNKNOWN
9761 && get_tv_number_chk(&argvars[1], &error) != 0
9762 && !error
9763 && (name = get_tv_string_chk(&argvars[0])) != NULL
9764 && !error)
9765 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9766
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009768 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009770 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771}
9772
9773/*
9774 * "bufwinnr(nr)" function
9775 */
9776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009777f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778{
9779#ifdef FEAT_WINDOWS
9780 win_T *wp;
9781 int winnr = 0;
9782#endif
9783 buf_T *buf;
9784
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009785 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009787 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788#ifdef FEAT_WINDOWS
9789 for (wp = firstwin; wp; wp = wp->w_next)
9790 {
9791 ++winnr;
9792 if (wp->w_buffer == buf)
9793 break;
9794 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009795 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009797 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798#endif
9799 --emsg_off;
9800}
9801
9802/*
9803 * "byte2line(byte)" function
9804 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009806f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807{
9808#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009809 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810#else
9811 long boff = 0;
9812
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009813 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009814 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009815 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009817 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818 (linenr_T)0, &boff);
9819#endif
9820}
9821
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009822 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009823byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009824{
9825#ifdef FEAT_MBYTE
9826 char_u *t;
9827#endif
9828 char_u *str;
9829 long idx;
9830
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009831 str = get_tv_string_chk(&argvars[0]);
9832 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009833 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009834 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009835 return;
9836
9837#ifdef FEAT_MBYTE
9838 t = str;
9839 for ( ; idx > 0; idx--)
9840 {
9841 if (*t == NUL) /* EOL reached */
9842 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009843 if (enc_utf8 && comp)
9844 t += utf_ptr2len(t);
9845 else
9846 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009847 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009848 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009849#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009850 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009851 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009852#endif
9853}
9854
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009855/*
9856 * "byteidx()" function
9857 */
9858 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009859f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009860{
9861 byteidx(argvars, rettv, FALSE);
9862}
9863
9864/*
9865 * "byteidxcomp()" function
9866 */
9867 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009868f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009869{
9870 byteidx(argvars, rettv, TRUE);
9871}
9872
Bram Moolenaardb913952012-06-29 12:54:53 +02009873 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009874func_call(
9875 char_u *name,
9876 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009877 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +01009878 dict_T *selfdict,
9879 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009880{
9881 listitem_T *item;
9882 typval_T argv[MAX_FUNC_ARGS + 1];
9883 int argc = 0;
9884 int dummy;
9885 int r = 0;
9886
9887 for (item = args->vval.v_list->lv_first; item != NULL;
9888 item = item->li_next)
9889 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009890 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +02009891 {
9892 EMSG(_("E699: Too many arguments"));
9893 break;
9894 }
9895 /* Make a copy of each argument. This is needed to be able to set
9896 * v_lock to VAR_FIXED in the copy without changing the original list.
9897 */
9898 copy_tv(&item->li_tv, &argv[argc++]);
9899 }
9900
9901 if (item == NULL)
9902 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9903 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009904 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +02009905
9906 /* Free the arguments. */
9907 while (argc > 0)
9908 clear_tv(&argv[--argc]);
9909
9910 return r;
9911}
9912
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009913/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009914 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009915 */
9916 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009917f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009918{
9919 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009920 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00009921 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009922
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009923 if (argvars[1].v_type != VAR_LIST)
9924 {
9925 EMSG(_(e_listreq));
9926 return;
9927 }
9928 if (argvars[1].vval.v_list == NULL)
9929 return;
9930
9931 if (argvars[0].v_type == VAR_FUNC)
9932 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009933 else if (argvars[0].v_type == VAR_PARTIAL)
9934 {
9935 partial = argvars[0].vval.v_partial;
9936 func = partial->pt_name;
9937 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009938 else
9939 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009940 if (*func == NUL)
9941 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009942
Bram Moolenaare9a41262005-01-15 22:18:47 +00009943 if (argvars[2].v_type != VAR_UNKNOWN)
9944 {
9945 if (argvars[2].v_type != VAR_DICT)
9946 {
9947 EMSG(_(e_dictreq));
9948 return;
9949 }
9950 selfdict = argvars[2].vval.v_dict;
9951 }
9952
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009953 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009954}
9955
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009956#ifdef FEAT_FLOAT
9957/*
9958 * "ceil({float})" function
9959 */
9960 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009961f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009962{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009963 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009964
9965 rettv->v_type = VAR_FLOAT;
9966 if (get_float_arg(argvars, &f) == OK)
9967 rettv->vval.v_float = ceil(f);
9968 else
9969 rettv->vval.v_float = 0.0;
9970}
9971#endif
9972
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01009973#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009974/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009975 * "ch_close()" function
9976 */
9977 static void
9978f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
9979{
Bram Moolenaarf65333c2016-03-08 18:27:21 +01009980 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009981
9982 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +01009983 {
Bram Moolenaar8b374212016-02-24 20:43:06 +01009984 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +01009985 channel_clear(channel);
9986 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009987}
9988
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01009989/*
9990 * "ch_getbufnr()" function
9991 */
9992 static void
9993f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
9994{
Bram Moolenaarf65333c2016-03-08 18:27:21 +01009995 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01009996
9997 rettv->vval.v_number = -1;
9998 if (channel != NULL)
9999 {
10000 char_u *what = get_tv_string(&argvars[1]);
10001 int part;
10002
10003 if (STRCMP(what, "err") == 0)
10004 part = PART_ERR;
10005 else if (STRCMP(what, "out") == 0)
10006 part = PART_OUT;
10007 else if (STRCMP(what, "in") == 0)
10008 part = PART_IN;
10009 else
10010 part = PART_SOCK;
10011 if (channel->ch_part[part].ch_buffer != NULL)
10012 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10013 }
10014}
10015
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010016/*
10017 * "ch_getjob()" function
10018 */
10019 static void
10020f_ch_getjob(typval_T *argvars, typval_T *rettv)
10021{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010022 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010023
10024 if (channel != NULL)
10025 {
10026 rettv->v_type = VAR_JOB;
10027 rettv->vval.v_job = channel->ch_job;
10028 if (channel->ch_job != NULL)
10029 ++channel->ch_job->jv_refcount;
10030 }
10031}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010032
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010033/*
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010034 * "ch_info()" function
10035 */
10036 static void
10037f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
10038{
10039 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
10040
10041 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
10042 channel_info(channel, rettv->vval.v_dict);
10043}
10044
10045/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010046 * "ch_log()" function
10047 */
10048 static void
10049f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10050{
10051 char_u *msg = get_tv_string(&argvars[0]);
10052 channel_T *channel = NULL;
10053
10054 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010055 channel = get_channel_arg(&argvars[1], TRUE);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010056
10057 ch_log(channel, (char *)msg);
10058}
10059
10060/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010061 * "ch_logfile()" function
10062 */
10063 static void
10064f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10065{
10066 char_u *fname;
10067 char_u *opt = (char_u *)"";
10068 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010069
10070 fname = get_tv_string(&argvars[0]);
10071 if (argvars[1].v_type == VAR_STRING)
10072 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010073 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010074}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010075
10076/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010077 * "ch_open()" function
10078 */
10079 static void
10080f_ch_open(typval_T *argvars, typval_T *rettv)
10081{
Bram Moolenaar77073442016-02-13 23:23:53 +010010082 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010083 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010084}
10085
10086/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010087 * "ch_read()" function
10088 */
10089 static void
10090f_ch_read(typval_T *argvars, typval_T *rettv)
10091{
10092 common_channel_read(argvars, rettv, FALSE);
10093}
10094
10095/*
10096 * "ch_readraw()" function
10097 */
10098 static void
10099f_ch_readraw(typval_T *argvars, typval_T *rettv)
10100{
10101 common_channel_read(argvars, rettv, TRUE);
10102}
10103
10104/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010105 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010106 */
10107 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010108f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10109{
10110 ch_expr_common(argvars, rettv, TRUE);
10111}
10112
10113/*
10114 * "ch_sendexpr()" function
10115 */
10116 static void
10117f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10118{
10119 ch_expr_common(argvars, rettv, FALSE);
10120}
10121
10122/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010123 * "ch_evalraw()" function
10124 */
10125 static void
10126f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10127{
10128 ch_raw_common(argvars, rettv, TRUE);
10129}
10130
10131/*
10132 * "ch_sendraw()" function
10133 */
10134 static void
10135f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10136{
10137 ch_raw_common(argvars, rettv, FALSE);
10138}
10139
10140/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010141 * "ch_setoptions()" function
10142 */
10143 static void
10144f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10145{
10146 channel_T *channel;
10147 jobopt_T opt;
10148
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010149 channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010150 if (channel == NULL)
10151 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010152 clear_job_options(&opt);
10153 if (get_job_options(&argvars[1], &opt,
10154 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == FAIL)
Bram Moolenaar132006c2016-02-19 22:38:15 +010010155 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010156 channel_set_options(channel, &opt);
10157}
10158
10159/*
10160 * "ch_status()" function
10161 */
10162 static void
10163f_ch_status(typval_T *argvars, typval_T *rettv)
10164{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010165 channel_T *channel;
10166
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010167 /* return an empty string by default */
10168 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010169 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010170
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010171 channel = get_channel_arg(&argvars[0], FALSE);
10172 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010173}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010174#endif
10175
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010176/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010177 * "changenr()" function
10178 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010179 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010180f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010181{
10182 rettv->vval.v_number = curbuf->b_u_seq_cur;
10183}
10184
10185/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186 * "char2nr(string)" function
10187 */
10188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010189f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190{
10191#ifdef FEAT_MBYTE
10192 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010193 {
10194 int utf8 = 0;
10195
10196 if (argvars[1].v_type != VAR_UNKNOWN)
10197 utf8 = get_tv_number_chk(&argvars[1], NULL);
10198
10199 if (utf8)
10200 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10201 else
10202 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010204 else
10205#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010206 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010207}
10208
10209/*
10210 * "cindent(lnum)" function
10211 */
10212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010213f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214{
10215#ifdef FEAT_CINDENT
10216 pos_T pos;
10217 linenr_T lnum;
10218
10219 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010220 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10222 {
10223 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010224 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225 curwin->w_cursor = pos;
10226 }
10227 else
10228#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010229 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230}
10231
10232/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010233 * "clearmatches()" function
10234 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010236f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010237{
10238#ifdef FEAT_SEARCH_EXTRA
10239 clear_matches(curwin);
10240#endif
10241}
10242
10243/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244 * "col(string)" function
10245 */
10246 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010247f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010248{
10249 colnr_T col = 0;
10250 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010251 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010253 fp = var2fpos(&argvars[0], FALSE, &fnum);
10254 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010255 {
10256 if (fp->col == MAXCOL)
10257 {
10258 /* '> can be MAXCOL, get the length of the line then */
10259 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010260 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010261 else
10262 col = MAXCOL;
10263 }
10264 else
10265 {
10266 col = fp->col + 1;
10267#ifdef FEAT_VIRTUALEDIT
10268 /* col(".") when the cursor is on the NUL at the end of the line
10269 * because of "coladd" can be seen as an extra column. */
10270 if (virtual_active() && fp == &curwin->w_cursor)
10271 {
10272 char_u *p = ml_get_cursor();
10273
10274 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10275 curwin->w_virtcol - curwin->w_cursor.coladd))
10276 {
10277# ifdef FEAT_MBYTE
10278 int l;
10279
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010280 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281 col += l;
10282# else
10283 if (*p != NUL && p[1] == NUL)
10284 ++col;
10285# endif
10286 }
10287 }
10288#endif
10289 }
10290 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010291 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292}
10293
Bram Moolenaar572cb562005-08-05 21:35:02 +000010294#if defined(FEAT_INS_EXPAND)
10295/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010296 * "complete()" function
10297 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010298 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010299f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010300{
10301 int startcol;
10302
10303 if ((State & INSERT) == 0)
10304 {
10305 EMSG(_("E785: complete() can only be used in Insert mode"));
10306 return;
10307 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010308
10309 /* Check for undo allowed here, because if something was already inserted
10310 * the line was already saved for undo and this check isn't done. */
10311 if (!undo_allowed())
10312 return;
10313
Bram Moolenaarade00832006-03-10 21:46:58 +000010314 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10315 {
10316 EMSG(_(e_invarg));
10317 return;
10318 }
10319
10320 startcol = get_tv_number_chk(&argvars[0], NULL);
10321 if (startcol <= 0)
10322 return;
10323
10324 set_completion(startcol - 1, argvars[1].vval.v_list);
10325}
10326
10327/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010328 * "complete_add()" function
10329 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010330 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010331f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010332{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010333 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010334}
10335
10336/*
10337 * "complete_check()" function
10338 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010339 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010340f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010341{
10342 int saved = RedrawingDisabled;
10343
10344 RedrawingDisabled = 0;
10345 ins_compl_check_keys(0);
10346 rettv->vval.v_number = compl_interrupted;
10347 RedrawingDisabled = saved;
10348}
10349#endif
10350
Bram Moolenaar071d4272004-06-13 20:20:40 +000010351/*
10352 * "confirm(message, buttons[, default [, type]])" function
10353 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010355f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356{
10357#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10358 char_u *message;
10359 char_u *buttons = NULL;
10360 char_u buf[NUMBUFLEN];
10361 char_u buf2[NUMBUFLEN];
10362 int def = 1;
10363 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010364 char_u *typestr;
10365 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010366
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010367 message = get_tv_string_chk(&argvars[0]);
10368 if (message == NULL)
10369 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010370 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010372 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10373 if (buttons == NULL)
10374 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010375 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010376 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010377 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010378 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010380 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10381 if (typestr == NULL)
10382 error = TRUE;
10383 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010385 switch (TOUPPER_ASC(*typestr))
10386 {
10387 case 'E': type = VIM_ERROR; break;
10388 case 'Q': type = VIM_QUESTION; break;
10389 case 'I': type = VIM_INFO; break;
10390 case 'W': type = VIM_WARNING; break;
10391 case 'G': type = VIM_GENERIC; break;
10392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393 }
10394 }
10395 }
10396 }
10397
10398 if (buttons == NULL || *buttons == NUL)
10399 buttons = (char_u *)_("&Ok");
10400
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010401 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010402 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010403 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404#endif
10405}
10406
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010407/*
10408 * "copy()" function
10409 */
10410 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010411f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010412{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010413 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010414}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010415
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010416#ifdef FEAT_FLOAT
10417/*
10418 * "cos()" function
10419 */
10420 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010421f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010422{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010423 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010424
10425 rettv->v_type = VAR_FLOAT;
10426 if (get_float_arg(argvars, &f) == OK)
10427 rettv->vval.v_float = cos(f);
10428 else
10429 rettv->vval.v_float = 0.0;
10430}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010431
10432/*
10433 * "cosh()" function
10434 */
10435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010436f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010437{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010438 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010439
10440 rettv->v_type = VAR_FLOAT;
10441 if (get_float_arg(argvars, &f) == OK)
10442 rettv->vval.v_float = cosh(f);
10443 else
10444 rettv->vval.v_float = 0.0;
10445}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010446#endif
10447
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010449 * "count()" function
10450 */
10451 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010452f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010453{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010454 long n = 0;
10455 int ic = FALSE;
10456
Bram Moolenaare9a41262005-01-15 22:18:47 +000010457 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010458 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010459 listitem_T *li;
10460 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010461 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010462
Bram Moolenaare9a41262005-01-15 22:18:47 +000010463 if ((l = argvars[0].vval.v_list) != NULL)
10464 {
10465 li = l->lv_first;
10466 if (argvars[2].v_type != VAR_UNKNOWN)
10467 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010468 int error = FALSE;
10469
10470 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010471 if (argvars[3].v_type != VAR_UNKNOWN)
10472 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010473 idx = get_tv_number_chk(&argvars[3], &error);
10474 if (!error)
10475 {
10476 li = list_find(l, idx);
10477 if (li == NULL)
10478 EMSGN(_(e_listidx), idx);
10479 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010480 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010481 if (error)
10482 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010483 }
10484
10485 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010486 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010487 ++n;
10488 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010489 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010490 else if (argvars[0].v_type == VAR_DICT)
10491 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010492 int todo;
10493 dict_T *d;
10494 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010495
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010496 if ((d = argvars[0].vval.v_dict) != NULL)
10497 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010498 int error = FALSE;
10499
Bram Moolenaare9a41262005-01-15 22:18:47 +000010500 if (argvars[2].v_type != VAR_UNKNOWN)
10501 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010502 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010503 if (argvars[3].v_type != VAR_UNKNOWN)
10504 EMSG(_(e_invarg));
10505 }
10506
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010507 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010508 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010509 {
10510 if (!HASHITEM_EMPTY(hi))
10511 {
10512 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010513 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010514 ++n;
10515 }
10516 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010517 }
10518 }
10519 else
10520 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010521 rettv->vval.v_number = n;
10522}
10523
10524/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010525 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10526 *
10527 * Checks the existence of a cscope connection.
10528 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010530f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531{
10532#ifdef FEAT_CSCOPE
10533 int num = 0;
10534 char_u *dbpath = NULL;
10535 char_u *prepend = NULL;
10536 char_u buf[NUMBUFLEN];
10537
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010538 if (argvars[0].v_type != VAR_UNKNOWN
10539 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010540 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010541 num = (int)get_tv_number(&argvars[0]);
10542 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010543 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010544 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010545 }
10546
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010547 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548#endif
10549}
10550
10551/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010552 * "cursor(lnum, col)" function, or
10553 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010554 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010555 * Moves the cursor to the specified line and column.
10556 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010559f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010560{
10561 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010562#ifdef FEAT_VIRTUALEDIT
10563 long coladd = 0;
10564#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010565 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010566
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010567 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010568 if (argvars[1].v_type == VAR_UNKNOWN)
10569 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010570 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010571 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010572
Bram Moolenaar493c1782014-05-28 14:34:46 +020010573 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010574 {
10575 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010576 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010577 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010578 line = pos.lnum;
10579 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010580#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010581 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010582#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010583 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010584 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010585 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010586 set_curswant = FALSE;
10587 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010588 }
10589 else
10590 {
10591 line = get_tv_lnum(argvars);
10592 col = get_tv_number_chk(&argvars[1], NULL);
10593#ifdef FEAT_VIRTUALEDIT
10594 if (argvars[2].v_type != VAR_UNKNOWN)
10595 coladd = get_tv_number_chk(&argvars[2], NULL);
10596#endif
10597 }
10598 if (line < 0 || col < 0
10599#ifdef FEAT_VIRTUALEDIT
10600 || coladd < 0
10601#endif
10602 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010603 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010604 if (line > 0)
10605 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010606 if (col > 0)
10607 curwin->w_cursor.col = col - 1;
10608#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010609 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010610#endif
10611
10612 /* Make sure the cursor is in a valid position. */
10613 check_cursor();
10614#ifdef FEAT_MBYTE
10615 /* Correct cursor for multi-byte character. */
10616 if (has_mbyte)
10617 mb_adjust_cursor();
10618#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010619
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010620 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010621 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622}
10623
10624/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010625 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010626 */
10627 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010628f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010629{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010630 int noref = 0;
10631
10632 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010633 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010634 if (noref < 0 || noref > 1)
10635 EMSG(_(e_invarg));
10636 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010637 {
10638 current_copyID += COPYID_INC;
10639 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010641}
10642
10643/*
10644 * "delete()" function
10645 */
10646 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010647f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010648{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010649 char_u nbuf[NUMBUFLEN];
10650 char_u *name;
10651 char_u *flags;
10652
10653 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010654 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010655 return;
10656
10657 name = get_tv_string(&argvars[0]);
10658 if (name == NULL || *name == NUL)
10659 {
10660 EMSG(_(e_invarg));
10661 return;
10662 }
10663
10664 if (argvars[1].v_type != VAR_UNKNOWN)
10665 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010667 flags = (char_u *)"";
10668
10669 if (*flags == NUL)
10670 /* delete a file */
10671 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10672 else if (STRCMP(flags, "d") == 0)
10673 /* delete an empty directory */
10674 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10675 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010676 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010677 rettv->vval.v_number = delete_recursive(name);
10678 else
10679 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010680}
10681
10682/*
10683 * "did_filetype()" function
10684 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010685 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010686f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687{
10688#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010689 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010690#endif
10691}
10692
10693/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010694 * "diff_filler()" function
10695 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010696 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010697f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010698{
10699#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010700 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010701#endif
10702}
10703
10704/*
10705 * "diff_hlID()" function
10706 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010707 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010708f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010709{
10710#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010711 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010712 static linenr_T prev_lnum = 0;
10713 static int changedtick = 0;
10714 static int fnum = 0;
10715 static int change_start = 0;
10716 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010717 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010718 int filler_lines;
10719 int col;
10720
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010721 if (lnum < 0) /* ignore type error in {lnum} arg */
10722 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010723 if (lnum != prev_lnum
10724 || changedtick != curbuf->b_changedtick
10725 || fnum != curbuf->b_fnum)
10726 {
10727 /* New line, buffer, change: need to get the values. */
10728 filler_lines = diff_check(curwin, lnum);
10729 if (filler_lines < 0)
10730 {
10731 if (filler_lines == -1)
10732 {
10733 change_start = MAXCOL;
10734 change_end = -1;
10735 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10736 hlID = HLF_ADD; /* added line */
10737 else
10738 hlID = HLF_CHD; /* changed line */
10739 }
10740 else
10741 hlID = HLF_ADD; /* added line */
10742 }
10743 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010744 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010745 prev_lnum = lnum;
10746 changedtick = curbuf->b_changedtick;
10747 fnum = curbuf->b_fnum;
10748 }
10749
10750 if (hlID == HLF_CHD || hlID == HLF_TXD)
10751 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010752 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010753 if (col >= change_start && col <= change_end)
10754 hlID = HLF_TXD; /* changed text */
10755 else
10756 hlID = HLF_CHD; /* changed line */
10757 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010758 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010759#endif
10760}
10761
10762/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010010763 * "disable_char_avail_for_testing({expr})" function
10764 */
10765 static void
10766f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
10767{
10768 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
10769}
10770
10771/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010772 * "empty({expr})" function
10773 */
10774 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010775f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010776{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010010777 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010778
10779 switch (argvars[0].v_type)
10780 {
10781 case VAR_STRING:
10782 case VAR_FUNC:
10783 n = argvars[0].vval.v_string == NULL
10784 || *argvars[0].vval.v_string == NUL;
10785 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010786 case VAR_PARTIAL:
10787 n = FALSE;
10788 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010789 case VAR_NUMBER:
10790 n = argvars[0].vval.v_number == 0;
10791 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010792 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010010793#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010794 n = argvars[0].vval.v_float == 0.0;
10795 break;
10796#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010797 case VAR_LIST:
10798 n = argvars[0].vval.v_list == NULL
10799 || argvars[0].vval.v_list->lv_first == NULL;
10800 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010801 case VAR_DICT:
10802 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010803 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010804 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010805 case VAR_SPECIAL:
10806 n = argvars[0].vval.v_number != VVAL_TRUE;
10807 break;
10808
Bram Moolenaar835dc632016-02-07 14:27:38 +010010809 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010810#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010811 n = argvars[0].vval.v_job == NULL
10812 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
10813 break;
10814#endif
10815 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010816#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010817 n = argvars[0].vval.v_channel == NULL
10818 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010010819 break;
10820#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010010821 case VAR_UNKNOWN:
10822 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
10823 n = TRUE;
10824 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010825 }
10826
10827 rettv->vval.v_number = n;
10828}
10829
10830/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010831 * "escape({string}, {chars})" function
10832 */
10833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010834f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010835{
10836 char_u buf[NUMBUFLEN];
10837
Bram Moolenaar758711c2005-02-02 23:11:38 +000010838 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10839 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010840 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841}
10842
10843/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010844 * "eval()" function
10845 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010847f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010848{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010849 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010850
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010851 s = get_tv_string_chk(&argvars[0]);
10852 if (s != NULL)
10853 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010854
Bram Moolenaar615b9972015-01-14 17:15:05 +010010855 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010856 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10857 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010858 if (p != NULL && !aborting())
10859 EMSG2(_(e_invexpr2), p);
10860 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010861 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010862 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010863 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010864 else if (*s != NUL)
10865 EMSG(_(e_trailing));
10866}
10867
10868/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010869 * "eventhandler()" function
10870 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010872f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010874 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875}
10876
10877/*
10878 * "executable()" function
10879 */
10880 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010881f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010882{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010883 char_u *name = get_tv_string(&argvars[0]);
10884
10885 /* Check in $PATH and also check directly if there is a directory name. */
10886 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10887 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010888}
10889
10890/*
10891 * "exepath()" function
10892 */
10893 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010894f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010895{
10896 char_u *p = NULL;
10897
Bram Moolenaarb5971142015-03-21 17:32:19 +010010898 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010899 rettv->v_type = VAR_STRING;
10900 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010901}
10902
10903/*
10904 * "exists()" function
10905 */
10906 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010907f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010908{
10909 char_u *p;
10910 char_u *name;
10911 int n = FALSE;
10912 int len = 0;
10913
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010914 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010915 if (*p == '$') /* environment variable */
10916 {
10917 /* first try "normal" environment variables (fast) */
10918 if (mch_getenv(p + 1) != NULL)
10919 n = TRUE;
10920 else
10921 {
10922 /* try expanding things like $VIM and ${HOME} */
10923 p = expand_env_save(p);
10924 if (p != NULL && *p != '$')
10925 n = TRUE;
10926 vim_free(p);
10927 }
10928 }
10929 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010930 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010931 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010932 if (*skipwhite(p) != NUL)
10933 n = FALSE; /* trailing garbage */
10934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010935 else if (*p == '*') /* internal or user defined function */
10936 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010937 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010938 }
10939 else if (*p == ':')
10940 {
10941 n = cmd_exists(p + 1);
10942 }
10943 else if (*p == '#')
10944 {
10945#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010946 if (p[1] == '#')
10947 n = autocmd_supported(p + 2);
10948 else
10949 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950#endif
10951 }
10952 else /* internal variable */
10953 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010954 char_u *tofree;
10955 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010957 /* get_name_len() takes care of expanding curly braces */
10958 name = p;
10959 len = get_name_len(&p, &tofree, TRUE, FALSE);
10960 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010962 if (tofree != NULL)
10963 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010964 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010965 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010967 /* handle d.key, l[idx], f(expr) */
10968 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10969 if (n)
10970 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010971 }
10972 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010973 if (*p != NUL)
10974 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010976 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977 }
10978
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010979 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980}
10981
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010982#ifdef FEAT_FLOAT
10983/*
10984 * "exp()" function
10985 */
10986 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010987f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010988{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010989 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010990
10991 rettv->v_type = VAR_FLOAT;
10992 if (get_float_arg(argvars, &f) == OK)
10993 rettv->vval.v_float = exp(f);
10994 else
10995 rettv->vval.v_float = 0.0;
10996}
10997#endif
10998
Bram Moolenaar071d4272004-06-13 20:20:40 +000010999/*
11000 * "expand()" function
11001 */
11002 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011003f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011004{
11005 char_u *s;
11006 int len;
11007 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011008 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011009 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011010 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011011 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011012
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011013 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011014 if (argvars[1].v_type != VAR_UNKNOWN
11015 && argvars[2].v_type != VAR_UNKNOWN
11016 && get_tv_number_chk(&argvars[2], &error)
11017 && !error)
11018 {
11019 rettv->v_type = VAR_LIST;
11020 rettv->vval.v_list = NULL;
11021 }
11022
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011023 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011024 if (*s == '%' || *s == '#' || *s == '<')
11025 {
11026 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011027 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011028 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011029 if (rettv->v_type == VAR_LIST)
11030 {
11031 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11032 list_append_string(rettv->vval.v_list, result, -1);
11033 else
11034 vim_free(result);
11035 }
11036 else
11037 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011038 }
11039 else
11040 {
11041 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011042 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011043 if (argvars[1].v_type != VAR_UNKNOWN
11044 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011045 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011046 if (!error)
11047 {
11048 ExpandInit(&xpc);
11049 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011050 if (p_wic)
11051 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011052 if (rettv->v_type == VAR_STRING)
11053 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11054 options, WILD_ALL);
11055 else if (rettv_list_alloc(rettv) != FAIL)
11056 {
11057 int i;
11058
11059 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11060 for (i = 0; i < xpc.xp_numfiles; i++)
11061 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11062 ExpandCleanup(&xpc);
11063 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011064 }
11065 else
11066 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011067 }
11068}
11069
11070/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011071 * Go over all entries in "d2" and add them to "d1".
11072 * When "action" is "error" then a duplicate key is an error.
11073 * When "action" is "force" then a duplicate key is overwritten.
11074 * Otherwise duplicate keys are ignored ("action" is "keep").
11075 */
11076 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011077dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011078{
11079 dictitem_T *di1;
11080 hashitem_T *hi2;
11081 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011082 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011083
11084 todo = (int)d2->dv_hashtab.ht_used;
11085 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11086 {
11087 if (!HASHITEM_EMPTY(hi2))
11088 {
11089 --todo;
11090 di1 = dict_find(d1, hi2->hi_key, -1);
11091 if (d1->dv_scope != 0)
11092 {
11093 /* Disallow replacing a builtin function in l: and g:.
11094 * Check the key to be valid when adding to any
11095 * scope. */
11096 if (d1->dv_scope == VAR_DEF_SCOPE
11097 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11098 && var_check_func_name(hi2->hi_key,
11099 di1 == NULL))
11100 break;
11101 if (!valid_varname(hi2->hi_key))
11102 break;
11103 }
11104 if (di1 == NULL)
11105 {
11106 di1 = dictitem_copy(HI2DI(hi2));
11107 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11108 dictitem_free(di1);
11109 }
11110 else if (*action == 'e')
11111 {
11112 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11113 break;
11114 }
11115 else if (*action == 'f' && HI2DI(hi2) != di1)
11116 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011117 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11118 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011119 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011120 clear_tv(&di1->di_tv);
11121 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11122 }
11123 }
11124 }
11125}
11126
11127/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011128 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011129 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011130 */
11131 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011132f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011133{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011134 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011135
Bram Moolenaare9a41262005-01-15 22:18:47 +000011136 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011137 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011138 list_T *l1, *l2;
11139 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011140 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011141 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011142
Bram Moolenaare9a41262005-01-15 22:18:47 +000011143 l1 = argvars[0].vval.v_list;
11144 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011145 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011146 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011147 {
11148 if (argvars[2].v_type != VAR_UNKNOWN)
11149 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011150 before = get_tv_number_chk(&argvars[2], &error);
11151 if (error)
11152 return; /* type error; errmsg already given */
11153
Bram Moolenaar758711c2005-02-02 23:11:38 +000011154 if (before == l1->lv_len)
11155 item = NULL;
11156 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011157 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011158 item = list_find(l1, before);
11159 if (item == NULL)
11160 {
11161 EMSGN(_(e_listidx), before);
11162 return;
11163 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011164 }
11165 }
11166 else
11167 item = NULL;
11168 list_extend(l1, l2, item);
11169
Bram Moolenaare9a41262005-01-15 22:18:47 +000011170 copy_tv(&argvars[0], rettv);
11171 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011172 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011173 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11174 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011175 dict_T *d1, *d2;
11176 char_u *action;
11177 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011178
11179 d1 = argvars[0].vval.v_dict;
11180 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011181 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011182 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011183 {
11184 /* Check the third argument. */
11185 if (argvars[2].v_type != VAR_UNKNOWN)
11186 {
11187 static char *(av[]) = {"keep", "force", "error"};
11188
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011189 action = get_tv_string_chk(&argvars[2]);
11190 if (action == NULL)
11191 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011192 for (i = 0; i < 3; ++i)
11193 if (STRCMP(action, av[i]) == 0)
11194 break;
11195 if (i == 3)
11196 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011197 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011198 return;
11199 }
11200 }
11201 else
11202 action = (char_u *)"force";
11203
Bram Moolenaara9922d62013-05-30 13:01:18 +020011204 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011205
Bram Moolenaare9a41262005-01-15 22:18:47 +000011206 copy_tv(&argvars[0], rettv);
11207 }
11208 }
11209 else
11210 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011211}
11212
11213/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011214 * "feedkeys()" function
11215 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011217f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011218{
11219 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011220 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011221 char_u *keys, *flags;
11222 char_u nbuf[NUMBUFLEN];
11223 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011224 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011225 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011226
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011227 /* This is not allowed in the sandbox. If the commands would still be
11228 * executed in the sandbox it would be OK, but it probably happens later,
11229 * when "sandbox" is no longer set. */
11230 if (check_secure())
11231 return;
11232
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011233 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011234
11235 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011236 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011237 flags = get_tv_string_buf(&argvars[1], nbuf);
11238 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011239 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011240 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011241 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011242 case 'n': remap = FALSE; break;
11243 case 'm': remap = TRUE; break;
11244 case 't': typed = TRUE; break;
11245 case 'i': insert = TRUE; break;
11246 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011247 }
11248 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011249 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011250
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011251 if (*keys != NUL || execute)
11252 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011253 /* Need to escape K_SPECIAL and CSI before putting the string in the
11254 * typeahead buffer. */
11255 keys_esc = vim_strsave_escape_csi(keys);
11256 if (keys_esc != NULL)
11257 {
11258 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011259 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011260 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011261 if (vgetc_busy)
11262 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011263 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011264 {
11265 int save_msg_scroll = msg_scroll;
11266
11267 /* Avoid a 1 second delay when the keys start Insert mode. */
11268 msg_scroll = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011269 exec_normal(TRUE);
Bram Moolenaar9e496852016-03-11 19:31:47 +010011270 msg_scroll |= save_msg_scroll;
11271 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011272 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011273 }
11274}
11275
11276/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011277 * "filereadable()" function
11278 */
11279 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011280f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011281{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011282 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011283 char_u *p;
11284 int n;
11285
Bram Moolenaarc236c162008-07-13 17:41:49 +000011286#ifndef O_NONBLOCK
11287# define O_NONBLOCK 0
11288#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011289 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011290 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11291 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011292 {
11293 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011294 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011295 }
11296 else
11297 n = FALSE;
11298
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011299 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011300}
11301
11302/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011303 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011304 * rights to write into.
11305 */
11306 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011307f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011308{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011309 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011310}
11311
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011312 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011313findfilendir(
11314 typval_T *argvars UNUSED,
11315 typval_T *rettv,
11316 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011317{
11318#ifdef FEAT_SEARCHPATH
11319 char_u *fname;
11320 char_u *fresult = NULL;
11321 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11322 char_u *p;
11323 char_u pathbuf[NUMBUFLEN];
11324 int count = 1;
11325 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011326 int error = FALSE;
11327#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011328
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011329 rettv->vval.v_string = NULL;
11330 rettv->v_type = VAR_STRING;
11331
11332#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011333 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011334
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011335 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011336 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011337 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11338 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011339 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011340 else
11341 {
11342 if (*p != NUL)
11343 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011344
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011345 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011346 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011347 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011348 }
11349
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011350 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11351 error = TRUE;
11352
11353 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011354 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011355 do
11356 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011357 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011358 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011359 fresult = find_file_in_path_option(first ? fname : NULL,
11360 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011361 0, first, path,
11362 find_what,
11363 curbuf->b_ffname,
11364 find_what == FINDFILE_DIR
11365 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011366 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011367
11368 if (fresult != NULL && rettv->v_type == VAR_LIST)
11369 list_append_string(rettv->vval.v_list, fresult, -1);
11370
11371 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011372 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011373
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011374 if (rettv->v_type == VAR_STRING)
11375 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011376#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011377}
11378
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011379static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11380static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011381
11382/*
11383 * Implementation of map() and filter().
11384 */
11385 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011386filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011387{
11388 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011389 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011390 listitem_T *li, *nli;
11391 list_T *l = NULL;
11392 dictitem_T *di;
11393 hashtab_T *ht;
11394 hashitem_T *hi;
11395 dict_T *d = NULL;
11396 typval_T save_val;
11397 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011398 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011399 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011400 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011401 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011402 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011403 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011404 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011405
Bram Moolenaare9a41262005-01-15 22:18:47 +000011406 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011407 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011408 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011409 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011410 return;
11411 }
11412 else if (argvars[0].v_type == VAR_DICT)
11413 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011414 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011415 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011416 return;
11417 }
11418 else
11419 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011420 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011421 return;
11422 }
11423
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011424 expr = get_tv_string_buf_chk(&argvars[1], buf);
11425 /* On type errors, the preceding call has already displayed an error
11426 * message. Avoid a misleading error message for an empty string that
11427 * was not passed as argument. */
11428 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011429 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011430 prepare_vimvar(VV_VAL, &save_val);
11431 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011432
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011433 /* We reset "did_emsg" to be able to detect whether an error
11434 * occurred during evaluation of the expression. */
11435 save_did_emsg = did_emsg;
11436 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011437
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011438 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011439 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011440 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011441 vimvars[VV_KEY].vv_type = VAR_STRING;
11442
11443 ht = &d->dv_hashtab;
11444 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011445 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011446 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011447 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011448 if (!HASHITEM_EMPTY(hi))
11449 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011450 int r;
11451
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011452 --todo;
11453 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011454 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011455 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11456 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011457 break;
11458 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011459 r = filter_map_one(&di->di_tv, expr, map, &rem);
11460 clear_tv(&vimvars[VV_KEY].vv_tv);
11461 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011462 break;
11463 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011464 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011465 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11466 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011467 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011468 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011469 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011470 }
11471 }
11472 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011473 }
11474 else
11475 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011476 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11477
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011478 for (li = l->lv_first; li != NULL; li = nli)
11479 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011480 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011481 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011482 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011483 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011484 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011485 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011486 break;
11487 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011488 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011489 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011490 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011491 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011492
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011493 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011494 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011495
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011496 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011497 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011498
11499 copy_tv(&argvars[0], rettv);
11500}
11501
11502 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011503filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011504{
Bram Moolenaar33570922005-01-25 22:26:29 +000011505 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011506 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011507 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011508
Bram Moolenaar33570922005-01-25 22:26:29 +000011509 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011510 s = expr;
11511 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011512 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011513 if (*s != NUL) /* check for trailing chars after expr */
11514 {
11515 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011516 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011517 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011518 }
11519 if (map)
11520 {
11521 /* map(): replace the list item value */
11522 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011523 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011524 *tv = rettv;
11525 }
11526 else
11527 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011528 int error = FALSE;
11529
Bram Moolenaare9a41262005-01-15 22:18:47 +000011530 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011531 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011532 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011533 /* On type error, nothing has been removed; return FAIL to stop the
11534 * loop. The error message was given by get_tv_number_chk(). */
11535 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011536 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011537 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011538 retval = OK;
11539theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011540 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011541 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011542}
11543
11544/*
11545 * "filter()" function
11546 */
11547 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011548f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011549{
11550 filter_map(argvars, rettv, FALSE);
11551}
11552
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011553/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011554 * "finddir({fname}[, {path}[, {count}]])" function
11555 */
11556 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011557f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011558{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011559 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011560}
11561
11562/*
11563 * "findfile({fname}[, {path}[, {count}]])" function
11564 */
11565 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011566f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011567{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011568 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011569}
11570
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011571#ifdef FEAT_FLOAT
11572/*
11573 * "float2nr({float})" function
11574 */
11575 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011576f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011577{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011578 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011579
11580 if (get_float_arg(argvars, &f) == OK)
11581 {
11582 if (f < -0x7fffffff)
11583 rettv->vval.v_number = -0x7fffffff;
11584 else if (f > 0x7fffffff)
11585 rettv->vval.v_number = 0x7fffffff;
11586 else
11587 rettv->vval.v_number = (varnumber_T)f;
11588 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011589}
11590
11591/*
11592 * "floor({float})" function
11593 */
11594 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011595f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011596{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011597 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011598
11599 rettv->v_type = VAR_FLOAT;
11600 if (get_float_arg(argvars, &f) == OK)
11601 rettv->vval.v_float = floor(f);
11602 else
11603 rettv->vval.v_float = 0.0;
11604}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011605
11606/*
11607 * "fmod()" function
11608 */
11609 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011610f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011611{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011612 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011613
11614 rettv->v_type = VAR_FLOAT;
11615 if (get_float_arg(argvars, &fx) == OK
11616 && get_float_arg(&argvars[1], &fy) == OK)
11617 rettv->vval.v_float = fmod(fx, fy);
11618 else
11619 rettv->vval.v_float = 0.0;
11620}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011621#endif
11622
Bram Moolenaar0d660222005-01-07 21:51:51 +000011623/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011624 * "fnameescape({string})" function
11625 */
11626 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011627f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011628{
11629 rettv->vval.v_string = vim_strsave_fnameescape(
11630 get_tv_string(&argvars[0]), FALSE);
11631 rettv->v_type = VAR_STRING;
11632}
11633
11634/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011635 * "fnamemodify({fname}, {mods})" function
11636 */
11637 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011638f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011639{
11640 char_u *fname;
11641 char_u *mods;
11642 int usedlen = 0;
11643 int len;
11644 char_u *fbuf = NULL;
11645 char_u buf[NUMBUFLEN];
11646
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011647 fname = get_tv_string_chk(&argvars[0]);
11648 mods = get_tv_string_buf_chk(&argvars[1], buf);
11649 if (fname == NULL || mods == NULL)
11650 fname = NULL;
11651 else
11652 {
11653 len = (int)STRLEN(fname);
11654 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011656
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011657 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011658 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011659 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011660 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011661 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011662 vim_free(fbuf);
11663}
11664
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011665static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011666
11667/*
11668 * "foldclosed()" function
11669 */
11670 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011671foldclosed_both(
11672 typval_T *argvars UNUSED,
11673 typval_T *rettv,
11674 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011675{
11676#ifdef FEAT_FOLDING
11677 linenr_T lnum;
11678 linenr_T first, last;
11679
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011680 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011681 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11682 {
11683 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11684 {
11685 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011686 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011687 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011688 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011689 return;
11690 }
11691 }
11692#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011693 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011694}
11695
11696/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011697 * "foldclosed()" function
11698 */
11699 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011700f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011701{
11702 foldclosed_both(argvars, rettv, FALSE);
11703}
11704
11705/*
11706 * "foldclosedend()" function
11707 */
11708 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011709f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011710{
11711 foldclosed_both(argvars, rettv, TRUE);
11712}
11713
11714/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011715 * "foldlevel()" function
11716 */
11717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011718f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011719{
11720#ifdef FEAT_FOLDING
11721 linenr_T lnum;
11722
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011723 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011724 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011725 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011727}
11728
11729/*
11730 * "foldtext()" function
11731 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011732 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011733f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011734{
11735#ifdef FEAT_FOLDING
11736 linenr_T lnum;
11737 char_u *s;
11738 char_u *r;
11739 int len;
11740 char *txt;
11741#endif
11742
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011743 rettv->v_type = VAR_STRING;
11744 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011745#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011746 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11747 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11748 <= curbuf->b_ml.ml_line_count
11749 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011750 {
11751 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011752 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11753 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011754 {
11755 if (!linewhite(lnum))
11756 break;
11757 ++lnum;
11758 }
11759
11760 /* Find interesting text in this line. */
11761 s = skipwhite(ml_get(lnum));
11762 /* skip C comment-start */
11763 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011764 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011765 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011766 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011767 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011768 {
11769 s = skipwhite(ml_get(lnum + 1));
11770 if (*s == '*')
11771 s = skipwhite(s + 1);
11772 }
11773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011774 txt = _("+-%s%3ld lines: ");
11775 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011776 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011777 + 20 /* for %3ld */
11778 + STRLEN(s))); /* concatenated */
11779 if (r != NULL)
11780 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011781 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11782 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11783 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011784 len = (int)STRLEN(r);
11785 STRCAT(r, s);
11786 /* remove 'foldmarker' and 'commentstring' */
11787 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011788 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011789 }
11790 }
11791#endif
11792}
11793
11794/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011795 * "foldtextresult(lnum)" function
11796 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011797 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011798f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011799{
11800#ifdef FEAT_FOLDING
11801 linenr_T lnum;
11802 char_u *text;
11803 char_u buf[51];
11804 foldinfo_T foldinfo;
11805 int fold_count;
11806#endif
11807
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011808 rettv->v_type = VAR_STRING;
11809 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011810#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011811 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011812 /* treat illegal types and illegal string values for {lnum} the same */
11813 if (lnum < 0)
11814 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011815 fold_count = foldedCount(curwin, lnum, &foldinfo);
11816 if (fold_count > 0)
11817 {
11818 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11819 &foldinfo, buf);
11820 if (text == buf)
11821 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011822 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011823 }
11824#endif
11825}
11826
11827/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011828 * "foreground()" function
11829 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011830 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011831f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011832{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011833#ifdef FEAT_GUI
11834 if (gui.in_use)
11835 gui_mch_set_foreground();
11836#else
11837# ifdef WIN32
11838 win32_set_foreground();
11839# endif
11840#endif
11841}
11842
11843/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011844 * "function()" function
11845 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011847f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011848{
11849 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011850 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011851 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011852 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011853
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011854 if (argvars[0].v_type == VAR_FUNC)
11855 {
11856 /* function(MyFunc, [arg], dict) */
11857 s = argvars[0].vval.v_string;
11858 }
11859 else if (argvars[0].v_type == VAR_PARTIAL
11860 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011861 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011862 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011863 arg_pt = argvars[0].vval.v_partial;
11864 s = arg_pt->pt_name;
11865 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011866 else
11867 {
11868 /* function('MyFunc', [arg], dict) */
11869 s = get_tv_string(&argvars[0]);
11870 use_string = TRUE;
11871 }
11872
11873 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011874 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011875 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011876 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
11877 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011878 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011879 else
11880 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010011881 int dict_idx = 0;
11882 int arg_idx = 0;
11883 list_T *list = NULL;
11884
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011885 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011886 {
11887 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011888 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011889
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011890 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11891 * also be called from another script. Using trans_function_name()
11892 * would also work, but some plugins depend on the name being
11893 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011894 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011895 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
11896 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011897 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011898 STRCPY(name, sid_buf);
11899 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011900 }
11901 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011902 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011903 name = vim_strsave(s);
11904
11905 if (argvars[1].v_type != VAR_UNKNOWN)
11906 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011907 if (argvars[2].v_type != VAR_UNKNOWN)
11908 {
11909 /* function(name, [args], dict) */
11910 arg_idx = 1;
11911 dict_idx = 2;
11912 }
11913 else if (argvars[1].v_type == VAR_DICT)
11914 /* function(name, dict) */
11915 dict_idx = 1;
11916 else
11917 /* function(name, [args]) */
11918 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010011919 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011920 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010011921 if (argvars[dict_idx].v_type != VAR_DICT)
11922 {
11923 EMSG(_("E922: expected a dict"));
11924 vim_free(name);
11925 return;
11926 }
11927 if (argvars[dict_idx].vval.v_dict == NULL)
11928 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011929 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010011930 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011931 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010011932 if (argvars[arg_idx].v_type != VAR_LIST)
11933 {
11934 EMSG(_("E923: Second argument of function() must be a list or a dict"));
11935 vim_free(name);
11936 return;
11937 }
11938 list = argvars[arg_idx].vval.v_list;
11939 if (list == NULL || list->lv_len == 0)
11940 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011941 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010011942 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011943 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010011944 {
11945 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011946
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011947 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010011948 if (pt == NULL)
11949 vim_free(name);
11950 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011951 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011952 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011953 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011954 listitem_T *li;
11955 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011956 int arg_len = 0;
11957 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011958
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011959 if (arg_pt != NULL)
11960 arg_len = arg_pt->pt_argc;
11961 if (list != NULL)
11962 lv_len = list->lv_len;
11963 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011964 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011965 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011966 if (pt->pt_argv == NULL)
11967 {
11968 vim_free(pt);
11969 vim_free(name);
11970 return;
11971 }
11972 else
11973 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011974 for (i = 0; i < arg_len; i++)
11975 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
11976 if (lv_len > 0)
11977 for (li = list->lv_first; li != NULL;
11978 li = li->li_next)
11979 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011980 }
11981 }
11982
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010011983 /* For "function(dict.func, [], dict)" and "func" is a partial
11984 * use "dict". That is backwards compatible. */
11985 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011986 {
11987 pt->pt_dict = argvars[dict_idx].vval.v_dict;
11988 ++pt->pt_dict->dv_refcount;
11989 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011990 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010011991 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011992 pt->pt_dict = arg_pt->pt_dict;
11993 if (pt->pt_dict != NULL)
11994 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010011995 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011996
11997 pt->pt_refcount = 1;
11998 pt->pt_name = name;
11999 func_ref(pt->pt_name);
12000 }
12001 rettv->v_type = VAR_PARTIAL;
12002 rettv->vval.v_partial = pt;
12003 }
12004 else
12005 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012006 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012007 rettv->v_type = VAR_FUNC;
12008 rettv->vval.v_string = name;
12009 func_ref(name);
12010 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012011 }
12012}
12013
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012014 static void
12015partial_free(partial_T *pt)
12016{
12017 int i;
12018
12019 for (i = 0; i < pt->pt_argc; ++i)
12020 clear_tv(&pt->pt_argv[i]);
12021 vim_free(pt->pt_argv);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010012022 dict_unref(pt->pt_dict);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012023 func_unref(pt->pt_name);
12024 vim_free(pt->pt_name);
12025 vim_free(pt);
12026}
12027
12028/*
12029 * Unreference a closure: decrement the reference count and free it when it
12030 * becomes zero.
12031 */
12032 void
12033partial_unref(partial_T *pt)
12034{
12035 if (pt != NULL && --pt->pt_refcount <= 0)
12036 partial_free(pt);
12037}
12038
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012039/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012040 * "garbagecollect()" function
12041 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012042 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012043f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012044{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012045 /* This is postponed until we are back at the toplevel, because we may be
12046 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12047 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012048
12049 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12050 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012051}
12052
12053/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012054 * "get()" function
12055 */
12056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012057f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012058{
Bram Moolenaar33570922005-01-25 22:26:29 +000012059 listitem_T *li;
12060 list_T *l;
12061 dictitem_T *di;
12062 dict_T *d;
12063 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012064
Bram Moolenaare9a41262005-01-15 22:18:47 +000012065 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012066 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012067 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012068 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012069 int error = FALSE;
12070
12071 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12072 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012073 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012074 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012075 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012076 else if (argvars[0].v_type == VAR_DICT)
12077 {
12078 if ((d = argvars[0].vval.v_dict) != NULL)
12079 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012080 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012081 if (di != NULL)
12082 tv = &di->di_tv;
12083 }
12084 }
12085 else
12086 EMSG2(_(e_listdictarg), "get()");
12087
12088 if (tv == NULL)
12089 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012090 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012091 copy_tv(&argvars[2], rettv);
12092 }
12093 else
12094 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012095}
12096
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012097static 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 +000012098
12099/*
12100 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012101 * Return a range (from start to end) of lines in rettv from the specified
12102 * buffer.
12103 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012104 */
12105 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012106get_buffer_lines(
12107 buf_T *buf,
12108 linenr_T start,
12109 linenr_T end,
12110 int retlist,
12111 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012112{
12113 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012114
Bram Moolenaar959a1432013-12-14 12:17:38 +010012115 rettv->v_type = VAR_STRING;
12116 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012117 if (retlist && rettv_list_alloc(rettv) == FAIL)
12118 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012119
12120 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12121 return;
12122
12123 if (!retlist)
12124 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012125 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12126 p = ml_get_buf(buf, start, FALSE);
12127 else
12128 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012129 rettv->vval.v_string = vim_strsave(p);
12130 }
12131 else
12132 {
12133 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012134 return;
12135
12136 if (start < 1)
12137 start = 1;
12138 if (end > buf->b_ml.ml_line_count)
12139 end = buf->b_ml.ml_line_count;
12140 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012141 if (list_append_string(rettv->vval.v_list,
12142 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012143 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012144 }
12145}
12146
12147/*
12148 * "getbufline()" function
12149 */
12150 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012151f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012152{
12153 linenr_T lnum;
12154 linenr_T end;
12155 buf_T *buf;
12156
12157 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12158 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012159 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012160 --emsg_off;
12161
Bram Moolenaar661b1822005-07-28 22:36:45 +000012162 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012163 if (argvars[2].v_type == VAR_UNKNOWN)
12164 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012165 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012166 end = get_tv_lnum_buf(&argvars[2], buf);
12167
Bram Moolenaar342337a2005-07-21 21:11:17 +000012168 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012169}
12170
Bram Moolenaar0d660222005-01-07 21:51:51 +000012171/*
12172 * "getbufvar()" function
12173 */
12174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012175f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012176{
12177 buf_T *buf;
12178 buf_T *save_curbuf;
12179 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012180 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012181 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012182
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012183 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12184 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012185 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012186 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012187
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012188 rettv->v_type = VAR_STRING;
12189 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012190
12191 if (buf != NULL && varname != NULL)
12192 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012193 /* set curbuf to be our buf, temporarily */
12194 save_curbuf = curbuf;
12195 curbuf = buf;
12196
Bram Moolenaar0d660222005-01-07 21:51:51 +000012197 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012198 {
12199 if (get_option_tv(&varname, rettv, TRUE) == OK)
12200 done = TRUE;
12201 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012202 else if (STRCMP(varname, "changedtick") == 0)
12203 {
12204 rettv->v_type = VAR_NUMBER;
12205 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012206 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012207 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012208 else
12209 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012210 /* Look up the variable. */
12211 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12212 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12213 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012214 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012215 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012216 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012217 done = TRUE;
12218 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012219 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012220
12221 /* restore previous notion of curbuf */
12222 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012223 }
12224
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012225 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12226 /* use the default value */
12227 copy_tv(&argvars[2], rettv);
12228
Bram Moolenaar0d660222005-01-07 21:51:51 +000012229 --emsg_off;
12230}
12231
12232/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012233 * "getchar()" function
12234 */
12235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012236f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012237{
12238 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012239 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012240
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012241 /* Position the cursor. Needed after a message that ends in a space. */
12242 windgoto(msg_row, msg_col);
12243
Bram Moolenaar071d4272004-06-13 20:20:40 +000012244 ++no_mapping;
12245 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012246 for (;;)
12247 {
12248 if (argvars[0].v_type == VAR_UNKNOWN)
12249 /* getchar(): blocking wait. */
12250 n = safe_vgetc();
12251 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12252 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012253 n = vpeekc_any();
12254 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012255 /* illegal argument or getchar(0) and no char avail: return zero */
12256 n = 0;
12257 else
12258 /* getchar(0) and char avail: return char */
12259 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012260
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012261 if (n == K_IGNORE)
12262 continue;
12263 break;
12264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012265 --no_mapping;
12266 --allow_keys;
12267
Bram Moolenaar219b8702006-11-01 14:32:36 +000012268 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12269 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12270 vimvars[VV_MOUSE_COL].vv_nr = 0;
12271
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012272 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012273 if (IS_SPECIAL(n) || mod_mask != 0)
12274 {
12275 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12276 int i = 0;
12277
12278 /* Turn a special key into three bytes, plus modifier. */
12279 if (mod_mask != 0)
12280 {
12281 temp[i++] = K_SPECIAL;
12282 temp[i++] = KS_MODIFIER;
12283 temp[i++] = mod_mask;
12284 }
12285 if (IS_SPECIAL(n))
12286 {
12287 temp[i++] = K_SPECIAL;
12288 temp[i++] = K_SECOND(n);
12289 temp[i++] = K_THIRD(n);
12290 }
12291#ifdef FEAT_MBYTE
12292 else if (has_mbyte)
12293 i += (*mb_char2bytes)(n, temp + i);
12294#endif
12295 else
12296 temp[i++] = n;
12297 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012298 rettv->v_type = VAR_STRING;
12299 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012300
12301#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012302 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012303 {
12304 int row = mouse_row;
12305 int col = mouse_col;
12306 win_T *win;
12307 linenr_T lnum;
12308# ifdef FEAT_WINDOWS
12309 win_T *wp;
12310# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012311 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012312
12313 if (row >= 0 && col >= 0)
12314 {
12315 /* Find the window at the mouse coordinates and compute the
12316 * text position. */
12317 win = mouse_find_win(&row, &col);
12318 (void)mouse_comp_pos(win, &row, &col, &lnum);
12319# ifdef FEAT_WINDOWS
12320 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012321 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012322# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012323 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012324 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12325 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12326 }
12327 }
12328#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012329 }
12330}
12331
12332/*
12333 * "getcharmod()" function
12334 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012335 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012336f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012337{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012338 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012339}
12340
12341/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012342 * "getcharsearch()" function
12343 */
12344 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012345f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012346{
12347 if (rettv_dict_alloc(rettv) != FAIL)
12348 {
12349 dict_T *dict = rettv->vval.v_dict;
12350
12351 dict_add_nr_str(dict, "char", 0L, last_csearch());
12352 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12353 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12354 }
12355}
12356
12357/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012358 * "getcmdline()" function
12359 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012360 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012361f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012362{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012363 rettv->v_type = VAR_STRING;
12364 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012365}
12366
12367/*
12368 * "getcmdpos()" function
12369 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012370 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012371f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012372{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012373 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012374}
12375
12376/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012377 * "getcmdtype()" function
12378 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012379 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012380f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012381{
12382 rettv->v_type = VAR_STRING;
12383 rettv->vval.v_string = alloc(2);
12384 if (rettv->vval.v_string != NULL)
12385 {
12386 rettv->vval.v_string[0] = get_cmdline_type();
12387 rettv->vval.v_string[1] = NUL;
12388 }
12389}
12390
12391/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012392 * "getcmdwintype()" function
12393 */
12394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012395f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012396{
12397 rettv->v_type = VAR_STRING;
12398 rettv->vval.v_string = NULL;
12399#ifdef FEAT_CMDWIN
12400 rettv->vval.v_string = alloc(2);
12401 if (rettv->vval.v_string != NULL)
12402 {
12403 rettv->vval.v_string[0] = cmdwin_type;
12404 rettv->vval.v_string[1] = NUL;
12405 }
12406#endif
12407}
12408
12409/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012410 * "getcwd()" function
12411 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012412 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012413f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012414{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012415 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012416 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012417
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012418 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012419 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012420
12421 wp = find_tabwin(&argvars[0], &argvars[1]);
12422 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012423 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012424 if (wp->w_localdir != NULL)
12425 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12426 else if(globaldir != NULL)
12427 rettv->vval.v_string = vim_strsave(globaldir);
12428 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012429 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012430 cwd = alloc(MAXPATHL);
12431 if (cwd != NULL)
12432 {
12433 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12434 rettv->vval.v_string = vim_strsave(cwd);
12435 vim_free(cwd);
12436 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012437 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012438#ifdef BACKSLASH_IN_FILENAME
12439 if (rettv->vval.v_string != NULL)
12440 slash_adjust(rettv->vval.v_string);
12441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012442 }
12443}
12444
12445/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012446 * "getfontname()" function
12447 */
12448 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012449f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012450{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012451 rettv->v_type = VAR_STRING;
12452 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012453#ifdef FEAT_GUI
12454 if (gui.in_use)
12455 {
12456 GuiFont font;
12457 char_u *name = NULL;
12458
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012459 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012460 {
12461 /* Get the "Normal" font. Either the name saved by
12462 * hl_set_font_name() or from the font ID. */
12463 font = gui.norm_font;
12464 name = hl_get_font_name();
12465 }
12466 else
12467 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012468 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012469 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12470 return;
12471 font = gui_mch_get_font(name, FALSE);
12472 if (font == NOFONT)
12473 return; /* Invalid font name, return empty string. */
12474 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012475 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012476 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012477 gui_mch_free_font(font);
12478 }
12479#endif
12480}
12481
12482/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012483 * "getfperm({fname})" function
12484 */
12485 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012486f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012487{
12488 char_u *fname;
12489 struct stat st;
12490 char_u *perm = NULL;
12491 char_u flags[] = "rwx";
12492 int i;
12493
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012494 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012495
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012496 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012497 if (mch_stat((char *)fname, &st) >= 0)
12498 {
12499 perm = vim_strsave((char_u *)"---------");
12500 if (perm != NULL)
12501 {
12502 for (i = 0; i < 9; i++)
12503 {
12504 if (st.st_mode & (1 << (8 - i)))
12505 perm[i] = flags[i % 3];
12506 }
12507 }
12508 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012509 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012510}
12511
12512/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012513 * "getfsize({fname})" function
12514 */
12515 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012516f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012517{
12518 char_u *fname;
12519 struct stat st;
12520
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012521 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012522
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012523 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012524
12525 if (mch_stat((char *)fname, &st) >= 0)
12526 {
12527 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012528 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012529 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012530 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012531 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012532
12533 /* non-perfect check for overflow */
12534 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12535 rettv->vval.v_number = -2;
12536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012537 }
12538 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012539 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012540}
12541
12542/*
12543 * "getftime({fname})" function
12544 */
12545 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012546f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012547{
12548 char_u *fname;
12549 struct stat st;
12550
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012551 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012552
12553 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012554 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012555 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012556 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012557}
12558
12559/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012560 * "getftype({fname})" function
12561 */
12562 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012563f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012564{
12565 char_u *fname;
12566 struct stat st;
12567 char_u *type = NULL;
12568 char *t;
12569
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012570 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012571
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012572 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012573 if (mch_lstat((char *)fname, &st) >= 0)
12574 {
12575#ifdef S_ISREG
12576 if (S_ISREG(st.st_mode))
12577 t = "file";
12578 else if (S_ISDIR(st.st_mode))
12579 t = "dir";
12580# ifdef S_ISLNK
12581 else if (S_ISLNK(st.st_mode))
12582 t = "link";
12583# endif
12584# ifdef S_ISBLK
12585 else if (S_ISBLK(st.st_mode))
12586 t = "bdev";
12587# endif
12588# ifdef S_ISCHR
12589 else if (S_ISCHR(st.st_mode))
12590 t = "cdev";
12591# endif
12592# ifdef S_ISFIFO
12593 else if (S_ISFIFO(st.st_mode))
12594 t = "fifo";
12595# endif
12596# ifdef S_ISSOCK
12597 else if (S_ISSOCK(st.st_mode))
12598 t = "fifo";
12599# endif
12600 else
12601 t = "other";
12602#else
12603# ifdef S_IFMT
12604 switch (st.st_mode & S_IFMT)
12605 {
12606 case S_IFREG: t = "file"; break;
12607 case S_IFDIR: t = "dir"; break;
12608# ifdef S_IFLNK
12609 case S_IFLNK: t = "link"; break;
12610# endif
12611# ifdef S_IFBLK
12612 case S_IFBLK: t = "bdev"; break;
12613# endif
12614# ifdef S_IFCHR
12615 case S_IFCHR: t = "cdev"; break;
12616# endif
12617# ifdef S_IFIFO
12618 case S_IFIFO: t = "fifo"; break;
12619# endif
12620# ifdef S_IFSOCK
12621 case S_IFSOCK: t = "socket"; break;
12622# endif
12623 default: t = "other";
12624 }
12625# else
12626 if (mch_isdir(fname))
12627 t = "dir";
12628 else
12629 t = "file";
12630# endif
12631#endif
12632 type = vim_strsave((char_u *)t);
12633 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012634 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012635}
12636
12637/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012638 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012639 */
12640 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012641f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012642{
12643 linenr_T lnum;
12644 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012645 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012646
12647 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012648 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012649 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012650 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012651 retlist = FALSE;
12652 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012653 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012654 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012655 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012656 retlist = TRUE;
12657 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012658
Bram Moolenaar342337a2005-07-21 21:11:17 +000012659 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012660}
12661
12662/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012663 * "getmatches()" function
12664 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012665 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012666f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012667{
12668#ifdef FEAT_SEARCH_EXTRA
12669 dict_T *dict;
12670 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012671 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012672
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012673 if (rettv_list_alloc(rettv) == OK)
12674 {
12675 while (cur != NULL)
12676 {
12677 dict = dict_alloc();
12678 if (dict == NULL)
12679 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012680 if (cur->match.regprog == NULL)
12681 {
12682 /* match added with matchaddpos() */
12683 for (i = 0; i < MAXPOSMATCH; ++i)
12684 {
12685 llpos_T *llpos;
12686 char buf[6];
12687 list_T *l;
12688
12689 llpos = &cur->pos.pos[i];
12690 if (llpos->lnum == 0)
12691 break;
12692 l = list_alloc();
12693 if (l == NULL)
12694 break;
12695 list_append_number(l, (varnumber_T)llpos->lnum);
12696 if (llpos->col > 0)
12697 {
12698 list_append_number(l, (varnumber_T)llpos->col);
12699 list_append_number(l, (varnumber_T)llpos->len);
12700 }
12701 sprintf(buf, "pos%d", i + 1);
12702 dict_add_list(dict, buf, l);
12703 }
12704 }
12705 else
12706 {
12707 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12708 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012709 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012710 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12711 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012712# ifdef FEAT_CONCEAL
12713 if (cur->conceal_char)
12714 {
12715 char_u buf[MB_MAXBYTES + 1];
12716
12717 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12718 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12719 }
12720# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012721 list_append_dict(rettv->vval.v_list, dict);
12722 cur = cur->next;
12723 }
12724 }
12725#endif
12726}
12727
12728/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012729 * "getpid()" function
12730 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012731 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012732f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012733{
12734 rettv->vval.v_number = mch_get_pid();
12735}
12736
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012737static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012738
12739/*
12740 * "getcurpos()" function
12741 */
12742 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012743f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012744{
12745 getpos_both(argvars, rettv, TRUE);
12746}
12747
Bram Moolenaar18081e32008-02-20 19:11:07 +000012748/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012749 * "getpos(string)" function
12750 */
12751 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012752f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012753{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012754 getpos_both(argvars, rettv, FALSE);
12755}
12756
12757 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012758getpos_both(
12759 typval_T *argvars,
12760 typval_T *rettv,
12761 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012762{
Bram Moolenaara5525202006-03-02 22:52:09 +000012763 pos_T *fp;
12764 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012765 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012766
12767 if (rettv_list_alloc(rettv) == OK)
12768 {
12769 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012770 if (getcurpos)
12771 fp = &curwin->w_cursor;
12772 else
12773 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012774 if (fnum != -1)
12775 list_append_number(l, (varnumber_T)fnum);
12776 else
12777 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012778 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12779 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012780 list_append_number(l, (fp != NULL)
12781 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012782 : (varnumber_T)0);
12783 list_append_number(l,
12784#ifdef FEAT_VIRTUALEDIT
12785 (fp != NULL) ? (varnumber_T)fp->coladd :
12786#endif
12787 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012788 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012789 {
12790 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010012791 list_append_number(l, curwin->w_curswant == MAXCOL ?
12792 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012793 }
Bram Moolenaara5525202006-03-02 22:52:09 +000012794 }
12795 else
12796 rettv->vval.v_number = FALSE;
12797}
12798
12799/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012800 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012801 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012803f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012804{
12805#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012806 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012807#endif
12808
Bram Moolenaar2641f772005-03-25 21:58:17 +000012809#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012810 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012811 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012812 wp = NULL;
12813 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12814 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012815 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012816 if (wp == NULL)
12817 return;
12818 }
12819
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012820 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012821 }
12822#endif
12823}
12824
12825/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012826 * "getreg()" function
12827 */
12828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012829f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012830{
12831 char_u *strregname;
12832 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012833 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012834 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012835 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012836
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012837 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012838 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012839 strregname = get_tv_string_chk(&argvars[0]);
12840 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012841 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012842 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012843 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012844 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12845 return_list = get_tv_number_chk(&argvars[2], &error);
12846 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012849 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012850
12851 if (error)
12852 return;
12853
Bram Moolenaar071d4272004-06-13 20:20:40 +000012854 regname = (strregname == NULL ? '"' : *strregname);
12855 if (regname == 0)
12856 regname = '"';
12857
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012858 if (return_list)
12859 {
12860 rettv->v_type = VAR_LIST;
12861 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12862 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012863 if (rettv->vval.v_list != NULL)
12864 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012865 }
12866 else
12867 {
12868 rettv->v_type = VAR_STRING;
12869 rettv->vval.v_string = get_reg_contents(regname,
12870 arg2 ? GREG_EXPR_SRC : 0);
12871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012872}
12873
12874/*
12875 * "getregtype()" function
12876 */
12877 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012878f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012879{
12880 char_u *strregname;
12881 int regname;
12882 char_u buf[NUMBUFLEN + 2];
12883 long reglen = 0;
12884
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012885 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012886 {
12887 strregname = get_tv_string_chk(&argvars[0]);
12888 if (strregname == NULL) /* type error; errmsg already given */
12889 {
12890 rettv->v_type = VAR_STRING;
12891 rettv->vval.v_string = NULL;
12892 return;
12893 }
12894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012895 else
12896 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012897 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012898
12899 regname = (strregname == NULL ? '"' : *strregname);
12900 if (regname == 0)
12901 regname = '"';
12902
12903 buf[0] = NUL;
12904 buf[1] = NUL;
12905 switch (get_reg_type(regname, &reglen))
12906 {
12907 case MLINE: buf[0] = 'V'; break;
12908 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012909 case MBLOCK:
12910 buf[0] = Ctrl_V;
12911 sprintf((char *)buf + 1, "%ld", reglen + 1);
12912 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012913 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012914 rettv->v_type = VAR_STRING;
12915 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012916}
12917
12918/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012919 * "gettabvar()" function
12920 */
12921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012922f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012923{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012924 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012925 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012926 dictitem_T *v;
12927 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012928 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012929
12930 rettv->v_type = VAR_STRING;
12931 rettv->vval.v_string = NULL;
12932
12933 varname = get_tv_string_chk(&argvars[1]);
12934 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12935 if (tp != NULL && varname != NULL)
12936 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012937 /* Set tp to be our tabpage, temporarily. Also set the window to the
12938 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012939 if (switch_win(&oldcurwin, &oldtabpage,
12940 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012941 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012942 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012943 /* look up the variable */
12944 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12945 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12946 if (v != NULL)
12947 {
12948 copy_tv(&v->di_tv, rettv);
12949 done = TRUE;
12950 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012951 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012952
12953 /* restore previous notion of curwin */
12954 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012955 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012956
12957 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012958 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012959}
12960
12961/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012962 * "gettabwinvar()" function
12963 */
12964 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012965f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012966{
12967 getwinvar(argvars, rettv, 1);
12968}
12969
12970/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012971 * "getwinposx()" function
12972 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012973 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012974f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012975{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012976 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012977#ifdef FEAT_GUI
12978 if (gui.in_use)
12979 {
12980 int x, y;
12981
12982 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012983 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012984 }
12985#endif
12986}
12987
12988/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010012989 * "win_findbuf()" function
12990 */
12991 static void
12992f_win_findbuf(typval_T *argvars, typval_T *rettv)
12993{
12994 if (rettv_list_alloc(rettv) != FAIL)
12995 win_findbuf(argvars, rettv->vval.v_list);
12996}
12997
12998/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010012999 * "win_getid()" function
13000 */
13001 static void
13002f_win_getid(typval_T *argvars, typval_T *rettv)
13003{
13004 rettv->vval.v_number = win_getid(argvars);
13005}
13006
13007/*
13008 * "win_gotoid()" function
13009 */
13010 static void
13011f_win_gotoid(typval_T *argvars, typval_T *rettv)
13012{
13013 rettv->vval.v_number = win_gotoid(argvars);
13014}
13015
13016/*
13017 * "win_id2tabwin()" function
13018 */
13019 static void
13020f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13021{
13022 if (rettv_list_alloc(rettv) != FAIL)
13023 win_id2tabwin(argvars, rettv->vval.v_list);
13024}
13025
13026/*
13027 * "win_id2win()" function
13028 */
13029 static void
13030f_win_id2win(typval_T *argvars, typval_T *rettv)
13031{
13032 rettv->vval.v_number = win_id2win(argvars);
13033}
13034
13035/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013036 * "getwinposy()" function
13037 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013038 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013039f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013040{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013041 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013042#ifdef FEAT_GUI
13043 if (gui.in_use)
13044 {
13045 int x, y;
13046
13047 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013048 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013049 }
13050#endif
13051}
13052
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013053/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013054 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013055 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013056 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013057find_win_by_nr(
13058 typval_T *vp,
13059 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013060{
13061#ifdef FEAT_WINDOWS
13062 win_T *wp;
13063#endif
13064 int nr;
13065
13066 nr = get_tv_number_chk(vp, NULL);
13067
13068#ifdef FEAT_WINDOWS
13069 if (nr < 0)
13070 return NULL;
13071 if (nr == 0)
13072 return curwin;
13073
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013074 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13075 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013076 if (--nr <= 0)
13077 break;
13078 return wp;
13079#else
13080 if (nr == 0 || nr == 1)
13081 return curwin;
13082 return NULL;
13083#endif
13084}
13085
Bram Moolenaar071d4272004-06-13 20:20:40 +000013086/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013087 * Find window specified by "wvp" in tabpage "tvp".
13088 */
13089 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013090find_tabwin(
13091 typval_T *wvp, /* VAR_UNKNOWN for current window */
13092 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013093{
13094 win_T *wp = NULL;
13095 tabpage_T *tp = NULL;
13096 long n;
13097
13098 if (wvp->v_type != VAR_UNKNOWN)
13099 {
13100 if (tvp->v_type != VAR_UNKNOWN)
13101 {
13102 n = get_tv_number(tvp);
13103 if (n >= 0)
13104 tp = find_tabpage(n);
13105 }
13106 else
13107 tp = curtab;
13108
13109 if (tp != NULL)
13110 wp = find_win_by_nr(wvp, tp);
13111 }
13112 else
13113 wp = curwin;
13114
13115 return wp;
13116}
13117
13118/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013119 * "getwinvar()" function
13120 */
13121 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013122f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013123{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013124 getwinvar(argvars, rettv, 0);
13125}
13126
13127/*
13128 * getwinvar() and gettabwinvar()
13129 */
13130 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013131getwinvar(
13132 typval_T *argvars,
13133 typval_T *rettv,
13134 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013135{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013136 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013137 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013138 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013139 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013140 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013141#ifdef FEAT_WINDOWS
13142 win_T *oldcurwin;
13143 tabpage_T *oldtabpage;
13144 int need_switch_win;
13145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013146
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013147#ifdef FEAT_WINDOWS
13148 if (off == 1)
13149 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13150 else
13151 tp = curtab;
13152#endif
13153 win = find_win_by_nr(&argvars[off], tp);
13154 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013155 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013156
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013157 rettv->v_type = VAR_STRING;
13158 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013159
13160 if (win != NULL && varname != NULL)
13161 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013162#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013163 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013164 * otherwise the window is not valid. Only do this when needed,
13165 * autocommands get blocked. */
13166 need_switch_win = !(tp == curtab && win == curwin);
13167 if (!need_switch_win
13168 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13169#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013170 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013171 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013172 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013173 if (get_option_tv(&varname, rettv, 1) == OK)
13174 done = TRUE;
13175 }
13176 else
13177 {
13178 /* Look up the variable. */
13179 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13180 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13181 varname, FALSE);
13182 if (v != NULL)
13183 {
13184 copy_tv(&v->di_tv, rettv);
13185 done = TRUE;
13186 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013188 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013189
Bram Moolenaarba117c22015-09-29 16:53:22 +020013190#ifdef FEAT_WINDOWS
13191 if (need_switch_win)
13192 /* restore previous notion of curwin */
13193 restore_win(oldcurwin, oldtabpage, TRUE);
13194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013195 }
13196
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013197 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13198 /* use the default return value */
13199 copy_tv(&argvars[off + 2], rettv);
13200
Bram Moolenaar071d4272004-06-13 20:20:40 +000013201 --emsg_off;
13202}
13203
13204/*
13205 * "glob()" function
13206 */
13207 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013208f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013209{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013210 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013211 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013212 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013213
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013214 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013215 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013216 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013217 if (argvars[1].v_type != VAR_UNKNOWN)
13218 {
13219 if (get_tv_number_chk(&argvars[1], &error))
13220 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013221 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013222 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013223 if (get_tv_number_chk(&argvars[2], &error))
13224 {
13225 rettv->v_type = VAR_LIST;
13226 rettv->vval.v_list = NULL;
13227 }
13228 if (argvars[3].v_type != VAR_UNKNOWN
13229 && get_tv_number_chk(&argvars[3], &error))
13230 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013231 }
13232 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013233 if (!error)
13234 {
13235 ExpandInit(&xpc);
13236 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013237 if (p_wic)
13238 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013239 if (rettv->v_type == VAR_STRING)
13240 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013241 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013242 else if (rettv_list_alloc(rettv) != FAIL)
13243 {
13244 int i;
13245
13246 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13247 NULL, options, WILD_ALL_KEEP);
13248 for (i = 0; i < xpc.xp_numfiles; i++)
13249 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13250
13251 ExpandCleanup(&xpc);
13252 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013253 }
13254 else
13255 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013256}
13257
13258/*
13259 * "globpath()" function
13260 */
13261 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013262f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013263{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013264 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013265 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013266 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013267 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013268 garray_T ga;
13269 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013270
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013271 /* When the optional second argument is non-zero, don't remove matches
13272 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013273 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013274 if (argvars[2].v_type != VAR_UNKNOWN)
13275 {
13276 if (get_tv_number_chk(&argvars[2], &error))
13277 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013278 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013279 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013280 if (get_tv_number_chk(&argvars[3], &error))
13281 {
13282 rettv->v_type = VAR_LIST;
13283 rettv->vval.v_list = NULL;
13284 }
13285 if (argvars[4].v_type != VAR_UNKNOWN
13286 && get_tv_number_chk(&argvars[4], &error))
13287 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013288 }
13289 }
13290 if (file != NULL && !error)
13291 {
13292 ga_init2(&ga, (int)sizeof(char_u *), 10);
13293 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13294 if (rettv->v_type == VAR_STRING)
13295 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13296 else if (rettv_list_alloc(rettv) != FAIL)
13297 for (i = 0; i < ga.ga_len; ++i)
13298 list_append_string(rettv->vval.v_list,
13299 ((char_u **)(ga.ga_data))[i], -1);
13300 ga_clear_strings(&ga);
13301 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013302 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013303 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013304}
13305
13306/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013307 * "glob2regpat()" function
13308 */
13309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013310f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013311{
13312 char_u *pat = get_tv_string_chk(&argvars[0]);
13313
13314 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013315 rettv->vval.v_string = (pat == NULL)
13316 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013317}
13318
13319/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013320 * "has()" function
13321 */
13322 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013323f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013324{
13325 int i;
13326 char_u *name;
13327 int n = FALSE;
13328 static char *(has_list[]) =
13329 {
13330#ifdef AMIGA
13331 "amiga",
13332# ifdef FEAT_ARP
13333 "arp",
13334# endif
13335#endif
13336#ifdef __BEOS__
13337 "beos",
13338#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013339#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013340 "mac",
13341#endif
13342#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013343 "macunix", /* built with 'darwin' enabled */
13344#endif
13345#if defined(__APPLE__) && __APPLE__ == 1
13346 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013348#ifdef __QNX__
13349 "qnx",
13350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013351#ifdef UNIX
13352 "unix",
13353#endif
13354#ifdef VMS
13355 "vms",
13356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013357#ifdef WIN32
13358 "win32",
13359#endif
13360#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13361 "win32unix",
13362#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013363#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013364 "win64",
13365#endif
13366#ifdef EBCDIC
13367 "ebcdic",
13368#endif
13369#ifndef CASE_INSENSITIVE_FILENAME
13370 "fname_case",
13371#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013372#ifdef HAVE_ACL
13373 "acl",
13374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013375#ifdef FEAT_ARABIC
13376 "arabic",
13377#endif
13378#ifdef FEAT_AUTOCMD
13379 "autocmd",
13380#endif
13381#ifdef FEAT_BEVAL
13382 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013383# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13384 "balloon_multiline",
13385# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013386#endif
13387#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13388 "builtin_terms",
13389# ifdef ALL_BUILTIN_TCAPS
13390 "all_builtin_terms",
13391# endif
13392#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013393#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13394 || defined(FEAT_GUI_W32) \
13395 || defined(FEAT_GUI_MOTIF))
13396 "browsefilter",
13397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013398#ifdef FEAT_BYTEOFF
13399 "byte_offset",
13400#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013401#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013402 "channel",
13403#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013404#ifdef FEAT_CINDENT
13405 "cindent",
13406#endif
13407#ifdef FEAT_CLIENTSERVER
13408 "clientserver",
13409#endif
13410#ifdef FEAT_CLIPBOARD
13411 "clipboard",
13412#endif
13413#ifdef FEAT_CMDL_COMPL
13414 "cmdline_compl",
13415#endif
13416#ifdef FEAT_CMDHIST
13417 "cmdline_hist",
13418#endif
13419#ifdef FEAT_COMMENTS
13420 "comments",
13421#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013422#ifdef FEAT_CONCEAL
13423 "conceal",
13424#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013425#ifdef FEAT_CRYPT
13426 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013427 "crypt-blowfish",
13428 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013429#endif
13430#ifdef FEAT_CSCOPE
13431 "cscope",
13432#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013433#ifdef FEAT_CURSORBIND
13434 "cursorbind",
13435#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013436#ifdef CURSOR_SHAPE
13437 "cursorshape",
13438#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013439#ifdef DEBUG
13440 "debug",
13441#endif
13442#ifdef FEAT_CON_DIALOG
13443 "dialog_con",
13444#endif
13445#ifdef FEAT_GUI_DIALOG
13446 "dialog_gui",
13447#endif
13448#ifdef FEAT_DIFF
13449 "diff",
13450#endif
13451#ifdef FEAT_DIGRAPHS
13452 "digraphs",
13453#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013454#ifdef FEAT_DIRECTX
13455 "directx",
13456#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013457#ifdef FEAT_DND
13458 "dnd",
13459#endif
13460#ifdef FEAT_EMACS_TAGS
13461 "emacs_tags",
13462#endif
13463 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013464 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013465#ifdef FEAT_SEARCH_EXTRA
13466 "extra_search",
13467#endif
13468#ifdef FEAT_FKMAP
13469 "farsi",
13470#endif
13471#ifdef FEAT_SEARCHPATH
13472 "file_in_path",
13473#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013474#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013475 "filterpipe",
13476#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013477#ifdef FEAT_FIND_ID
13478 "find_in_path",
13479#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013480#ifdef FEAT_FLOAT
13481 "float",
13482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013483#ifdef FEAT_FOLDING
13484 "folding",
13485#endif
13486#ifdef FEAT_FOOTER
13487 "footer",
13488#endif
13489#if !defined(USE_SYSTEM) && defined(UNIX)
13490 "fork",
13491#endif
13492#ifdef FEAT_GETTEXT
13493 "gettext",
13494#endif
13495#ifdef FEAT_GUI
13496 "gui",
13497#endif
13498#ifdef FEAT_GUI_ATHENA
13499# ifdef FEAT_GUI_NEXTAW
13500 "gui_neXtaw",
13501# else
13502 "gui_athena",
13503# endif
13504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013505#ifdef FEAT_GUI_GTK
13506 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013507# ifdef USE_GTK3
13508 "gui_gtk3",
13509# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013510 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013511# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013512#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013513#ifdef FEAT_GUI_GNOME
13514 "gui_gnome",
13515#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013516#ifdef FEAT_GUI_MAC
13517 "gui_mac",
13518#endif
13519#ifdef FEAT_GUI_MOTIF
13520 "gui_motif",
13521#endif
13522#ifdef FEAT_GUI_PHOTON
13523 "gui_photon",
13524#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013525#ifdef FEAT_GUI_W32
13526 "gui_win32",
13527#endif
13528#ifdef FEAT_HANGULIN
13529 "hangul_input",
13530#endif
13531#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13532 "iconv",
13533#endif
13534#ifdef FEAT_INS_EXPAND
13535 "insert_expand",
13536#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013537#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010013538 "job",
13539#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013540#ifdef FEAT_JUMPLIST
13541 "jumplist",
13542#endif
13543#ifdef FEAT_KEYMAP
13544 "keymap",
13545#endif
13546#ifdef FEAT_LANGMAP
13547 "langmap",
13548#endif
13549#ifdef FEAT_LIBCALL
13550 "libcall",
13551#endif
13552#ifdef FEAT_LINEBREAK
13553 "linebreak",
13554#endif
13555#ifdef FEAT_LISP
13556 "lispindent",
13557#endif
13558#ifdef FEAT_LISTCMDS
13559 "listcmds",
13560#endif
13561#ifdef FEAT_LOCALMAP
13562 "localmap",
13563#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013564#ifdef FEAT_LUA
13565# ifndef DYNAMIC_LUA
13566 "lua",
13567# endif
13568#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013569#ifdef FEAT_MENU
13570 "menu",
13571#endif
13572#ifdef FEAT_SESSION
13573 "mksession",
13574#endif
13575#ifdef FEAT_MODIFY_FNAME
13576 "modify_fname",
13577#endif
13578#ifdef FEAT_MOUSE
13579 "mouse",
13580#endif
13581#ifdef FEAT_MOUSESHAPE
13582 "mouseshape",
13583#endif
13584#if defined(UNIX) || defined(VMS)
13585# ifdef FEAT_MOUSE_DEC
13586 "mouse_dec",
13587# endif
13588# ifdef FEAT_MOUSE_GPM
13589 "mouse_gpm",
13590# endif
13591# ifdef FEAT_MOUSE_JSB
13592 "mouse_jsbterm",
13593# endif
13594# ifdef FEAT_MOUSE_NET
13595 "mouse_netterm",
13596# endif
13597# ifdef FEAT_MOUSE_PTERM
13598 "mouse_pterm",
13599# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013600# ifdef FEAT_MOUSE_SGR
13601 "mouse_sgr",
13602# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013603# ifdef FEAT_SYSMOUSE
13604 "mouse_sysmouse",
13605# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013606# ifdef FEAT_MOUSE_URXVT
13607 "mouse_urxvt",
13608# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013609# ifdef FEAT_MOUSE_XTERM
13610 "mouse_xterm",
13611# endif
13612#endif
13613#ifdef FEAT_MBYTE
13614 "multi_byte",
13615#endif
13616#ifdef FEAT_MBYTE_IME
13617 "multi_byte_ime",
13618#endif
13619#ifdef FEAT_MULTI_LANG
13620 "multi_lang",
13621#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013622#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013623#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013624 "mzscheme",
13625#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013626#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013627#ifdef FEAT_OLE
13628 "ole",
13629#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013630 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013631#ifdef FEAT_PATH_EXTRA
13632 "path_extra",
13633#endif
13634#ifdef FEAT_PERL
13635#ifndef DYNAMIC_PERL
13636 "perl",
13637#endif
13638#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013639#ifdef FEAT_PERSISTENT_UNDO
13640 "persistent_undo",
13641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013642#ifdef FEAT_PYTHON
13643#ifndef DYNAMIC_PYTHON
13644 "python",
13645#endif
13646#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013647#ifdef FEAT_PYTHON3
13648#ifndef DYNAMIC_PYTHON3
13649 "python3",
13650#endif
13651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013652#ifdef FEAT_POSTSCRIPT
13653 "postscript",
13654#endif
13655#ifdef FEAT_PRINTER
13656 "printer",
13657#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013658#ifdef FEAT_PROFILE
13659 "profile",
13660#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013661#ifdef FEAT_RELTIME
13662 "reltime",
13663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013664#ifdef FEAT_QUICKFIX
13665 "quickfix",
13666#endif
13667#ifdef FEAT_RIGHTLEFT
13668 "rightleft",
13669#endif
13670#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13671 "ruby",
13672#endif
13673#ifdef FEAT_SCROLLBIND
13674 "scrollbind",
13675#endif
13676#ifdef FEAT_CMDL_INFO
13677 "showcmd",
13678 "cmdline_info",
13679#endif
13680#ifdef FEAT_SIGNS
13681 "signs",
13682#endif
13683#ifdef FEAT_SMARTINDENT
13684 "smartindent",
13685#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013686#ifdef STARTUPTIME
13687 "startuptime",
13688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013689#ifdef FEAT_STL_OPT
13690 "statusline",
13691#endif
13692#ifdef FEAT_SUN_WORKSHOP
13693 "sun_workshop",
13694#endif
13695#ifdef FEAT_NETBEANS_INTG
13696 "netbeans_intg",
13697#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013698#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013699 "spell",
13700#endif
13701#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013702 "syntax",
13703#endif
13704#if defined(USE_SYSTEM) || !defined(UNIX)
13705 "system",
13706#endif
13707#ifdef FEAT_TAG_BINS
13708 "tag_binary",
13709#endif
13710#ifdef FEAT_TAG_OLDSTATIC
13711 "tag_old_static",
13712#endif
13713#ifdef FEAT_TAG_ANYWHITE
13714 "tag_any_white",
13715#endif
13716#ifdef FEAT_TCL
13717# ifndef DYNAMIC_TCL
13718 "tcl",
13719# endif
13720#endif
13721#ifdef TERMINFO
13722 "terminfo",
13723#endif
13724#ifdef FEAT_TERMRESPONSE
13725 "termresponse",
13726#endif
13727#ifdef FEAT_TEXTOBJ
13728 "textobjects",
13729#endif
13730#ifdef HAVE_TGETENT
13731 "tgetent",
13732#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010013733#ifdef FEAT_TIMERS
13734 "timers",
13735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013736#ifdef FEAT_TITLE
13737 "title",
13738#endif
13739#ifdef FEAT_TOOLBAR
13740 "toolbar",
13741#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013742#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13743 "unnamedplus",
13744#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013745#ifdef FEAT_USR_CMDS
13746 "user-commands", /* was accidentally included in 5.4 */
13747 "user_commands",
13748#endif
13749#ifdef FEAT_VIMINFO
13750 "viminfo",
13751#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010013752#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013753 "vertsplit",
13754#endif
13755#ifdef FEAT_VIRTUALEDIT
13756 "virtualedit",
13757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013758 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013759#ifdef FEAT_VISUALEXTRA
13760 "visualextra",
13761#endif
13762#ifdef FEAT_VREPLACE
13763 "vreplace",
13764#endif
13765#ifdef FEAT_WILDIGN
13766 "wildignore",
13767#endif
13768#ifdef FEAT_WILDMENU
13769 "wildmenu",
13770#endif
13771#ifdef FEAT_WINDOWS
13772 "windows",
13773#endif
13774#ifdef FEAT_WAK
13775 "winaltkeys",
13776#endif
13777#ifdef FEAT_WRITEBACKUP
13778 "writebackup",
13779#endif
13780#ifdef FEAT_XIM
13781 "xim",
13782#endif
13783#ifdef FEAT_XFONTSET
13784 "xfontset",
13785#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013786#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013787 "xpm",
13788 "xpm_w32", /* for backward compatibility */
13789#else
13790# if defined(HAVE_XPM)
13791 "xpm",
13792# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013794#ifdef USE_XSMP
13795 "xsmp",
13796#endif
13797#ifdef USE_XSMP_INTERACT
13798 "xsmp_interact",
13799#endif
13800#ifdef FEAT_XCLIPBOARD
13801 "xterm_clipboard",
13802#endif
13803#ifdef FEAT_XTERM_SAVE
13804 "xterm_save",
13805#endif
13806#if defined(UNIX) && defined(FEAT_X11)
13807 "X11",
13808#endif
13809 NULL
13810 };
13811
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013812 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013813 for (i = 0; has_list[i] != NULL; ++i)
13814 if (STRICMP(name, has_list[i]) == 0)
13815 {
13816 n = TRUE;
13817 break;
13818 }
13819
13820 if (n == FALSE)
13821 {
13822 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013823 {
13824 if (name[5] == '-'
13825 && STRLEN(name) > 11
13826 && vim_isdigit(name[6])
13827 && vim_isdigit(name[8])
13828 && vim_isdigit(name[10]))
13829 {
13830 int major = atoi((char *)name + 6);
13831 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013832
13833 /* Expect "patch-9.9.01234". */
13834 n = (major < VIM_VERSION_MAJOR
13835 || (major == VIM_VERSION_MAJOR
13836 && (minor < VIM_VERSION_MINOR
13837 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013838 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013839 }
13840 else
13841 n = has_patch(atoi((char *)name + 5));
13842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013843 else if (STRICMP(name, "vim_starting") == 0)
13844 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013845#ifdef FEAT_MBYTE
13846 else if (STRICMP(name, "multi_byte_encoding") == 0)
13847 n = has_mbyte;
13848#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013849#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13850 else if (STRICMP(name, "balloon_multiline") == 0)
13851 n = multiline_balloon_available();
13852#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013853#ifdef DYNAMIC_TCL
13854 else if (STRICMP(name, "tcl") == 0)
13855 n = tcl_enabled(FALSE);
13856#endif
13857#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13858 else if (STRICMP(name, "iconv") == 0)
13859 n = iconv_enabled(FALSE);
13860#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013861#ifdef DYNAMIC_LUA
13862 else if (STRICMP(name, "lua") == 0)
13863 n = lua_enabled(FALSE);
13864#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013865#ifdef DYNAMIC_MZSCHEME
13866 else if (STRICMP(name, "mzscheme") == 0)
13867 n = mzscheme_enabled(FALSE);
13868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013869#ifdef DYNAMIC_RUBY
13870 else if (STRICMP(name, "ruby") == 0)
13871 n = ruby_enabled(FALSE);
13872#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013873#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013874#ifdef DYNAMIC_PYTHON
13875 else if (STRICMP(name, "python") == 0)
13876 n = python_enabled(FALSE);
13877#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013878#endif
13879#ifdef FEAT_PYTHON3
13880#ifdef DYNAMIC_PYTHON3
13881 else if (STRICMP(name, "python3") == 0)
13882 n = python3_enabled(FALSE);
13883#endif
13884#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013885#ifdef DYNAMIC_PERL
13886 else if (STRICMP(name, "perl") == 0)
13887 n = perl_enabled(FALSE);
13888#endif
13889#ifdef FEAT_GUI
13890 else if (STRICMP(name, "gui_running") == 0)
13891 n = (gui.in_use || gui.starting);
13892# ifdef FEAT_GUI_W32
13893 else if (STRICMP(name, "gui_win32s") == 0)
13894 n = gui_is_win32s();
13895# endif
13896# ifdef FEAT_BROWSE
13897 else if (STRICMP(name, "browse") == 0)
13898 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13899# endif
13900#endif
13901#ifdef FEAT_SYN_HL
13902 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013903 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013904#endif
13905#if defined(WIN3264)
13906 else if (STRICMP(name, "win95") == 0)
13907 n = mch_windows95();
13908#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013909#ifdef FEAT_NETBEANS_INTG
13910 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013911 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013912#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013913 }
13914
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013915 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013916}
13917
13918/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013919 * "has_key()" function
13920 */
13921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013922f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013923{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013924 if (argvars[0].v_type != VAR_DICT)
13925 {
13926 EMSG(_(e_dictreq));
13927 return;
13928 }
13929 if (argvars[0].vval.v_dict == NULL)
13930 return;
13931
13932 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013933 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013934}
13935
13936/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013937 * "haslocaldir()" function
13938 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013939 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013940f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013941{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013942 win_T *wp = NULL;
13943
13944 wp = find_tabwin(&argvars[0], &argvars[1]);
13945 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013946}
13947
13948/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013949 * "hasmapto()" function
13950 */
13951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013952f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013953{
13954 char_u *name;
13955 char_u *mode;
13956 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013957 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013958
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013959 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013960 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013961 mode = (char_u *)"nvo";
13962 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013963 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013964 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013965 if (argvars[2].v_type != VAR_UNKNOWN)
13966 abbr = get_tv_number(&argvars[2]);
13967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013968
Bram Moolenaar2c932302006-03-18 21:42:09 +000013969 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013970 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013971 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013972 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013973}
13974
13975/*
13976 * "histadd()" function
13977 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013978 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013979f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013980{
13981#ifdef FEAT_CMDHIST
13982 int histype;
13983 char_u *str;
13984 char_u buf[NUMBUFLEN];
13985#endif
13986
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013987 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013988 if (check_restricted() || check_secure())
13989 return;
13990#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013991 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13992 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013993 if (histype >= 0)
13994 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013995 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013996 if (*str != NUL)
13997 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013998 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013999 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014000 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014001 return;
14002 }
14003 }
14004#endif
14005}
14006
14007/*
14008 * "histdel()" function
14009 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014010 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014011f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014012{
14013#ifdef FEAT_CMDHIST
14014 int n;
14015 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014016 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014017
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014018 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14019 if (str == NULL)
14020 n = 0;
14021 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014022 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014023 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014024 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014025 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014026 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014027 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014028 else
14029 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014030 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014031 get_tv_string_buf(&argvars[1], buf));
14032 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014033#endif
14034}
14035
14036/*
14037 * "histget()" function
14038 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014039 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014040f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014041{
14042#ifdef FEAT_CMDHIST
14043 int type;
14044 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014045 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014046
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014047 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14048 if (str == NULL)
14049 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014050 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014051 {
14052 type = get_histtype(str);
14053 if (argvars[1].v_type == VAR_UNKNOWN)
14054 idx = get_history_idx(type);
14055 else
14056 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14057 /* -1 on type error */
14058 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014060#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014061 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014062#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014063 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014064}
14065
14066/*
14067 * "histnr()" function
14068 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014069 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014070f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014071{
14072 int i;
14073
14074#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014075 char_u *history = get_tv_string_chk(&argvars[0]);
14076
14077 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014078 if (i >= HIST_CMD && i < HIST_COUNT)
14079 i = get_history_idx(i);
14080 else
14081#endif
14082 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014083 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014084}
14085
14086/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014087 * "highlightID(name)" function
14088 */
14089 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014090f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014091{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014092 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014093}
14094
14095/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014096 * "highlight_exists()" function
14097 */
14098 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014099f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014100{
14101 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14102}
14103
14104/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014105 * "hostname()" function
14106 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014107 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014108f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014109{
14110 char_u hostname[256];
14111
14112 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014113 rettv->v_type = VAR_STRING;
14114 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014115}
14116
14117/*
14118 * iconv() function
14119 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014120 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014121f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014122{
14123#ifdef FEAT_MBYTE
14124 char_u buf1[NUMBUFLEN];
14125 char_u buf2[NUMBUFLEN];
14126 char_u *from, *to, *str;
14127 vimconv_T vimconv;
14128#endif
14129
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014130 rettv->v_type = VAR_STRING;
14131 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014132
14133#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014134 str = get_tv_string(&argvars[0]);
14135 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14136 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014137 vimconv.vc_type = CONV_NONE;
14138 convert_setup(&vimconv, from, to);
14139
14140 /* If the encodings are equal, no conversion needed. */
14141 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014142 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014143 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014144 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014145
14146 convert_setup(&vimconv, NULL, NULL);
14147 vim_free(from);
14148 vim_free(to);
14149#endif
14150}
14151
14152/*
14153 * "indent()" function
14154 */
14155 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014156f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014157{
14158 linenr_T lnum;
14159
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014160 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014161 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014162 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014163 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014164 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014165}
14166
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014167/*
14168 * "index()" function
14169 */
14170 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014171f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014172{
Bram Moolenaar33570922005-01-25 22:26:29 +000014173 list_T *l;
14174 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014175 long idx = 0;
14176 int ic = FALSE;
14177
14178 rettv->vval.v_number = -1;
14179 if (argvars[0].v_type != VAR_LIST)
14180 {
14181 EMSG(_(e_listreq));
14182 return;
14183 }
14184 l = argvars[0].vval.v_list;
14185 if (l != NULL)
14186 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014187 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014188 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014189 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014190 int error = FALSE;
14191
Bram Moolenaar758711c2005-02-02 23:11:38 +000014192 /* Start at specified item. Use the cached index that list_find()
14193 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014194 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014195 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014196 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014197 ic = get_tv_number_chk(&argvars[3], &error);
14198 if (error)
14199 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014200 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014201
Bram Moolenaar758711c2005-02-02 23:11:38 +000014202 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014203 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014204 {
14205 rettv->vval.v_number = idx;
14206 break;
14207 }
14208 }
14209}
14210
Bram Moolenaar071d4272004-06-13 20:20:40 +000014211static int inputsecret_flag = 0;
14212
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014213static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014214
Bram Moolenaar071d4272004-06-13 20:20:40 +000014215/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014216 * This function is used by f_input() and f_inputdialog() functions. The third
14217 * argument to f_input() specifies the type of completion to use at the
14218 * prompt. The third argument to f_inputdialog() specifies the value to return
14219 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014220 */
14221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014222get_user_input(
14223 typval_T *argvars,
14224 typval_T *rettv,
14225 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014226{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014227 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014228 char_u *p = NULL;
14229 int c;
14230 char_u buf[NUMBUFLEN];
14231 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014232 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014233 int xp_type = EXPAND_NOTHING;
14234 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014235
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014236 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014237 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014238
14239#ifdef NO_CONSOLE_INPUT
14240 /* While starting up, there is no place to enter text. */
14241 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014242 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014243#endif
14244
14245 cmd_silent = FALSE; /* Want to see the prompt. */
14246 if (prompt != NULL)
14247 {
14248 /* Only the part of the message after the last NL is considered as
14249 * prompt for the command line */
14250 p = vim_strrchr(prompt, '\n');
14251 if (p == NULL)
14252 p = prompt;
14253 else
14254 {
14255 ++p;
14256 c = *p;
14257 *p = NUL;
14258 msg_start();
14259 msg_clr_eos();
14260 msg_puts_attr(prompt, echo_attr);
14261 msg_didout = FALSE;
14262 msg_starthere();
14263 *p = c;
14264 }
14265 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014266
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014267 if (argvars[1].v_type != VAR_UNKNOWN)
14268 {
14269 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14270 if (defstr != NULL)
14271 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014272
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014273 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014274 {
14275 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014276 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014277 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014278
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014279 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014280 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014281
Bram Moolenaar4463f292005-09-25 22:20:24 +000014282 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14283 if (xp_name == NULL)
14284 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014285
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014286 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014287
Bram Moolenaar4463f292005-09-25 22:20:24 +000014288 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14289 &xp_arg) == FAIL)
14290 return;
14291 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014292 }
14293
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014294 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014295 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014296 int save_ex_normal_busy = ex_normal_busy;
14297 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014298 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014299 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14300 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014301 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014302 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014303 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014304 && argvars[1].v_type != VAR_UNKNOWN
14305 && argvars[2].v_type != VAR_UNKNOWN)
14306 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14307 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014308
14309 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014310
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014311 /* since the user typed this, no need to wait for return */
14312 need_wait_return = FALSE;
14313 msg_didout = FALSE;
14314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014315 cmd_silent = cmd_silent_save;
14316}
14317
14318/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014319 * "input()" function
14320 * Also handles inputsecret() when inputsecret is set.
14321 */
14322 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014323f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014324{
14325 get_user_input(argvars, rettv, FALSE);
14326}
14327
14328/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014329 * "inputdialog()" function
14330 */
14331 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014332f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014333{
14334#if defined(FEAT_GUI_TEXTDIALOG)
14335 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14336 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14337 {
14338 char_u *message;
14339 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014340 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014341
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014342 message = get_tv_string_chk(&argvars[0]);
14343 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014344 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014345 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014346 else
14347 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014348 if (message != NULL && defstr != NULL
14349 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014350 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014351 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014352 else
14353 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014354 if (message != NULL && defstr != NULL
14355 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014356 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014357 rettv->vval.v_string = vim_strsave(
14358 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014359 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014360 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014361 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014362 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014363 }
14364 else
14365#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014366 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014367}
14368
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014369/*
14370 * "inputlist()" function
14371 */
14372 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014373f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014374{
14375 listitem_T *li;
14376 int selected;
14377 int mouse_used;
14378
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014379#ifdef NO_CONSOLE_INPUT
14380 /* While starting up, there is no place to enter text. */
14381 if (no_console_input())
14382 return;
14383#endif
14384 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14385 {
14386 EMSG2(_(e_listarg), "inputlist()");
14387 return;
14388 }
14389
14390 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014391 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014392 lines_left = Rows; /* avoid more prompt */
14393 msg_scroll = TRUE;
14394 msg_clr_eos();
14395
14396 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14397 {
14398 msg_puts(get_tv_string(&li->li_tv));
14399 msg_putchar('\n');
14400 }
14401
14402 /* Ask for choice. */
14403 selected = prompt_for_number(&mouse_used);
14404 if (mouse_used)
14405 selected -= lines_left;
14406
14407 rettv->vval.v_number = selected;
14408}
14409
14410
Bram Moolenaar071d4272004-06-13 20:20:40 +000014411static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14412
14413/*
14414 * "inputrestore()" function
14415 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014416 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014417f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014418{
14419 if (ga_userinput.ga_len > 0)
14420 {
14421 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014422 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14423 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014424 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014425 }
14426 else if (p_verbose > 1)
14427 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014428 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014429 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014430 }
14431}
14432
14433/*
14434 * "inputsave()" function
14435 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014436 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014437f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014438{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014439 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014440 if (ga_grow(&ga_userinput, 1) == OK)
14441 {
14442 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14443 + ga_userinput.ga_len);
14444 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014445 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014446 }
14447 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014448 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014449}
14450
14451/*
14452 * "inputsecret()" function
14453 */
14454 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014455f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014456{
14457 ++cmdline_star;
14458 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014459 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014460 --cmdline_star;
14461 --inputsecret_flag;
14462}
14463
14464/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014465 * "insert()" function
14466 */
14467 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014468f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014469{
14470 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014471 listitem_T *item;
14472 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014473 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014474
14475 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014476 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014477 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014478 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014479 {
14480 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014481 before = get_tv_number_chk(&argvars[2], &error);
14482 if (error)
14483 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014484
Bram Moolenaar758711c2005-02-02 23:11:38 +000014485 if (before == l->lv_len)
14486 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014487 else
14488 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014489 item = list_find(l, before);
14490 if (item == NULL)
14491 {
14492 EMSGN(_(e_listidx), before);
14493 l = NULL;
14494 }
14495 }
14496 if (l != NULL)
14497 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014498 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014499 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014500 }
14501 }
14502}
14503
14504/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014505 * "invert(expr)" function
14506 */
14507 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014508f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014509{
14510 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14511}
14512
14513/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014514 * "isdirectory()" function
14515 */
14516 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014517f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014518{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014519 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014520}
14521
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014522/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014523 * "islocked()" function
14524 */
14525 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014526f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014527{
14528 lval_T lv;
14529 char_u *end;
14530 dictitem_T *di;
14531
14532 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014533 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14534 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014535 if (end != NULL && lv.ll_name != NULL)
14536 {
14537 if (*end != NUL)
14538 EMSG(_(e_trailing));
14539 else
14540 {
14541 if (lv.ll_tv == NULL)
14542 {
14543 if (check_changedtick(lv.ll_name))
14544 rettv->vval.v_number = 1; /* always locked */
14545 else
14546 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014547 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014548 if (di != NULL)
14549 {
14550 /* Consider a variable locked when:
14551 * 1. the variable itself is locked
14552 * 2. the value of the variable is locked.
14553 * 3. the List or Dict value is locked.
14554 */
14555 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14556 || tv_islocked(&di->di_tv));
14557 }
14558 }
14559 }
14560 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014561 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014562 else if (lv.ll_newkey != NULL)
14563 EMSG2(_(e_dictkey), lv.ll_newkey);
14564 else if (lv.ll_list != NULL)
14565 /* List item. */
14566 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14567 else
14568 /* Dictionary item. */
14569 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14570 }
14571 }
14572
14573 clear_lval(&lv);
14574}
14575
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014576#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14577/*
14578 * "isnan()" function
14579 */
14580 static void
14581f_isnan(typval_T *argvars, typval_T *rettv)
14582{
14583 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14584 && isnan(argvars[0].vval.v_float);
14585}
14586#endif
14587
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014588static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014589
14590/*
14591 * Turn a dict into a list:
14592 * "what" == 0: list of keys
14593 * "what" == 1: list of values
14594 * "what" == 2: list of items
14595 */
14596 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014597dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014598{
Bram Moolenaar33570922005-01-25 22:26:29 +000014599 list_T *l2;
14600 dictitem_T *di;
14601 hashitem_T *hi;
14602 listitem_T *li;
14603 listitem_T *li2;
14604 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014605 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014606
Bram Moolenaar8c711452005-01-14 21:53:12 +000014607 if (argvars[0].v_type != VAR_DICT)
14608 {
14609 EMSG(_(e_dictreq));
14610 return;
14611 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014612 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014613 return;
14614
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014615 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014616 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014617
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014618 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014619 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014620 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014621 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014622 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014623 --todo;
14624 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014625
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014626 li = listitem_alloc();
14627 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014628 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014629 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014630
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014631 if (what == 0)
14632 {
14633 /* keys() */
14634 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014635 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014636 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14637 }
14638 else if (what == 1)
14639 {
14640 /* values() */
14641 copy_tv(&di->di_tv, &li->li_tv);
14642 }
14643 else
14644 {
14645 /* items() */
14646 l2 = list_alloc();
14647 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014648 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014649 li->li_tv.vval.v_list = l2;
14650 if (l2 == NULL)
14651 break;
14652 ++l2->lv_refcount;
14653
14654 li2 = listitem_alloc();
14655 if (li2 == NULL)
14656 break;
14657 list_append(l2, li2);
14658 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014659 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014660 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14661
14662 li2 = listitem_alloc();
14663 if (li2 == NULL)
14664 break;
14665 list_append(l2, li2);
14666 copy_tv(&di->di_tv, &li2->li_tv);
14667 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014668 }
14669 }
14670}
14671
14672/*
14673 * "items(dict)" function
14674 */
14675 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014676f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014677{
14678 dict_list(argvars, rettv, 2);
14679}
14680
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014681#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014682/*
14683 * Get the job from the argument.
14684 * Returns NULL if the job is invalid.
14685 */
14686 static job_T *
14687get_job_arg(typval_T *tv)
14688{
14689 job_T *job;
14690
14691 if (tv->v_type != VAR_JOB)
14692 {
14693 EMSG2(_(e_invarg2), get_tv_string(tv));
14694 return NULL;
14695 }
14696 job = tv->vval.v_job;
14697
14698 if (job == NULL)
14699 EMSG(_("E916: not a valid job"));
14700 return job;
14701}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014702
Bram Moolenaar835dc632016-02-07 14:27:38 +010014703/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014704 * "job_getchannel()" function
14705 */
14706 static void
14707f_job_getchannel(typval_T *argvars, typval_T *rettv)
14708{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014709 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014710
Bram Moolenaar65edff82016-02-21 16:40:11 +010014711 if (job != NULL)
14712 {
Bram Moolenaar77073442016-02-13 23:23:53 +010014713 rettv->v_type = VAR_CHANNEL;
14714 rettv->vval.v_channel = job->jv_channel;
14715 if (job->jv_channel != NULL)
14716 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014717 }
14718}
14719
14720/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010014721 * "job_info()" function
14722 */
14723 static void
14724f_job_info(typval_T *argvars, typval_T *rettv)
14725{
14726 job_T *job = get_job_arg(&argvars[0]);
14727
14728 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
14729 job_info(job, rettv->vval.v_dict);
14730}
14731
14732/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010014733 * "job_setoptions()" function
14734 */
14735 static void
14736f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
14737{
14738 job_T *job = get_job_arg(&argvars[0]);
14739 jobopt_T opt;
14740
14741 if (job == NULL)
14742 return;
14743 clear_job_options(&opt);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014744 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014745 return;
14746 job_set_options(job, &opt);
14747}
14748
14749/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014750 * "job_start()" function
14751 */
14752 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010014753f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014754{
Bram Moolenaar835dc632016-02-07 14:27:38 +010014755 rettv->v_type = VAR_JOB;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014756 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014757}
14758
14759/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014760 * "job_status()" function
14761 */
14762 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014763f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014764{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014765 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014766
Bram Moolenaar65edff82016-02-21 16:40:11 +010014767 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014768 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010014769 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010014770 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010014771 }
14772}
14773
14774/*
14775 * "job_stop()" function
14776 */
14777 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014778f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014779{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014780 job_T *job = get_job_arg(&argvars[0]);
14781
14782 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014783 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014784}
14785#endif
14786
Bram Moolenaar071d4272004-06-13 20:20:40 +000014787/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014788 * "join()" function
14789 */
14790 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014791f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014792{
14793 garray_T ga;
14794 char_u *sep;
14795
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014796 if (argvars[0].v_type != VAR_LIST)
14797 {
14798 EMSG(_(e_listreq));
14799 return;
14800 }
14801 if (argvars[0].vval.v_list == NULL)
14802 return;
14803 if (argvars[1].v_type == VAR_UNKNOWN)
14804 sep = (char_u *)" ";
14805 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014806 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014807
14808 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014809
14810 if (sep != NULL)
14811 {
14812 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014813 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014814 ga_append(&ga, NUL);
14815 rettv->vval.v_string = (char_u *)ga.ga_data;
14816 }
14817 else
14818 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014819}
14820
14821/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014822 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014823 */
14824 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014825f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014826{
14827 js_read_T reader;
14828
14829 reader.js_buf = get_tv_string(&argvars[0]);
14830 reader.js_fill = NULL;
14831 reader.js_used = 0;
14832 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
14833 EMSG(_(e_invarg));
14834}
14835
14836/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014837 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014838 */
14839 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014840f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014841{
14842 rettv->v_type = VAR_STRING;
14843 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
14844}
14845
14846/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014847 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014848 */
14849 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014850f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014851{
14852 js_read_T reader;
14853
14854 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010014855 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014856 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014857 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014858 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014859}
14860
14861/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014862 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014863 */
14864 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014865f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014866{
14867 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014868 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014869}
14870
14871/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014872 * "keys()" function
14873 */
14874 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014875f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014876{
14877 dict_list(argvars, rettv, 0);
14878}
14879
14880/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014881 * "last_buffer_nr()" function.
14882 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014884f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014885{
14886 int n = 0;
14887 buf_T *buf;
14888
14889 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14890 if (n < buf->b_fnum)
14891 n = buf->b_fnum;
14892
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014893 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014894}
14895
14896/*
14897 * "len()" function
14898 */
14899 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014900f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014901{
14902 switch (argvars[0].v_type)
14903 {
14904 case VAR_STRING:
14905 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014906 rettv->vval.v_number = (varnumber_T)STRLEN(
14907 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014908 break;
14909 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014910 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014911 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014912 case VAR_DICT:
14913 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14914 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010014915 case VAR_UNKNOWN:
14916 case VAR_SPECIAL:
14917 case VAR_FLOAT:
14918 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010014919 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010014920 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010014921 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014922 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014923 break;
14924 }
14925}
14926
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014927static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014928
14929 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014930libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014931{
14932#ifdef FEAT_LIBCALL
14933 char_u *string_in;
14934 char_u **string_result;
14935 int nr_result;
14936#endif
14937
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014938 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014939 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014940 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014941
14942 if (check_restricted() || check_secure())
14943 return;
14944
14945#ifdef FEAT_LIBCALL
14946 /* The first two args must be strings, otherwise its meaningless */
14947 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14948 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014949 string_in = NULL;
14950 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014951 string_in = argvars[2].vval.v_string;
14952 if (type == VAR_NUMBER)
14953 string_result = NULL;
14954 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014955 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014956 if (mch_libcall(argvars[0].vval.v_string,
14957 argvars[1].vval.v_string,
14958 string_in,
14959 argvars[2].vval.v_number,
14960 string_result,
14961 &nr_result) == OK
14962 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014963 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014964 }
14965#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014966}
14967
14968/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014969 * "libcall()" function
14970 */
14971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014972f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014973{
14974 libcall_common(argvars, rettv, VAR_STRING);
14975}
14976
14977/*
14978 * "libcallnr()" function
14979 */
14980 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014981f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014982{
14983 libcall_common(argvars, rettv, VAR_NUMBER);
14984}
14985
14986/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014987 * "line(string)" function
14988 */
14989 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014990f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014991{
14992 linenr_T lnum = 0;
14993 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014994 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014995
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014996 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014997 if (fp != NULL)
14998 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014999 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015000}
15001
15002/*
15003 * "line2byte(lnum)" function
15004 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015005 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015006f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015007{
15008#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015009 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015010#else
15011 linenr_T lnum;
15012
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015013 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015014 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015015 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015016 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015017 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15018 if (rettv->vval.v_number >= 0)
15019 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015020#endif
15021}
15022
15023/*
15024 * "lispindent(lnum)" function
15025 */
15026 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015027f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015028{
15029#ifdef FEAT_LISP
15030 pos_T pos;
15031 linenr_T lnum;
15032
15033 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015034 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015035 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15036 {
15037 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015038 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015039 curwin->w_cursor = pos;
15040 }
15041 else
15042#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015043 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015044}
15045
15046/*
15047 * "localtime()" function
15048 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015049 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015050f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015051{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015052 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015053}
15054
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015055static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015056
15057 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015058get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015059{
15060 char_u *keys;
15061 char_u *which;
15062 char_u buf[NUMBUFLEN];
15063 char_u *keys_buf = NULL;
15064 char_u *rhs;
15065 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015066 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015067 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015068 mapblock_T *mp;
15069 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015070
15071 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015072 rettv->v_type = VAR_STRING;
15073 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015074
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015075 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015076 if (*keys == NUL)
15077 return;
15078
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015079 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015080 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015081 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015082 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015083 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015084 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015085 if (argvars[3].v_type != VAR_UNKNOWN)
15086 get_dict = get_tv_number(&argvars[3]);
15087 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015089 else
15090 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015091 if (which == NULL)
15092 return;
15093
Bram Moolenaar071d4272004-06-13 20:20:40 +000015094 mode = get_map_mode(&which, 0);
15095
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015096 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015097 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015098 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015099
15100 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015101 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015102 /* Return a string. */
15103 if (rhs != NULL)
15104 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015105
Bram Moolenaarbd743252010-10-20 21:23:33 +020015106 }
15107 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15108 {
15109 /* Return a dictionary. */
15110 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15111 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15112 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015113
Bram Moolenaarbd743252010-10-20 21:23:33 +020015114 dict_add_nr_str(dict, "lhs", 0L, lhs);
15115 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15116 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15117 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15118 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15119 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15120 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015121 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015122 dict_add_nr_str(dict, "mode", 0L, mapmode);
15123
15124 vim_free(lhs);
15125 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015126 }
15127}
15128
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015129#ifdef FEAT_FLOAT
15130/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015131 * "log()" function
15132 */
15133 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015134f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015135{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015136 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015137
15138 rettv->v_type = VAR_FLOAT;
15139 if (get_float_arg(argvars, &f) == OK)
15140 rettv->vval.v_float = log(f);
15141 else
15142 rettv->vval.v_float = 0.0;
15143}
15144
15145/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015146 * "log10()" function
15147 */
15148 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015149f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015150{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015151 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015152
15153 rettv->v_type = VAR_FLOAT;
15154 if (get_float_arg(argvars, &f) == OK)
15155 rettv->vval.v_float = log10(f);
15156 else
15157 rettv->vval.v_float = 0.0;
15158}
15159#endif
15160
Bram Moolenaar1dced572012-04-05 16:54:08 +020015161#ifdef FEAT_LUA
15162/*
15163 * "luaeval()" function
15164 */
15165 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015166f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015167{
15168 char_u *str;
15169 char_u buf[NUMBUFLEN];
15170
15171 str = get_tv_string_buf(&argvars[0], buf);
15172 do_luaeval(str, argvars + 1, rettv);
15173}
15174#endif
15175
Bram Moolenaar071d4272004-06-13 20:20:40 +000015176/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015177 * "map()" function
15178 */
15179 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015180f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015181{
15182 filter_map(argvars, rettv, TRUE);
15183}
15184
15185/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015186 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015187 */
15188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015189f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015190{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015191 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015192}
15193
15194/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015195 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015196 */
15197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015198f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015199{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015200 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015201}
15202
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015203static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015204
15205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015206find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015207{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015208 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015209 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015210 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015211 char_u *pat;
15212 regmatch_T regmatch;
15213 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015214 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015215 char_u *save_cpo;
15216 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015217 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015218 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015219 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015220 list_T *l = NULL;
15221 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015222 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015223 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015224
15225 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15226 save_cpo = p_cpo;
15227 p_cpo = (char_u *)"";
15228
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015229 rettv->vval.v_number = -1;
15230 if (type == 3)
15231 {
15232 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015233 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015234 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015235 }
15236 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015237 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015238 rettv->v_type = VAR_STRING;
15239 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015241
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015242 if (argvars[0].v_type == VAR_LIST)
15243 {
15244 if ((l = argvars[0].vval.v_list) == NULL)
15245 goto theend;
15246 li = l->lv_first;
15247 }
15248 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015249 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015250 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015251 len = (long)STRLEN(str);
15252 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015253
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015254 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15255 if (pat == NULL)
15256 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015257
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015258 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015259 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015260 int error = FALSE;
15261
15262 start = get_tv_number_chk(&argvars[2], &error);
15263 if (error)
15264 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015265 if (l != NULL)
15266 {
15267 li = list_find(l, start);
15268 if (li == NULL)
15269 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015270 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015271 }
15272 else
15273 {
15274 if (start < 0)
15275 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015276 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015277 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015278 /* When "count" argument is there ignore matches before "start",
15279 * otherwise skip part of the string. Differs when pattern is "^"
15280 * or "\<". */
15281 if (argvars[3].v_type != VAR_UNKNOWN)
15282 startcol = start;
15283 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015284 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015285 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015286 len -= start;
15287 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015288 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015289
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015290 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015291 nth = get_tv_number_chk(&argvars[3], &error);
15292 if (error)
15293 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015294 }
15295
15296 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15297 if (regmatch.regprog != NULL)
15298 {
15299 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015300
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015301 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015302 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015303 if (l != NULL)
15304 {
15305 if (li == NULL)
15306 {
15307 match = FALSE;
15308 break;
15309 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015310 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015311 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015312 if (str == NULL)
15313 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015314 }
15315
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015316 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015317
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015318 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015319 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015320 if (l == NULL && !match)
15321 break;
15322
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015323 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015324 if (l != NULL)
15325 {
15326 li = li->li_next;
15327 ++idx;
15328 }
15329 else
15330 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015331#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015332 startcol = (colnr_T)(regmatch.startp[0]
15333 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015334#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015335 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015336#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015337 if (startcol > (colnr_T)len
15338 || str + startcol <= regmatch.startp[0])
15339 {
15340 match = FALSE;
15341 break;
15342 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015343 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015344 }
15345
15346 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015347 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015348 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015349 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015350 int i;
15351
15352 /* return list with matched string and submatches */
15353 for (i = 0; i < NSUBEXP; ++i)
15354 {
15355 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015356 {
15357 if (list_append_string(rettv->vval.v_list,
15358 (char_u *)"", 0) == FAIL)
15359 break;
15360 }
15361 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015362 regmatch.startp[i],
15363 (int)(regmatch.endp[i] - regmatch.startp[i]))
15364 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015365 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015366 }
15367 }
15368 else if (type == 2)
15369 {
15370 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015371 if (l != NULL)
15372 copy_tv(&li->li_tv, rettv);
15373 else
15374 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015375 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015376 }
15377 else if (l != NULL)
15378 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015379 else
15380 {
15381 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015382 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015383 (varnumber_T)(regmatch.startp[0] - str);
15384 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015385 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015386 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015387 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015388 }
15389 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015390 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015391 }
15392
15393theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015394 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015395 p_cpo = save_cpo;
15396}
15397
15398/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015399 * "match()" function
15400 */
15401 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015402f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015403{
15404 find_some_match(argvars, rettv, 1);
15405}
15406
15407/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015408 * "matchadd()" function
15409 */
15410 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015411f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015412{
15413#ifdef FEAT_SEARCH_EXTRA
15414 char_u buf[NUMBUFLEN];
15415 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15416 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15417 int prio = 10; /* default priority */
15418 int id = -1;
15419 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015420 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015421
15422 rettv->vval.v_number = -1;
15423
15424 if (grp == NULL || pat == NULL)
15425 return;
15426 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015427 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015428 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015429 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015430 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015431 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015432 if (argvars[4].v_type != VAR_UNKNOWN)
15433 {
15434 if (argvars[4].v_type != VAR_DICT)
15435 {
15436 EMSG(_(e_dictreq));
15437 return;
15438 }
15439 if (dict_find(argvars[4].vval.v_dict,
15440 (char_u *)"conceal", -1) != NULL)
15441 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15442 (char_u *)"conceal", FALSE);
15443 }
15444 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015445 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015446 if (error == TRUE)
15447 return;
15448 if (id >= 1 && id <= 3)
15449 {
15450 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15451 return;
15452 }
15453
Bram Moolenaar6561d522015-07-21 15:48:27 +020015454 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15455 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015456#endif
15457}
15458
15459/*
15460 * "matchaddpos()" function
15461 */
15462 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015463f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015464{
15465#ifdef FEAT_SEARCH_EXTRA
15466 char_u buf[NUMBUFLEN];
15467 char_u *group;
15468 int prio = 10;
15469 int id = -1;
15470 int error = FALSE;
15471 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015472 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015473
15474 rettv->vval.v_number = -1;
15475
15476 group = get_tv_string_buf_chk(&argvars[0], buf);
15477 if (group == NULL)
15478 return;
15479
15480 if (argvars[1].v_type != VAR_LIST)
15481 {
15482 EMSG2(_(e_listarg), "matchaddpos()");
15483 return;
15484 }
15485 l = argvars[1].vval.v_list;
15486 if (l == NULL)
15487 return;
15488
15489 if (argvars[2].v_type != VAR_UNKNOWN)
15490 {
15491 prio = get_tv_number_chk(&argvars[2], &error);
15492 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015493 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015494 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015495 if (argvars[4].v_type != VAR_UNKNOWN)
15496 {
15497 if (argvars[4].v_type != VAR_DICT)
15498 {
15499 EMSG(_(e_dictreq));
15500 return;
15501 }
15502 if (dict_find(argvars[4].vval.v_dict,
15503 (char_u *)"conceal", -1) != NULL)
15504 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15505 (char_u *)"conceal", FALSE);
15506 }
15507 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015508 }
15509 if (error == TRUE)
15510 return;
15511
15512 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15513 if (id == 1 || id == 2)
15514 {
15515 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15516 return;
15517 }
15518
Bram Moolenaar6561d522015-07-21 15:48:27 +020015519 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15520 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015521#endif
15522}
15523
15524/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015525 * "matcharg()" function
15526 */
15527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015528f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015529{
15530 if (rettv_list_alloc(rettv) == OK)
15531 {
15532#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015533 int id = get_tv_number(&argvars[0]);
15534 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015535
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015536 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015537 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015538 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15539 {
15540 list_append_string(rettv->vval.v_list,
15541 syn_id2name(m->hlg_id), -1);
15542 list_append_string(rettv->vval.v_list, m->pattern, -1);
15543 }
15544 else
15545 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015546 list_append_string(rettv->vval.v_list, NULL, -1);
15547 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015548 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015549 }
15550#endif
15551 }
15552}
15553
15554/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015555 * "matchdelete()" function
15556 */
15557 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015558f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015559{
15560#ifdef FEAT_SEARCH_EXTRA
15561 rettv->vval.v_number = match_delete(curwin,
15562 (int)get_tv_number(&argvars[0]), TRUE);
15563#endif
15564}
15565
15566/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015567 * "matchend()" function
15568 */
15569 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015570f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015571{
15572 find_some_match(argvars, rettv, 0);
15573}
15574
15575/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015576 * "matchlist()" function
15577 */
15578 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015579f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015580{
15581 find_some_match(argvars, rettv, 3);
15582}
15583
15584/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015585 * "matchstr()" function
15586 */
15587 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015588f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015589{
15590 find_some_match(argvars, rettv, 2);
15591}
15592
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015593static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015594
15595 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015596max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015597{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015598 long n = 0;
15599 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015600 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015601
15602 if (argvars[0].v_type == VAR_LIST)
15603 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015604 list_T *l;
15605 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015606
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015607 l = argvars[0].vval.v_list;
15608 if (l != NULL)
15609 {
15610 li = l->lv_first;
15611 if (li != NULL)
15612 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015613 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015614 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015615 {
15616 li = li->li_next;
15617 if (li == NULL)
15618 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015619 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015620 if (domax ? i > n : i < n)
15621 n = i;
15622 }
15623 }
15624 }
15625 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015626 else if (argvars[0].v_type == VAR_DICT)
15627 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015628 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015629 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015630 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015631 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015632
15633 d = argvars[0].vval.v_dict;
15634 if (d != NULL)
15635 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015636 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015637 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015638 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015639 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015640 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015641 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015642 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015643 if (first)
15644 {
15645 n = i;
15646 first = FALSE;
15647 }
15648 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015649 n = i;
15650 }
15651 }
15652 }
15653 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015654 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015655 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015656 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015657}
15658
15659/*
15660 * "max()" function
15661 */
15662 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015663f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015664{
15665 max_min(argvars, rettv, TRUE);
15666}
15667
15668/*
15669 * "min()" function
15670 */
15671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015672f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015673{
15674 max_min(argvars, rettv, FALSE);
15675}
15676
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015677static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015678
15679/*
15680 * Create the directory in which "dir" is located, and higher levels when
15681 * needed.
15682 */
15683 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015684mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015685{
15686 char_u *p;
15687 char_u *updir;
15688 int r = FAIL;
15689
15690 /* Get end of directory name in "dir".
15691 * We're done when it's "/" or "c:/". */
15692 p = gettail_sep(dir);
15693 if (p <= get_past_head(dir))
15694 return OK;
15695
15696 /* If the directory exists we're done. Otherwise: create it.*/
15697 updir = vim_strnsave(dir, (int)(p - dir));
15698 if (updir == NULL)
15699 return FAIL;
15700 if (mch_isdir(updir))
15701 r = OK;
15702 else if (mkdir_recurse(updir, prot) == OK)
15703 r = vim_mkdir_emsg(updir, prot);
15704 vim_free(updir);
15705 return r;
15706}
15707
15708#ifdef vim_mkdir
15709/*
15710 * "mkdir()" function
15711 */
15712 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015713f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015714{
15715 char_u *dir;
15716 char_u buf[NUMBUFLEN];
15717 int prot = 0755;
15718
15719 rettv->vval.v_number = FAIL;
15720 if (check_restricted() || check_secure())
15721 return;
15722
15723 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015724 if (*dir == NUL)
15725 rettv->vval.v_number = FAIL;
15726 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015727 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015728 if (*gettail(dir) == NUL)
15729 /* remove trailing slashes */
15730 *gettail_sep(dir) = NUL;
15731
15732 if (argvars[1].v_type != VAR_UNKNOWN)
15733 {
15734 if (argvars[2].v_type != VAR_UNKNOWN)
15735 prot = get_tv_number_chk(&argvars[2], NULL);
15736 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15737 mkdir_recurse(dir, prot);
15738 }
15739 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015740 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015741}
15742#endif
15743
Bram Moolenaar0d660222005-01-07 21:51:51 +000015744/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015745 * "mode()" function
15746 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015748f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015749{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015750 char_u buf[3];
15751
15752 buf[1] = NUL;
15753 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015754
Bram Moolenaar071d4272004-06-13 20:20:40 +000015755 if (VIsual_active)
15756 {
15757 if (VIsual_select)
15758 buf[0] = VIsual_mode + 's' - 'v';
15759 else
15760 buf[0] = VIsual_mode;
15761 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015762 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015763 || State == CONFIRM)
15764 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015765 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015766 if (State == ASKMORE)
15767 buf[1] = 'm';
15768 else if (State == CONFIRM)
15769 buf[1] = '?';
15770 }
15771 else if (State == EXTERNCMD)
15772 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015773 else if (State & INSERT)
15774 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015775#ifdef FEAT_VREPLACE
15776 if (State & VREPLACE_FLAG)
15777 {
15778 buf[0] = 'R';
15779 buf[1] = 'v';
15780 }
15781 else
15782#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015783 if (State & REPLACE_FLAG)
15784 buf[0] = 'R';
15785 else
15786 buf[0] = 'i';
15787 }
15788 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015789 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015790 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015791 if (exmode_active)
15792 buf[1] = 'v';
15793 }
15794 else if (exmode_active)
15795 {
15796 buf[0] = 'c';
15797 buf[1] = 'e';
15798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015799 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015800 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015801 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015802 if (finish_op)
15803 buf[1] = 'o';
15804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015805
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015806 /* Clear out the minor mode when the argument is not a non-zero number or
15807 * non-empty string. */
15808 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015809 buf[1] = NUL;
15810
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015811 rettv->vval.v_string = vim_strsave(buf);
15812 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015813}
15814
Bram Moolenaar429fa852013-04-15 12:27:36 +020015815#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015816/*
15817 * "mzeval()" function
15818 */
15819 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015820f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015821{
15822 char_u *str;
15823 char_u buf[NUMBUFLEN];
15824
15825 str = get_tv_string_buf(&argvars[0], buf);
15826 do_mzeval(str, rettv);
15827}
Bram Moolenaar75676462013-01-30 14:55:42 +010015828
15829 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015830mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015831{
15832 typval_T argvars[3];
15833
15834 argvars[0].v_type = VAR_STRING;
15835 argvars[0].vval.v_string = name;
15836 copy_tv(args, &argvars[1]);
15837 argvars[2].v_type = VAR_UNKNOWN;
15838 f_call(argvars, rettv);
15839 clear_tv(&argvars[1]);
15840}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015841#endif
15842
Bram Moolenaar071d4272004-06-13 20:20:40 +000015843/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015844 * "nextnonblank()" function
15845 */
15846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015847f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015848{
15849 linenr_T lnum;
15850
15851 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15852 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015853 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015854 {
15855 lnum = 0;
15856 break;
15857 }
15858 if (*skipwhite(ml_get(lnum)) != NUL)
15859 break;
15860 }
15861 rettv->vval.v_number = lnum;
15862}
15863
15864/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015865 * "nr2char()" function
15866 */
15867 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015868f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015869{
15870 char_u buf[NUMBUFLEN];
15871
15872#ifdef FEAT_MBYTE
15873 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015874 {
15875 int utf8 = 0;
15876
15877 if (argvars[1].v_type != VAR_UNKNOWN)
15878 utf8 = get_tv_number_chk(&argvars[1], NULL);
15879 if (utf8)
15880 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15881 else
15882 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015884 else
15885#endif
15886 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015887 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015888 buf[1] = NUL;
15889 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015890 rettv->v_type = VAR_STRING;
15891 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015892}
15893
15894/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015895 * "or(expr, expr)" function
15896 */
15897 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015898f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015899{
15900 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15901 | get_tv_number_chk(&argvars[1], NULL);
15902}
15903
15904/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015905 * "pathshorten()" function
15906 */
15907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015908f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015909{
15910 char_u *p;
15911
15912 rettv->v_type = VAR_STRING;
15913 p = get_tv_string_chk(&argvars[0]);
15914 if (p == NULL)
15915 rettv->vval.v_string = NULL;
15916 else
15917 {
15918 p = vim_strsave(p);
15919 rettv->vval.v_string = p;
15920 if (p != NULL)
15921 shorten_dir(p);
15922 }
15923}
15924
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015925#ifdef FEAT_PERL
15926/*
15927 * "perleval()" function
15928 */
15929 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015930f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015931{
15932 char_u *str;
15933 char_u buf[NUMBUFLEN];
15934
15935 str = get_tv_string_buf(&argvars[0], buf);
15936 do_perleval(str, rettv);
15937}
15938#endif
15939
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015940#ifdef FEAT_FLOAT
15941/*
15942 * "pow()" function
15943 */
15944 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015945f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015946{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015947 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015948
15949 rettv->v_type = VAR_FLOAT;
15950 if (get_float_arg(argvars, &fx) == OK
15951 && get_float_arg(&argvars[1], &fy) == OK)
15952 rettv->vval.v_float = pow(fx, fy);
15953 else
15954 rettv->vval.v_float = 0.0;
15955}
15956#endif
15957
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015958/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015959 * "prevnonblank()" function
15960 */
15961 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015962f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015963{
15964 linenr_T lnum;
15965
15966 lnum = get_tv_lnum(argvars);
15967 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15968 lnum = 0;
15969 else
15970 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15971 --lnum;
15972 rettv->vval.v_number = lnum;
15973}
15974
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015975/* This dummy va_list is here because:
15976 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15977 * - locally in the function results in a "used before set" warning
15978 * - using va_start() to initialize it gives "function with fixed args" error */
15979static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015980
Bram Moolenaar8c711452005-01-14 21:53:12 +000015981/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015982 * "printf()" function
15983 */
15984 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015985f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015986{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015987 char_u buf[NUMBUFLEN];
15988 int len;
15989 char_u *s;
15990 int saved_did_emsg = did_emsg;
15991 char *fmt;
15992
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015993 rettv->v_type = VAR_STRING;
15994 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015995
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015996 /* Get the required length, allocate the buffer and do it for real. */
15997 did_emsg = FALSE;
15998 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
15999 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16000 if (!did_emsg)
16001 {
16002 s = alloc(len + 1);
16003 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016004 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016005 rettv->vval.v_string = s;
16006 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016007 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016008 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016009 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016010}
16011
16012/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016013 * "pumvisible()" function
16014 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016015 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016016f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016017{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016018#ifdef FEAT_INS_EXPAND
16019 if (pum_visible())
16020 rettv->vval.v_number = 1;
16021#endif
16022}
16023
Bram Moolenaardb913952012-06-29 12:54:53 +020016024#ifdef FEAT_PYTHON3
16025/*
16026 * "py3eval()" function
16027 */
16028 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016029f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016030{
16031 char_u *str;
16032 char_u buf[NUMBUFLEN];
16033
16034 str = get_tv_string_buf(&argvars[0], buf);
16035 do_py3eval(str, rettv);
16036}
16037#endif
16038
16039#ifdef FEAT_PYTHON
16040/*
16041 * "pyeval()" function
16042 */
16043 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016044f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016045{
16046 char_u *str;
16047 char_u buf[NUMBUFLEN];
16048
16049 str = get_tv_string_buf(&argvars[0], buf);
16050 do_pyeval(str, rettv);
16051}
16052#endif
16053
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016054/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016055 * "range()" function
16056 */
16057 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016058f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016059{
16060 long start;
16061 long end;
16062 long stride = 1;
16063 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016064 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016065
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016066 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016067 if (argvars[1].v_type == VAR_UNKNOWN)
16068 {
16069 end = start - 1;
16070 start = 0;
16071 }
16072 else
16073 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016074 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016075 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016076 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016077 }
16078
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016079 if (error)
16080 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016081 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016082 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016083 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016084 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016085 else
16086 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016087 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016088 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016089 if (list_append_number(rettv->vval.v_list,
16090 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016091 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016092 }
16093}
16094
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016095/*
16096 * "readfile()" function
16097 */
16098 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016099f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016100{
16101 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016102 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016103 char_u *fname;
16104 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016105 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16106 int io_size = sizeof(buf);
16107 int readlen; /* size of last fread() */
16108 char_u *prev = NULL; /* previously read bytes, if any */
16109 long prevlen = 0; /* length of data in prev */
16110 long prevsize = 0; /* size of prev buffer */
16111 long maxline = MAXLNUM;
16112 long cnt = 0;
16113 char_u *p; /* position in buf */
16114 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016115
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016116 if (argvars[1].v_type != VAR_UNKNOWN)
16117 {
16118 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16119 binary = TRUE;
16120 if (argvars[2].v_type != VAR_UNKNOWN)
16121 maxline = get_tv_number(&argvars[2]);
16122 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016123
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016124 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016125 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016126
16127 /* Always open the file in binary mode, library functions have a mind of
16128 * their own about CR-LF conversion. */
16129 fname = get_tv_string(&argvars[0]);
16130 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16131 {
16132 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16133 return;
16134 }
16135
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016136 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016137 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016138 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016139
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016140 /* This for loop processes what was read, but is also entered at end
16141 * of file so that either:
16142 * - an incomplete line gets written
16143 * - a "binary" file gets an empty line at the end if it ends in a
16144 * newline. */
16145 for (p = buf, start = buf;
16146 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16147 ++p)
16148 {
16149 if (*p == '\n' || readlen <= 0)
16150 {
16151 listitem_T *li;
16152 char_u *s = NULL;
16153 long_u len = p - start;
16154
16155 /* Finished a line. Remove CRs before NL. */
16156 if (readlen > 0 && !binary)
16157 {
16158 while (len > 0 && start[len - 1] == '\r')
16159 --len;
16160 /* removal may cross back to the "prev" string */
16161 if (len == 0)
16162 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16163 --prevlen;
16164 }
16165 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016166 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016167 else
16168 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016169 /* Change "prev" buffer to be the right size. This way
16170 * the bytes are only copied once, and very long lines are
16171 * allocated only once. */
16172 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016173 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016174 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016175 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016176 prev = NULL; /* the list will own the string */
16177 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016178 }
16179 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016180 if (s == NULL)
16181 {
16182 do_outofmem_msg((long_u) prevlen + len + 1);
16183 failed = TRUE;
16184 break;
16185 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016186
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016187 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016188 {
16189 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016190 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016191 break;
16192 }
16193 li->li_tv.v_type = VAR_STRING;
16194 li->li_tv.v_lock = 0;
16195 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016196 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016197
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016198 start = p + 1; /* step over newline */
16199 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016200 break;
16201 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016202 else if (*p == NUL)
16203 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016204#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016205 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16206 * when finding the BF and check the previous two bytes. */
16207 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016208 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016209 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16210 * + 1, these may be in the "prev" string. */
16211 char_u back1 = p >= buf + 1 ? p[-1]
16212 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16213 char_u back2 = p >= buf + 2 ? p[-2]
16214 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16215 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016216
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016217 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016218 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016219 char_u *dest = p - 2;
16220
16221 /* Usually a BOM is at the beginning of a file, and so at
16222 * the beginning of a line; then we can just step over it.
16223 */
16224 if (start == dest)
16225 start = p + 1;
16226 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016227 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016228 /* have to shuffle buf to close gap */
16229 int adjust_prevlen = 0;
16230
16231 if (dest < buf)
16232 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016233 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016234 dest = buf;
16235 }
16236 if (readlen > p - buf + 1)
16237 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16238 readlen -= 3 - adjust_prevlen;
16239 prevlen -= adjust_prevlen;
16240 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016241 }
16242 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016243 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016244#endif
16245 } /* for */
16246
16247 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16248 break;
16249 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016250 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016251 /* There's part of a line in buf, store it in "prev". */
16252 if (p - start + prevlen >= prevsize)
16253 {
16254 /* need bigger "prev" buffer */
16255 char_u *newprev;
16256
16257 /* A common use case is ordinary text files and "prev" gets a
16258 * fragment of a line, so the first allocation is made
16259 * small, to avoid repeatedly 'allocing' large and
16260 * 'reallocing' small. */
16261 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016262 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016263 else
16264 {
16265 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016266 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016267 prevsize = grow50pc > growmin ? grow50pc : growmin;
16268 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016269 newprev = prev == NULL ? alloc(prevsize)
16270 : vim_realloc(prev, prevsize);
16271 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016272 {
16273 do_outofmem_msg((long_u)prevsize);
16274 failed = TRUE;
16275 break;
16276 }
16277 prev = newprev;
16278 }
16279 /* Add the line part to end of "prev". */
16280 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016281 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016282 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016283 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016284
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016285 /*
16286 * For a negative line count use only the lines at the end of the file,
16287 * free the rest.
16288 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016289 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016290 while (cnt > -maxline)
16291 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016292 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016293 --cnt;
16294 }
16295
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016296 if (failed)
16297 {
16298 list_free(rettv->vval.v_list, TRUE);
16299 /* readfile doc says an empty list is returned on error */
16300 rettv->vval.v_list = list_alloc();
16301 }
16302
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016303 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016304 fclose(fd);
16305}
16306
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016307#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016308static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016309
16310/*
16311 * Convert a List to proftime_T.
16312 * Return FAIL when there is something wrong.
16313 */
16314 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016315list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016316{
16317 long n1, n2;
16318 int error = FALSE;
16319
16320 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16321 || arg->vval.v_list->lv_len != 2)
16322 return FAIL;
16323 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16324 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16325# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016326 tm->HighPart = n1;
16327 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016328# else
16329 tm->tv_sec = n1;
16330 tm->tv_usec = n2;
16331# endif
16332 return error ? FAIL : OK;
16333}
16334#endif /* FEAT_RELTIME */
16335
16336/*
16337 * "reltime()" function
16338 */
16339 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016340f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016341{
16342#ifdef FEAT_RELTIME
16343 proftime_T res;
16344 proftime_T start;
16345
16346 if (argvars[0].v_type == VAR_UNKNOWN)
16347 {
16348 /* No arguments: get current time. */
16349 profile_start(&res);
16350 }
16351 else if (argvars[1].v_type == VAR_UNKNOWN)
16352 {
16353 if (list2proftime(&argvars[0], &res) == FAIL)
16354 return;
16355 profile_end(&res);
16356 }
16357 else
16358 {
16359 /* Two arguments: compute the difference. */
16360 if (list2proftime(&argvars[0], &start) == FAIL
16361 || list2proftime(&argvars[1], &res) == FAIL)
16362 return;
16363 profile_sub(&res, &start);
16364 }
16365
16366 if (rettv_list_alloc(rettv) == OK)
16367 {
16368 long n1, n2;
16369
16370# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016371 n1 = res.HighPart;
16372 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016373# else
16374 n1 = res.tv_sec;
16375 n2 = res.tv_usec;
16376# endif
16377 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16378 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16379 }
16380#endif
16381}
16382
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016383#ifdef FEAT_FLOAT
16384/*
16385 * "reltimefloat()" function
16386 */
16387 static void
16388f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16389{
16390# ifdef FEAT_RELTIME
16391 proftime_T tm;
16392# endif
16393
16394 rettv->v_type = VAR_FLOAT;
16395 rettv->vval.v_float = 0;
16396# ifdef FEAT_RELTIME
16397 if (list2proftime(&argvars[0], &tm) == OK)
16398 rettv->vval.v_float = profile_float(&tm);
16399# endif
16400}
16401#endif
16402
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016403/*
16404 * "reltimestr()" function
16405 */
16406 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016407f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016408{
16409#ifdef FEAT_RELTIME
16410 proftime_T tm;
16411#endif
16412
16413 rettv->v_type = VAR_STRING;
16414 rettv->vval.v_string = NULL;
16415#ifdef FEAT_RELTIME
16416 if (list2proftime(&argvars[0], &tm) == OK)
16417 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16418#endif
16419}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016420
Bram Moolenaar0d660222005-01-07 21:51:51 +000016421#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016422static void make_connection(void);
16423static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016424
16425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016426make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016427{
16428 if (X_DISPLAY == NULL
16429# ifdef FEAT_GUI
16430 && !gui.in_use
16431# endif
16432 )
16433 {
16434 x_force_connect = TRUE;
16435 setup_term_clip();
16436 x_force_connect = FALSE;
16437 }
16438}
16439
16440 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016441check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016442{
16443 make_connection();
16444 if (X_DISPLAY == NULL)
16445 {
16446 EMSG(_("E240: No connection to Vim server"));
16447 return FAIL;
16448 }
16449 return OK;
16450}
16451#endif
16452
16453#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016454static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016455
16456 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016457remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016458{
16459 char_u *server_name;
16460 char_u *keys;
16461 char_u *r = NULL;
16462 char_u buf[NUMBUFLEN];
16463# ifdef WIN32
16464 HWND w;
16465# else
16466 Window w;
16467# endif
16468
16469 if (check_restricted() || check_secure())
16470 return;
16471
16472# ifdef FEAT_X11
16473 if (check_connection() == FAIL)
16474 return;
16475# endif
16476
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016477 server_name = get_tv_string_chk(&argvars[0]);
16478 if (server_name == NULL)
16479 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016480 keys = get_tv_string_buf(&argvars[1], buf);
16481# ifdef WIN32
16482 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16483# else
16484 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16485 < 0)
16486# endif
16487 {
16488 if (r != NULL)
16489 EMSG(r); /* sending worked but evaluation failed */
16490 else
16491 EMSG2(_("E241: Unable to send to %s"), server_name);
16492 return;
16493 }
16494
16495 rettv->vval.v_string = r;
16496
16497 if (argvars[2].v_type != VAR_UNKNOWN)
16498 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016499 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016500 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016501 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016502
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016503 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016504 v.di_tv.v_type = VAR_STRING;
16505 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016506 idvar = get_tv_string_chk(&argvars[2]);
16507 if (idvar != NULL)
16508 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016509 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016510 }
16511}
16512#endif
16513
16514/*
16515 * "remote_expr()" function
16516 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016517 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016518f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016519{
16520 rettv->v_type = VAR_STRING;
16521 rettv->vval.v_string = NULL;
16522#ifdef FEAT_CLIENTSERVER
16523 remote_common(argvars, rettv, TRUE);
16524#endif
16525}
16526
16527/*
16528 * "remote_foreground()" function
16529 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016530 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016531f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016532{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016533#ifdef FEAT_CLIENTSERVER
16534# ifdef WIN32
16535 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016536 {
16537 char_u *server_name = get_tv_string_chk(&argvars[0]);
16538
16539 if (server_name != NULL)
16540 serverForeground(server_name);
16541 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016542# else
16543 /* Send a foreground() expression to the server. */
16544 argvars[1].v_type = VAR_STRING;
16545 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16546 argvars[2].v_type = VAR_UNKNOWN;
16547 remote_common(argvars, rettv, TRUE);
16548 vim_free(argvars[1].vval.v_string);
16549# endif
16550#endif
16551}
16552
Bram Moolenaar0d660222005-01-07 21:51:51 +000016553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016554f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016555{
16556#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016557 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016558 char_u *s = NULL;
16559# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016560 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016561# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016562 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016563
16564 if (check_restricted() || check_secure())
16565 {
16566 rettv->vval.v_number = -1;
16567 return;
16568 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016569 serverid = get_tv_string_chk(&argvars[0]);
16570 if (serverid == NULL)
16571 {
16572 rettv->vval.v_number = -1;
16573 return; /* type error; errmsg already given */
16574 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016575# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016576 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016577 if (n == 0)
16578 rettv->vval.v_number = -1;
16579 else
16580 {
16581 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16582 rettv->vval.v_number = (s != NULL);
16583 }
16584# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016585 if (check_connection() == FAIL)
16586 return;
16587
16588 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016589 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016590# endif
16591
16592 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16593 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016594 char_u *retvar;
16595
Bram Moolenaar33570922005-01-25 22:26:29 +000016596 v.di_tv.v_type = VAR_STRING;
16597 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016598 retvar = get_tv_string_chk(&argvars[1]);
16599 if (retvar != NULL)
16600 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016601 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016602 }
16603#else
16604 rettv->vval.v_number = -1;
16605#endif
16606}
16607
Bram Moolenaar0d660222005-01-07 21:51:51 +000016608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016609f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016610{
16611 char_u *r = NULL;
16612
16613#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016614 char_u *serverid = get_tv_string_chk(&argvars[0]);
16615
16616 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016617 {
16618# ifdef WIN32
16619 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016620 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016621
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016622 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016623 if (n != 0)
16624 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16625 if (r == NULL)
16626# else
16627 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016628 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016629# endif
16630 EMSG(_("E277: Unable to read a server reply"));
16631 }
16632#endif
16633 rettv->v_type = VAR_STRING;
16634 rettv->vval.v_string = r;
16635}
16636
16637/*
16638 * "remote_send()" function
16639 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016640 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016641f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016642{
16643 rettv->v_type = VAR_STRING;
16644 rettv->vval.v_string = NULL;
16645#ifdef FEAT_CLIENTSERVER
16646 remote_common(argvars, rettv, FALSE);
16647#endif
16648}
16649
16650/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016651 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016652 */
16653 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016654f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016655{
Bram Moolenaar33570922005-01-25 22:26:29 +000016656 list_T *l;
16657 listitem_T *item, *item2;
16658 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016659 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016660 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016661 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016662 dict_T *d;
16663 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016664 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016665
Bram Moolenaar8c711452005-01-14 21:53:12 +000016666 if (argvars[0].v_type == VAR_DICT)
16667 {
16668 if (argvars[2].v_type != VAR_UNKNOWN)
16669 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016670 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016671 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016672 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016673 key = get_tv_string_chk(&argvars[1]);
16674 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016675 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016676 di = dict_find(d, key, -1);
16677 if (di == NULL)
16678 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016679 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16680 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016681 {
16682 *rettv = di->di_tv;
16683 init_tv(&di->di_tv);
16684 dictitem_remove(d, di);
16685 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016686 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016687 }
16688 }
16689 else if (argvars[0].v_type != VAR_LIST)
16690 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016691 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016692 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016693 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016694 int error = FALSE;
16695
16696 idx = get_tv_number_chk(&argvars[1], &error);
16697 if (error)
16698 ; /* type error: do nothing, errmsg already given */
16699 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016700 EMSGN(_(e_listidx), idx);
16701 else
16702 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016703 if (argvars[2].v_type == VAR_UNKNOWN)
16704 {
16705 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016706 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016707 *rettv = item->li_tv;
16708 vim_free(item);
16709 }
16710 else
16711 {
16712 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016713 end = get_tv_number_chk(&argvars[2], &error);
16714 if (error)
16715 ; /* type error: do nothing */
16716 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016717 EMSGN(_(e_listidx), end);
16718 else
16719 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016720 int cnt = 0;
16721
16722 for (li = item; li != NULL; li = li->li_next)
16723 {
16724 ++cnt;
16725 if (li == item2)
16726 break;
16727 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016728 if (li == NULL) /* didn't find "item2" after "item" */
16729 EMSG(_(e_invrange));
16730 else
16731 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016732 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016733 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016734 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016735 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016736 l->lv_first = item;
16737 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016738 item->li_prev = NULL;
16739 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016740 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016741 }
16742 }
16743 }
16744 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016745 }
16746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016747}
16748
16749/*
16750 * "rename({from}, {to})" function
16751 */
16752 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016753f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016754{
16755 char_u buf[NUMBUFLEN];
16756
16757 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016758 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016759 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016760 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16761 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016762}
16763
16764/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016765 * "repeat()" function
16766 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016767 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016768f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016769{
16770 char_u *p;
16771 int n;
16772 int slen;
16773 int len;
16774 char_u *r;
16775 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016776
16777 n = get_tv_number(&argvars[1]);
16778 if (argvars[0].v_type == VAR_LIST)
16779 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016780 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016781 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016782 if (list_extend(rettv->vval.v_list,
16783 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016784 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016785 }
16786 else
16787 {
16788 p = get_tv_string(&argvars[0]);
16789 rettv->v_type = VAR_STRING;
16790 rettv->vval.v_string = NULL;
16791
16792 slen = (int)STRLEN(p);
16793 len = slen * n;
16794 if (len <= 0)
16795 return;
16796
16797 r = alloc(len + 1);
16798 if (r != NULL)
16799 {
16800 for (i = 0; i < n; i++)
16801 mch_memmove(r + i * slen, p, (size_t)slen);
16802 r[len] = NUL;
16803 }
16804
16805 rettv->vval.v_string = r;
16806 }
16807}
16808
16809/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016810 * "resolve()" function
16811 */
16812 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016813f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016814{
16815 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016816#ifdef HAVE_READLINK
16817 char_u *buf = NULL;
16818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016819
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016820 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016821#ifdef FEAT_SHORTCUT
16822 {
16823 char_u *v = NULL;
16824
16825 v = mch_resolve_shortcut(p);
16826 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016827 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016828 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016829 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016830 }
16831#else
16832# ifdef HAVE_READLINK
16833 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016834 char_u *cpy;
16835 int len;
16836 char_u *remain = NULL;
16837 char_u *q;
16838 int is_relative_to_current = FALSE;
16839 int has_trailing_pathsep = FALSE;
16840 int limit = 100;
16841
16842 p = vim_strsave(p);
16843
16844 if (p[0] == '.' && (vim_ispathsep(p[1])
16845 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16846 is_relative_to_current = TRUE;
16847
16848 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016849 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016850 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016851 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016852 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016854
16855 q = getnextcomp(p);
16856 if (*q != NUL)
16857 {
16858 /* Separate the first path component in "p", and keep the
16859 * remainder (beginning with the path separator). */
16860 remain = vim_strsave(q - 1);
16861 q[-1] = NUL;
16862 }
16863
Bram Moolenaard9462e32011-04-11 21:35:11 +020016864 buf = alloc(MAXPATHL + 1);
16865 if (buf == NULL)
16866 goto fail;
16867
Bram Moolenaar071d4272004-06-13 20:20:40 +000016868 for (;;)
16869 {
16870 for (;;)
16871 {
16872 len = readlink((char *)p, (char *)buf, MAXPATHL);
16873 if (len <= 0)
16874 break;
16875 buf[len] = NUL;
16876
16877 if (limit-- == 0)
16878 {
16879 vim_free(p);
16880 vim_free(remain);
16881 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016882 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016883 goto fail;
16884 }
16885
16886 /* Ensure that the result will have a trailing path separator
16887 * if the argument has one. */
16888 if (remain == NULL && has_trailing_pathsep)
16889 add_pathsep(buf);
16890
16891 /* Separate the first path component in the link value and
16892 * concatenate the remainders. */
16893 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16894 if (*q != NUL)
16895 {
16896 if (remain == NULL)
16897 remain = vim_strsave(q - 1);
16898 else
16899 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016900 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016901 if (cpy != NULL)
16902 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016903 vim_free(remain);
16904 remain = cpy;
16905 }
16906 }
16907 q[-1] = NUL;
16908 }
16909
16910 q = gettail(p);
16911 if (q > p && *q == NUL)
16912 {
16913 /* Ignore trailing path separator. */
16914 q[-1] = NUL;
16915 q = gettail(p);
16916 }
16917 if (q > p && !mch_isFullName(buf))
16918 {
16919 /* symlink is relative to directory of argument */
16920 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16921 if (cpy != NULL)
16922 {
16923 STRCPY(cpy, p);
16924 STRCPY(gettail(cpy), buf);
16925 vim_free(p);
16926 p = cpy;
16927 }
16928 }
16929 else
16930 {
16931 vim_free(p);
16932 p = vim_strsave(buf);
16933 }
16934 }
16935
16936 if (remain == NULL)
16937 break;
16938
16939 /* Append the first path component of "remain" to "p". */
16940 q = getnextcomp(remain + 1);
16941 len = q - remain - (*q != NUL);
16942 cpy = vim_strnsave(p, STRLEN(p) + len);
16943 if (cpy != NULL)
16944 {
16945 STRNCAT(cpy, remain, len);
16946 vim_free(p);
16947 p = cpy;
16948 }
16949 /* Shorten "remain". */
16950 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016951 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016952 else
16953 {
16954 vim_free(remain);
16955 remain = NULL;
16956 }
16957 }
16958
16959 /* If the result is a relative path name, make it explicitly relative to
16960 * the current directory if and only if the argument had this form. */
16961 if (!vim_ispathsep(*p))
16962 {
16963 if (is_relative_to_current
16964 && *p != NUL
16965 && !(p[0] == '.'
16966 && (p[1] == NUL
16967 || vim_ispathsep(p[1])
16968 || (p[1] == '.'
16969 && (p[2] == NUL
16970 || vim_ispathsep(p[2]))))))
16971 {
16972 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016973 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016974 if (cpy != NULL)
16975 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016976 vim_free(p);
16977 p = cpy;
16978 }
16979 }
16980 else if (!is_relative_to_current)
16981 {
16982 /* Strip leading "./". */
16983 q = p;
16984 while (q[0] == '.' && vim_ispathsep(q[1]))
16985 q += 2;
16986 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016987 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016988 }
16989 }
16990
16991 /* Ensure that the result will have no trailing path separator
16992 * if the argument had none. But keep "/" or "//". */
16993 if (!has_trailing_pathsep)
16994 {
16995 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016996 if (after_pathsep(p, q))
16997 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016998 }
16999
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017000 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017001 }
17002# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017003 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017004# endif
17005#endif
17006
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017007 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017008
17009#ifdef HAVE_READLINK
17010fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017011 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017012#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017013 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017014}
17015
17016/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017017 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017018 */
17019 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017020f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017021{
Bram Moolenaar33570922005-01-25 22:26:29 +000017022 list_T *l;
17023 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017024
Bram Moolenaar0d660222005-01-07 21:51:51 +000017025 if (argvars[0].v_type != VAR_LIST)
17026 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017027 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017028 && !tv_check_lock(l->lv_lock,
17029 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017030 {
17031 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017032 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017033 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017034 while (li != NULL)
17035 {
17036 ni = li->li_prev;
17037 list_append(l, li);
17038 li = ni;
17039 }
17040 rettv->vval.v_list = l;
17041 rettv->v_type = VAR_LIST;
17042 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017043 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017045}
17046
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017047#define SP_NOMOVE 0x01 /* don't move cursor */
17048#define SP_REPEAT 0x02 /* repeat to find outer pair */
17049#define SP_RETCOUNT 0x04 /* return matchcount */
17050#define SP_SETPCMARK 0x08 /* set previous context mark */
17051#define SP_START 0x10 /* accept match at start position */
17052#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17053#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017054#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017055
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017056static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017057
17058/*
17059 * Get flags for a search function.
17060 * Possibly sets "p_ws".
17061 * Returns BACKWARD, FORWARD or zero (for an error).
17062 */
17063 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017064get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017065{
17066 int dir = FORWARD;
17067 char_u *flags;
17068 char_u nbuf[NUMBUFLEN];
17069 int mask;
17070
17071 if (varp->v_type != VAR_UNKNOWN)
17072 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017073 flags = get_tv_string_buf_chk(varp, nbuf);
17074 if (flags == NULL)
17075 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017076 while (*flags != NUL)
17077 {
17078 switch (*flags)
17079 {
17080 case 'b': dir = BACKWARD; break;
17081 case 'w': p_ws = TRUE; break;
17082 case 'W': p_ws = FALSE; break;
17083 default: mask = 0;
17084 if (flagsp != NULL)
17085 switch (*flags)
17086 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017087 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017088 case 'e': mask = SP_END; break;
17089 case 'm': mask = SP_RETCOUNT; break;
17090 case 'n': mask = SP_NOMOVE; break;
17091 case 'p': mask = SP_SUBPAT; break;
17092 case 'r': mask = SP_REPEAT; break;
17093 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017094 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017095 }
17096 if (mask == 0)
17097 {
17098 EMSG2(_(e_invarg2), flags);
17099 dir = 0;
17100 }
17101 else
17102 *flagsp |= mask;
17103 }
17104 if (dir == 0)
17105 break;
17106 ++flags;
17107 }
17108 }
17109 return dir;
17110}
17111
Bram Moolenaar071d4272004-06-13 20:20:40 +000017112/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017113 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017114 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017115 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017116search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017117{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017118 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017119 char_u *pat;
17120 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017121 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017122 int save_p_ws = p_ws;
17123 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017124 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017125 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017126 proftime_T tm;
17127#ifdef FEAT_RELTIME
17128 long time_limit = 0;
17129#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017130 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017131 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017132
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017133 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017134 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017135 if (dir == 0)
17136 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017137 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017138 if (flags & SP_START)
17139 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017140 if (flags & SP_END)
17141 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017142 if (flags & SP_COLUMN)
17143 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017144
Bram Moolenaar76929292008-01-06 19:07:36 +000017145 /* Optional arguments: line number to stop searching and timeout. */
17146 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017147 {
17148 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17149 if (lnum_stop < 0)
17150 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017151#ifdef FEAT_RELTIME
17152 if (argvars[3].v_type != VAR_UNKNOWN)
17153 {
17154 time_limit = get_tv_number_chk(&argvars[3], NULL);
17155 if (time_limit < 0)
17156 goto theend;
17157 }
17158#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017159 }
17160
Bram Moolenaar76929292008-01-06 19:07:36 +000017161#ifdef FEAT_RELTIME
17162 /* Set the time limit, if there is one. */
17163 profile_setlimit(time_limit, &tm);
17164#endif
17165
Bram Moolenaar231334e2005-07-25 20:46:57 +000017166 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017167 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017168 * Check to make sure only those flags are set.
17169 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17170 * flags cannot be set. Check for that condition also.
17171 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017172 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017173 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017174 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017175 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017176 goto theend;
17177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017178
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017179 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017180 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017181 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017182 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017183 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017184 if (flags & SP_SUBPAT)
17185 retval = subpatnum;
17186 else
17187 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017188 if (flags & SP_SETPCMARK)
17189 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017190 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017191 if (match_pos != NULL)
17192 {
17193 /* Store the match cursor position */
17194 match_pos->lnum = pos.lnum;
17195 match_pos->col = pos.col + 1;
17196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017197 /* "/$" will put the cursor after the end of the line, may need to
17198 * correct that here */
17199 check_cursor();
17200 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017201
17202 /* If 'n' flag is used: restore cursor position. */
17203 if (flags & SP_NOMOVE)
17204 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017205 else
17206 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017207theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017208 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017209
17210 return retval;
17211}
17212
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017213#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017214
17215/*
17216 * round() is not in C90, use ceil() or floor() instead.
17217 */
17218 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017219vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017220{
17221 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17222}
17223
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017224/*
17225 * "round({float})" function
17226 */
17227 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017228f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017229{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017230 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017231
17232 rettv->v_type = VAR_FLOAT;
17233 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017234 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017235 else
17236 rettv->vval.v_float = 0.0;
17237}
17238#endif
17239
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017240/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017241 * "screenattr()" function
17242 */
17243 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017244f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017245{
17246 int row;
17247 int col;
17248 int c;
17249
17250 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17251 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17252 if (row < 0 || row >= screen_Rows
17253 || col < 0 || col >= screen_Columns)
17254 c = -1;
17255 else
17256 c = ScreenAttrs[LineOffset[row] + col];
17257 rettv->vval.v_number = c;
17258}
17259
17260/*
17261 * "screenchar()" function
17262 */
17263 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017264f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017265{
17266 int row;
17267 int col;
17268 int off;
17269 int c;
17270
17271 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17272 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17273 if (row < 0 || row >= screen_Rows
17274 || col < 0 || col >= screen_Columns)
17275 c = -1;
17276 else
17277 {
17278 off = LineOffset[row] + col;
17279#ifdef FEAT_MBYTE
17280 if (enc_utf8 && ScreenLinesUC[off] != 0)
17281 c = ScreenLinesUC[off];
17282 else
17283#endif
17284 c = ScreenLines[off];
17285 }
17286 rettv->vval.v_number = c;
17287}
17288
17289/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017290 * "screencol()" function
17291 *
17292 * First column is 1 to be consistent with virtcol().
17293 */
17294 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017295f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017296{
17297 rettv->vval.v_number = screen_screencol() + 1;
17298}
17299
17300/*
17301 * "screenrow()" function
17302 */
17303 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017304f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017305{
17306 rettv->vval.v_number = screen_screenrow() + 1;
17307}
17308
17309/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017310 * "search()" function
17311 */
17312 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017313f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017314{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017315 int flags = 0;
17316
17317 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017318}
17319
Bram Moolenaar071d4272004-06-13 20:20:40 +000017320/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017321 * "searchdecl()" function
17322 */
17323 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017324f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017325{
17326 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017327 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017328 int error = FALSE;
17329 char_u *name;
17330
17331 rettv->vval.v_number = 1; /* default: FAIL */
17332
17333 name = get_tv_string_chk(&argvars[0]);
17334 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017335 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017336 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017337 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17338 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17339 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017340 if (!error && name != NULL)
17341 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017342 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017343}
17344
17345/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017346 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017347 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017348 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017349searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017350{
17351 char_u *spat, *mpat, *epat;
17352 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017353 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017354 int dir;
17355 int flags = 0;
17356 char_u nbuf1[NUMBUFLEN];
17357 char_u nbuf2[NUMBUFLEN];
17358 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017359 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017360 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017361 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017362
Bram Moolenaar071d4272004-06-13 20:20:40 +000017363 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017364 spat = get_tv_string_chk(&argvars[0]);
17365 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17366 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17367 if (spat == NULL || mpat == NULL || epat == NULL)
17368 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017369
Bram Moolenaar071d4272004-06-13 20:20:40 +000017370 /* Handle the optional fourth argument: flags */
17371 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017372 if (dir == 0)
17373 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017374
17375 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017376 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17377 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017378 if ((flags & (SP_END | SP_SUBPAT)) != 0
17379 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017380 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017381 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017382 goto theend;
17383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017384
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017385 /* Using 'r' implies 'W', otherwise it doesn't work. */
17386 if (flags & SP_REPEAT)
17387 p_ws = FALSE;
17388
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017389 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017390 if (argvars[3].v_type == VAR_UNKNOWN
17391 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017392 skip = (char_u *)"";
17393 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017394 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017395 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017396 if (argvars[5].v_type != VAR_UNKNOWN)
17397 {
17398 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17399 if (lnum_stop < 0)
17400 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017401#ifdef FEAT_RELTIME
17402 if (argvars[6].v_type != VAR_UNKNOWN)
17403 {
17404 time_limit = get_tv_number_chk(&argvars[6], NULL);
17405 if (time_limit < 0)
17406 goto theend;
17407 }
17408#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017409 }
17410 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017411 if (skip == NULL)
17412 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017413
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017414 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017415 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017416
17417theend:
17418 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017419
17420 return retval;
17421}
17422
17423/*
17424 * "searchpair()" function
17425 */
17426 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017427f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017428{
17429 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17430}
17431
17432/*
17433 * "searchpairpos()" function
17434 */
17435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017436f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017437{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017438 pos_T match_pos;
17439 int lnum = 0;
17440 int col = 0;
17441
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017442 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017443 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017444
17445 if (searchpair_cmn(argvars, &match_pos) > 0)
17446 {
17447 lnum = match_pos.lnum;
17448 col = match_pos.col;
17449 }
17450
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017451 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17452 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017453}
17454
17455/*
17456 * Search for a start/middle/end thing.
17457 * Used by searchpair(), see its documentation for the details.
17458 * Returns 0 or -1 for no match,
17459 */
17460 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017461do_searchpair(
17462 char_u *spat, /* start pattern */
17463 char_u *mpat, /* middle pattern */
17464 char_u *epat, /* end pattern */
17465 int dir, /* BACKWARD or FORWARD */
17466 char_u *skip, /* skip expression */
17467 int flags, /* SP_SETPCMARK and other SP_ values */
17468 pos_T *match_pos,
17469 linenr_T lnum_stop, /* stop at this line if not zero */
17470 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017471{
17472 char_u *save_cpo;
17473 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17474 long retval = 0;
17475 pos_T pos;
17476 pos_T firstpos;
17477 pos_T foundpos;
17478 pos_T save_cursor;
17479 pos_T save_pos;
17480 int n;
17481 int r;
17482 int nest = 1;
17483 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017484 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017485 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017486
17487 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17488 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017489 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017490
Bram Moolenaar76929292008-01-06 19:07:36 +000017491#ifdef FEAT_RELTIME
17492 /* Set the time limit, if there is one. */
17493 profile_setlimit(time_limit, &tm);
17494#endif
17495
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017496 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17497 * start/middle/end (pat3, for the top pair). */
17498 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17499 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17500 if (pat2 == NULL || pat3 == NULL)
17501 goto theend;
17502 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17503 if (*mpat == NUL)
17504 STRCPY(pat3, pat2);
17505 else
17506 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17507 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017508 if (flags & SP_START)
17509 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017510
Bram Moolenaar071d4272004-06-13 20:20:40 +000017511 save_cursor = curwin->w_cursor;
17512 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017513 clearpos(&firstpos);
17514 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017515 pat = pat3;
17516 for (;;)
17517 {
17518 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017519 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017520 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17521 /* didn't find it or found the first match again: FAIL */
17522 break;
17523
17524 if (firstpos.lnum == 0)
17525 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017526 if (equalpos(pos, foundpos))
17527 {
17528 /* Found the same position again. Can happen with a pattern that
17529 * has "\zs" at the end and searching backwards. Advance one
17530 * character and try again. */
17531 if (dir == BACKWARD)
17532 decl(&pos);
17533 else
17534 incl(&pos);
17535 }
17536 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017537
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017538 /* clear the start flag to avoid getting stuck here */
17539 options &= ~SEARCH_START;
17540
Bram Moolenaar071d4272004-06-13 20:20:40 +000017541 /* If the skip pattern matches, ignore this match. */
17542 if (*skip != NUL)
17543 {
17544 save_pos = curwin->w_cursor;
17545 curwin->w_cursor = pos;
17546 r = eval_to_bool(skip, &err, NULL, FALSE);
17547 curwin->w_cursor = save_pos;
17548 if (err)
17549 {
17550 /* Evaluating {skip} caused an error, break here. */
17551 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017552 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017553 break;
17554 }
17555 if (r)
17556 continue;
17557 }
17558
17559 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17560 {
17561 /* Found end when searching backwards or start when searching
17562 * forward: nested pair. */
17563 ++nest;
17564 pat = pat2; /* nested, don't search for middle */
17565 }
17566 else
17567 {
17568 /* Found end when searching forward or start when searching
17569 * backward: end of (nested) pair; or found middle in outer pair. */
17570 if (--nest == 1)
17571 pat = pat3; /* outer level, search for middle */
17572 }
17573
17574 if (nest == 0)
17575 {
17576 /* Found the match: return matchcount or line number. */
17577 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017578 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017579 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017580 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017581 if (flags & SP_SETPCMARK)
17582 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017583 curwin->w_cursor = pos;
17584 if (!(flags & SP_REPEAT))
17585 break;
17586 nest = 1; /* search for next unmatched */
17587 }
17588 }
17589
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017590 if (match_pos != NULL)
17591 {
17592 /* Store the match cursor position */
17593 match_pos->lnum = curwin->w_cursor.lnum;
17594 match_pos->col = curwin->w_cursor.col + 1;
17595 }
17596
Bram Moolenaar071d4272004-06-13 20:20:40 +000017597 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017598 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017599 curwin->w_cursor = save_cursor;
17600
17601theend:
17602 vim_free(pat2);
17603 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017604 if (p_cpo == empty_option)
17605 p_cpo = save_cpo;
17606 else
17607 /* Darn, evaluating the {skip} expression changed the value. */
17608 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017609
17610 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017611}
17612
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017613/*
17614 * "searchpos()" function
17615 */
17616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017617f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017618{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017619 pos_T match_pos;
17620 int lnum = 0;
17621 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017622 int n;
17623 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017624
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017625 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017626 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017627
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017628 n = search_cmn(argvars, &match_pos, &flags);
17629 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017630 {
17631 lnum = match_pos.lnum;
17632 col = match_pos.col;
17633 }
17634
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017635 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17636 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017637 if (flags & SP_SUBPAT)
17638 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017639}
17640
Bram Moolenaar0d660222005-01-07 21:51:51 +000017641 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017642f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017643{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017644#ifdef FEAT_CLIENTSERVER
17645 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017646 char_u *server = get_tv_string_chk(&argvars[0]);
17647 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017648
Bram Moolenaar0d660222005-01-07 21:51:51 +000017649 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017650 if (server == NULL || reply == NULL)
17651 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017652 if (check_restricted() || check_secure())
17653 return;
17654# ifdef FEAT_X11
17655 if (check_connection() == FAIL)
17656 return;
17657# endif
17658
17659 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017660 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017661 EMSG(_("E258: Unable to send to client"));
17662 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017663 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017664 rettv->vval.v_number = 0;
17665#else
17666 rettv->vval.v_number = -1;
17667#endif
17668}
17669
Bram Moolenaar0d660222005-01-07 21:51:51 +000017670 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017671f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017672{
17673 char_u *r = NULL;
17674
17675#ifdef FEAT_CLIENTSERVER
17676# ifdef WIN32
17677 r = serverGetVimNames();
17678# else
17679 make_connection();
17680 if (X_DISPLAY != NULL)
17681 r = serverGetVimNames(X_DISPLAY);
17682# endif
17683#endif
17684 rettv->v_type = VAR_STRING;
17685 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017686}
17687
17688/*
17689 * "setbufvar()" function
17690 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017691 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017692f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017693{
17694 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017695 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017696 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017697 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017698 char_u nbuf[NUMBUFLEN];
17699
17700 if (check_restricted() || check_secure())
17701 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017702 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17703 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017704 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017705 varp = &argvars[2];
17706
17707 if (buf != NULL && varname != NULL && varp != NULL)
17708 {
17709 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017710 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017711
17712 if (*varname == '&')
17713 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017714 long numval;
17715 char_u *strval;
17716 int error = FALSE;
17717
Bram Moolenaar071d4272004-06-13 20:20:40 +000017718 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017719 numval = get_tv_number_chk(varp, &error);
17720 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017721 if (!error && strval != NULL)
17722 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017723 }
17724 else
17725 {
17726 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17727 if (bufvarname != NULL)
17728 {
17729 STRCPY(bufvarname, "b:");
17730 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017731 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017732 vim_free(bufvarname);
17733 }
17734 }
17735
17736 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017737 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017739}
17740
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017741 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017742f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017743{
17744 dict_T *d;
17745 dictitem_T *di;
17746 char_u *csearch;
17747
17748 if (argvars[0].v_type != VAR_DICT)
17749 {
17750 EMSG(_(e_dictreq));
17751 return;
17752 }
17753
17754 if ((d = argvars[0].vval.v_dict) != NULL)
17755 {
17756 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17757 if (csearch != NULL)
17758 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017759#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017760 if (enc_utf8)
17761 {
17762 int pcc[MAX_MCO];
17763 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017764
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017765 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17766 }
17767 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017768#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017769 set_last_csearch(PTR2CHAR(csearch),
17770 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017771 }
17772
17773 di = dict_find(d, (char_u *)"forward", -1);
17774 if (di != NULL)
17775 set_csearch_direction(get_tv_number(&di->di_tv)
17776 ? FORWARD : BACKWARD);
17777
17778 di = dict_find(d, (char_u *)"until", -1);
17779 if (di != NULL)
17780 set_csearch_until(!!get_tv_number(&di->di_tv));
17781 }
17782}
17783
Bram Moolenaar071d4272004-06-13 20:20:40 +000017784/*
17785 * "setcmdpos()" function
17786 */
17787 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017788f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017789{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017790 int pos = (int)get_tv_number(&argvars[0]) - 1;
17791
17792 if (pos >= 0)
17793 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017794}
17795
17796/*
Bram Moolenaar80492532016-03-08 17:08:53 +010017797 * "setfperm({fname}, {mode})" function
17798 */
17799 static void
17800f_setfperm(typval_T *argvars, typval_T *rettv)
17801{
17802 char_u *fname;
17803 char_u modebuf[NUMBUFLEN];
17804 char_u *mode_str;
17805 int i;
17806 int mask;
17807 int mode = 0;
17808
17809 rettv->vval.v_number = 0;
17810 fname = get_tv_string_chk(&argvars[0]);
17811 if (fname == NULL)
17812 return;
17813 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
17814 if (mode_str == NULL)
17815 return;
17816 if (STRLEN(mode_str) != 9)
17817 {
17818 EMSG2(_(e_invarg2), mode_str);
17819 return;
17820 }
17821
17822 mask = 1;
17823 for (i = 8; i >= 0; --i)
17824 {
17825 if (mode_str[i] != '-')
17826 mode |= mask;
17827 mask = mask << 1;
17828 }
17829 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
17830}
17831
17832/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017833 * "setline()" function
17834 */
17835 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017836f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017837{
17838 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017839 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017840 list_T *l = NULL;
17841 listitem_T *li = NULL;
17842 long added = 0;
17843 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017844
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017845 lnum = get_tv_lnum(&argvars[0]);
17846 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017847 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017848 l = argvars[1].vval.v_list;
17849 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017850 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017851 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017852 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017853
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017854 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017855 for (;;)
17856 {
17857 if (l != NULL)
17858 {
17859 /* list argument, get next string */
17860 if (li == NULL)
17861 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017862 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017863 li = li->li_next;
17864 }
17865
17866 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017867 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017868 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017869
17870 /* When coming here from Insert mode, sync undo, so that this can be
17871 * undone separately from what was previously inserted. */
17872 if (u_sync_once == 2)
17873 {
17874 u_sync_once = 1; /* notify that u_sync() was called */
17875 u_sync(TRUE);
17876 }
17877
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017878 if (lnum <= curbuf->b_ml.ml_line_count)
17879 {
17880 /* existing line, replace it */
17881 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17882 {
17883 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017884 if (lnum == curwin->w_cursor.lnum)
17885 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017886 rettv->vval.v_number = 0; /* OK */
17887 }
17888 }
17889 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17890 {
17891 /* lnum is one past the last line, append the line */
17892 ++added;
17893 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17894 rettv->vval.v_number = 0; /* OK */
17895 }
17896
17897 if (l == NULL) /* only one string argument */
17898 break;
17899 ++lnum;
17900 }
17901
17902 if (added > 0)
17903 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017904}
17905
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017906static 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 +000017907
Bram Moolenaar071d4272004-06-13 20:20:40 +000017908/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017909 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017910 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017912set_qf_ll_list(
17913 win_T *wp UNUSED,
17914 typval_T *list_arg UNUSED,
17915 typval_T *action_arg UNUSED,
17916 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017917{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017918#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017919 char_u *act;
17920 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017921#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017922
Bram Moolenaar2641f772005-03-25 21:58:17 +000017923 rettv->vval.v_number = -1;
17924
17925#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017926 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017927 EMSG(_(e_listreq));
17928 else
17929 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017930 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017931
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017932 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017933 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017934 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017935 if (act == NULL)
17936 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017937 if (*act == 'a' || *act == 'r')
17938 action = *act;
17939 }
17940
Bram Moolenaar81484f42012-12-05 15:16:47 +010017941 if (l != NULL && set_errorlist(wp, l, action,
17942 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017943 rettv->vval.v_number = 0;
17944 }
17945#endif
17946}
17947
17948/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017949 * "setloclist()" function
17950 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017952f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017953{
17954 win_T *win;
17955
17956 rettv->vval.v_number = -1;
17957
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017958 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017959 if (win != NULL)
17960 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17961}
17962
17963/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017964 * "setmatches()" function
17965 */
17966 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017967f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017968{
17969#ifdef FEAT_SEARCH_EXTRA
17970 list_T *l;
17971 listitem_T *li;
17972 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017973 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017974
17975 rettv->vval.v_number = -1;
17976 if (argvars[0].v_type != VAR_LIST)
17977 {
17978 EMSG(_(e_listreq));
17979 return;
17980 }
17981 if ((l = argvars[0].vval.v_list) != NULL)
17982 {
17983
17984 /* To some extent make sure that we are dealing with a list from
17985 * "getmatches()". */
17986 li = l->lv_first;
17987 while (li != NULL)
17988 {
17989 if (li->li_tv.v_type != VAR_DICT
17990 || (d = li->li_tv.vval.v_dict) == NULL)
17991 {
17992 EMSG(_(e_invarg));
17993 return;
17994 }
17995 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017996 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17997 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017998 && dict_find(d, (char_u *)"priority", -1) != NULL
17999 && dict_find(d, (char_u *)"id", -1) != NULL))
18000 {
18001 EMSG(_(e_invarg));
18002 return;
18003 }
18004 li = li->li_next;
18005 }
18006
18007 clear_matches(curwin);
18008 li = l->lv_first;
18009 while (li != NULL)
18010 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018011 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018012 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018013 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018014 char_u *group;
18015 int priority;
18016 int id;
18017 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018018
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018019 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018020 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18021 {
18022 if (s == NULL)
18023 {
18024 s = list_alloc();
18025 if (s == NULL)
18026 return;
18027 }
18028
18029 /* match from matchaddpos() */
18030 for (i = 1; i < 9; i++)
18031 {
18032 sprintf((char *)buf, (char *)"pos%d", i);
18033 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18034 {
18035 if (di->di_tv.v_type != VAR_LIST)
18036 return;
18037
18038 list_append_tv(s, &di->di_tv);
18039 s->lv_refcount++;
18040 }
18041 else
18042 break;
18043 }
18044 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018045
18046 group = get_dict_string(d, (char_u *)"group", FALSE);
18047 priority = (int)get_dict_number(d, (char_u *)"priority");
18048 id = (int)get_dict_number(d, (char_u *)"id");
18049 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18050 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18051 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018052 if (i == 0)
18053 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018054 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018055 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018056 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018057 }
18058 else
18059 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018060 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018061 list_unref(s);
18062 s = NULL;
18063 }
18064
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018065 li = li->li_next;
18066 }
18067 rettv->vval.v_number = 0;
18068 }
18069#endif
18070}
18071
18072/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018073 * "setpos()" function
18074 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018075 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018076f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018077{
18078 pos_T pos;
18079 int fnum;
18080 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018081 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018082
Bram Moolenaar08250432008-02-13 11:42:46 +000018083 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018084 name = get_tv_string_chk(argvars);
18085 if (name != NULL)
18086 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018087 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018088 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018089 if (--pos.col < 0)
18090 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018091 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018092 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018093 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018094 if (fnum == curbuf->b_fnum)
18095 {
18096 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018097 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018098 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018099 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018100 curwin->w_set_curswant = FALSE;
18101 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018102 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018103 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018104 }
18105 else
18106 EMSG(_(e_invarg));
18107 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018108 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18109 {
18110 /* set mark */
18111 if (setmark_pos(name[1], &pos, fnum) == OK)
18112 rettv->vval.v_number = 0;
18113 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018114 else
18115 EMSG(_(e_invarg));
18116 }
18117 }
18118}
18119
18120/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018121 * "setqflist()" function
18122 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018123 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018124f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018125{
18126 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18127}
18128
18129/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018130 * "setreg()" function
18131 */
18132 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018133f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018134{
18135 int regname;
18136 char_u *strregname;
18137 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018138 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018139 int append;
18140 char_u yank_type;
18141 long block_len;
18142
18143 block_len = -1;
18144 yank_type = MAUTO;
18145 append = FALSE;
18146
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018147 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018148 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018149
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018150 if (strregname == NULL)
18151 return; /* type error; errmsg already given */
18152 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018153 if (regname == 0 || regname == '@')
18154 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018155
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018156 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018157 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018158 stropt = get_tv_string_chk(&argvars[2]);
18159 if (stropt == NULL)
18160 return; /* type error */
18161 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018162 switch (*stropt)
18163 {
18164 case 'a': case 'A': /* append */
18165 append = TRUE;
18166 break;
18167 case 'v': case 'c': /* character-wise selection */
18168 yank_type = MCHAR;
18169 break;
18170 case 'V': case 'l': /* line-wise selection */
18171 yank_type = MLINE;
18172 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018173 case 'b': case Ctrl_V: /* block-wise selection */
18174 yank_type = MBLOCK;
18175 if (VIM_ISDIGIT(stropt[1]))
18176 {
18177 ++stropt;
18178 block_len = getdigits(&stropt) - 1;
18179 --stropt;
18180 }
18181 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018182 }
18183 }
18184
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018185 if (argvars[1].v_type == VAR_LIST)
18186 {
18187 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018188 char_u **allocval;
18189 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018190 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018191 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018192 int len = argvars[1].vval.v_list->lv_len;
18193 listitem_T *li;
18194
Bram Moolenaar7d647822014-04-05 21:28:56 +020018195 /* First half: use for pointers to result lines; second half: use for
18196 * pointers to allocated copies. */
18197 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018198 if (lstval == NULL)
18199 return;
18200 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018201 allocval = lstval + len + 2;
18202 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018203
18204 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18205 li = li->li_next)
18206 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018207 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018208 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018209 goto free_lstval;
18210 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018211 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018212 /* Need to make a copy, next get_tv_string_buf_chk() will
18213 * overwrite the string. */
18214 strval = vim_strsave(buf);
18215 if (strval == NULL)
18216 goto free_lstval;
18217 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018218 }
18219 *curval++ = strval;
18220 }
18221 *curval++ = NULL;
18222
18223 write_reg_contents_lst(regname, lstval, -1,
18224 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018225free_lstval:
18226 while (curallocval > allocval)
18227 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018228 vim_free(lstval);
18229 }
18230 else
18231 {
18232 strval = get_tv_string_chk(&argvars[1]);
18233 if (strval == NULL)
18234 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018235 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018236 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018237 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018238 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018239}
18240
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018241/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018242 * "settabvar()" function
18243 */
18244 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018245f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018246{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018247#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018248 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018249 tabpage_T *tp;
18250#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018251 char_u *varname, *tabvarname;
18252 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018253
18254 rettv->vval.v_number = 0;
18255
18256 if (check_restricted() || check_secure())
18257 return;
18258
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018259#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018260 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018261#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018262 varname = get_tv_string_chk(&argvars[1]);
18263 varp = &argvars[2];
18264
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018265 if (varname != NULL && varp != NULL
18266#ifdef FEAT_WINDOWS
18267 && tp != NULL
18268#endif
18269 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018270 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018271#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018272 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018273 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018274#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018275
18276 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18277 if (tabvarname != NULL)
18278 {
18279 STRCPY(tabvarname, "t:");
18280 STRCPY(tabvarname + 2, varname);
18281 set_var(tabvarname, varp, TRUE);
18282 vim_free(tabvarname);
18283 }
18284
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018285#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018286 /* Restore current tabpage */
18287 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018288 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018289#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018290 }
18291}
18292
18293/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018294 * "settabwinvar()" function
18295 */
18296 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018297f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018298{
18299 setwinvar(argvars, rettv, 1);
18300}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018301
18302/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018303 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018304 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018305 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018306f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018307{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018308 setwinvar(argvars, rettv, 0);
18309}
18310
18311/*
18312 * "setwinvar()" and "settabwinvar()" functions
18313 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018314
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018315 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018316setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018317{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018318 win_T *win;
18319#ifdef FEAT_WINDOWS
18320 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018321 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018322 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018323#endif
18324 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018325 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018326 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018327 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018328
18329 if (check_restricted() || check_secure())
18330 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018331
18332#ifdef FEAT_WINDOWS
18333 if (off == 1)
18334 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18335 else
18336 tp = curtab;
18337#endif
18338 win = find_win_by_nr(&argvars[off], tp);
18339 varname = get_tv_string_chk(&argvars[off + 1]);
18340 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018341
18342 if (win != NULL && varname != NULL && varp != NULL)
18343 {
18344#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018345 need_switch_win = !(tp == curtab && win == curwin);
18346 if (!need_switch_win
18347 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018349 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018350 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018351 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018352 long numval;
18353 char_u *strval;
18354 int error = FALSE;
18355
18356 ++varname;
18357 numval = get_tv_number_chk(varp, &error);
18358 strval = get_tv_string_buf_chk(varp, nbuf);
18359 if (!error && strval != NULL)
18360 set_option_value(varname, numval, strval, OPT_LOCAL);
18361 }
18362 else
18363 {
18364 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18365 if (winvarname != NULL)
18366 {
18367 STRCPY(winvarname, "w:");
18368 STRCPY(winvarname + 2, varname);
18369 set_var(winvarname, varp, TRUE);
18370 vim_free(winvarname);
18371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018372 }
18373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018374#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018375 if (need_switch_win)
18376 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018377#endif
18378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018379}
18380
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018381#ifdef FEAT_CRYPT
18382/*
18383 * "sha256({string})" function
18384 */
18385 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018386f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018387{
18388 char_u *p;
18389
18390 p = get_tv_string(&argvars[0]);
18391 rettv->vval.v_string = vim_strsave(
18392 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18393 rettv->v_type = VAR_STRING;
18394}
18395#endif /* FEAT_CRYPT */
18396
Bram Moolenaar071d4272004-06-13 20:20:40 +000018397/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018398 * "shellescape({string})" function
18399 */
18400 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018401f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018402{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018403 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018404 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018405 rettv->v_type = VAR_STRING;
18406}
18407
18408/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018409 * shiftwidth() function
18410 */
18411 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018412f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018413{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018414 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018415}
18416
18417/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018418 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018419 */
18420 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018421f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018422{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018423 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018424
Bram Moolenaar0d660222005-01-07 21:51:51 +000018425 p = get_tv_string(&argvars[0]);
18426 rettv->vval.v_string = vim_strsave(p);
18427 simplify_filename(rettv->vval.v_string); /* simplify in place */
18428 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018429}
18430
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018431#ifdef FEAT_FLOAT
18432/*
18433 * "sin()" function
18434 */
18435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018436f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018437{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018438 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018439
18440 rettv->v_type = VAR_FLOAT;
18441 if (get_float_arg(argvars, &f) == OK)
18442 rettv->vval.v_float = sin(f);
18443 else
18444 rettv->vval.v_float = 0.0;
18445}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018446
18447/*
18448 * "sinh()" function
18449 */
18450 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018451f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018452{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018453 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018454
18455 rettv->v_type = VAR_FLOAT;
18456 if (get_float_arg(argvars, &f) == OK)
18457 rettv->vval.v_float = sinh(f);
18458 else
18459 rettv->vval.v_float = 0.0;
18460}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018461#endif
18462
Bram Moolenaar0d660222005-01-07 21:51:51 +000018463static int
18464#ifdef __BORLANDC__
18465 _RTLENTRYF
18466#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018467 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018468static int
18469#ifdef __BORLANDC__
18470 _RTLENTRYF
18471#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018472 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018473
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018474/* struct used in the array that's given to qsort() */
18475typedef struct
18476{
18477 listitem_T *item;
18478 int idx;
18479} sortItem_T;
18480
Bram Moolenaar0b962472016-02-22 22:51:33 +010018481/* struct storing information about current sort */
18482typedef struct
18483{
18484 int item_compare_ic;
18485 int item_compare_numeric;
18486 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018487#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018488 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018489#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018490 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018491 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018492 dict_T *item_compare_selfdict;
18493 int item_compare_func_err;
18494 int item_compare_keep_zero;
18495} sortinfo_T;
18496static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018497static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018498#define ITEM_COMPARE_FAIL 999
18499
Bram Moolenaar071d4272004-06-13 20:20:40 +000018500/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018501 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018502 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018503 static int
18504#ifdef __BORLANDC__
18505_RTLENTRYF
18506#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018507item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018508{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018509 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018510 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018511 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018512 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018513 int res;
18514 char_u numbuf1[NUMBUFLEN];
18515 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018516
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018517 si1 = (sortItem_T *)s1;
18518 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018519 tv1 = &si1->item->li_tv;
18520 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018521
Bram Moolenaar0b962472016-02-22 22:51:33 +010018522 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018523 {
18524 long v1 = get_tv_number(tv1);
18525 long v2 = get_tv_number(tv2);
18526
18527 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18528 }
18529
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018530#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018531 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018532 {
18533 float_T v1 = get_tv_float(tv1);
18534 float_T v2 = get_tv_float(tv2);
18535
18536 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18537 }
18538#endif
18539
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018540 /* tv2string() puts quotes around a string and allocates memory. Don't do
18541 * that for string variables. Use a single quote when comparing with a
18542 * non-string to do what the docs promise. */
18543 if (tv1->v_type == VAR_STRING)
18544 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018545 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018546 p1 = (char_u *)"'";
18547 else
18548 p1 = tv1->vval.v_string;
18549 }
18550 else
18551 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18552 if (tv2->v_type == VAR_STRING)
18553 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018554 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018555 p2 = (char_u *)"'";
18556 else
18557 p2 = tv2->vval.v_string;
18558 }
18559 else
18560 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018561 if (p1 == NULL)
18562 p1 = (char_u *)"";
18563 if (p2 == NULL)
18564 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010018565 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018566 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018567 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018568 res = STRICMP(p1, p2);
18569 else
18570 res = STRCMP(p1, p2);
18571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018572 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018573 {
18574 double n1, n2;
18575 n1 = strtod((char *)p1, (char **)&p1);
18576 n2 = strtod((char *)p2, (char **)&p2);
18577 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18578 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018579
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018580 /* When the result would be zero, compare the item indexes. Makes the
18581 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018582 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018583 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018584
Bram Moolenaar0d660222005-01-07 21:51:51 +000018585 vim_free(tofree1);
18586 vim_free(tofree2);
18587 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018588}
18589
18590 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018591#ifdef __BORLANDC__
18592_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018593#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018594item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018595{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018596 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018597 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018598 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018599 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018600 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018601 char_u *func_name;
18602 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018603
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018604 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018605 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018606 return 0;
18607
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018608 si1 = (sortItem_T *)s1;
18609 si2 = (sortItem_T *)s2;
18610
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018611 if (partial == NULL)
18612 func_name = sortinfo->item_compare_func;
18613 else
18614 func_name = partial->pt_name;
18615
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018616 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018617 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018618 copy_tv(&si1->item->li_tv, &argv[0]);
18619 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018620
18621 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018622 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018623 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018624 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018625 clear_tv(&argv[0]);
18626 clear_tv(&argv[1]);
18627
18628 if (res == FAIL)
18629 res = ITEM_COMPARE_FAIL;
18630 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010018631 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
18632 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018633 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018634 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018635
18636 /* When the result would be zero, compare the pointers themselves. Makes
18637 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018638 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018639 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018640
Bram Moolenaar0d660222005-01-07 21:51:51 +000018641 return res;
18642}
18643
18644/*
18645 * "sort({list})" function
18646 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018647 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018648do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018649{
Bram Moolenaar33570922005-01-25 22:26:29 +000018650 list_T *l;
18651 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018652 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018653 sortinfo_T *old_sortinfo;
18654 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018655 long len;
18656 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018657
Bram Moolenaar0b962472016-02-22 22:51:33 +010018658 /* Pointer to current info struct used in compare function. Save and
18659 * restore the current one for nested calls. */
18660 old_sortinfo = sortinfo;
18661 sortinfo = &info;
18662
Bram Moolenaar0d660222005-01-07 21:51:51 +000018663 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018664 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018665 else
18666 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018667 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018668 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018669 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18670 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010018671 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018672 rettv->vval.v_list = l;
18673 rettv->v_type = VAR_LIST;
18674 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018675
Bram Moolenaar0d660222005-01-07 21:51:51 +000018676 len = list_len(l);
18677 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018678 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018679
Bram Moolenaar0b962472016-02-22 22:51:33 +010018680 info.item_compare_ic = FALSE;
18681 info.item_compare_numeric = FALSE;
18682 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018683#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018684 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018685#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018686 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018687 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018688 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018689 if (argvars[1].v_type != VAR_UNKNOWN)
18690 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018691 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018692 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018693 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018694 else if (argvars[1].v_type == VAR_PARTIAL)
18695 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018696 else
18697 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018698 int error = FALSE;
18699
18700 i = get_tv_number_chk(&argvars[1], &error);
18701 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018702 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018703 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018704 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010018705 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018706 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010018707 else if (i != 0)
18708 {
18709 EMSG(_(e_invarg));
18710 goto theend;
18711 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018712 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018713 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010018714 if (*info.item_compare_func == NUL)
18715 {
18716 /* empty string means default sort */
18717 info.item_compare_func = NULL;
18718 }
18719 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018720 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018721 info.item_compare_func = NULL;
18722 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018723 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018724 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018725 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018726 info.item_compare_func = NULL;
18727 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018728 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018729#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018730 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018731 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018732 info.item_compare_func = NULL;
18733 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018734 }
18735#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018736 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018737 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018738 info.item_compare_func = NULL;
18739 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018740 }
18741 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018742 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018743
18744 if (argvars[2].v_type != VAR_UNKNOWN)
18745 {
18746 /* optional third argument: {dict} */
18747 if (argvars[2].v_type != VAR_DICT)
18748 {
18749 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010018750 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018751 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018752 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018753 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018755
Bram Moolenaar0d660222005-01-07 21:51:51 +000018756 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018757 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018758 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018759 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018760
Bram Moolenaar327aa022014-03-25 18:24:23 +010018761 i = 0;
18762 if (sort)
18763 {
18764 /* sort(): ptrs will be the list to sort */
18765 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018766 {
18767 ptrs[i].item = li;
18768 ptrs[i].idx = i;
18769 ++i;
18770 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018771
Bram Moolenaar0b962472016-02-22 22:51:33 +010018772 info.item_compare_func_err = FALSE;
18773 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018774 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018775 if ((info.item_compare_func != NULL
18776 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018777 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018778 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018779 EMSG(_("E702: Sort compare function failed"));
18780 else
18781 {
18782 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018783 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010018784 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018785 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010018786 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018787
Bram Moolenaar0b962472016-02-22 22:51:33 +010018788 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018789 {
18790 /* Clear the List and append the items in sorted order. */
18791 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18792 l->lv_len = 0;
18793 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018794 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018795 }
18796 }
18797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018798 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018799 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018800 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018801
18802 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018803 info.item_compare_func_err = FALSE;
18804 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018805 item_compare_func_ptr = info.item_compare_func != NULL
18806 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010018807 ? item_compare2 : item_compare;
18808
18809 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18810 li = li->li_next)
18811 {
18812 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18813 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018814 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018815 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018816 {
18817 EMSG(_("E882: Uniq compare function failed"));
18818 break;
18819 }
18820 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018821
Bram Moolenaar0b962472016-02-22 22:51:33 +010018822 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018823 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018824 while (--i >= 0)
18825 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018826 li = ptrs[i].item->li_next;
18827 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018828 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018829 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018830 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018831 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018832 list_fix_watch(l, li);
18833 listitem_free(li);
18834 l->lv_len--;
18835 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018836 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018837 }
18838
18839 vim_free(ptrs);
18840 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018841theend:
18842 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018843}
18844
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018845/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018846 * "sort({list})" function
18847 */
18848 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018849f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018850{
18851 do_sort_uniq(argvars, rettv, TRUE);
18852}
18853
18854/*
18855 * "uniq({list})" function
18856 */
18857 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018858f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018859{
18860 do_sort_uniq(argvars, rettv, FALSE);
18861}
18862
18863/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018864 * "soundfold({word})" function
18865 */
18866 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018867f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018868{
18869 char_u *s;
18870
18871 rettv->v_type = VAR_STRING;
18872 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018873#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018874 rettv->vval.v_string = eval_soundfold(s);
18875#else
18876 rettv->vval.v_string = vim_strsave(s);
18877#endif
18878}
18879
18880/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018881 * "spellbadword()" function
18882 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018884f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018885{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018886 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018887 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018888 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018889
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018890 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018891 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018892
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018893#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018894 if (argvars[0].v_type == VAR_UNKNOWN)
18895 {
18896 /* Find the start and length of the badly spelled word. */
18897 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18898 if (len != 0)
18899 word = ml_get_cursor();
18900 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018901 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018902 {
18903 char_u *str = get_tv_string_chk(&argvars[0]);
18904 int capcol = -1;
18905
18906 if (str != NULL)
18907 {
18908 /* Check the argument for spelling. */
18909 while (*str != NUL)
18910 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018911 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018912 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018913 {
18914 word = str;
18915 break;
18916 }
18917 str += len;
18918 }
18919 }
18920 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018921#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018922
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018923 list_append_string(rettv->vval.v_list, word, len);
18924 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018925 attr == HLF_SPB ? "bad" :
18926 attr == HLF_SPR ? "rare" :
18927 attr == HLF_SPL ? "local" :
18928 attr == HLF_SPC ? "caps" :
18929 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018930}
18931
18932/*
18933 * "spellsuggest()" function
18934 */
18935 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018936f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018937{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018938#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018939 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018940 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018941 int maxcount;
18942 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018943 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018944 listitem_T *li;
18945 int need_capital = FALSE;
18946#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018947
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018948 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018949 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018950
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018951#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018952 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018953 {
18954 str = get_tv_string(&argvars[0]);
18955 if (argvars[1].v_type != VAR_UNKNOWN)
18956 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018957 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018958 if (maxcount <= 0)
18959 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018960 if (argvars[2].v_type != VAR_UNKNOWN)
18961 {
18962 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18963 if (typeerr)
18964 return;
18965 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018966 }
18967 else
18968 maxcount = 25;
18969
Bram Moolenaar4770d092006-01-12 23:22:24 +000018970 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018971
18972 for (i = 0; i < ga.ga_len; ++i)
18973 {
18974 str = ((char_u **)ga.ga_data)[i];
18975
18976 li = listitem_alloc();
18977 if (li == NULL)
18978 vim_free(str);
18979 else
18980 {
18981 li->li_tv.v_type = VAR_STRING;
18982 li->li_tv.v_lock = 0;
18983 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018984 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018985 }
18986 }
18987 ga_clear(&ga);
18988 }
18989#endif
18990}
18991
Bram Moolenaar0d660222005-01-07 21:51:51 +000018992 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018993f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018994{
18995 char_u *str;
18996 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018997 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018998 regmatch_T regmatch;
18999 char_u patbuf[NUMBUFLEN];
19000 char_u *save_cpo;
19001 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019002 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019003 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019004 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019005
19006 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19007 save_cpo = p_cpo;
19008 p_cpo = (char_u *)"";
19009
19010 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019011 if (argvars[1].v_type != VAR_UNKNOWN)
19012 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019013 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19014 if (pat == NULL)
19015 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019016 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019017 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019018 }
19019 if (pat == NULL || *pat == NUL)
19020 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019021
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019022 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019023 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019024 if (typeerr)
19025 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019026
Bram Moolenaar0d660222005-01-07 21:51:51 +000019027 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19028 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019029 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019030 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019031 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019032 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019033 if (*str == NUL)
19034 match = FALSE; /* empty item at the end */
19035 else
19036 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019037 if (match)
19038 end = regmatch.startp[0];
19039 else
19040 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019041 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19042 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019043 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019044 if (list_append_string(rettv->vval.v_list, str,
19045 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019046 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019047 }
19048 if (!match)
19049 break;
19050 /* Advance to just after the match. */
19051 if (regmatch.endp[0] > str)
19052 col = 0;
19053 else
19054 {
19055 /* Don't get stuck at the same match. */
19056#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019057 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019058#else
19059 col = 1;
19060#endif
19061 }
19062 str = regmatch.endp[0];
19063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019064
Bram Moolenaar473de612013-06-08 18:19:48 +020019065 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019067
Bram Moolenaar0d660222005-01-07 21:51:51 +000019068 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019069}
19070
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019071#ifdef FEAT_FLOAT
19072/*
19073 * "sqrt()" function
19074 */
19075 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019076f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019077{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019078 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019079
19080 rettv->v_type = VAR_FLOAT;
19081 if (get_float_arg(argvars, &f) == OK)
19082 rettv->vval.v_float = sqrt(f);
19083 else
19084 rettv->vval.v_float = 0.0;
19085}
19086
19087/*
19088 * "str2float()" function
19089 */
19090 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019091f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019092{
19093 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19094
19095 if (*p == '+')
19096 p = skipwhite(p + 1);
19097 (void)string2float(p, &rettv->vval.v_float);
19098 rettv->v_type = VAR_FLOAT;
19099}
19100#endif
19101
Bram Moolenaar2c932302006-03-18 21:42:09 +000019102/*
19103 * "str2nr()" function
19104 */
19105 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019106f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019107{
19108 int base = 10;
19109 char_u *p;
19110 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019111 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019112
19113 if (argvars[1].v_type != VAR_UNKNOWN)
19114 {
19115 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019116 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019117 {
19118 EMSG(_(e_invarg));
19119 return;
19120 }
19121 }
19122
19123 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019124 if (*p == '+')
19125 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019126 switch (base)
19127 {
19128 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19129 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19130 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19131 default: what = 0;
19132 }
19133 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019134 rettv->vval.v_number = n;
19135}
19136
Bram Moolenaar071d4272004-06-13 20:20:40 +000019137#ifdef HAVE_STRFTIME
19138/*
19139 * "strftime({format}[, {time}])" function
19140 */
19141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019142f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019143{
19144 char_u result_buf[256];
19145 struct tm *curtime;
19146 time_t seconds;
19147 char_u *p;
19148
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019149 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019150
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019151 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019152 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019153 seconds = time(NULL);
19154 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019155 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019156 curtime = localtime(&seconds);
19157 /* MSVC returns NULL for an invalid value of seconds. */
19158 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019159 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019160 else
19161 {
19162# ifdef FEAT_MBYTE
19163 vimconv_T conv;
19164 char_u *enc;
19165
19166 conv.vc_type = CONV_NONE;
19167 enc = enc_locale();
19168 convert_setup(&conv, p_enc, enc);
19169 if (conv.vc_type != CONV_NONE)
19170 p = string_convert(&conv, p, NULL);
19171# endif
19172 if (p != NULL)
19173 (void)strftime((char *)result_buf, sizeof(result_buf),
19174 (char *)p, curtime);
19175 else
19176 result_buf[0] = NUL;
19177
19178# ifdef FEAT_MBYTE
19179 if (conv.vc_type != CONV_NONE)
19180 vim_free(p);
19181 convert_setup(&conv, enc, p_enc);
19182 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019183 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019184 else
19185# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019186 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019187
19188# ifdef FEAT_MBYTE
19189 /* Release conversion descriptors */
19190 convert_setup(&conv, NULL, NULL);
19191 vim_free(enc);
19192# endif
19193 }
19194}
19195#endif
19196
19197/*
19198 * "stridx()" function
19199 */
19200 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019201f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019202{
19203 char_u buf[NUMBUFLEN];
19204 char_u *needle;
19205 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019206 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019207 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019208 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019209
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019210 needle = get_tv_string_chk(&argvars[1]);
19211 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019212 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019213 if (needle == NULL || haystack == NULL)
19214 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019215
Bram Moolenaar33570922005-01-25 22:26:29 +000019216 if (argvars[2].v_type != VAR_UNKNOWN)
19217 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019218 int error = FALSE;
19219
19220 start_idx = get_tv_number_chk(&argvars[2], &error);
19221 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019222 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019223 if (start_idx >= 0)
19224 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019225 }
19226
19227 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19228 if (pos != NULL)
19229 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019230}
19231
19232/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019233 * "string()" function
19234 */
19235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019236f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019237{
19238 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019239 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019240
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019241 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019242 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019243 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019244 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019245 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019246}
19247
19248/*
19249 * "strlen()" function
19250 */
19251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019252f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019253{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019254 rettv->vval.v_number = (varnumber_T)(STRLEN(
19255 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019256}
19257
19258/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019259 * "strchars()" function
19260 */
19261 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019262f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019263{
19264 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019265 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019266#ifdef FEAT_MBYTE
19267 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019268 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019269#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019270
19271 if (argvars[1].v_type != VAR_UNKNOWN)
19272 skipcc = get_tv_number_chk(&argvars[1], NULL);
19273 if (skipcc < 0 || skipcc > 1)
19274 EMSG(_(e_invarg));
19275 else
19276 {
19277#ifdef FEAT_MBYTE
19278 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19279 while (*s != NUL)
19280 {
19281 func_mb_ptr2char_adv(&s);
19282 ++len;
19283 }
19284 rettv->vval.v_number = len;
19285#else
19286 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19287#endif
19288 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019289}
19290
19291/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019292 * "strdisplaywidth()" function
19293 */
19294 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019295f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019296{
19297 char_u *s = get_tv_string(&argvars[0]);
19298 int col = 0;
19299
19300 if (argvars[1].v_type != VAR_UNKNOWN)
19301 col = get_tv_number(&argvars[1]);
19302
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019303 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019304}
19305
19306/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019307 * "strwidth()" function
19308 */
19309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019310f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019311{
19312 char_u *s = get_tv_string(&argvars[0]);
19313
19314 rettv->vval.v_number = (varnumber_T)(
19315#ifdef FEAT_MBYTE
19316 mb_string2cells(s, -1)
19317#else
19318 STRLEN(s)
19319#endif
19320 );
19321}
19322
19323/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019324 * "strpart()" function
19325 */
19326 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019327f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019328{
19329 char_u *p;
19330 int n;
19331 int len;
19332 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019333 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019334
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019335 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019336 slen = (int)STRLEN(p);
19337
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019338 n = get_tv_number_chk(&argvars[1], &error);
19339 if (error)
19340 len = 0;
19341 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019342 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019343 else
19344 len = slen - n; /* default len: all bytes that are available. */
19345
19346 /*
19347 * Only return the overlap between the specified part and the actual
19348 * string.
19349 */
19350 if (n < 0)
19351 {
19352 len += n;
19353 n = 0;
19354 }
19355 else if (n > slen)
19356 n = slen;
19357 if (len < 0)
19358 len = 0;
19359 else if (n + len > slen)
19360 len = slen - n;
19361
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019362 rettv->v_type = VAR_STRING;
19363 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019364}
19365
19366/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019367 * "strridx()" function
19368 */
19369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019370f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019371{
19372 char_u buf[NUMBUFLEN];
19373 char_u *needle;
19374 char_u *haystack;
19375 char_u *rest;
19376 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019377 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019378
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019379 needle = get_tv_string_chk(&argvars[1]);
19380 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019381
19382 rettv->vval.v_number = -1;
19383 if (needle == NULL || haystack == NULL)
19384 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019385
19386 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019387 if (argvars[2].v_type != VAR_UNKNOWN)
19388 {
19389 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019390 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019391 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019392 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019393 }
19394 else
19395 end_idx = haystack_len;
19396
Bram Moolenaar0d660222005-01-07 21:51:51 +000019397 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019398 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019399 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019400 lastmatch = haystack + end_idx;
19401 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019402 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019403 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019404 for (rest = haystack; *rest != '\0'; ++rest)
19405 {
19406 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019407 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019408 break;
19409 lastmatch = rest;
19410 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019411 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019412
19413 if (lastmatch == NULL)
19414 rettv->vval.v_number = -1;
19415 else
19416 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19417}
19418
19419/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019420 * "strtrans()" function
19421 */
19422 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019423f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019424{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019425 rettv->v_type = VAR_STRING;
19426 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019427}
19428
19429/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019430 * "submatch()" function
19431 */
19432 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019433f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019434{
Bram Moolenaar41571762014-04-02 19:00:58 +020019435 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019436 int no;
19437 int retList = 0;
19438
19439 no = (int)get_tv_number_chk(&argvars[0], &error);
19440 if (error)
19441 return;
19442 error = FALSE;
19443 if (argvars[1].v_type != VAR_UNKNOWN)
19444 retList = get_tv_number_chk(&argvars[1], &error);
19445 if (error)
19446 return;
19447
19448 if (retList == 0)
19449 {
19450 rettv->v_type = VAR_STRING;
19451 rettv->vval.v_string = reg_submatch(no);
19452 }
19453 else
19454 {
19455 rettv->v_type = VAR_LIST;
19456 rettv->vval.v_list = reg_submatch_list(no);
19457 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019458}
19459
19460/*
19461 * "substitute()" function
19462 */
19463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019464f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019465{
19466 char_u patbuf[NUMBUFLEN];
19467 char_u subbuf[NUMBUFLEN];
19468 char_u flagsbuf[NUMBUFLEN];
19469
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019470 char_u *str = get_tv_string_chk(&argvars[0]);
19471 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19472 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19473 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19474
Bram Moolenaar0d660222005-01-07 21:51:51 +000019475 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019476 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19477 rettv->vval.v_string = NULL;
19478 else
19479 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019480}
19481
19482/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019483 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019484 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019485 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019486f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019487{
19488 int id = 0;
19489#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019490 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019491 long col;
19492 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019493 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019494
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019495 lnum = get_tv_lnum(argvars); /* -1 on type error */
19496 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19497 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019498
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019499 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019500 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019501 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019502#endif
19503
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019504 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019505}
19506
19507/*
19508 * "synIDattr(id, what [, mode])" function
19509 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019510 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019511f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019512{
19513 char_u *p = NULL;
19514#ifdef FEAT_SYN_HL
19515 int id;
19516 char_u *what;
19517 char_u *mode;
19518 char_u modebuf[NUMBUFLEN];
19519 int modec;
19520
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019521 id = get_tv_number(&argvars[0]);
19522 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019523 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019524 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019525 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019526 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019527 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019528 modec = 0; /* replace invalid with current */
19529 }
19530 else
19531 {
19532#ifdef FEAT_GUI
19533 if (gui.in_use)
19534 modec = 'g';
19535 else
19536#endif
19537 if (t_colors > 1)
19538 modec = 'c';
19539 else
19540 modec = 't';
19541 }
19542
19543
19544 switch (TOLOWER_ASC(what[0]))
19545 {
19546 case 'b':
19547 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19548 p = highlight_color(id, what, modec);
19549 else /* bold */
19550 p = highlight_has_attr(id, HL_BOLD, modec);
19551 break;
19552
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019553 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019554 p = highlight_color(id, what, modec);
19555 break;
19556
19557 case 'i':
19558 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19559 p = highlight_has_attr(id, HL_INVERSE, modec);
19560 else /* italic */
19561 p = highlight_has_attr(id, HL_ITALIC, modec);
19562 break;
19563
19564 case 'n': /* name */
19565 p = get_highlight_name(NULL, id - 1);
19566 break;
19567
19568 case 'r': /* reverse */
19569 p = highlight_has_attr(id, HL_INVERSE, modec);
19570 break;
19571
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019572 case 's':
19573 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19574 p = highlight_color(id, what, modec);
19575 else /* standout */
19576 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019577 break;
19578
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019579 case 'u':
19580 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19581 /* underline */
19582 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19583 else
19584 /* undercurl */
19585 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019586 break;
19587 }
19588
19589 if (p != NULL)
19590 p = vim_strsave(p);
19591#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019592 rettv->v_type = VAR_STRING;
19593 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019594}
19595
19596/*
19597 * "synIDtrans(id)" function
19598 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019600f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019601{
19602 int id;
19603
19604#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019605 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019606
19607 if (id > 0)
19608 id = syn_get_final_id(id);
19609 else
19610#endif
19611 id = 0;
19612
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019613 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019614}
19615
19616/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019617 * "synconcealed(lnum, col)" function
19618 */
19619 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019620f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019621{
19622#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19623 long lnum;
19624 long col;
19625 int syntax_flags = 0;
19626 int cchar;
19627 int matchid = 0;
19628 char_u str[NUMBUFLEN];
19629#endif
19630
19631 rettv->v_type = VAR_LIST;
19632 rettv->vval.v_list = NULL;
19633
19634#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19635 lnum = get_tv_lnum(argvars); /* -1 on type error */
19636 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19637
19638 vim_memset(str, NUL, sizeof(str));
19639
19640 if (rettv_list_alloc(rettv) != FAIL)
19641 {
19642 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19643 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19644 && curwin->w_p_cole > 0)
19645 {
19646 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19647 syntax_flags = get_syntax_info(&matchid);
19648
19649 /* get the conceal character */
19650 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19651 {
19652 cchar = syn_get_sub_char();
19653 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19654 cchar = lcs_conceal;
19655 if (cchar != NUL)
19656 {
19657# ifdef FEAT_MBYTE
19658 if (has_mbyte)
19659 (*mb_char2bytes)(cchar, str);
19660 else
19661# endif
19662 str[0] = cchar;
19663 }
19664 }
19665 }
19666
19667 list_append_number(rettv->vval.v_list,
19668 (syntax_flags & HL_CONCEAL) != 0);
19669 /* -1 to auto-determine strlen */
19670 list_append_string(rettv->vval.v_list, str, -1);
19671 list_append_number(rettv->vval.v_list, matchid);
19672 }
19673#endif
19674}
19675
19676/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019677 * "synstack(lnum, col)" function
19678 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019679 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019680f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019681{
19682#ifdef FEAT_SYN_HL
19683 long lnum;
19684 long col;
19685 int i;
19686 int id;
19687#endif
19688
19689 rettv->v_type = VAR_LIST;
19690 rettv->vval.v_list = NULL;
19691
19692#ifdef FEAT_SYN_HL
19693 lnum = get_tv_lnum(argvars); /* -1 on type error */
19694 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19695
19696 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019697 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019698 && rettv_list_alloc(rettv) != FAIL)
19699 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019700 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019701 for (i = 0; ; ++i)
19702 {
19703 id = syn_get_stack_item(i);
19704 if (id < 0)
19705 break;
19706 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19707 break;
19708 }
19709 }
19710#endif
19711}
19712
Bram Moolenaar071d4272004-06-13 20:20:40 +000019713 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019714get_cmd_output_as_rettv(
19715 typval_T *argvars,
19716 typval_T *rettv,
19717 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019718{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019719 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019720 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019721 char_u *infile = NULL;
19722 char_u buf[NUMBUFLEN];
19723 int err = FALSE;
19724 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019725 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019726 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019727
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019728 rettv->v_type = VAR_STRING;
19729 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019730 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019731 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019732
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019733 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019734 {
19735 /*
19736 * Write the string to a temp file, to be used for input of the shell
19737 * command.
19738 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019739 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019740 {
19741 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019742 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019743 }
19744
19745 fd = mch_fopen((char *)infile, WRITEBIN);
19746 if (fd == NULL)
19747 {
19748 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019749 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019750 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019751 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019752 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019753 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19754 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019755 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019756 else
19757 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019758 size_t len;
19759
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019760 p = get_tv_string_buf_chk(&argvars[1], buf);
19761 if (p == NULL)
19762 {
19763 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019764 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019765 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019766 len = STRLEN(p);
19767 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019768 err = TRUE;
19769 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019770 if (fclose(fd) != 0)
19771 err = TRUE;
19772 if (err)
19773 {
19774 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019775 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019776 }
19777 }
19778
Bram Moolenaar52a72462014-08-29 15:53:52 +020019779 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19780 * echoes typeahead, that messes up the display. */
19781 if (!msg_silent)
19782 flags += SHELL_COOKED;
19783
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019784 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019785 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019786 int len;
19787 listitem_T *li;
19788 char_u *s = NULL;
19789 char_u *start;
19790 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019791 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019792
Bram Moolenaar52a72462014-08-29 15:53:52 +020019793 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019794 if (res == NULL)
19795 goto errret;
19796
19797 list = list_alloc();
19798 if (list == NULL)
19799 goto errret;
19800
19801 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019802 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019803 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019804 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019805 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019806 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019807
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019808 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019809 if (s == NULL)
19810 goto errret;
19811
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019812 for (p = s; start < end; ++p, ++start)
19813 *p = *start == NUL ? NL : *start;
19814 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019815
19816 li = listitem_alloc();
19817 if (li == NULL)
19818 {
19819 vim_free(s);
19820 goto errret;
19821 }
19822 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019823 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019824 li->li_tv.vval.v_string = s;
19825 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019826 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019827
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019828 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019829 rettv->v_type = VAR_LIST;
19830 rettv->vval.v_list = list;
19831 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019832 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019833 else
19834 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019835 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019836#ifdef USE_CR
19837 /* translate <CR> into <NL> */
19838 if (res != NULL)
19839 {
19840 char_u *s;
19841
19842 for (s = res; *s; ++s)
19843 {
19844 if (*s == CAR)
19845 *s = NL;
19846 }
19847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019848#else
19849# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019850 /* translate <CR><NL> into <NL> */
19851 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019852 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019853 char_u *s, *d;
19854
19855 d = res;
19856 for (s = res; *s; ++s)
19857 {
19858 if (s[0] == CAR && s[1] == NL)
19859 ++s;
19860 *d++ = *s;
19861 }
19862 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019864# endif
19865#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019866 rettv->vval.v_string = res;
19867 res = NULL;
19868 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019869
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019870errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019871 if (infile != NULL)
19872 {
19873 mch_remove(infile);
19874 vim_free(infile);
19875 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019876 if (res != NULL)
19877 vim_free(res);
19878 if (list != NULL)
19879 list_free(list, TRUE);
19880}
19881
19882/*
19883 * "system()" function
19884 */
19885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019886f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019887{
19888 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19889}
19890
19891/*
19892 * "systemlist()" function
19893 */
19894 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019895f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019896{
19897 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019898}
19899
19900/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019901 * "tabpagebuflist()" function
19902 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019903 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019904f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019905{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019906#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019907 tabpage_T *tp;
19908 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019909
19910 if (argvars[0].v_type == VAR_UNKNOWN)
19911 wp = firstwin;
19912 else
19913 {
19914 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19915 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019916 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019917 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019918 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019919 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019920 for (; wp != NULL; wp = wp->w_next)
19921 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019922 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019923 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019924 }
19925#endif
19926}
19927
19928
19929/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019930 * "tabpagenr()" function
19931 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019933f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019934{
19935 int nr = 1;
19936#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019937 char_u *arg;
19938
19939 if (argvars[0].v_type != VAR_UNKNOWN)
19940 {
19941 arg = get_tv_string_chk(&argvars[0]);
19942 nr = 0;
19943 if (arg != NULL)
19944 {
19945 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019946 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019947 else
19948 EMSG2(_(e_invexpr2), arg);
19949 }
19950 }
19951 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019952 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019953#endif
19954 rettv->vval.v_number = nr;
19955}
19956
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019957
19958#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019959static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019960
19961/*
19962 * Common code for tabpagewinnr() and winnr().
19963 */
19964 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019965get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019966{
19967 win_T *twin;
19968 int nr = 1;
19969 win_T *wp;
19970 char_u *arg;
19971
19972 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19973 if (argvar->v_type != VAR_UNKNOWN)
19974 {
19975 arg = get_tv_string_chk(argvar);
19976 if (arg == NULL)
19977 nr = 0; /* type error; errmsg already given */
19978 else if (STRCMP(arg, "$") == 0)
19979 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19980 else if (STRCMP(arg, "#") == 0)
19981 {
19982 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19983 if (twin == NULL)
19984 nr = 0;
19985 }
19986 else
19987 {
19988 EMSG2(_(e_invexpr2), arg);
19989 nr = 0;
19990 }
19991 }
19992
19993 if (nr > 0)
19994 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19995 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019996 {
19997 if (wp == NULL)
19998 {
19999 /* didn't find it in this tabpage */
20000 nr = 0;
20001 break;
20002 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020003 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020004 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020005 return nr;
20006}
20007#endif
20008
20009/*
20010 * "tabpagewinnr()" function
20011 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020012 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020013f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020014{
20015 int nr = 1;
20016#ifdef FEAT_WINDOWS
20017 tabpage_T *tp;
20018
20019 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20020 if (tp == NULL)
20021 nr = 0;
20022 else
20023 nr = get_winnr(tp, &argvars[1]);
20024#endif
20025 rettv->vval.v_number = nr;
20026}
20027
20028
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020029/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020030 * "tagfiles()" function
20031 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020032 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020033f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020034{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020035 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020036 tagname_T tn;
20037 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020038
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020039 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020040 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020041 fname = alloc(MAXPATHL);
20042 if (fname == NULL)
20043 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020044
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020045 for (first = TRUE; ; first = FALSE)
20046 if (get_tagfname(&tn, first, fname) == FAIL
20047 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020048 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020049 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020050 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020051}
20052
20053/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020054 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020055 */
20056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020057f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020058{
20059 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020060
20061 tag_pattern = get_tv_string(&argvars[0]);
20062
20063 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020064 if (*tag_pattern == NUL)
20065 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020066
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020067 if (rettv_list_alloc(rettv) == OK)
20068 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020069}
20070
20071/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020072 * "tempname()" function
20073 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020074 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020075f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020076{
20077 static int x = 'A';
20078
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020079 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020080 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020081
20082 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20083 * names. Skip 'I' and 'O', they are used for shell redirection. */
20084 do
20085 {
20086 if (x == 'Z')
20087 x = '0';
20088 else if (x == '9')
20089 x = 'A';
20090 else
20091 {
20092#ifdef EBCDIC
20093 if (x == 'I')
20094 x = 'J';
20095 else if (x == 'R')
20096 x = 'S';
20097 else
20098#endif
20099 ++x;
20100 }
20101 } while (x == 'I' || x == 'O');
20102}
20103
20104/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020105 * "test(list)" function: Just checking the walls...
20106 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020107 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020108f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020109{
20110 /* Used for unit testing. Change the code below to your liking. */
20111#if 0
20112 listitem_T *li;
20113 list_T *l;
20114 char_u *bad, *good;
20115
20116 if (argvars[0].v_type != VAR_LIST)
20117 return;
20118 l = argvars[0].vval.v_list;
20119 if (l == NULL)
20120 return;
20121 li = l->lv_first;
20122 if (li == NULL)
20123 return;
20124 bad = get_tv_string(&li->li_tv);
20125 li = li->li_next;
20126 if (li == NULL)
20127 return;
20128 good = get_tv_string(&li->li_tv);
20129 rettv->vval.v_number = test_edit_score(bad, good);
20130#endif
20131}
20132
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020133#ifdef FEAT_FLOAT
20134/*
20135 * "tan()" function
20136 */
20137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020138f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020139{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020140 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020141
20142 rettv->v_type = VAR_FLOAT;
20143 if (get_float_arg(argvars, &f) == OK)
20144 rettv->vval.v_float = tan(f);
20145 else
20146 rettv->vval.v_float = 0.0;
20147}
20148
20149/*
20150 * "tanh()" function
20151 */
20152 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020153f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020154{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020155 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020156
20157 rettv->v_type = VAR_FLOAT;
20158 if (get_float_arg(argvars, &f) == OK)
20159 rettv->vval.v_float = tanh(f);
20160 else
20161 rettv->vval.v_float = 0.0;
20162}
20163#endif
20164
Bram Moolenaar975b5272016-03-15 23:10:59 +010020165#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
20166/*
20167 * Get a callback from "arg". It can be a Funcref or a function name.
20168 * When "arg" is zero return an empty string.
20169 * Return NULL for an invalid argument.
20170 */
20171 char_u *
20172get_callback(typval_T *arg, partial_T **pp)
20173{
20174 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
20175 {
20176 *pp = arg->vval.v_partial;
20177 return (*pp)->pt_name;
20178 }
20179 *pp = NULL;
20180 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
20181 return arg->vval.v_string;
20182 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
20183 return (char_u *)"";
20184 EMSG(_("E921: Invalid callback argument"));
20185 return NULL;
20186}
20187#endif
20188
20189#ifdef FEAT_TIMERS
20190/*
20191 * "timer_start(time, callback [, options])" function
20192 */
20193 static void
20194f_timer_start(typval_T *argvars, typval_T *rettv)
20195{
20196 long msec = get_tv_number(&argvars[0]);
20197 timer_T *timer;
20198 int repeat = 0;
20199 char_u *callback;
20200 dict_T *dict;
20201
20202 if (argvars[2].v_type != VAR_UNKNOWN)
20203 {
20204 if (argvars[2].v_type != VAR_DICT
20205 || (dict = argvars[2].vval.v_dict) == NULL)
20206 {
20207 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
20208 return;
20209 }
20210 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
20211 repeat = get_dict_number(dict, (char_u *)"repeat");
20212 }
20213
20214 timer = create_timer(msec, repeat);
20215 callback = get_callback(&argvars[1], &timer->tr_partial);
20216 if (callback == NULL)
20217 {
20218 stop_timer(timer);
20219 rettv->vval.v_number = -1;
20220 }
20221 else
20222 {
20223 timer->tr_callback = vim_strsave(callback);
20224 rettv->vval.v_number = timer->tr_id;
20225 }
20226}
20227
20228/*
20229 * "timer_stop(timer)" function
20230 */
20231 static void
20232f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
20233{
20234 timer_T *timer = find_timer(get_tv_number(&argvars[0]));
20235
20236 if (timer != NULL)
20237 stop_timer(timer);
20238}
20239#endif
20240
Bram Moolenaard52d9742005-08-21 22:20:28 +000020241/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020242 * "tolower(string)" function
20243 */
20244 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020245f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020246{
20247 char_u *p;
20248
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020249 p = vim_strsave(get_tv_string(&argvars[0]));
20250 rettv->v_type = VAR_STRING;
20251 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020252
20253 if (p != NULL)
20254 while (*p != NUL)
20255 {
20256#ifdef FEAT_MBYTE
20257 int l;
20258
20259 if (enc_utf8)
20260 {
20261 int c, lc;
20262
20263 c = utf_ptr2char(p);
20264 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020265 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020266 /* TODO: reallocate string when byte count changes. */
20267 if (utf_char2len(lc) == l)
20268 utf_char2bytes(lc, p);
20269 p += l;
20270 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020271 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020272 p += l; /* skip multi-byte character */
20273 else
20274#endif
20275 {
20276 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20277 ++p;
20278 }
20279 }
20280}
20281
20282/*
20283 * "toupper(string)" function
20284 */
20285 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020286f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020287{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020288 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020289 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020290}
20291
20292/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020293 * "tr(string, fromstr, tostr)" function
20294 */
20295 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020296f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020297{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020298 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020299 char_u *fromstr;
20300 char_u *tostr;
20301 char_u *p;
20302#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020303 int inlen;
20304 int fromlen;
20305 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020306 int idx;
20307 char_u *cpstr;
20308 int cplen;
20309 int first = TRUE;
20310#endif
20311 char_u buf[NUMBUFLEN];
20312 char_u buf2[NUMBUFLEN];
20313 garray_T ga;
20314
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020315 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020316 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20317 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020318
20319 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020320 rettv->v_type = VAR_STRING;
20321 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020322 if (fromstr == NULL || tostr == NULL)
20323 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020324 ga_init2(&ga, (int)sizeof(char), 80);
20325
20326#ifdef FEAT_MBYTE
20327 if (!has_mbyte)
20328#endif
20329 /* not multi-byte: fromstr and tostr must be the same length */
20330 if (STRLEN(fromstr) != STRLEN(tostr))
20331 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020332#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020333error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020334#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020335 EMSG2(_(e_invarg2), fromstr);
20336 ga_clear(&ga);
20337 return;
20338 }
20339
20340 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020341 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020342 {
20343#ifdef FEAT_MBYTE
20344 if (has_mbyte)
20345 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020346 inlen = (*mb_ptr2len)(in_str);
20347 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020348 cplen = inlen;
20349 idx = 0;
20350 for (p = fromstr; *p != NUL; p += fromlen)
20351 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020352 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020353 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020354 {
20355 for (p = tostr; *p != NUL; p += tolen)
20356 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020357 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020358 if (idx-- == 0)
20359 {
20360 cplen = tolen;
20361 cpstr = p;
20362 break;
20363 }
20364 }
20365 if (*p == NUL) /* tostr is shorter than fromstr */
20366 goto error;
20367 break;
20368 }
20369 ++idx;
20370 }
20371
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020372 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020373 {
20374 /* Check that fromstr and tostr have the same number of
20375 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020376 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020377 first = FALSE;
20378 for (p = tostr; *p != NUL; p += tolen)
20379 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020380 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020381 --idx;
20382 }
20383 if (idx != 0)
20384 goto error;
20385 }
20386
Bram Moolenaarcde88542015-08-11 19:14:00 +020020387 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020388 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020389 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020390
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020391 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020392 }
20393 else
20394#endif
20395 {
20396 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020397 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020398 if (p != NULL)
20399 ga_append(&ga, tostr[p - fromstr]);
20400 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020401 ga_append(&ga, *in_str);
20402 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020403 }
20404 }
20405
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020406 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020407 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020408 ga_append(&ga, NUL);
20409
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020410 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020411}
20412
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020413#ifdef FEAT_FLOAT
20414/*
20415 * "trunc({float})" function
20416 */
20417 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020418f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020419{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020420 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020421
20422 rettv->v_type = VAR_FLOAT;
20423 if (get_float_arg(argvars, &f) == OK)
20424 /* trunc() is not in C90, use floor() or ceil() instead. */
20425 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20426 else
20427 rettv->vval.v_float = 0.0;
20428}
20429#endif
20430
Bram Moolenaar8299df92004-07-10 09:47:34 +000020431/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020432 * "type(expr)" function
20433 */
20434 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020435f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020436{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020437 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020438
20439 switch (argvars[0].v_type)
20440 {
20441 case VAR_NUMBER: n = 0; break;
20442 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010020443 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020444 case VAR_FUNC: n = 2; break;
20445 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020446 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020447 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020448 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020449 if (argvars[0].vval.v_number == VVAL_FALSE
20450 || argvars[0].vval.v_number == VVAL_TRUE)
20451 n = 6;
20452 else
20453 n = 7;
20454 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020455 case VAR_JOB: n = 8; break;
20456 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020457 case VAR_UNKNOWN:
20458 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20459 n = -1;
20460 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020461 }
20462 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020463}
20464
20465/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020466 * "undofile(name)" function
20467 */
20468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020469f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020470{
20471 rettv->v_type = VAR_STRING;
20472#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020473 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020474 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020475
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020476 if (*fname == NUL)
20477 {
20478 /* If there is no file name there will be no undo file. */
20479 rettv->vval.v_string = NULL;
20480 }
20481 else
20482 {
20483 char_u *ffname = FullName_save(fname, FALSE);
20484
20485 if (ffname != NULL)
20486 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20487 vim_free(ffname);
20488 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020489 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020490#else
20491 rettv->vval.v_string = NULL;
20492#endif
20493}
20494
20495/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020496 * "undotree()" function
20497 */
20498 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020499f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020500{
20501 if (rettv_dict_alloc(rettv) == OK)
20502 {
20503 dict_T *dict = rettv->vval.v_dict;
20504 list_T *list;
20505
Bram Moolenaar730cde92010-06-27 05:18:54 +020020506 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020507 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020508 dict_add_nr_str(dict, "save_last",
20509 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020510 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20511 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020512 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020513
20514 list = list_alloc();
20515 if (list != NULL)
20516 {
20517 u_eval_tree(curbuf->b_u_oldhead, list);
20518 dict_add_list(dict, "entries", list);
20519 }
20520 }
20521}
20522
20523/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020524 * "values(dict)" function
20525 */
20526 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020527f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020528{
20529 dict_list(argvars, rettv, 1);
20530}
20531
20532/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020533 * "virtcol(string)" function
20534 */
20535 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020536f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020537{
20538 colnr_T vcol = 0;
20539 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020540 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020541
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020542 fp = var2fpos(&argvars[0], FALSE, &fnum);
20543 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20544 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020545 {
20546 getvvcol(curwin, fp, NULL, NULL, &vcol);
20547 ++vcol;
20548 }
20549
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020550 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020551}
20552
20553/*
20554 * "visualmode()" function
20555 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020556 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010020557f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020558{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020559 char_u str[2];
20560
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020561 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020562 str[0] = curbuf->b_visual_mode_eval;
20563 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020564 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020565
20566 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020567 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020568 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020569}
20570
20571/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020572 * "wildmenumode()" function
20573 */
20574 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020575f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020576{
20577#ifdef FEAT_WILDMENU
20578 if (wild_menu_showing)
20579 rettv->vval.v_number = 1;
20580#endif
20581}
20582
20583/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020584 * "winbufnr(nr)" function
20585 */
20586 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020587f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020588{
20589 win_T *wp;
20590
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020591 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020592 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020593 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020594 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020595 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020596}
20597
20598/*
20599 * "wincol()" function
20600 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020601 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020602f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020603{
20604 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020605 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020606}
20607
20608/*
20609 * "winheight(nr)" function
20610 */
20611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020612f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020613{
20614 win_T *wp;
20615
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020616 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020617 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020618 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020619 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020620 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020621}
20622
20623/*
20624 * "winline()" function
20625 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020626 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020627f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020628{
20629 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020630 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020631}
20632
20633/*
20634 * "winnr()" function
20635 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020636 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020637f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020638{
20639 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020640
Bram Moolenaar071d4272004-06-13 20:20:40 +000020641#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020642 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020643#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020644 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020645}
20646
20647/*
20648 * "winrestcmd()" function
20649 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020650 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020651f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020652{
20653#ifdef FEAT_WINDOWS
20654 win_T *wp;
20655 int winnr = 1;
20656 garray_T ga;
20657 char_u buf[50];
20658
20659 ga_init2(&ga, (int)sizeof(char), 70);
20660 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20661 {
20662 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20663 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020664 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20665 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020666 ++winnr;
20667 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020668 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020669
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020670 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020671#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020672 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020673#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020674 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020675}
20676
20677/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020678 * "winrestview()" function
20679 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020680 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020681f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020682{
20683 dict_T *dict;
20684
20685 if (argvars[0].v_type != VAR_DICT
20686 || (dict = argvars[0].vval.v_dict) == NULL)
20687 EMSG(_(e_invarg));
20688 else
20689 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020690 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20691 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20692 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20693 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020694#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020695 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20696 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020697#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020698 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20699 {
20700 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20701 curwin->w_set_curswant = FALSE;
20702 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020703
Bram Moolenaar82c25852014-05-28 16:47:16 +020020704 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20705 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020706#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020707 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20708 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020709#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020710 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20711 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20712 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20713 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020714
20715 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020716 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010020717# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020020718 win_new_width(curwin, W_WIDTH(curwin));
20719# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020720 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020721
Bram Moolenaarb851a962014-10-31 15:45:52 +010020722 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020723 curwin->w_topline = 1;
20724 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20725 curwin->w_topline = curbuf->b_ml.ml_line_count;
20726#ifdef FEAT_DIFF
20727 check_topfill(curwin, TRUE);
20728#endif
20729 }
20730}
20731
20732/*
20733 * "winsaveview()" function
20734 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020735 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020736f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020737{
20738 dict_T *dict;
20739
Bram Moolenaara800b422010-06-27 01:15:55 +020020740 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020741 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020742 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020743
20744 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20745 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20746#ifdef FEAT_VIRTUALEDIT
20747 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20748#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020749 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020750 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20751
20752 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20753#ifdef FEAT_DIFF
20754 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20755#endif
20756 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20757 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20758}
20759
20760/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020761 * "winwidth(nr)" function
20762 */
20763 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020764f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020765{
20766 win_T *wp;
20767
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020768 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020769 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020770 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020771 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010020772#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020773 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020774#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020775 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020776#endif
20777}
20778
Bram Moolenaar071d4272004-06-13 20:20:40 +000020779/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020780 * "wordcount()" function
20781 */
20782 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020783f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010020784{
20785 if (rettv_dict_alloc(rettv) == FAIL)
20786 return;
20787 cursor_pos_info(rettv->vval.v_dict);
20788}
20789
20790/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020791 * Write list of strings to file
20792 */
20793 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020794write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020795{
20796 listitem_T *li;
20797 int c;
20798 int ret = OK;
20799 char_u *s;
20800
20801 for (li = list->lv_first; li != NULL; li = li->li_next)
20802 {
20803 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20804 {
20805 if (*s == '\n')
20806 c = putc(NUL, fd);
20807 else
20808 c = putc(*s, fd);
20809 if (c == EOF)
20810 {
20811 ret = FAIL;
20812 break;
20813 }
20814 }
20815 if (!binary || li->li_next != NULL)
20816 if (putc('\n', fd) == EOF)
20817 {
20818 ret = FAIL;
20819 break;
20820 }
20821 if (ret == FAIL)
20822 {
20823 EMSG(_(e_write));
20824 break;
20825 }
20826 }
20827 return ret;
20828}
20829
20830/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020831 * "writefile()" function
20832 */
20833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020834f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020835{
20836 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020837 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020838 char_u *fname;
20839 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020840 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020841
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020842 if (check_restricted() || check_secure())
20843 return;
20844
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020845 if (argvars[0].v_type != VAR_LIST)
20846 {
20847 EMSG2(_(e_listarg), "writefile()");
20848 return;
20849 }
20850 if (argvars[0].vval.v_list == NULL)
20851 return;
20852
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020853 if (argvars[2].v_type != VAR_UNKNOWN)
20854 {
20855 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20856 binary = TRUE;
20857 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20858 append = TRUE;
20859 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020860
20861 /* Always open the file in binary mode, library functions have a mind of
20862 * their own about CR-LF conversion. */
20863 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020864 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20865 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020866 {
20867 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20868 ret = -1;
20869 }
20870 else
20871 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020872 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20873 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020874 fclose(fd);
20875 }
20876
20877 rettv->vval.v_number = ret;
20878}
20879
20880/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020881 * "xor(expr, expr)" function
20882 */
20883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020884f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020885{
20886 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20887 ^ get_tv_number_chk(&argvars[1], NULL);
20888}
20889
20890
20891/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020892 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020893 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020894 */
20895 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020896var2fpos(
20897 typval_T *varp,
20898 int dollar_lnum, /* TRUE when $ is last line */
20899 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020900{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020901 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020902 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020903 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020904
Bram Moolenaara5525202006-03-02 22:52:09 +000020905 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020906 if (varp->v_type == VAR_LIST)
20907 {
20908 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020909 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020910 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020911 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020912
20913 l = varp->vval.v_list;
20914 if (l == NULL)
20915 return NULL;
20916
20917 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020918 pos.lnum = list_find_nr(l, 0L, &error);
20919 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020920 return NULL; /* invalid line number */
20921
20922 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020923 pos.col = list_find_nr(l, 1L, &error);
20924 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020925 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020926 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020927
20928 /* We accept "$" for the column number: last column. */
20929 li = list_find(l, 1L);
20930 if (li != NULL && li->li_tv.v_type == VAR_STRING
20931 && li->li_tv.vval.v_string != NULL
20932 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20933 pos.col = len + 1;
20934
Bram Moolenaara5525202006-03-02 22:52:09 +000020935 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020936 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020937 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020938 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020939
Bram Moolenaara5525202006-03-02 22:52:09 +000020940#ifdef FEAT_VIRTUALEDIT
20941 /* Get the virtual offset. Defaults to zero. */
20942 pos.coladd = list_find_nr(l, 2L, &error);
20943 if (error)
20944 pos.coladd = 0;
20945#endif
20946
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020947 return &pos;
20948 }
20949
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020950 name = get_tv_string_chk(varp);
20951 if (name == NULL)
20952 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020953 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020954 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020955 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20956 {
20957 if (VIsual_active)
20958 return &VIsual;
20959 return &curwin->w_cursor;
20960 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020961 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020962 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020963 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020964 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20965 return NULL;
20966 return pp;
20967 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020968
20969#ifdef FEAT_VIRTUALEDIT
20970 pos.coladd = 0;
20971#endif
20972
Bram Moolenaar477933c2007-07-17 14:32:23 +000020973 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020974 {
20975 pos.col = 0;
20976 if (name[1] == '0') /* "w0": first visible line */
20977 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020978 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020979 pos.lnum = curwin->w_topline;
20980 return &pos;
20981 }
20982 else if (name[1] == '$') /* "w$": last visible line */
20983 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020984 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020985 pos.lnum = curwin->w_botline - 1;
20986 return &pos;
20987 }
20988 }
20989 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020990 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020991 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020992 {
20993 pos.lnum = curbuf->b_ml.ml_line_count;
20994 pos.col = 0;
20995 }
20996 else
20997 {
20998 pos.lnum = curwin->w_cursor.lnum;
20999 pos.col = (colnr_T)STRLEN(ml_get_curline());
21000 }
21001 return &pos;
21002 }
21003 return NULL;
21004}
21005
21006/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021007 * Convert list in "arg" into a position and optional file number.
21008 * When "fnump" is NULL there is no file number, only 3 items.
21009 * Note that the column is passed on as-is, the caller may want to decrement
21010 * it to use 1 for the first column.
21011 * Return FAIL when conversion is not possible, doesn't check the position for
21012 * validity.
21013 */
21014 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021015list2fpos(
21016 typval_T *arg,
21017 pos_T *posp,
21018 int *fnump,
21019 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021020{
21021 list_T *l = arg->vval.v_list;
21022 long i = 0;
21023 long n;
21024
Bram Moolenaar493c1782014-05-28 14:34:46 +020021025 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21026 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021027 if (arg->v_type != VAR_LIST
21028 || l == NULL
21029 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021030 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021031 return FAIL;
21032
21033 if (fnump != NULL)
21034 {
21035 n = list_find_nr(l, i++, NULL); /* fnum */
21036 if (n < 0)
21037 return FAIL;
21038 if (n == 0)
21039 n = curbuf->b_fnum; /* current buffer */
21040 *fnump = n;
21041 }
21042
21043 n = list_find_nr(l, i++, NULL); /* lnum */
21044 if (n < 0)
21045 return FAIL;
21046 posp->lnum = n;
21047
21048 n = list_find_nr(l, i++, NULL); /* col */
21049 if (n < 0)
21050 return FAIL;
21051 posp->col = n;
21052
21053#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021054 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021055 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021056 posp->coladd = 0;
21057 else
21058 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021059#endif
21060
Bram Moolenaar493c1782014-05-28 14:34:46 +020021061 if (curswantp != NULL)
21062 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21063
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021064 return OK;
21065}
21066
21067/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021068 * Get the length of an environment variable name.
21069 * Advance "arg" to the first character after the name.
21070 * Return 0 for error.
21071 */
21072 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021073get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021074{
21075 char_u *p;
21076 int len;
21077
21078 for (p = *arg; vim_isIDc(*p); ++p)
21079 ;
21080 if (p == *arg) /* no name found */
21081 return 0;
21082
21083 len = (int)(p - *arg);
21084 *arg = p;
21085 return len;
21086}
21087
21088/*
21089 * Get the length of the name of a function or internal variable.
21090 * "arg" is advanced to the first non-white character after the name.
21091 * Return 0 if something is wrong.
21092 */
21093 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021094get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021095{
21096 char_u *p;
21097 int len;
21098
21099 /* Find the end of the name. */
21100 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021101 {
21102 if (*p == ':')
21103 {
21104 /* "s:" is start of "s:var", but "n:" is not and can be used in
21105 * slice "[n:]". Also "xx:" is not a namespace. */
21106 len = (int)(p - *arg);
21107 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21108 || len > 1)
21109 break;
21110 }
21111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021112 if (p == *arg) /* no name found */
21113 return 0;
21114
21115 len = (int)(p - *arg);
21116 *arg = skipwhite(p);
21117
21118 return len;
21119}
21120
21121/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021122 * Get the length of the name of a variable or function.
21123 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021124 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021125 * Return -1 if curly braces expansion failed.
21126 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021127 * If the name contains 'magic' {}'s, expand them and return the
21128 * expanded name in an allocated string via 'alias' - caller must free.
21129 */
21130 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021131get_name_len(
21132 char_u **arg,
21133 char_u **alias,
21134 int evaluate,
21135 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021136{
21137 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021138 char_u *p;
21139 char_u *expr_start;
21140 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021141
21142 *alias = NULL; /* default to no alias */
21143
21144 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21145 && (*arg)[2] == (int)KE_SNR)
21146 {
21147 /* hard coded <SNR>, already translated */
21148 *arg += 3;
21149 return get_id_len(arg) + 3;
21150 }
21151 len = eval_fname_script(*arg);
21152 if (len > 0)
21153 {
21154 /* literal "<SID>", "s:" or "<SNR>" */
21155 *arg += len;
21156 }
21157
Bram Moolenaar071d4272004-06-13 20:20:40 +000021158 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021159 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021160 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021161 p = find_name_end(*arg, &expr_start, &expr_end,
21162 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021163 if (expr_start != NULL)
21164 {
21165 char_u *temp_string;
21166
21167 if (!evaluate)
21168 {
21169 len += (int)(p - *arg);
21170 *arg = skipwhite(p);
21171 return len;
21172 }
21173
21174 /*
21175 * Include any <SID> etc in the expanded string:
21176 * Thus the -len here.
21177 */
21178 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21179 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021180 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021181 *alias = temp_string;
21182 *arg = skipwhite(p);
21183 return (int)STRLEN(temp_string);
21184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021185
21186 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021187 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021188 EMSG2(_(e_invexpr2), *arg);
21189
21190 return len;
21191}
21192
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021193/*
21194 * Find the end of a variable or function name, taking care of magic braces.
21195 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21196 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021197 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021198 * Return a pointer to just after the name. Equal to "arg" if there is no
21199 * valid name.
21200 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021201 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021202find_name_end(
21203 char_u *arg,
21204 char_u **expr_start,
21205 char_u **expr_end,
21206 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021207{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021208 int mb_nest = 0;
21209 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021210 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021211 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021212
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021213 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021214 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021215 *expr_start = NULL;
21216 *expr_end = NULL;
21217 }
21218
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021219 /* Quick check for valid starting character. */
21220 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21221 return arg;
21222
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021223 for (p = arg; *p != NUL
21224 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021225 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021226 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021227 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021228 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021229 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021230 if (*p == '\'')
21231 {
21232 /* skip over 'string' to avoid counting [ and ] inside it. */
21233 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21234 ;
21235 if (*p == NUL)
21236 break;
21237 }
21238 else if (*p == '"')
21239 {
21240 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21241 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21242 if (*p == '\\' && p[1] != NUL)
21243 ++p;
21244 if (*p == NUL)
21245 break;
21246 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021247 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21248 {
21249 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021250 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021251 len = (int)(p - arg);
21252 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021253 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021254 break;
21255 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021256
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021257 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021258 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021259 if (*p == '[')
21260 ++br_nest;
21261 else if (*p == ']')
21262 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021263 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021264
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021265 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021266 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021267 if (*p == '{')
21268 {
21269 mb_nest++;
21270 if (expr_start != NULL && *expr_start == NULL)
21271 *expr_start = p;
21272 }
21273 else if (*p == '}')
21274 {
21275 mb_nest--;
21276 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21277 *expr_end = p;
21278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021280 }
21281
21282 return p;
21283}
21284
21285/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021286 * Expands out the 'magic' {}'s in a variable/function name.
21287 * Note that this can call itself recursively, to deal with
21288 * constructs like foo{bar}{baz}{bam}
21289 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21290 * "in_start" ^
21291 * "expr_start" ^
21292 * "expr_end" ^
21293 * "in_end" ^
21294 *
21295 * Returns a new allocated string, which the caller must free.
21296 * Returns NULL for failure.
21297 */
21298 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021299make_expanded_name(
21300 char_u *in_start,
21301 char_u *expr_start,
21302 char_u *expr_end,
21303 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021304{
21305 char_u c1;
21306 char_u *retval = NULL;
21307 char_u *temp_result;
21308 char_u *nextcmd = NULL;
21309
21310 if (expr_end == NULL || in_end == NULL)
21311 return NULL;
21312 *expr_start = NUL;
21313 *expr_end = NUL;
21314 c1 = *in_end;
21315 *in_end = NUL;
21316
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021317 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021318 if (temp_result != NULL && nextcmd == NULL)
21319 {
21320 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21321 + (in_end - expr_end) + 1));
21322 if (retval != NULL)
21323 {
21324 STRCPY(retval, in_start);
21325 STRCAT(retval, temp_result);
21326 STRCAT(retval, expr_end + 1);
21327 }
21328 }
21329 vim_free(temp_result);
21330
21331 *in_end = c1; /* put char back for error messages */
21332 *expr_start = '{';
21333 *expr_end = '}';
21334
21335 if (retval != NULL)
21336 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021337 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021338 if (expr_start != NULL)
21339 {
21340 /* Further expansion! */
21341 temp_result = make_expanded_name(retval, expr_start,
21342 expr_end, temp_result);
21343 vim_free(retval);
21344 retval = temp_result;
21345 }
21346 }
21347
21348 return retval;
21349}
21350
21351/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021352 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021353 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021354 */
21355 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021356eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021357{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021358 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21359}
21360
21361/*
21362 * Return TRUE if character "c" can be used as the first character in a
21363 * variable or function name (excluding '{' and '}').
21364 */
21365 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021366eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021367{
21368 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021369}
21370
21371/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021372 * Set number v: variable to "val".
21373 */
21374 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021375set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021376{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021377 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021378}
21379
21380/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021381 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021382 */
21383 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021384get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021385{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021386 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021387}
21388
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021389/*
21390 * Get string v: variable value. Uses a static buffer, can only be used once.
21391 */
21392 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021393get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021394{
21395 return get_tv_string(&vimvars[idx].vv_tv);
21396}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021397
Bram Moolenaar071d4272004-06-13 20:20:40 +000021398/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021399 * Get List v: variable value. Caller must take care of reference count when
21400 * needed.
21401 */
21402 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021403get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021404{
21405 return vimvars[idx].vv_list;
21406}
21407
21408/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021409 * Set v:char to character "c".
21410 */
21411 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021412set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021413{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021414 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021415
21416#ifdef FEAT_MBYTE
21417 if (has_mbyte)
21418 buf[(*mb_char2bytes)(c, buf)] = NUL;
21419 else
21420#endif
21421 {
21422 buf[0] = c;
21423 buf[1] = NUL;
21424 }
21425 set_vim_var_string(VV_CHAR, buf, -1);
21426}
21427
21428/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021429 * Set v:count to "count" and v:count1 to "count1".
21430 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021431 */
21432 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021433set_vcount(
21434 long count,
21435 long count1,
21436 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021437{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021438 if (set_prevcount)
21439 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021440 vimvars[VV_COUNT].vv_nr = count;
21441 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021442}
21443
21444/*
21445 * Set string v: variable to a copy of "val".
21446 */
21447 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021448set_vim_var_string(
21449 int idx,
21450 char_u *val,
21451 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021452{
Bram Moolenaara542c682016-01-31 16:28:04 +010021453 clear_tv(&vimvars[idx].vv_di.di_tv);
21454 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021455 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021456 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021457 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021458 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021459 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021460 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021461}
21462
21463/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021464 * Set List v: variable to "val".
21465 */
21466 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021467set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021468{
Bram Moolenaara542c682016-01-31 16:28:04 +010021469 clear_tv(&vimvars[idx].vv_di.di_tv);
21470 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021471 vimvars[idx].vv_list = val;
21472 if (val != NULL)
21473 ++val->lv_refcount;
21474}
21475
21476/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021477 * Set Dictionary v: variable to "val".
21478 */
21479 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021480set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021481{
21482 int todo;
21483 hashitem_T *hi;
21484
Bram Moolenaara542c682016-01-31 16:28:04 +010021485 clear_tv(&vimvars[idx].vv_di.di_tv);
21486 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021487 vimvars[idx].vv_dict = val;
21488 if (val != NULL)
21489 {
21490 ++val->dv_refcount;
21491
21492 /* Set readonly */
21493 todo = (int)val->dv_hashtab.ht_used;
21494 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21495 {
21496 if (HASHITEM_EMPTY(hi))
21497 continue;
21498 --todo;
21499 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21500 }
21501 }
21502}
21503
21504/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021505 * Set v:register if needed.
21506 */
21507 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021508set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021509{
21510 char_u regname;
21511
21512 if (c == 0 || c == ' ')
21513 regname = '"';
21514 else
21515 regname = c;
21516 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021517 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021518 set_vim_var_string(VV_REG, &regname, 1);
21519}
21520
21521/*
21522 * Get or set v:exception. If "oldval" == NULL, return the current value.
21523 * Otherwise, restore the value to "oldval" and return NULL.
21524 * Must always be called in pairs to save and restore v:exception! Does not
21525 * take care of memory allocations.
21526 */
21527 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021528v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021529{
21530 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021531 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021532
Bram Moolenaare9a41262005-01-15 22:18:47 +000021533 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021534 return NULL;
21535}
21536
21537/*
21538 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21539 * Otherwise, restore the value to "oldval" and return NULL.
21540 * Must always be called in pairs to save and restore v:throwpoint! Does not
21541 * take care of memory allocations.
21542 */
21543 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021544v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021545{
21546 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021547 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021548
Bram Moolenaare9a41262005-01-15 22:18:47 +000021549 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021550 return NULL;
21551}
21552
21553#if defined(FEAT_AUTOCMD) || defined(PROTO)
21554/*
21555 * Set v:cmdarg.
21556 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21557 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21558 * Must always be called in pairs!
21559 */
21560 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021561set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021562{
21563 char_u *oldval;
21564 char_u *newval;
21565 unsigned len;
21566
Bram Moolenaare9a41262005-01-15 22:18:47 +000021567 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021568 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021569 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021570 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021571 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021572 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021573 }
21574
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021575 if (eap->force_bin == FORCE_BIN)
21576 len = 6;
21577 else if (eap->force_bin == FORCE_NOBIN)
21578 len = 8;
21579 else
21580 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021581
21582 if (eap->read_edit)
21583 len += 7;
21584
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021585 if (eap->force_ff != 0)
21586 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21587# ifdef FEAT_MBYTE
21588 if (eap->force_enc != 0)
21589 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021590 if (eap->bad_char != 0)
21591 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021592# endif
21593
21594 newval = alloc(len + 1);
21595 if (newval == NULL)
21596 return NULL;
21597
21598 if (eap->force_bin == FORCE_BIN)
21599 sprintf((char *)newval, " ++bin");
21600 else if (eap->force_bin == FORCE_NOBIN)
21601 sprintf((char *)newval, " ++nobin");
21602 else
21603 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021604
21605 if (eap->read_edit)
21606 STRCAT(newval, " ++edit");
21607
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021608 if (eap->force_ff != 0)
21609 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21610 eap->cmd + eap->force_ff);
21611# ifdef FEAT_MBYTE
21612 if (eap->force_enc != 0)
21613 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21614 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021615 if (eap->bad_char == BAD_KEEP)
21616 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21617 else if (eap->bad_char == BAD_DROP)
21618 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21619 else if (eap->bad_char != 0)
21620 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021621# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021622 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021623 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021624}
21625#endif
21626
21627/*
21628 * Get the value of internal variable "name".
21629 * Return OK or FAIL.
21630 */
21631 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021632get_var_tv(
21633 char_u *name,
21634 int len, /* length of "name" */
21635 typval_T *rettv, /* NULL when only checking existence */
21636 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21637 int verbose, /* may give error message */
21638 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021639{
21640 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021641 typval_T *tv = NULL;
21642 typval_T atv;
21643 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021644 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021645
21646 /* truncate the name, so that we can use strcmp() */
21647 cc = name[len];
21648 name[len] = NUL;
21649
21650 /*
21651 * Check for "b:changedtick".
21652 */
21653 if (STRCMP(name, "b:changedtick") == 0)
21654 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021655 atv.v_type = VAR_NUMBER;
21656 atv.vval.v_number = curbuf->b_changedtick;
21657 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021658 }
21659
21660 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021661 * Check for user-defined variables.
21662 */
21663 else
21664 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021665 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021666 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021667 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021668 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021669 if (dip != NULL)
21670 *dip = v;
21671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021672 }
21673
Bram Moolenaare9a41262005-01-15 22:18:47 +000021674 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021675 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021676 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021677 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021678 ret = FAIL;
21679 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021680 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021681 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021682
21683 name[len] = cc;
21684
21685 return ret;
21686}
21687
21688/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021689 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21690 * Also handle function call with Funcref variable: func(expr)
21691 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21692 */
21693 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021694handle_subscript(
21695 char_u **arg,
21696 typval_T *rettv,
21697 int evaluate, /* do more than finding the end */
21698 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021699{
21700 int ret = OK;
21701 dict_T *selfdict = NULL;
21702 char_u *s;
21703 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021704 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021705
21706 while (ret == OK
21707 && (**arg == '['
21708 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021709 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
21710 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021711 && !vim_iswhite(*(*arg - 1)))
21712 {
21713 if (**arg == '(')
21714 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010021715 partial_T *pt = NULL;
21716
Bram Moolenaard9fba312005-06-26 22:34:35 +000021717 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021718 if (evaluate)
21719 {
21720 functv = *rettv;
21721 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021722
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021723 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021724 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021725 {
21726 pt = functv.vval.v_partial;
21727 s = pt->pt_name;
21728 }
21729 else
21730 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021731 }
21732 else
21733 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021734 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021735 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021736 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000021737
21738 /* Clear the funcref afterwards, so that deleting it while
21739 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021740 if (evaluate)
21741 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021742
21743 /* Stop the expression evaluation when immediately aborting on
21744 * error, or when an interrupt occurred or an exception was thrown
21745 * but not caught. */
21746 if (aborting())
21747 {
21748 if (ret == OK)
21749 clear_tv(rettv);
21750 ret = FAIL;
21751 }
21752 dict_unref(selfdict);
21753 selfdict = NULL;
21754 }
21755 else /* **arg == '[' || **arg == '.' */
21756 {
21757 dict_unref(selfdict);
21758 if (rettv->v_type == VAR_DICT)
21759 {
21760 selfdict = rettv->vval.v_dict;
21761 if (selfdict != NULL)
21762 ++selfdict->dv_refcount;
21763 }
21764 else
21765 selfdict = NULL;
21766 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21767 {
21768 clear_tv(rettv);
21769 ret = FAIL;
21770 }
21771 }
21772 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021773
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021774 if ((rettv->v_type == VAR_FUNC || rettv->v_type == VAR_PARTIAL)
21775 && selfdict != NULL)
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021776 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021777 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
21778 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010021779 char_u *tofree = NULL;
21780 ufunc_T *fp;
21781 char_u fname_buf[FLEN_FIXED + 1];
21782 int error;
21783
21784 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021785 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010021786 fp = find_func(fname);
21787 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021788
21789 /* Turn "dict.Func" into a partial for "Func" with "dict". */
Bram Moolenaar65639032016-03-16 21:40:30 +010021790 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021791 {
Bram Moolenaar65639032016-03-16 21:40:30 +010021792 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
21793
21794 if (pt != NULL)
21795 {
21796 pt->pt_refcount = 1;
21797 pt->pt_dict = selfdict;
21798 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021799 if (rettv->v_type == VAR_FUNC)
21800 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010021801 /* Just a function: Take over the function name and use
21802 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021803 pt->pt_name = rettv->vval.v_string;
21804 }
21805 else
21806 {
21807 partial_T *ret_pt = rettv->vval.v_partial;
21808 int i;
21809
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010021810 /* Partial: copy the function name, use selfdict and copy
21811 * args. Can't take over name or args, the partial might
21812 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021813 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010021814 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021815 if (ret_pt->pt_argc > 0)
21816 {
21817 pt->pt_argv = (typval_T *)alloc(
21818 sizeof(typval_T) * ret_pt->pt_argc);
21819 if (pt->pt_argv == NULL)
21820 /* out of memory: drop the arguments */
21821 pt->pt_argc = 0;
21822 else
21823 {
21824 pt->pt_argc = ret_pt->pt_argc;
21825 for (i = 0; i < pt->pt_argc; i++)
21826 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
21827 }
21828 }
21829 partial_unref(ret_pt);
21830 }
Bram Moolenaar65639032016-03-16 21:40:30 +010021831 rettv->v_type = VAR_PARTIAL;
21832 rettv->vval.v_partial = pt;
21833 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021834 }
21835 }
21836
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021837 dict_unref(selfdict);
21838 return ret;
21839}
21840
21841/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021842 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021843 * value).
21844 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010021845 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021846alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021847{
Bram Moolenaar33570922005-01-25 22:26:29 +000021848 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021849}
21850
21851/*
21852 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021853 * The string "s" must have been allocated, it is consumed.
21854 * Return NULL for out of memory, the variable otherwise.
21855 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021856 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021857alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021858{
Bram Moolenaar33570922005-01-25 22:26:29 +000021859 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021860
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021861 rettv = alloc_tv();
21862 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021863 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021864 rettv->v_type = VAR_STRING;
21865 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021866 }
21867 else
21868 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021869 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021870}
21871
21872/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021873 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021874 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021875 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021876free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021877{
21878 if (varp != NULL)
21879 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021880 switch (varp->v_type)
21881 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021882 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021883 func_unref(varp->vval.v_string);
21884 /*FALLTHROUGH*/
21885 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021886 vim_free(varp->vval.v_string);
21887 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021888 case VAR_PARTIAL:
21889 partial_unref(varp->vval.v_partial);
21890 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021891 case VAR_LIST:
21892 list_unref(varp->vval.v_list);
21893 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021894 case VAR_DICT:
21895 dict_unref(varp->vval.v_dict);
21896 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021897 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021898#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021899 job_unref(varp->vval.v_job);
21900 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021901#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021902 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021903#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021904 channel_unref(varp->vval.v_channel);
21905 break;
21906#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021907 case VAR_NUMBER:
21908 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021909 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010021910 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021911 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021913 vim_free(varp);
21914 }
21915}
21916
21917/*
21918 * Free the memory for a variable value and set the value to NULL or 0.
21919 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021920 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021921clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021922{
21923 if (varp != NULL)
21924 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021925 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021926 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021927 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021928 func_unref(varp->vval.v_string);
21929 /*FALLTHROUGH*/
21930 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021931 vim_free(varp->vval.v_string);
21932 varp->vval.v_string = NULL;
21933 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021934 case VAR_PARTIAL:
21935 partial_unref(varp->vval.v_partial);
21936 varp->vval.v_partial = NULL;
21937 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021938 case VAR_LIST:
21939 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021940 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021941 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021942 case VAR_DICT:
21943 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021944 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021945 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021946 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021947 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021948 varp->vval.v_number = 0;
21949 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021950 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021951#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021952 varp->vval.v_float = 0.0;
21953 break;
21954#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021955 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021956#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021957 job_unref(varp->vval.v_job);
21958 varp->vval.v_job = NULL;
21959#endif
21960 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021961 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021962#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021963 channel_unref(varp->vval.v_channel);
21964 varp->vval.v_channel = NULL;
21965#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021966 case VAR_UNKNOWN:
21967 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021968 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021969 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021970 }
21971}
21972
21973/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021974 * Set the value of a variable to NULL without freeing items.
21975 */
21976 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021977init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021978{
21979 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021980 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021981}
21982
21983/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021984 * Get the number value of a variable.
21985 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021986 * For incompatible types, return 0.
21987 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21988 * caller of incompatible types: it sets *denote to TRUE if "denote"
21989 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021990 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010021991 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021992get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021993{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021994 int error = FALSE;
21995
21996 return get_tv_number_chk(varp, &error); /* return 0L on error */
21997}
21998
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021999 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022000get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022001{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022002 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022003
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022004 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022005 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022006 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022007 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022008 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022009#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022010 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022011 break;
22012#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022013 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022014 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022015 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022016 break;
22017 case VAR_STRING:
22018 if (varp->vval.v_string != NULL)
22019 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022020 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022021 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022022 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022023 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022024 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022025 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022026 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022027 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022028 case VAR_SPECIAL:
22029 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22030 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022031 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022032#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022033 EMSG(_("E910: Using a Job as a Number"));
22034 break;
22035#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022036 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022037#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022038 EMSG(_("E913: Using a Channel as a Number"));
22039 break;
22040#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022041 case VAR_UNKNOWN:
22042 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022043 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022044 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022045 if (denote == NULL) /* useful for values that must be unsigned */
22046 n = -1;
22047 else
22048 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022049 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022050}
22051
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022052#ifdef FEAT_FLOAT
22053 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022054get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022055{
22056 switch (varp->v_type)
22057 {
22058 case VAR_NUMBER:
22059 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022060 case VAR_FLOAT:
22061 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022062 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022063 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022064 EMSG(_("E891: Using a Funcref as a Float"));
22065 break;
22066 case VAR_STRING:
22067 EMSG(_("E892: Using a String as a Float"));
22068 break;
22069 case VAR_LIST:
22070 EMSG(_("E893: Using a List as a Float"));
22071 break;
22072 case VAR_DICT:
22073 EMSG(_("E894: Using a Dictionary as a Float"));
22074 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022075 case VAR_SPECIAL:
22076 EMSG(_("E907: Using a special value as a Float"));
22077 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022078 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022079# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022080 EMSG(_("E911: Using a Job as a Float"));
22081 break;
22082# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022083 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022084# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022085 EMSG(_("E914: Using a Channel as a Float"));
22086 break;
22087# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022088 case VAR_UNKNOWN:
22089 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022090 break;
22091 }
22092 return 0;
22093}
22094#endif
22095
Bram Moolenaar071d4272004-06-13 20:20:40 +000022096/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022097 * Get the lnum from the first argument.
22098 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022099 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022100 */
22101 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022102get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022103{
Bram Moolenaar33570922005-01-25 22:26:29 +000022104 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022105 linenr_T lnum;
22106
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022107 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022108 if (lnum == 0) /* no valid number, try using line() */
22109 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022110 rettv.v_type = VAR_NUMBER;
22111 f_line(argvars, &rettv);
22112 lnum = rettv.vval.v_number;
22113 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022114 }
22115 return lnum;
22116}
22117
22118/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022119 * Get the lnum from the first argument.
22120 * Also accepts "$", then "buf" is used.
22121 * Returns 0 on error.
22122 */
22123 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022124get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022125{
22126 if (argvars[0].v_type == VAR_STRING
22127 && argvars[0].vval.v_string != NULL
22128 && argvars[0].vval.v_string[0] == '$'
22129 && buf != NULL)
22130 return buf->b_ml.ml_line_count;
22131 return get_tv_number_chk(&argvars[0], NULL);
22132}
22133
22134/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022135 * Get the string value of a variable.
22136 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022137 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22138 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022139 * If the String variable has never been set, return an empty string.
22140 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022141 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22142 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022143 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022144 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022145get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022146{
22147 static char_u mybuf[NUMBUFLEN];
22148
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022149 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022150}
22151
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022152 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022153get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022154{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022155 char_u *res = get_tv_string_buf_chk(varp, buf);
22156
22157 return res != NULL ? res : (char_u *)"";
22158}
22159
Bram Moolenaar7d647822014-04-05 21:28:56 +020022160/*
22161 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22162 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022163 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022164get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022165{
22166 static char_u mybuf[NUMBUFLEN];
22167
22168 return get_tv_string_buf_chk(varp, mybuf);
22169}
22170
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022171 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022172get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022173{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022174 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022175 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022176 case VAR_NUMBER:
22177 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22178 return buf;
22179 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022180 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022181 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022182 break;
22183 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022184 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022185 break;
22186 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022187 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022188 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022189 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022190#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022191 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022192 break;
22193#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022194 case VAR_STRING:
22195 if (varp->vval.v_string != NULL)
22196 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022197 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022198 case VAR_SPECIAL:
22199 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22200 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022201 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022202#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022203 {
22204 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022205 char *status;
22206
22207 if (job == NULL)
22208 return (char_u *)"no process";
22209 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022210 : job->jv_status == JOB_ENDED ? "dead"
22211 : "run";
22212# ifdef UNIX
22213 vim_snprintf((char *)buf, NUMBUFLEN,
22214 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022215# elif defined(WIN32)
22216 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022217 "process %ld %s",
22218 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022219 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022220# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022221 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022222 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22223# endif
22224 return buf;
22225 }
22226#endif
22227 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022228 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022229#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022230 {
22231 channel_T *channel = varp->vval.v_channel;
22232 char *status = channel_status(channel);
22233
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022234 if (channel == NULL)
22235 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22236 else
22237 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022238 "channel %d %s", channel->ch_id, status);
22239 return buf;
22240 }
22241#endif
22242 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022243 case VAR_UNKNOWN:
22244 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022245 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022246 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022247 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022248}
22249
22250/*
22251 * Find variable "name" in the list of variables.
22252 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022253 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022254 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022255 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022256 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022257 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022258find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022259{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022260 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022261 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022262
Bram Moolenaara7043832005-01-21 11:56:39 +000022263 ht = find_var_ht(name, &varname);
22264 if (htp != NULL)
22265 *htp = ht;
22266 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022267 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022268 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022269}
22270
22271/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022272 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022273 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022274 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022275 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022276find_var_in_ht(
22277 hashtab_T *ht,
22278 int htname,
22279 char_u *varname,
22280 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022281{
Bram Moolenaar33570922005-01-25 22:26:29 +000022282 hashitem_T *hi;
22283
22284 if (*varname == NUL)
22285 {
22286 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022287 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022288 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022289 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022290 case 'g': return &globvars_var;
22291 case 'v': return &vimvars_var;
22292 case 'b': return &curbuf->b_bufvar;
22293 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022294#ifdef FEAT_WINDOWS
22295 case 't': return &curtab->tp_winvar;
22296#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022297 case 'l': return current_funccal == NULL
22298 ? NULL : &current_funccal->l_vars_var;
22299 case 'a': return current_funccal == NULL
22300 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022301 }
22302 return NULL;
22303 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022304
22305 hi = hash_find(ht, varname);
22306 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022307 {
22308 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022309 * worked find the variable again. Don't auto-load a script if it was
22310 * loaded already, otherwise it would be loaded every time when
22311 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022312 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022313 {
22314 /* Note: script_autoload() may make "hi" invalid. It must either
22315 * be obtained again or not used. */
22316 if (!script_autoload(varname, FALSE) || aborting())
22317 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022318 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022319 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022320 if (HASHITEM_EMPTY(hi))
22321 return NULL;
22322 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022323 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022324}
22325
22326/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022327 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022328 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022329 * Set "varname" to the start of name without ':'.
22330 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022331 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022332find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022333{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022334 hashitem_T *hi;
22335
Bram Moolenaar73627d02015-08-11 15:46:09 +020022336 if (name[0] == NUL)
22337 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022338 if (name[1] != ':')
22339 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022340 /* The name must not start with a colon or #. */
22341 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022342 return NULL;
22343 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022344
22345 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022346 hi = hash_find(&compat_hashtab, name);
22347 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022348 return &compat_hashtab;
22349
Bram Moolenaar071d4272004-06-13 20:20:40 +000022350 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022351 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022352 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022353 }
22354 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022355 if (*name == 'g') /* global variable */
22356 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022357 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22358 */
22359 if (vim_strchr(name + 2, ':') != NULL
22360 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022361 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022362 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022363 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022364 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022365 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022366#ifdef FEAT_WINDOWS
22367 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022368 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022369#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022370 if (*name == 'v') /* v: variable */
22371 return &vimvarht;
22372 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022373 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022374 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022375 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022376 if (*name == 's' /* script variable */
22377 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22378 return &SCRIPT_VARS(current_SID);
22379 return NULL;
22380}
22381
22382/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022383 * Get function call environment based on bactrace debug level
22384 */
22385 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022386get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022387{
22388 int i;
22389 funccall_T *funccal;
22390 funccall_T *temp_funccal;
22391
22392 funccal = current_funccal;
22393 if (debug_backtrace_level > 0)
22394 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022395 for (i = 0; i < debug_backtrace_level; i++)
22396 {
22397 temp_funccal = funccal->caller;
22398 if (temp_funccal)
22399 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022400 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022401 /* backtrace level overflow. reset to max */
22402 debug_backtrace_level = i;
22403 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022404 }
22405 return funccal;
22406}
22407
22408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022409 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022410 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022411 * Returns NULL when it doesn't exist.
22412 */
22413 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022414get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022415{
Bram Moolenaar33570922005-01-25 22:26:29 +000022416 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022417
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022418 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022419 if (v == NULL)
22420 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022421 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022422}
22423
22424/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022425 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022426 * sourcing this script and when executing functions defined in the script.
22427 */
22428 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022429new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022430{
Bram Moolenaara7043832005-01-21 11:56:39 +000022431 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022432 hashtab_T *ht;
22433 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022434
Bram Moolenaar071d4272004-06-13 20:20:40 +000022435 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22436 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022437 /* Re-allocating ga_data means that an ht_array pointing to
22438 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022439 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022440 for (i = 1; i <= ga_scripts.ga_len; ++i)
22441 {
22442 ht = &SCRIPT_VARS(i);
22443 if (ht->ht_mask == HT_INIT_SIZE - 1)
22444 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022445 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022446 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022447 }
22448
Bram Moolenaar071d4272004-06-13 20:20:40 +000022449 while (ga_scripts.ga_len < id)
22450 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022451 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022452 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022453 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022454 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022455 }
22456 }
22457}
22458
22459/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022460 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22461 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022462 */
22463 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022464init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022465{
Bram Moolenaar33570922005-01-25 22:26:29 +000022466 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022467 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022468 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022469 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022470 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022471 dict_var->di_tv.vval.v_dict = dict;
22472 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022473 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022474 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22475 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022476}
22477
22478/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022479 * Unreference a dictionary initialized by init_var_dict().
22480 */
22481 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022482unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022483{
22484 /* Now the dict needs to be freed if no one else is using it, go back to
22485 * normal reference counting. */
22486 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22487 dict_unref(dict);
22488}
22489
22490/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022491 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022492 * Frees all allocated variables and the value they contain.
22493 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022494 */
22495 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022496vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022497{
22498 vars_clear_ext(ht, TRUE);
22499}
22500
22501/*
22502 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22503 */
22504 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022505vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022506{
Bram Moolenaara7043832005-01-21 11:56:39 +000022507 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022508 hashitem_T *hi;
22509 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022510
Bram Moolenaar33570922005-01-25 22:26:29 +000022511 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022512 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022513 for (hi = ht->ht_array; todo > 0; ++hi)
22514 {
22515 if (!HASHITEM_EMPTY(hi))
22516 {
22517 --todo;
22518
Bram Moolenaar33570922005-01-25 22:26:29 +000022519 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022520 * ht_array might change then. hash_clear() takes care of it
22521 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022522 v = HI2DI(hi);
22523 if (free_val)
22524 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022525 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022526 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022527 }
22528 }
22529 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022530 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022531}
22532
Bram Moolenaara7043832005-01-21 11:56:39 +000022533/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022534 * Delete a variable from hashtab "ht" at item "hi".
22535 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022536 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022537 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022538delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022539{
Bram Moolenaar33570922005-01-25 22:26:29 +000022540 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022541
22542 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022543 clear_tv(&di->di_tv);
22544 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022545}
22546
22547/*
22548 * List the value of one internal variable.
22549 */
22550 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022551list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022552{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022553 char_u *tofree;
22554 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022555 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022556
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022557 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022558 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022559 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022560 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022561}
22562
Bram Moolenaar071d4272004-06-13 20:20:40 +000022563 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022564list_one_var_a(
22565 char_u *prefix,
22566 char_u *name,
22567 int type,
22568 char_u *string,
22569 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022570{
Bram Moolenaar31859182007-08-14 20:41:13 +000022571 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22572 msg_start();
22573 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022574 if (name != NULL) /* "a:" vars don't have a name stored */
22575 msg_puts(name);
22576 msg_putchar(' ');
22577 msg_advance(22);
22578 if (type == VAR_NUMBER)
22579 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022580 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022581 msg_putchar('*');
22582 else if (type == VAR_LIST)
22583 {
22584 msg_putchar('[');
22585 if (*string == '[')
22586 ++string;
22587 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022588 else if (type == VAR_DICT)
22589 {
22590 msg_putchar('{');
22591 if (*string == '{')
22592 ++string;
22593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022594 else
22595 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022596
Bram Moolenaar071d4272004-06-13 20:20:40 +000022597 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022598
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022599 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022600 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022601 if (*first)
22602 {
22603 msg_clr_eos();
22604 *first = FALSE;
22605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022606}
22607
22608/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022609 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022610 * If the variable already exists, the value is updated.
22611 * Otherwise the variable is created.
22612 */
22613 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022614set_var(
22615 char_u *name,
22616 typval_T *tv,
22617 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022618{
Bram Moolenaar33570922005-01-25 22:26:29 +000022619 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022620 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022621 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022622
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022623 ht = find_var_ht(name, &varname);
22624 if (ht == NULL || *varname == NUL)
22625 {
22626 EMSG2(_(e_illvar), name);
22627 return;
22628 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022629 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022630
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022631 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
22632 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022633 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022634
Bram Moolenaar33570922005-01-25 22:26:29 +000022635 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022636 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022637 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022638 if (var_check_ro(v->di_flags, name, FALSE)
22639 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022640 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022641
22642 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022643 * Handle setting internal v: variables separately where needed to
22644 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022645 */
22646 if (ht == &vimvarht)
22647 {
22648 if (v->di_tv.v_type == VAR_STRING)
22649 {
22650 vim_free(v->di_tv.vval.v_string);
22651 if (copy || tv->v_type != VAR_STRING)
22652 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22653 else
22654 {
22655 /* Take over the string to avoid an extra alloc/free. */
22656 v->di_tv.vval.v_string = tv->vval.v_string;
22657 tv->vval.v_string = NULL;
22658 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022659 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022660 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022661 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022662 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022663 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022664 if (STRCMP(varname, "searchforward") == 0)
22665 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022666#ifdef FEAT_SEARCH_EXTRA
22667 else if (STRCMP(varname, "hlsearch") == 0)
22668 {
22669 no_hlsearch = !v->di_tv.vval.v_number;
22670 redraw_all_later(SOME_VALID);
22671 }
22672#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022673 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022674 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022675 else if (v->di_tv.v_type != tv->v_type)
22676 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022677 }
22678
22679 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022680 }
22681 else /* add a new variable */
22682 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022683 /* Can't add "v:" variable. */
22684 if (ht == &vimvarht)
22685 {
22686 EMSG2(_(e_illvar), name);
22687 return;
22688 }
22689
Bram Moolenaar92124a32005-06-17 22:03:40 +000022690 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022691 if (!valid_varname(varname))
22692 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022693
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022694 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22695 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022696 if (v == NULL)
22697 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022698 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022699 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022700 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022701 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022702 return;
22703 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022704 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022705 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022706
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022707 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022708 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022709 else
22710 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022711 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022712 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022713 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022715}
22716
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022717/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022718 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022719 * Also give an error message.
22720 */
22721 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022722var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022723{
22724 if (flags & DI_FLAGS_RO)
22725 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022726 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022727 return TRUE;
22728 }
22729 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22730 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022731 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022732 return TRUE;
22733 }
22734 return FALSE;
22735}
22736
22737/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022738 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22739 * Also give an error message.
22740 */
22741 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022742var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022743{
22744 if (flags & DI_FLAGS_FIX)
22745 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022746 EMSG2(_("E795: Cannot delete variable %s"),
22747 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022748 return TRUE;
22749 }
22750 return FALSE;
22751}
22752
22753/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022754 * Check if a funcref is assigned to a valid variable name.
22755 * Return TRUE and give an error if not.
22756 */
22757 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022758var_check_func_name(
22759 char_u *name, /* points to start of variable name */
22760 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022761{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022762 /* Allow for w: b: s: and t:. */
22763 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022764 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22765 ? name[2] : name[0]))
22766 {
22767 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22768 name);
22769 return TRUE;
22770 }
22771 /* Don't allow hiding a function. When "v" is not NULL we might be
22772 * assigning another function to the same var, the type is checked
22773 * below. */
22774 if (new_var && function_exists(name))
22775 {
22776 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22777 name);
22778 return TRUE;
22779 }
22780 return FALSE;
22781}
22782
22783/*
22784 * Check if a variable name is valid.
22785 * Return FALSE and give an error if not.
22786 */
22787 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022788valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022789{
22790 char_u *p;
22791
22792 for (p = varname; *p != NUL; ++p)
22793 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22794 && *p != AUTOLOAD_CHAR)
22795 {
22796 EMSG2(_(e_illvar), varname);
22797 return FALSE;
22798 }
22799 return TRUE;
22800}
22801
22802/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022803 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022804 * Also give an error message, using "name" or _("name") when use_gettext is
22805 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022806 */
22807 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022808tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022809{
22810 if (lock & VAR_LOCKED)
22811 {
22812 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022813 name == NULL ? (char_u *)_("Unknown")
22814 : use_gettext ? (char_u *)_(name)
22815 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022816 return TRUE;
22817 }
22818 if (lock & VAR_FIXED)
22819 {
22820 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022821 name == NULL ? (char_u *)_("Unknown")
22822 : use_gettext ? (char_u *)_(name)
22823 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022824 return TRUE;
22825 }
22826 return FALSE;
22827}
22828
22829/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022830 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022831 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022832 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022833 * It is OK for "from" and "to" to point to the same item. This is used to
22834 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022835 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022836 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022837copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022838{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022839 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022840 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022841 switch (from->v_type)
22842 {
22843 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022844 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022845 to->vval.v_number = from->vval.v_number;
22846 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022847 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022848#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022849 to->vval.v_float = from->vval.v_float;
22850 break;
22851#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022852 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022853#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022854 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010022855 if (to->vval.v_job != NULL)
22856 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022857 break;
22858#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022859 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022860#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022861 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022862 if (to->vval.v_channel != NULL)
22863 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010022864 break;
22865#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022866 case VAR_STRING:
22867 case VAR_FUNC:
22868 if (from->vval.v_string == NULL)
22869 to->vval.v_string = NULL;
22870 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022871 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022872 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022873 if (from->v_type == VAR_FUNC)
22874 func_ref(to->vval.v_string);
22875 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022876 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022877 case VAR_PARTIAL:
22878 if (from->vval.v_partial == NULL)
22879 to->vval.v_partial = NULL;
22880 else
22881 {
22882 to->vval.v_partial = from->vval.v_partial;
22883 ++to->vval.v_partial->pt_refcount;
22884 }
22885 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022886 case VAR_LIST:
22887 if (from->vval.v_list == NULL)
22888 to->vval.v_list = NULL;
22889 else
22890 {
22891 to->vval.v_list = from->vval.v_list;
22892 ++to->vval.v_list->lv_refcount;
22893 }
22894 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022895 case VAR_DICT:
22896 if (from->vval.v_dict == NULL)
22897 to->vval.v_dict = NULL;
22898 else
22899 {
22900 to->vval.v_dict = from->vval.v_dict;
22901 ++to->vval.v_dict->dv_refcount;
22902 }
22903 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022904 case VAR_UNKNOWN:
22905 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022906 break;
22907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022908}
22909
22910/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022911 * Make a copy of an item.
22912 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022913 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22914 * reference to an already copied list/dict can be used.
22915 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022916 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022917 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022918item_copy(
22919 typval_T *from,
22920 typval_T *to,
22921 int deep,
22922 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022923{
22924 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022925 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022926
Bram Moolenaar33570922005-01-25 22:26:29 +000022927 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022928 {
22929 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022930 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022931 }
22932 ++recurse;
22933
22934 switch (from->v_type)
22935 {
22936 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022937 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022938 case VAR_STRING:
22939 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022940 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010022941 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022942 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010022943 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022944 copy_tv(from, to);
22945 break;
22946 case VAR_LIST:
22947 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022948 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022949 if (from->vval.v_list == NULL)
22950 to->vval.v_list = NULL;
22951 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22952 {
22953 /* use the copy made earlier */
22954 to->vval.v_list = from->vval.v_list->lv_copylist;
22955 ++to->vval.v_list->lv_refcount;
22956 }
22957 else
22958 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22959 if (to->vval.v_list == NULL)
22960 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022961 break;
22962 case VAR_DICT:
22963 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022964 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022965 if (from->vval.v_dict == NULL)
22966 to->vval.v_dict = NULL;
22967 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22968 {
22969 /* use the copy made earlier */
22970 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22971 ++to->vval.v_dict->dv_refcount;
22972 }
22973 else
22974 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22975 if (to->vval.v_dict == NULL)
22976 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022977 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022978 case VAR_UNKNOWN:
22979 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022980 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022981 }
22982 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022983 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022984}
22985
22986/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022987 * ":echo expr1 ..." print each argument separated with a space, add a
22988 * newline at the end.
22989 * ":echon expr1 ..." print each argument plain.
22990 */
22991 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022992ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022993{
22994 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022995 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022996 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022997 char_u *p;
22998 int needclr = TRUE;
22999 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023000 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023001
23002 if (eap->skip)
23003 ++emsg_skip;
23004 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23005 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023006 /* If eval1() causes an error message the text from the command may
23007 * still need to be cleared. E.g., "echo 22,44". */
23008 need_clr_eos = needclr;
23009
Bram Moolenaar071d4272004-06-13 20:20:40 +000023010 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023011 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023012 {
23013 /*
23014 * Report the invalid expression unless the expression evaluation
23015 * has been cancelled due to an aborting error, an interrupt, or an
23016 * exception.
23017 */
23018 if (!aborting())
23019 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023020 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023021 break;
23022 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023023 need_clr_eos = FALSE;
23024
Bram Moolenaar071d4272004-06-13 20:20:40 +000023025 if (!eap->skip)
23026 {
23027 if (atstart)
23028 {
23029 atstart = FALSE;
23030 /* Call msg_start() after eval1(), evaluating the expression
23031 * may cause a message to appear. */
23032 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023033 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023034 /* Mark the saved text as finishing the line, so that what
23035 * follows is displayed on a new line when scrolling back
23036 * at the more prompt. */
23037 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023038 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023040 }
23041 else if (eap->cmdidx == CMD_echo)
23042 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023043 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023044 if (p != NULL)
23045 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023046 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023047 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023048 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023049 if (*p != TAB && needclr)
23050 {
23051 /* remove any text still there from the command */
23052 msg_clr_eos();
23053 needclr = FALSE;
23054 }
23055 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023056 }
23057 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023058 {
23059#ifdef FEAT_MBYTE
23060 if (has_mbyte)
23061 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023062 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023063
23064 (void)msg_outtrans_len_attr(p, i, echo_attr);
23065 p += i - 1;
23066 }
23067 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023068#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023069 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023071 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023072 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023073 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023074 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023075 arg = skipwhite(arg);
23076 }
23077 eap->nextcmd = check_nextcmd(arg);
23078
23079 if (eap->skip)
23080 --emsg_skip;
23081 else
23082 {
23083 /* remove text that may still be there from the command */
23084 if (needclr)
23085 msg_clr_eos();
23086 if (eap->cmdidx == CMD_echo)
23087 msg_end();
23088 }
23089}
23090
23091/*
23092 * ":echohl {name}".
23093 */
23094 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023095ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023096{
23097 int id;
23098
23099 id = syn_name2id(eap->arg);
23100 if (id == 0)
23101 echo_attr = 0;
23102 else
23103 echo_attr = syn_id2attr(id);
23104}
23105
23106/*
23107 * ":execute expr1 ..." execute the result of an expression.
23108 * ":echomsg expr1 ..." Print a message
23109 * ":echoerr expr1 ..." Print an error
23110 * Each gets spaces around each argument and a newline at the end for
23111 * echo commands
23112 */
23113 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023114ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023115{
23116 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023117 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023118 int ret = OK;
23119 char_u *p;
23120 garray_T ga;
23121 int len;
23122 int save_did_emsg;
23123
23124 ga_init2(&ga, 1, 80);
23125
23126 if (eap->skip)
23127 ++emsg_skip;
23128 while (*arg != NUL && *arg != '|' && *arg != '\n')
23129 {
23130 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023131 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023132 {
23133 /*
23134 * Report the invalid expression unless the expression evaluation
23135 * has been cancelled due to an aborting error, an interrupt, or an
23136 * exception.
23137 */
23138 if (!aborting())
23139 EMSG2(_(e_invexpr2), p);
23140 ret = FAIL;
23141 break;
23142 }
23143
23144 if (!eap->skip)
23145 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023146 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023147 len = (int)STRLEN(p);
23148 if (ga_grow(&ga, len + 2) == FAIL)
23149 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023150 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023151 ret = FAIL;
23152 break;
23153 }
23154 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023155 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023156 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023157 ga.ga_len += len;
23158 }
23159
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023160 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023161 arg = skipwhite(arg);
23162 }
23163
23164 if (ret != FAIL && ga.ga_data != NULL)
23165 {
23166 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023167 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023168 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023169 out_flush();
23170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023171 else if (eap->cmdidx == CMD_echoerr)
23172 {
23173 /* We don't want to abort following commands, restore did_emsg. */
23174 save_did_emsg = did_emsg;
23175 EMSG((char_u *)ga.ga_data);
23176 if (!force_abort)
23177 did_emsg = save_did_emsg;
23178 }
23179 else if (eap->cmdidx == CMD_execute)
23180 do_cmdline((char_u *)ga.ga_data,
23181 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23182 }
23183
23184 ga_clear(&ga);
23185
23186 if (eap->skip)
23187 --emsg_skip;
23188
23189 eap->nextcmd = check_nextcmd(arg);
23190}
23191
23192/*
23193 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23194 * "arg" points to the "&" or '+' when called, to "option" when returning.
23195 * Returns NULL when no option name found. Otherwise pointer to the char
23196 * after the option name.
23197 */
23198 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023199find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023200{
23201 char_u *p = *arg;
23202
23203 ++p;
23204 if (*p == 'g' && p[1] == ':')
23205 {
23206 *opt_flags = OPT_GLOBAL;
23207 p += 2;
23208 }
23209 else if (*p == 'l' && p[1] == ':')
23210 {
23211 *opt_flags = OPT_LOCAL;
23212 p += 2;
23213 }
23214 else
23215 *opt_flags = 0;
23216
23217 if (!ASCII_ISALPHA(*p))
23218 return NULL;
23219 *arg = p;
23220
23221 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23222 p += 4; /* termcap option */
23223 else
23224 while (ASCII_ISALPHA(*p))
23225 ++p;
23226 return p;
23227}
23228
23229/*
23230 * ":function"
23231 */
23232 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023233ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023234{
23235 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023236 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023237 int j;
23238 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023239 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023240 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023241 char_u *name = NULL;
23242 char_u *p;
23243 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023244 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023245 garray_T newargs;
23246 garray_T newlines;
23247 int varargs = FALSE;
23248 int mustend = FALSE;
23249 int flags = 0;
23250 ufunc_T *fp;
23251 int indent;
23252 int nesting;
23253 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023254 dictitem_T *v;
23255 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023256 static int func_nr = 0; /* number for nameless function */
23257 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023258 hashtab_T *ht;
23259 int todo;
23260 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023261 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023262
23263 /*
23264 * ":function" without argument: list functions.
23265 */
23266 if (ends_excmd(*eap->arg))
23267 {
23268 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023269 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023270 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023271 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023272 {
23273 if (!HASHITEM_EMPTY(hi))
23274 {
23275 --todo;
23276 fp = HI2UF(hi);
23277 if (!isdigit(*fp->uf_name))
23278 list_func_head(fp, FALSE);
23279 }
23280 }
23281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023282 eap->nextcmd = check_nextcmd(eap->arg);
23283 return;
23284 }
23285
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023286 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023287 * ":function /pat": list functions matching pattern.
23288 */
23289 if (*eap->arg == '/')
23290 {
23291 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23292 if (!eap->skip)
23293 {
23294 regmatch_T regmatch;
23295
23296 c = *p;
23297 *p = NUL;
23298 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23299 *p = c;
23300 if (regmatch.regprog != NULL)
23301 {
23302 regmatch.rm_ic = p_ic;
23303
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023304 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023305 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23306 {
23307 if (!HASHITEM_EMPTY(hi))
23308 {
23309 --todo;
23310 fp = HI2UF(hi);
23311 if (!isdigit(*fp->uf_name)
23312 && vim_regexec(&regmatch, fp->uf_name, 0))
23313 list_func_head(fp, FALSE);
23314 }
23315 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023316 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023317 }
23318 }
23319 if (*p == '/')
23320 ++p;
23321 eap->nextcmd = check_nextcmd(p);
23322 return;
23323 }
23324
23325 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023326 * Get the function name. There are these situations:
23327 * func normal function name
23328 * "name" == func, "fudi.fd_dict" == NULL
23329 * dict.func new dictionary entry
23330 * "name" == NULL, "fudi.fd_dict" set,
23331 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23332 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023333 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023334 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23335 * dict.func existing dict entry that's not a Funcref
23336 * "name" == NULL, "fudi.fd_dict" set,
23337 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023338 * s:func script-local function name
23339 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023340 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023341 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010023342 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023343 paren = (vim_strchr(p, '(') != NULL);
23344 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023345 {
23346 /*
23347 * Return on an invalid expression in braces, unless the expression
23348 * evaluation has been cancelled due to an aborting error, an
23349 * interrupt, or an exception.
23350 */
23351 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023352 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023353 if (!eap->skip && fudi.fd_newkey != NULL)
23354 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023355 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023356 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023358 else
23359 eap->skip = TRUE;
23360 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023361
Bram Moolenaar071d4272004-06-13 20:20:40 +000023362 /* An error in a function call during evaluation of an expression in magic
23363 * braces should not cause the function not to be defined. */
23364 saved_did_emsg = did_emsg;
23365 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023366
23367 /*
23368 * ":function func" with only function name: list function.
23369 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023370 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023371 {
23372 if (!ends_excmd(*skipwhite(p)))
23373 {
23374 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023375 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023376 }
23377 eap->nextcmd = check_nextcmd(p);
23378 if (eap->nextcmd != NULL)
23379 *p = NUL;
23380 if (!eap->skip && !got_int)
23381 {
23382 fp = find_func(name);
23383 if (fp != NULL)
23384 {
23385 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023386 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023387 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023388 if (FUNCLINE(fp, j) == NULL)
23389 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023390 msg_putchar('\n');
23391 msg_outnum((long)(j + 1));
23392 if (j < 9)
23393 msg_putchar(' ');
23394 if (j < 99)
23395 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023396 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023397 out_flush(); /* show a line at a time */
23398 ui_breakcheck();
23399 }
23400 if (!got_int)
23401 {
23402 msg_putchar('\n');
23403 msg_puts((char_u *)" endfunction");
23404 }
23405 }
23406 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023407 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023408 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023409 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023410 }
23411
23412 /*
23413 * ":function name(arg1, arg2)" Define function.
23414 */
23415 p = skipwhite(p);
23416 if (*p != '(')
23417 {
23418 if (!eap->skip)
23419 {
23420 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023421 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023422 }
23423 /* attempt to continue by skipping some text */
23424 if (vim_strchr(p, '(') != NULL)
23425 p = vim_strchr(p, '(');
23426 }
23427 p = skipwhite(p + 1);
23428
23429 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23430 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23431
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023432 if (!eap->skip)
23433 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023434 /* Check the name of the function. Unless it's a dictionary function
23435 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023436 if (name != NULL)
23437 arg = name;
23438 else
23439 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023440 if (arg != NULL && (fudi.fd_di == NULL
23441 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023442 {
23443 if (*arg == K_SPECIAL)
23444 j = 3;
23445 else
23446 j = 0;
23447 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23448 : eval_isnamec(arg[j])))
23449 ++j;
23450 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023451 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023452 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023453 /* Disallow using the g: dict. */
23454 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23455 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023456 }
23457
Bram Moolenaar071d4272004-06-13 20:20:40 +000023458 /*
23459 * Isolate the arguments: "arg1, arg2, ...)"
23460 */
23461 while (*p != ')')
23462 {
23463 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23464 {
23465 varargs = TRUE;
23466 p += 3;
23467 mustend = TRUE;
23468 }
23469 else
23470 {
23471 arg = p;
23472 while (ASCII_ISALNUM(*p) || *p == '_')
23473 ++p;
23474 if (arg == p || isdigit(*arg)
23475 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23476 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23477 {
23478 if (!eap->skip)
23479 EMSG2(_("E125: Illegal argument: %s"), arg);
23480 break;
23481 }
23482 if (ga_grow(&newargs, 1) == FAIL)
23483 goto erret;
23484 c = *p;
23485 *p = NUL;
23486 arg = vim_strsave(arg);
23487 if (arg == NULL)
23488 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023489
23490 /* Check for duplicate argument name. */
23491 for (i = 0; i < newargs.ga_len; ++i)
23492 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23493 {
23494 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023495 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023496 goto erret;
23497 }
23498
Bram Moolenaar071d4272004-06-13 20:20:40 +000023499 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23500 *p = c;
23501 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023502 if (*p == ',')
23503 ++p;
23504 else
23505 mustend = TRUE;
23506 }
23507 p = skipwhite(p);
23508 if (mustend && *p != ')')
23509 {
23510 if (!eap->skip)
23511 EMSG2(_(e_invarg2), eap->arg);
23512 break;
23513 }
23514 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023515 if (*p != ')')
23516 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023517 ++p; /* skip the ')' */
23518
Bram Moolenaare9a41262005-01-15 22:18:47 +000023519 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023520 for (;;)
23521 {
23522 p = skipwhite(p);
23523 if (STRNCMP(p, "range", 5) == 0)
23524 {
23525 flags |= FC_RANGE;
23526 p += 5;
23527 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023528 else if (STRNCMP(p, "dict", 4) == 0)
23529 {
23530 flags |= FC_DICT;
23531 p += 4;
23532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023533 else if (STRNCMP(p, "abort", 5) == 0)
23534 {
23535 flags |= FC_ABORT;
23536 p += 5;
23537 }
23538 else
23539 break;
23540 }
23541
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023542 /* When there is a line break use what follows for the function body.
23543 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23544 if (*p == '\n')
23545 line_arg = p + 1;
23546 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023547 EMSG(_(e_trailing));
23548
23549 /*
23550 * Read the body of the function, until ":endfunction" is found.
23551 */
23552 if (KeyTyped)
23553 {
23554 /* Check if the function already exists, don't let the user type the
23555 * whole function before telling him it doesn't work! For a script we
23556 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023557 if (!eap->skip && !eap->forceit)
23558 {
23559 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23560 EMSG(_(e_funcdict));
23561 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023562 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023564
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023565 if (!eap->skip && did_emsg)
23566 goto erret;
23567
Bram Moolenaar071d4272004-06-13 20:20:40 +000023568 msg_putchar('\n'); /* don't overwrite the function name */
23569 cmdline_row = msg_row;
23570 }
23571
23572 indent = 2;
23573 nesting = 0;
23574 for (;;)
23575 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023576 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023577 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023578 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023579 saved_wait_return = FALSE;
23580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023581 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023582 sourcing_lnum_off = sourcing_lnum;
23583
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023584 if (line_arg != NULL)
23585 {
23586 /* Use eap->arg, split up in parts by line breaks. */
23587 theline = line_arg;
23588 p = vim_strchr(theline, '\n');
23589 if (p == NULL)
23590 line_arg += STRLEN(line_arg);
23591 else
23592 {
23593 *p = NUL;
23594 line_arg = p + 1;
23595 }
23596 }
23597 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023598 theline = getcmdline(':', 0L, indent);
23599 else
23600 theline = eap->getline(':', eap->cookie, indent);
23601 if (KeyTyped)
23602 lines_left = Rows - 1;
23603 if (theline == NULL)
23604 {
23605 EMSG(_("E126: Missing :endfunction"));
23606 goto erret;
23607 }
23608
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023609 /* Detect line continuation: sourcing_lnum increased more than one. */
23610 if (sourcing_lnum > sourcing_lnum_off + 1)
23611 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23612 else
23613 sourcing_lnum_off = 0;
23614
Bram Moolenaar071d4272004-06-13 20:20:40 +000023615 if (skip_until != NULL)
23616 {
23617 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23618 * don't check for ":endfunc". */
23619 if (STRCMP(theline, skip_until) == 0)
23620 {
23621 vim_free(skip_until);
23622 skip_until = NULL;
23623 }
23624 }
23625 else
23626 {
23627 /* skip ':' and blanks*/
23628 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23629 ;
23630
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023631 /* Check for "endfunction". */
23632 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023633 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023634 if (line_arg == NULL)
23635 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023636 break;
23637 }
23638
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023639 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023640 * at "end". */
23641 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23642 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023643 else if (STRNCMP(p, "if", 2) == 0
23644 || STRNCMP(p, "wh", 2) == 0
23645 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023646 || STRNCMP(p, "try", 3) == 0)
23647 indent += 2;
23648
23649 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023650 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023651 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023652 if (*p == '!')
23653 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023654 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010023655 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010023656 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023657 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023658 ++nesting;
23659 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023660 }
23661 }
23662
23663 /* Check for ":append" or ":insert". */
23664 p = skip_range(p, NULL);
23665 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23666 || (p[0] == 'i'
23667 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23668 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23669 skip_until = vim_strsave((char_u *)".");
23670
23671 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23672 arg = skipwhite(skiptowhite(p));
23673 if (arg[0] == '<' && arg[1] =='<'
23674 && ((p[0] == 'p' && p[1] == 'y'
23675 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23676 || (p[0] == 'p' && p[1] == 'e'
23677 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23678 || (p[0] == 't' && p[1] == 'c'
23679 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023680 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23681 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023682 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23683 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023684 || (p[0] == 'm' && p[1] == 'z'
23685 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023686 ))
23687 {
23688 /* ":python <<" continues until a dot, like ":append" */
23689 p = skipwhite(arg + 2);
23690 if (*p == NUL)
23691 skip_until = vim_strsave((char_u *)".");
23692 else
23693 skip_until = vim_strsave(p);
23694 }
23695 }
23696
23697 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023698 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023699 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023700 if (line_arg == NULL)
23701 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023702 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023703 }
23704
23705 /* Copy the line to newly allocated memory. get_one_sourceline()
23706 * allocates 250 bytes per line, this saves 80% on average. The cost
23707 * is an extra alloc/free. */
23708 p = vim_strsave(theline);
23709 if (p != NULL)
23710 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023711 if (line_arg == NULL)
23712 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023713 theline = p;
23714 }
23715
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023716 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23717
23718 /* Add NULL lines for continuation lines, so that the line count is
23719 * equal to the index in the growarray. */
23720 while (sourcing_lnum_off-- > 0)
23721 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023722
23723 /* Check for end of eap->arg. */
23724 if (line_arg != NULL && *line_arg == NUL)
23725 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023726 }
23727
23728 /* Don't define the function when skipping commands or when an error was
23729 * detected. */
23730 if (eap->skip || did_emsg)
23731 goto erret;
23732
23733 /*
23734 * If there are no errors, add the function
23735 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023736 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023737 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023738 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023739 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023740 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023741 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023742 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023743 goto erret;
23744 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023745
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023746 fp = find_func(name);
23747 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023748 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023749 if (!eap->forceit)
23750 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023751 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023752 goto erret;
23753 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023754 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023755 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023756 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023757 name);
23758 goto erret;
23759 }
23760 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023761 ga_clear_strings(&(fp->uf_args));
23762 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023763 vim_free(name);
23764 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023766 }
23767 else
23768 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023769 char numbuf[20];
23770
23771 fp = NULL;
23772 if (fudi.fd_newkey == NULL && !eap->forceit)
23773 {
23774 EMSG(_(e_funcdict));
23775 goto erret;
23776 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023777 if (fudi.fd_di == NULL)
23778 {
23779 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023780 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023781 goto erret;
23782 }
23783 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023784 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023785 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023786
23787 /* Give the function a sequential number. Can only be used with a
23788 * Funcref! */
23789 vim_free(name);
23790 sprintf(numbuf, "%d", ++func_nr);
23791 name = vim_strsave((char_u *)numbuf);
23792 if (name == NULL)
23793 goto erret;
23794 }
23795
23796 if (fp == NULL)
23797 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023798 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023799 {
23800 int slen, plen;
23801 char_u *scriptname;
23802
23803 /* Check that the autoload name matches the script name. */
23804 j = FAIL;
23805 if (sourcing_name != NULL)
23806 {
23807 scriptname = autoload_name(name);
23808 if (scriptname != NULL)
23809 {
23810 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023811 plen = (int)STRLEN(p);
23812 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023813 if (slen > plen && fnamecmp(p,
23814 sourcing_name + slen - plen) == 0)
23815 j = OK;
23816 vim_free(scriptname);
23817 }
23818 }
23819 if (j == FAIL)
23820 {
23821 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23822 goto erret;
23823 }
23824 }
23825
23826 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023827 if (fp == NULL)
23828 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023829
23830 if (fudi.fd_dict != NULL)
23831 {
23832 if (fudi.fd_di == NULL)
23833 {
23834 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023835 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023836 if (fudi.fd_di == NULL)
23837 {
23838 vim_free(fp);
23839 goto erret;
23840 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023841 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23842 {
23843 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023844 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023845 goto erret;
23846 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023847 }
23848 else
23849 /* overwrite existing dict entry */
23850 clear_tv(&fudi.fd_di->di_tv);
23851 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023852 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023853 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023854 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023855
23856 /* behave like "dict" was used */
23857 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023858 }
23859
Bram Moolenaar071d4272004-06-13 20:20:40 +000023860 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023861 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023862 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23863 {
23864 vim_free(fp);
23865 goto erret;
23866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023867 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023868 fp->uf_args = newargs;
23869 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023870#ifdef FEAT_PROFILE
23871 fp->uf_tml_count = NULL;
23872 fp->uf_tml_total = NULL;
23873 fp->uf_tml_self = NULL;
23874 fp->uf_profiling = FALSE;
23875 if (prof_def_func())
23876 func_do_profile(fp);
23877#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023878 fp->uf_varargs = varargs;
23879 fp->uf_flags = flags;
23880 fp->uf_calls = 0;
23881 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023882 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023883
23884erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023885 ga_clear_strings(&newargs);
23886 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023887ret_free:
23888 vim_free(skip_until);
23889 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023890 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023891 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023892 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023893}
23894
23895/*
23896 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023897 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023898 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023899 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010023900 * TFN_INT: internal function name OK
23901 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023902 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023903 * Advances "pp" to just after the function name (if no error).
23904 */
23905 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023906trans_function_name(
23907 char_u **pp,
23908 int skip, /* only find the end, don't evaluate */
23909 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010023910 funcdict_T *fdp, /* return: info about dictionary used */
23911 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023912{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023913 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023914 char_u *start;
23915 char_u *end;
23916 int lead;
23917 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023918 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023919 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023920
23921 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023922 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023923 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023924
23925 /* Check for hard coded <SNR>: already translated function ID (from a user
23926 * command). */
23927 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23928 && (*pp)[2] == (int)KE_SNR)
23929 {
23930 *pp += 3;
23931 len = get_id_len(pp) + 3;
23932 return vim_strnsave(start, len);
23933 }
23934
23935 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23936 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023937 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023938 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023939 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023940
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023941 /* Note that TFN_ flags use the same values as GLV_ flags. */
23942 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023943 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023944 if (end == start)
23945 {
23946 if (!skip)
23947 EMSG(_("E129: Function name required"));
23948 goto theend;
23949 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023950 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023951 {
23952 /*
23953 * Report an invalid expression in braces, unless the expression
23954 * evaluation has been cancelled due to an aborting error, an
23955 * interrupt, or an exception.
23956 */
23957 if (!aborting())
23958 {
23959 if (end != NULL)
23960 EMSG2(_(e_invarg2), start);
23961 }
23962 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023963 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023964 goto theend;
23965 }
23966
23967 if (lv.ll_tv != NULL)
23968 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023969 if (fdp != NULL)
23970 {
23971 fdp->fd_dict = lv.ll_dict;
23972 fdp->fd_newkey = lv.ll_newkey;
23973 lv.ll_newkey = NULL;
23974 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023975 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023976 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23977 {
23978 name = vim_strsave(lv.ll_tv->vval.v_string);
23979 *pp = end;
23980 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010023981 else if (lv.ll_tv->v_type == VAR_PARTIAL
23982 && lv.ll_tv->vval.v_partial != NULL)
23983 {
23984 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
23985 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010023986 if (partial != NULL)
23987 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010023988 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023989 else
23990 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023991 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23992 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023993 EMSG(_(e_funcref));
23994 else
23995 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023996 name = NULL;
23997 }
23998 goto theend;
23999 }
24000
24001 if (lv.ll_name == NULL)
24002 {
24003 /* Error found, but continue after the function name. */
24004 *pp = end;
24005 goto theend;
24006 }
24007
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024008 /* Check if the name is a Funcref. If so, use the value. */
24009 if (lv.ll_exp_name != NULL)
24010 {
24011 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010024012 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010024013 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024014 if (name == lv.ll_exp_name)
24015 name = NULL;
24016 }
24017 else
24018 {
24019 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010024020 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024021 if (name == *pp)
24022 name = NULL;
24023 }
24024 if (name != NULL)
24025 {
24026 name = vim_strsave(name);
24027 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024028 if (STRNCMP(name, "<SNR>", 5) == 0)
24029 {
24030 /* Change "<SNR>" to the byte sequence. */
24031 name[0] = K_SPECIAL;
24032 name[1] = KS_EXTRA;
24033 name[2] = (int)KE_SNR;
24034 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24035 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024036 goto theend;
24037 }
24038
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024039 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024040 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024041 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024042 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24043 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24044 {
24045 /* When there was "s:" already or the name expanded to get a
24046 * leading "s:" then remove it. */
24047 lv.ll_name += 2;
24048 len -= 2;
24049 lead = 2;
24050 }
24051 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024052 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024053 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024054 /* skip over "s:" and "g:" */
24055 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024056 lv.ll_name += 2;
24057 len = (int)(end - lv.ll_name);
24058 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024059
24060 /*
24061 * Copy the function name to allocated memory.
24062 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24063 * Accept <SNR>123_name() outside a script.
24064 */
24065 if (skip)
24066 lead = 0; /* do nothing */
24067 else if (lead > 0)
24068 {
24069 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024070 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24071 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024072 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024073 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024074 if (current_SID <= 0)
24075 {
24076 EMSG(_(e_usingsid));
24077 goto theend;
24078 }
24079 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24080 lead += (int)STRLEN(sid_buf);
24081 }
24082 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024083 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024084 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024085 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024086 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024087 goto theend;
24088 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024089 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024090 {
24091 char_u *cp = vim_strchr(lv.ll_name, ':');
24092
24093 if (cp != NULL && cp < end)
24094 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024095 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024096 goto theend;
24097 }
24098 }
24099
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024100 name = alloc((unsigned)(len + lead + 1));
24101 if (name != NULL)
24102 {
24103 if (lead > 0)
24104 {
24105 name[0] = K_SPECIAL;
24106 name[1] = KS_EXTRA;
24107 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024108 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024109 STRCPY(name + 3, sid_buf);
24110 }
24111 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024112 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024113 }
24114 *pp = end;
24115
24116theend:
24117 clear_lval(&lv);
24118 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024119}
24120
24121/*
24122 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24123 * Return 2 if "p" starts with "s:".
24124 * Return 0 otherwise.
24125 */
24126 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024127eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024128{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024129 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24130 * the standard library function. */
24131 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24132 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024133 return 5;
24134 if (p[0] == 's' && p[1] == ':')
24135 return 2;
24136 return 0;
24137}
24138
24139/*
24140 * Return TRUE if "p" starts with "<SID>" or "s:".
24141 * Only works if eval_fname_script() returned non-zero for "p"!
24142 */
24143 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024144eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024145{
24146 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24147}
24148
24149/*
24150 * List the head of the function: "name(arg1, arg2)".
24151 */
24152 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024153list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024154{
24155 int j;
24156
24157 msg_start();
24158 if (indent)
24159 MSG_PUTS(" ");
24160 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024161 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024162 {
24163 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024164 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024165 }
24166 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024167 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024168 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024169 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024170 {
24171 if (j)
24172 MSG_PUTS(", ");
24173 msg_puts(FUNCARG(fp, j));
24174 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024175 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024176 {
24177 if (j)
24178 MSG_PUTS(", ");
24179 MSG_PUTS("...");
24180 }
24181 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024182 if (fp->uf_flags & FC_ABORT)
24183 MSG_PUTS(" abort");
24184 if (fp->uf_flags & FC_RANGE)
24185 MSG_PUTS(" range");
24186 if (fp->uf_flags & FC_DICT)
24187 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024188 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024189 if (p_verbose > 0)
24190 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024191}
24192
24193/*
24194 * Find a function by name, return pointer to it in ufuncs.
24195 * Return NULL for unknown function.
24196 */
24197 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024198find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024199{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024200 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024201
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024202 hi = hash_find(&func_hashtab, name);
24203 if (!HASHITEM_EMPTY(hi))
24204 return HI2UF(hi);
24205 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024206}
24207
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024208#if defined(EXITFREE) || defined(PROTO)
24209 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024210free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024211{
24212 hashitem_T *hi;
24213
24214 /* Need to start all over every time, because func_free() may change the
24215 * hash table. */
24216 while (func_hashtab.ht_used > 0)
24217 for (hi = func_hashtab.ht_array; ; ++hi)
24218 if (!HASHITEM_EMPTY(hi))
24219 {
24220 func_free(HI2UF(hi));
24221 break;
24222 }
24223}
24224#endif
24225
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024226 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024227translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024228{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024229 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024230 return find_internal_func(name) >= 0;
24231 return find_func(name) != NULL;
24232}
24233
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024234/*
24235 * Return TRUE if a function "name" exists.
24236 */
24237 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024238function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024239{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024240 char_u *nm = name;
24241 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024242 int n = FALSE;
24243
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024244 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010024245 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024246 nm = skipwhite(nm);
24247
24248 /* Only accept "funcname", "funcname ", "funcname (..." and
24249 * "funcname(...", not "funcname!...". */
24250 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024251 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024252 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024253 return n;
24254}
24255
Bram Moolenaara1544c02013-05-30 12:35:52 +020024256 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024257get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024258{
24259 char_u *nm = name;
24260 char_u *p;
24261
Bram Moolenaar65639032016-03-16 21:40:30 +010024262 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020024263
24264 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024265 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024266 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024267
Bram Moolenaara1544c02013-05-30 12:35:52 +020024268 vim_free(p);
24269 return NULL;
24270}
24271
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024272/*
24273 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024274 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24275 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024276 */
24277 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024278builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024279{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024280 char_u *p;
24281
24282 if (!ASCII_ISLOWER(name[0]))
24283 return FALSE;
24284 p = vim_strchr(name, AUTOLOAD_CHAR);
24285 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024286}
24287
Bram Moolenaar05159a02005-02-26 23:04:13 +000024288#if defined(FEAT_PROFILE) || defined(PROTO)
24289/*
24290 * Start profiling function "fp".
24291 */
24292 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024293func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024294{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024295 int len = fp->uf_lines.ga_len;
24296
24297 if (len == 0)
24298 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024299 fp->uf_tm_count = 0;
24300 profile_zero(&fp->uf_tm_self);
24301 profile_zero(&fp->uf_tm_total);
24302 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024303 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024304 if (fp->uf_tml_total == NULL)
24305 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024306 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024307 if (fp->uf_tml_self == NULL)
24308 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024309 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024310 fp->uf_tml_idx = -1;
24311 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24312 || fp->uf_tml_self == NULL)
24313 return; /* out of memory */
24314
24315 fp->uf_profiling = TRUE;
24316}
24317
24318/*
24319 * Dump the profiling results for all functions in file "fd".
24320 */
24321 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024322func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024323{
24324 hashitem_T *hi;
24325 int todo;
24326 ufunc_T *fp;
24327 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024328 ufunc_T **sorttab;
24329 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024330
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024331 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024332 if (todo == 0)
24333 return; /* nothing to dump */
24334
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024335 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024336
Bram Moolenaar05159a02005-02-26 23:04:13 +000024337 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24338 {
24339 if (!HASHITEM_EMPTY(hi))
24340 {
24341 --todo;
24342 fp = HI2UF(hi);
24343 if (fp->uf_profiling)
24344 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024345 if (sorttab != NULL)
24346 sorttab[st_len++] = fp;
24347
Bram Moolenaar05159a02005-02-26 23:04:13 +000024348 if (fp->uf_name[0] == K_SPECIAL)
24349 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24350 else
24351 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24352 if (fp->uf_tm_count == 1)
24353 fprintf(fd, "Called 1 time\n");
24354 else
24355 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24356 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24357 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24358 fprintf(fd, "\n");
24359 fprintf(fd, "count total (s) self (s)\n");
24360
24361 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24362 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024363 if (FUNCLINE(fp, i) == NULL)
24364 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024365 prof_func_line(fd, fp->uf_tml_count[i],
24366 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024367 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24368 }
24369 fprintf(fd, "\n");
24370 }
24371 }
24372 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024373
24374 if (sorttab != NULL && st_len > 0)
24375 {
24376 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24377 prof_total_cmp);
24378 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24379 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24380 prof_self_cmp);
24381 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24382 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024383
24384 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024385}
Bram Moolenaar73830342005-02-28 22:48:19 +000024386
24387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024388prof_sort_list(
24389 FILE *fd,
24390 ufunc_T **sorttab,
24391 int st_len,
24392 char *title,
24393 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024394{
24395 int i;
24396 ufunc_T *fp;
24397
24398 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24399 fprintf(fd, "count total (s) self (s) function\n");
24400 for (i = 0; i < 20 && i < st_len; ++i)
24401 {
24402 fp = sorttab[i];
24403 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24404 prefer_self);
24405 if (fp->uf_name[0] == K_SPECIAL)
24406 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24407 else
24408 fprintf(fd, " %s()\n", fp->uf_name);
24409 }
24410 fprintf(fd, "\n");
24411}
24412
24413/*
24414 * Print the count and times for one function or function line.
24415 */
24416 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024417prof_func_line(
24418 FILE *fd,
24419 int count,
24420 proftime_T *total,
24421 proftime_T *self,
24422 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024423{
24424 if (count > 0)
24425 {
24426 fprintf(fd, "%5d ", count);
24427 if (prefer_self && profile_equal(total, self))
24428 fprintf(fd, " ");
24429 else
24430 fprintf(fd, "%s ", profile_msg(total));
24431 if (!prefer_self && profile_equal(total, self))
24432 fprintf(fd, " ");
24433 else
24434 fprintf(fd, "%s ", profile_msg(self));
24435 }
24436 else
24437 fprintf(fd, " ");
24438}
24439
24440/*
24441 * Compare function for total time sorting.
24442 */
24443 static int
24444#ifdef __BORLANDC__
24445_RTLENTRYF
24446#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024447prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024448{
24449 ufunc_T *p1, *p2;
24450
24451 p1 = *(ufunc_T **)s1;
24452 p2 = *(ufunc_T **)s2;
24453 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24454}
24455
24456/*
24457 * Compare function for self time sorting.
24458 */
24459 static int
24460#ifdef __BORLANDC__
24461_RTLENTRYF
24462#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024463prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024464{
24465 ufunc_T *p1, *p2;
24466
24467 p1 = *(ufunc_T **)s1;
24468 p2 = *(ufunc_T **)s2;
24469 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24470}
24471
Bram Moolenaar05159a02005-02-26 23:04:13 +000024472#endif
24473
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024474/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024475 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024476 * Return TRUE if a package was loaded.
24477 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024478 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024479script_autoload(
24480 char_u *name,
24481 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024482{
24483 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024484 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024485 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024486 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024487
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024488 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024489 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024490 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024491 return FALSE;
24492
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024493 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024494
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024495 /* Find the name in the list of previously loaded package names. Skip
24496 * "autoload/", it's always the same. */
24497 for (i = 0; i < ga_loaded.ga_len; ++i)
24498 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24499 break;
24500 if (!reload && i < ga_loaded.ga_len)
24501 ret = FALSE; /* was loaded already */
24502 else
24503 {
24504 /* Remember the name if it wasn't loaded already. */
24505 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24506 {
24507 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24508 tofree = NULL;
24509 }
24510
24511 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010024512 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024513 ret = TRUE;
24514 }
24515
24516 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024517 return ret;
24518}
24519
24520/*
24521 * Return the autoload script name for a function or variable name.
24522 * Returns NULL when out of memory.
24523 */
24524 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024525autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024526{
24527 char_u *p;
24528 char_u *scriptname;
24529
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024530 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024531 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24532 if (scriptname == NULL)
24533 return FALSE;
24534 STRCPY(scriptname, "autoload/");
24535 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024536 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024537 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024538 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024539 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024540 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024541}
24542
Bram Moolenaar071d4272004-06-13 20:20:40 +000024543#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24544
24545/*
24546 * Function given to ExpandGeneric() to obtain the list of user defined
24547 * function names.
24548 */
24549 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024550get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024551{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024552 static long_u done;
24553 static hashitem_T *hi;
24554 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024555
24556 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024557 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024558 done = 0;
24559 hi = func_hashtab.ht_array;
24560 }
24561 if (done < func_hashtab.ht_used)
24562 {
24563 if (done++ > 0)
24564 ++hi;
24565 while (HASHITEM_EMPTY(hi))
24566 ++hi;
24567 fp = HI2UF(hi);
24568
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024569 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024570 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024571
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024572 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24573 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024574
24575 cat_func_name(IObuff, fp);
24576 if (xp->xp_context != EXPAND_USER_FUNC)
24577 {
24578 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024579 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024580 STRCAT(IObuff, ")");
24581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024582 return IObuff;
24583 }
24584 return NULL;
24585}
24586
24587#endif /* FEAT_CMDL_COMPL */
24588
24589/*
24590 * Copy the function name of "fp" to buffer "buf".
24591 * "buf" must be able to hold the function name plus three bytes.
24592 * Takes care of script-local function names.
24593 */
24594 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024595cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024596{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024597 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024598 {
24599 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024600 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024601 }
24602 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024603 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024604}
24605
24606/*
24607 * ":delfunction {name}"
24608 */
24609 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024610ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024611{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024612 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024613 char_u *p;
24614 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024615 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024616
24617 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010024618 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024619 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024620 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024621 {
24622 if (fudi.fd_dict != NULL && !eap->skip)
24623 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024624 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024626 if (!ends_excmd(*skipwhite(p)))
24627 {
24628 vim_free(name);
24629 EMSG(_(e_trailing));
24630 return;
24631 }
24632 eap->nextcmd = check_nextcmd(p);
24633 if (eap->nextcmd != NULL)
24634 *p = NUL;
24635
24636 if (!eap->skip)
24637 fp = find_func(name);
24638 vim_free(name);
24639
24640 if (!eap->skip)
24641 {
24642 if (fp == NULL)
24643 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024644 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024645 return;
24646 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024647 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024648 {
24649 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24650 return;
24651 }
24652
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024653 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024654 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024655 /* Delete the dict item that refers to the function, it will
24656 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024657 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024658 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024659 else
24660 func_free(fp);
24661 }
24662}
24663
24664/*
24665 * Free a function and remove it from the list of functions.
24666 */
24667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024668func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024669{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024670 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024671
24672 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024673 ga_clear_strings(&(fp->uf_args));
24674 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024675#ifdef FEAT_PROFILE
24676 vim_free(fp->uf_tml_count);
24677 vim_free(fp->uf_tml_total);
24678 vim_free(fp->uf_tml_self);
24679#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024680
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024681 /* remove the function from the function hashtable */
24682 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24683 if (HASHITEM_EMPTY(hi))
24684 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024685 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024686 hash_remove(&func_hashtab, hi);
24687
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024688 vim_free(fp);
24689}
24690
24691/*
24692 * Unreference a Function: decrement the reference count and free it when it
24693 * becomes zero. Only for numbered functions.
24694 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024695 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024696func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024697{
24698 ufunc_T *fp;
24699
24700 if (name != NULL && isdigit(*name))
24701 {
24702 fp = find_func(name);
24703 if (fp == NULL)
24704 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024705 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024706 {
24707 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024708 * when "uf_calls" becomes zero. */
24709 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024710 func_free(fp);
24711 }
24712 }
24713}
24714
24715/*
24716 * Count a reference to a Function.
24717 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024718 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024719func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024720{
24721 ufunc_T *fp;
24722
24723 if (name != NULL && isdigit(*name))
24724 {
24725 fp = find_func(name);
24726 if (fp == NULL)
24727 EMSG2(_(e_intern2), "func_ref()");
24728 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024729 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024730 }
24731}
24732
24733/*
24734 * Call a user function.
24735 */
24736 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024737call_user_func(
24738 ufunc_T *fp, /* pointer to function */
24739 int argcount, /* nr of args */
24740 typval_T *argvars, /* arguments */
24741 typval_T *rettv, /* return value */
24742 linenr_T firstline, /* first line of range */
24743 linenr_T lastline, /* last line of range */
24744 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024745{
Bram Moolenaar33570922005-01-25 22:26:29 +000024746 char_u *save_sourcing_name;
24747 linenr_T save_sourcing_lnum;
24748 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024749 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024750 int save_did_emsg;
24751 static int depth = 0;
24752 dictitem_T *v;
24753 int fixvar_idx = 0; /* index in fixvar[] */
24754 int i;
24755 int ai;
24756 char_u numbuf[NUMBUFLEN];
24757 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024758 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024759#ifdef FEAT_PROFILE
24760 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024761 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024762#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024763
24764 /* If depth of calling is getting too high, don't execute the function */
24765 if (depth >= p_mfd)
24766 {
24767 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024768 rettv->v_type = VAR_NUMBER;
24769 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024770 return;
24771 }
24772 ++depth;
24773
24774 line_breakcheck(); /* check for CTRL-C hit */
24775
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024776 fc = (funccall_T *)alloc(sizeof(funccall_T));
24777 fc->caller = current_funccal;
24778 current_funccal = fc;
24779 fc->func = fp;
24780 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024781 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024782 fc->linenr = 0;
24783 fc->returned = FALSE;
24784 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024785 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024786 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24787 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024788
Bram Moolenaar33570922005-01-25 22:26:29 +000024789 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024790 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024791 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24792 * each argument variable and saves a lot of time.
24793 */
24794 /*
24795 * Init l: variables.
24796 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024797 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024798 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024799 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024800 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24801 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024802 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024803 name = v->di_key;
24804 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024805 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024806 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024807 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024808 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024809 v->di_tv.vval.v_dict = selfdict;
24810 ++selfdict->dv_refcount;
24811 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024812
Bram Moolenaar33570922005-01-25 22:26:29 +000024813 /*
24814 * Init a: variables.
24815 * Set a:0 to "argcount".
24816 * Set a:000 to a list with room for the "..." arguments.
24817 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024818 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024819 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024820 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024821 /* Use "name" to avoid a warning from some compiler that checks the
24822 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024823 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024824 name = v->di_key;
24825 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024826 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024827 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024828 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024829 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024830 v->di_tv.vval.v_list = &fc->l_varlist;
24831 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24832 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24833 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024834
24835 /*
24836 * Set a:firstline to "firstline" and a:lastline to "lastline".
24837 * Set a:name to named arguments.
24838 * Set a:N to the "..." arguments.
24839 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024840 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024841 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024842 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024843 (varnumber_T)lastline);
24844 for (i = 0; i < argcount; ++i)
24845 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024846 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024847 if (ai < 0)
24848 /* named argument a:name */
24849 name = FUNCARG(fp, i);
24850 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024851 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024852 /* "..." argument a:1, a:2, etc. */
24853 sprintf((char *)numbuf, "%d", ai + 1);
24854 name = numbuf;
24855 }
24856 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24857 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024858 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024859 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24860 }
24861 else
24862 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024863 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24864 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024865 if (v == NULL)
24866 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024867 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024868 }
24869 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024870 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024871
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024872 /* Note: the values are copied directly to avoid alloc/free.
24873 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024874 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024875 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024876
24877 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24878 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024879 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24880 fc->l_listitems[ai].li_tv = argvars[i];
24881 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024882 }
24883 }
24884
Bram Moolenaar071d4272004-06-13 20:20:40 +000024885 /* Don't redraw while executing the function. */
24886 ++RedrawingDisabled;
24887 save_sourcing_name = sourcing_name;
24888 save_sourcing_lnum = sourcing_lnum;
24889 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024890 /* need space for function name + ("function " + 3) or "[number]" */
24891 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24892 + STRLEN(fp->uf_name) + 20;
24893 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024894 if (sourcing_name != NULL)
24895 {
24896 if (save_sourcing_name != NULL
24897 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024898 sprintf((char *)sourcing_name, "%s[%d]..",
24899 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024900 else
24901 STRCPY(sourcing_name, "function ");
24902 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24903
24904 if (p_verbose >= 12)
24905 {
24906 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024907 verbose_enter_scroll();
24908
Bram Moolenaar555b2802005-05-19 21:08:39 +000024909 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024910 if (p_verbose >= 14)
24911 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024912 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024913 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024914 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024915 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024916
24917 msg_puts((char_u *)"(");
24918 for (i = 0; i < argcount; ++i)
24919 {
24920 if (i > 0)
24921 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024922 if (argvars[i].v_type == VAR_NUMBER)
24923 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024924 else
24925 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024926 /* Do not want errors such as E724 here. */
24927 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024928 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024929 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024930 if (s != NULL)
24931 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024932 if (vim_strsize(s) > MSG_BUF_CLEN)
24933 {
24934 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24935 s = buf;
24936 }
24937 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024938 vim_free(tofree);
24939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024940 }
24941 }
24942 msg_puts((char_u *)")");
24943 }
24944 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024945
24946 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024947 --no_wait_return;
24948 }
24949 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024950#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024951 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024952 {
24953 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24954 func_do_profile(fp);
24955 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024956 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024957 {
24958 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024959 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024960 profile_zero(&fp->uf_tm_children);
24961 }
24962 script_prof_save(&wait_start);
24963 }
24964#endif
24965
Bram Moolenaar071d4272004-06-13 20:20:40 +000024966 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024967 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024968 save_did_emsg = did_emsg;
24969 did_emsg = FALSE;
24970
24971 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024972 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024973 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24974
24975 --RedrawingDisabled;
24976
24977 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024978 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024979 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024980 clear_tv(rettv);
24981 rettv->v_type = VAR_NUMBER;
24982 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024983 }
24984
Bram Moolenaar05159a02005-02-26 23:04:13 +000024985#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024986 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024987 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024988 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024989 profile_end(&call_start);
24990 profile_sub_wait(&wait_start, &call_start);
24991 profile_add(&fp->uf_tm_total, &call_start);
24992 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024993 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024994 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024995 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24996 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024997 }
24998 }
24999#endif
25000
Bram Moolenaar071d4272004-06-13 20:20:40 +000025001 /* when being verbose, mention the return value */
25002 if (p_verbose >= 12)
25003 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025004 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025005 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025006
Bram Moolenaar071d4272004-06-13 20:20:40 +000025007 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025008 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025009 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025010 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025011 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025012 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025013 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025014 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025015 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025016 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025017 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025018
Bram Moolenaar555b2802005-05-19 21:08:39 +000025019 /* The value may be very long. Skip the middle part, so that we
25020 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025021 * truncate it at the end. Don't want errors such as E724 here. */
25022 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025023 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025024 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025025 if (s != NULL)
25026 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025027 if (vim_strsize(s) > MSG_BUF_CLEN)
25028 {
25029 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25030 s = buf;
25031 }
25032 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025033 vim_free(tofree);
25034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025035 }
25036 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025037
25038 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025039 --no_wait_return;
25040 }
25041
25042 vim_free(sourcing_name);
25043 sourcing_name = save_sourcing_name;
25044 sourcing_lnum = save_sourcing_lnum;
25045 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025046#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025047 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025048 script_prof_restore(&wait_start);
25049#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025050
25051 if (p_verbose >= 12 && sourcing_name != NULL)
25052 {
25053 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025054 verbose_enter_scroll();
25055
Bram Moolenaar555b2802005-05-19 21:08:39 +000025056 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025057 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025058
25059 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025060 --no_wait_return;
25061 }
25062
25063 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025064 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025065 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025066
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025067 /* If the a:000 list and the l: and a: dicts are not referenced we can
25068 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025069 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25070 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25071 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25072 {
25073 free_funccal(fc, FALSE);
25074 }
25075 else
25076 {
25077 hashitem_T *hi;
25078 listitem_T *li;
25079 int todo;
25080
25081 /* "fc" is still in use. This can happen when returning "a:000" or
25082 * assigning "l:" to a global variable.
25083 * Link "fc" in the list for garbage collection later. */
25084 fc->caller = previous_funccal;
25085 previous_funccal = fc;
25086
25087 /* Make a copy of the a: variables, since we didn't do that above. */
25088 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25089 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25090 {
25091 if (!HASHITEM_EMPTY(hi))
25092 {
25093 --todo;
25094 v = HI2DI(hi);
25095 copy_tv(&v->di_tv, &v->di_tv);
25096 }
25097 }
25098
25099 /* Make a copy of the a:000 items, since we didn't do that above. */
25100 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25101 copy_tv(&li->li_tv, &li->li_tv);
25102 }
25103}
25104
25105/*
25106 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025107 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025108 */
25109 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025110can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025111{
25112 return (fc->l_varlist.lv_copyID != copyID
25113 && fc->l_vars.dv_copyID != copyID
25114 && fc->l_avars.dv_copyID != copyID);
25115}
25116
25117/*
25118 * Free "fc" and what it contains.
25119 */
25120 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025121free_funccal(
25122 funccall_T *fc,
25123 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025124{
25125 listitem_T *li;
25126
25127 /* The a: variables typevals may not have been allocated, only free the
25128 * allocated variables. */
25129 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25130
25131 /* free all l: variables */
25132 vars_clear(&fc->l_vars.dv_hashtab);
25133
25134 /* Free the a:000 variables if they were allocated. */
25135 if (free_val)
25136 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25137 clear_tv(&li->li_tv);
25138
25139 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025140}
25141
25142/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025143 * Add a number variable "name" to dict "dp" with value "nr".
25144 */
25145 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025146add_nr_var(
25147 dict_T *dp,
25148 dictitem_T *v,
25149 char *name,
25150 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025151{
25152 STRCPY(v->di_key, name);
25153 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25154 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25155 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025156 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025157 v->di_tv.vval.v_number = nr;
25158}
25159
25160/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025161 * ":return [expr]"
25162 */
25163 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025164ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025165{
25166 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025167 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025168 int returning = FALSE;
25169
25170 if (current_funccal == NULL)
25171 {
25172 EMSG(_("E133: :return not inside a function"));
25173 return;
25174 }
25175
25176 if (eap->skip)
25177 ++emsg_skip;
25178
25179 eap->nextcmd = NULL;
25180 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025181 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025182 {
25183 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025184 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025185 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025186 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025187 }
25188 /* It's safer to return also on error. */
25189 else if (!eap->skip)
25190 {
25191 /*
25192 * Return unless the expression evaluation has been cancelled due to an
25193 * aborting error, an interrupt, or an exception.
25194 */
25195 if (!aborting())
25196 returning = do_return(eap, FALSE, TRUE, NULL);
25197 }
25198
25199 /* When skipping or the return gets pending, advance to the next command
25200 * in this line (!returning). Otherwise, ignore the rest of the line.
25201 * Following lines will be ignored by get_func_line(). */
25202 if (returning)
25203 eap->nextcmd = NULL;
25204 else if (eap->nextcmd == NULL) /* no argument */
25205 eap->nextcmd = check_nextcmd(arg);
25206
25207 if (eap->skip)
25208 --emsg_skip;
25209}
25210
25211/*
25212 * Return from a function. Possibly makes the return pending. Also called
25213 * for a pending return at the ":endtry" or after returning from an extra
25214 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025215 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025216 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025217 * FALSE when the return gets pending.
25218 */
25219 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025220do_return(
25221 exarg_T *eap,
25222 int reanimate,
25223 int is_cmd,
25224 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025225{
25226 int idx;
25227 struct condstack *cstack = eap->cstack;
25228
25229 if (reanimate)
25230 /* Undo the return. */
25231 current_funccal->returned = FALSE;
25232
25233 /*
25234 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25235 * not in its finally clause (which then is to be executed next) is found.
25236 * In this case, make the ":return" pending for execution at the ":endtry".
25237 * Otherwise, return normally.
25238 */
25239 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25240 if (idx >= 0)
25241 {
25242 cstack->cs_pending[idx] = CSTP_RETURN;
25243
25244 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025245 /* A pending return again gets pending. "rettv" points to an
25246 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025247 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025248 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025249 else
25250 {
25251 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025252 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025253 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025254 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025255
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025256 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025257 {
25258 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025259 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025260 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025261 else
25262 EMSG(_(e_outofmem));
25263 }
25264 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025265 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025266
25267 if (reanimate)
25268 {
25269 /* The pending return value could be overwritten by a ":return"
25270 * without argument in a finally clause; reset the default
25271 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025272 current_funccal->rettv->v_type = VAR_NUMBER;
25273 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025274 }
25275 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025276 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025277 }
25278 else
25279 {
25280 current_funccal->returned = TRUE;
25281
25282 /* If the return is carried out now, store the return value. For
25283 * a return immediately after reanimation, the value is already
25284 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025285 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025286 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025287 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025288 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025289 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025290 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025291 }
25292 }
25293
25294 return idx < 0;
25295}
25296
25297/*
25298 * Free the variable with a pending return value.
25299 */
25300 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025301discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025302{
Bram Moolenaar33570922005-01-25 22:26:29 +000025303 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025304}
25305
25306/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025307 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025308 * is an allocated string. Used by report_pending() for verbose messages.
25309 */
25310 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025311get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025312{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025313 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025314 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025315 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025316
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025317 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025318 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025319 if (s == NULL)
25320 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025321
25322 STRCPY(IObuff, ":return ");
25323 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25324 if (STRLEN(s) + 8 >= IOSIZE)
25325 STRCPY(IObuff + IOSIZE - 4, "...");
25326 vim_free(tofree);
25327 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025328}
25329
25330/*
25331 * Get next function line.
25332 * Called by do_cmdline() to get the next line.
25333 * Returns allocated string, or NULL for end of function.
25334 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025335 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025336get_func_line(
25337 int c UNUSED,
25338 void *cookie,
25339 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025340{
Bram Moolenaar33570922005-01-25 22:26:29 +000025341 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025342 ufunc_T *fp = fcp->func;
25343 char_u *retval;
25344 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025345
25346 /* If breakpoints have been added/deleted need to check for it. */
25347 if (fcp->dbg_tick != debug_tick)
25348 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025349 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025350 sourcing_lnum);
25351 fcp->dbg_tick = debug_tick;
25352 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025353#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025354 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025355 func_line_end(cookie);
25356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025357
Bram Moolenaar05159a02005-02-26 23:04:13 +000025358 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025359 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25360 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025361 retval = NULL;
25362 else
25363 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025364 /* Skip NULL lines (continuation lines). */
25365 while (fcp->linenr < gap->ga_len
25366 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25367 ++fcp->linenr;
25368 if (fcp->linenr >= gap->ga_len)
25369 retval = NULL;
25370 else
25371 {
25372 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25373 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025374#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025375 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025376 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025377#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025379 }
25380
25381 /* Did we encounter a breakpoint? */
25382 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25383 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025384 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025385 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025386 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025387 sourcing_lnum);
25388 fcp->dbg_tick = debug_tick;
25389 }
25390
25391 return retval;
25392}
25393
Bram Moolenaar05159a02005-02-26 23:04:13 +000025394#if defined(FEAT_PROFILE) || defined(PROTO)
25395/*
25396 * Called when starting to read a function line.
25397 * "sourcing_lnum" must be correct!
25398 * When skipping lines it may not actually be executed, but we won't find out
25399 * until later and we need to store the time now.
25400 */
25401 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025402func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025403{
25404 funccall_T *fcp = (funccall_T *)cookie;
25405 ufunc_T *fp = fcp->func;
25406
25407 if (fp->uf_profiling && sourcing_lnum >= 1
25408 && sourcing_lnum <= fp->uf_lines.ga_len)
25409 {
25410 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025411 /* Skip continuation lines. */
25412 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25413 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025414 fp->uf_tml_execed = FALSE;
25415 profile_start(&fp->uf_tml_start);
25416 profile_zero(&fp->uf_tml_children);
25417 profile_get_wait(&fp->uf_tml_wait);
25418 }
25419}
25420
25421/*
25422 * Called when actually executing a function line.
25423 */
25424 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025425func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025426{
25427 funccall_T *fcp = (funccall_T *)cookie;
25428 ufunc_T *fp = fcp->func;
25429
25430 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25431 fp->uf_tml_execed = TRUE;
25432}
25433
25434/*
25435 * Called when done with a function line.
25436 */
25437 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025438func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025439{
25440 funccall_T *fcp = (funccall_T *)cookie;
25441 ufunc_T *fp = fcp->func;
25442
25443 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25444 {
25445 if (fp->uf_tml_execed)
25446 {
25447 ++fp->uf_tml_count[fp->uf_tml_idx];
25448 profile_end(&fp->uf_tml_start);
25449 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025450 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025451 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25452 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025453 }
25454 fp->uf_tml_idx = -1;
25455 }
25456}
25457#endif
25458
Bram Moolenaar071d4272004-06-13 20:20:40 +000025459/*
25460 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025461 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025462 */
25463 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025464func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025465{
Bram Moolenaar33570922005-01-25 22:26:29 +000025466 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025467
25468 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25469 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025470 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025471 || fcp->returned);
25472}
25473
25474/*
25475 * return TRUE if cookie indicates a function which "abort"s on errors.
25476 */
25477 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025478func_has_abort(
25479 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025480{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025481 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025482}
25483
25484#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25485typedef enum
25486{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025487 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25488 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25489 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025490} var_flavour_T;
25491
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025492static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025493
25494 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025495var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025496{
25497 char_u *p = varname;
25498
25499 if (ASCII_ISUPPER(*p))
25500 {
25501 while (*(++p))
25502 if (ASCII_ISLOWER(*p))
25503 return VAR_FLAVOUR_SESSION;
25504 return VAR_FLAVOUR_VIMINFO;
25505 }
25506 else
25507 return VAR_FLAVOUR_DEFAULT;
25508}
25509#endif
25510
25511#if defined(FEAT_VIMINFO) || defined(PROTO)
25512/*
25513 * Restore global vars that start with a capital from the viminfo file
25514 */
25515 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025516read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025517{
25518 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025519 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025520 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025521 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025522
25523 if (!writing && (find_viminfo_parameter('!') != NULL))
25524 {
25525 tab = vim_strchr(virp->vir_line + 1, '\t');
25526 if (tab != NULL)
25527 {
25528 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025529 switch (*tab)
25530 {
25531 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025532#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025533 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025534#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025535 case 'D': type = VAR_DICT; break;
25536 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025537 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025539
25540 tab = vim_strchr(tab, '\t');
25541 if (tab != NULL)
25542 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025543 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025544 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025545 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025546 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025547#ifdef FEAT_FLOAT
25548 else if (type == VAR_FLOAT)
25549 (void)string2float(tab + 1, &tv.vval.v_float);
25550#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025551 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025552 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025553 if (type == VAR_DICT || type == VAR_LIST)
25554 {
25555 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25556
25557 if (etv == NULL)
25558 /* Failed to parse back the dict or list, use it as a
25559 * string. */
25560 tv.v_type = VAR_STRING;
25561 else
25562 {
25563 vim_free(tv.vval.v_string);
25564 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025565 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025566 }
25567 }
25568
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025569 /* when in a function use global variables */
25570 save_funccal = current_funccal;
25571 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025572 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025573 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025574
25575 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025576 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025577 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25578 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025579 }
25580 }
25581 }
25582
25583 return viminfo_readline(virp);
25584}
25585
25586/*
25587 * Write global vars that start with a capital to the viminfo file
25588 */
25589 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025590write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025591{
Bram Moolenaar33570922005-01-25 22:26:29 +000025592 hashitem_T *hi;
25593 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025594 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025595 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025596 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025597 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025598 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025599
25600 if (find_viminfo_parameter('!') == NULL)
25601 return;
25602
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025603 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025604
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025605 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025606 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025607 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025608 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025609 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025610 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025611 this_var = HI2DI(hi);
25612 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025613 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025614 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025615 {
25616 case VAR_STRING: s = "STR"; break;
25617 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025618 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025619 case VAR_DICT: s = "DIC"; break;
25620 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025621 case VAR_SPECIAL: s = "XPL"; break;
25622
25623 case VAR_UNKNOWN:
25624 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010025625 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025626 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025627 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025628 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025629 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025630 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025631 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025632 if (p != NULL)
25633 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025634 vim_free(tofree);
25635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025636 }
25637 }
25638}
25639#endif
25640
25641#if defined(FEAT_SESSION) || defined(PROTO)
25642 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025643store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025644{
Bram Moolenaar33570922005-01-25 22:26:29 +000025645 hashitem_T *hi;
25646 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025647 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025648 char_u *p, *t;
25649
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025650 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025651 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025652 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025653 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025654 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025655 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025656 this_var = HI2DI(hi);
25657 if ((this_var->di_tv.v_type == VAR_NUMBER
25658 || this_var->di_tv.v_type == VAR_STRING)
25659 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025660 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025661 /* Escape special characters with a backslash. Turn a LF and
25662 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025663 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025664 (char_u *)"\\\"\n\r");
25665 if (p == NULL) /* out of memory */
25666 break;
25667 for (t = p; *t != NUL; ++t)
25668 if (*t == '\n')
25669 *t = 'n';
25670 else if (*t == '\r')
25671 *t = 'r';
25672 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025673 this_var->di_key,
25674 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25675 : ' ',
25676 p,
25677 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25678 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025679 || put_eol(fd) == FAIL)
25680 {
25681 vim_free(p);
25682 return FAIL;
25683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025684 vim_free(p);
25685 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025686#ifdef FEAT_FLOAT
25687 else if (this_var->di_tv.v_type == VAR_FLOAT
25688 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25689 {
25690 float_T f = this_var->di_tv.vval.v_float;
25691 int sign = ' ';
25692
25693 if (f < 0)
25694 {
25695 f = -f;
25696 sign = '-';
25697 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025698 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025699 this_var->di_key, sign, f) < 0)
25700 || put_eol(fd) == FAIL)
25701 return FAIL;
25702 }
25703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025704 }
25705 }
25706 return OK;
25707}
25708#endif
25709
Bram Moolenaar661b1822005-07-28 22:36:45 +000025710/*
25711 * Display script name where an item was last set.
25712 * Should only be invoked when 'verbose' is non-zero.
25713 */
25714 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025715last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025716{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025717 char_u *p;
25718
Bram Moolenaar661b1822005-07-28 22:36:45 +000025719 if (scriptID != 0)
25720 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025721 p = home_replace_save(NULL, get_scriptname(scriptID));
25722 if (p != NULL)
25723 {
25724 verbose_enter();
25725 MSG_PUTS(_("\n\tLast set from "));
25726 MSG_PUTS(p);
25727 vim_free(p);
25728 verbose_leave();
25729 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025730 }
25731}
25732
Bram Moolenaard812df62008-11-09 12:46:09 +000025733/*
25734 * List v:oldfiles in a nice way.
25735 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025736 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025737ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025738{
25739 list_T *l = vimvars[VV_OLDFILES].vv_list;
25740 listitem_T *li;
25741 int nr = 0;
25742
25743 if (l == NULL)
25744 msg((char_u *)_("No old files"));
25745 else
25746 {
25747 msg_start();
25748 msg_scroll = TRUE;
25749 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25750 {
25751 msg_outnum((long)++nr);
25752 MSG_PUTS(": ");
25753 msg_outtrans(get_tv_string(&li->li_tv));
25754 msg_putchar('\n');
25755 out_flush(); /* output one line at a time */
25756 ui_breakcheck();
25757 }
25758 /* Assume "got_int" was set to truncate the listing. */
25759 got_int = FALSE;
25760
25761#ifdef FEAT_BROWSE_CMD
25762 if (cmdmod.browse)
25763 {
25764 quit_more = FALSE;
25765 nr = prompt_for_number(FALSE);
25766 msg_starthere();
25767 if (nr > 0)
25768 {
25769 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25770 (long)nr);
25771
25772 if (p != NULL)
25773 {
25774 p = expand_env_save(p);
25775 eap->arg = p;
25776 eap->cmdidx = CMD_edit;
25777 cmdmod.browse = FALSE;
25778 do_exedit(eap, NULL);
25779 vim_free(p);
25780 }
25781 }
25782 }
25783#endif
25784 }
25785}
25786
Bram Moolenaar53744302015-07-17 17:38:22 +020025787/* reset v:option_new, v:option_old and v:option_type */
25788 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025789reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025790{
25791 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25792 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25793 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25794}
25795
25796
Bram Moolenaar071d4272004-06-13 20:20:40 +000025797#endif /* FEAT_EVAL */
25798
Bram Moolenaar071d4272004-06-13 20:20:40 +000025799
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025800#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025801
25802#ifdef WIN3264
25803/*
25804 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25805 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025806static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25807static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25808static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025809
25810/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025811 * Get the short path (8.3) for the filename in "fnamep".
25812 * Only works for a valid file name.
25813 * When the path gets longer "fnamep" is changed and the allocated buffer
25814 * is put in "bufp".
25815 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25816 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025817 */
25818 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025819get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025820{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025821 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025822 char_u *newbuf;
25823
25824 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025825 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025826 if (l > len - 1)
25827 {
25828 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025829 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025830 newbuf = vim_strnsave(*fnamep, l);
25831 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025832 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025833
25834 vim_free(*bufp);
25835 *fnamep = *bufp = newbuf;
25836
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025837 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025838 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025839 }
25840
25841 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025842 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025843}
25844
25845/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025846 * Get the short path (8.3) for the filename in "fname". The converted
25847 * path is returned in "bufp".
25848 *
25849 * Some of the directories specified in "fname" may not exist. This function
25850 * will shorten the existing directories at the beginning of the path and then
25851 * append the remaining non-existing path.
25852 *
25853 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025854 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025855 * bufp - Pointer to an allocated buffer for the filename.
25856 * fnamelen - Length of the filename pointed to by fname
25857 *
25858 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025859 */
25860 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025861shortpath_for_invalid_fname(
25862 char_u **fname,
25863 char_u **bufp,
25864 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025865{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025866 char_u *short_fname, *save_fname, *pbuf_unused;
25867 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025868 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025869 int old_len, len;
25870 int new_len, sfx_len;
25871 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025872
25873 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025874 old_len = *fnamelen;
25875 save_fname = vim_strnsave(*fname, old_len);
25876 pbuf_unused = NULL;
25877 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025878
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025879 endp = save_fname + old_len - 1; /* Find the end of the copy */
25880 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025881
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025882 /*
25883 * Try shortening the supplied path till it succeeds by removing one
25884 * directory at a time from the tail of the path.
25885 */
25886 len = 0;
25887 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025888 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025889 /* go back one path-separator */
25890 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25891 --endp;
25892 if (endp <= save_fname)
25893 break; /* processed the complete path */
25894
25895 /*
25896 * Replace the path separator with a NUL and try to shorten the
25897 * resulting path.
25898 */
25899 ch = *endp;
25900 *endp = 0;
25901 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025902 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025903 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25904 {
25905 retval = FAIL;
25906 goto theend;
25907 }
25908 *endp = ch; /* preserve the string */
25909
25910 if (len > 0)
25911 break; /* successfully shortened the path */
25912
25913 /* failed to shorten the path. Skip the path separator */
25914 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025915 }
25916
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025917 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025918 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025919 /*
25920 * Succeeded in shortening the path. Now concatenate the shortened
25921 * path with the remaining path at the tail.
25922 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025923
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025924 /* Compute the length of the new path. */
25925 sfx_len = (int)(save_endp - endp) + 1;
25926 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025927
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025928 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025929 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025930 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025931 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025932 /* There is not enough space in the currently allocated string,
25933 * copy it to a buffer big enough. */
25934 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025935 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025936 {
25937 retval = FAIL;
25938 goto theend;
25939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025940 }
25941 else
25942 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025943 /* Transfer short_fname to the main buffer (it's big enough),
25944 * unless get_short_pathname() did its work in-place. */
25945 *fname = *bufp = save_fname;
25946 if (short_fname != save_fname)
25947 vim_strncpy(save_fname, short_fname, len);
25948 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025949 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025950
25951 /* concat the not-shortened part of the path */
25952 vim_strncpy(*fname + len, endp, sfx_len);
25953 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025954 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025955
25956theend:
25957 vim_free(pbuf_unused);
25958 vim_free(save_fname);
25959
25960 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025961}
25962
25963/*
25964 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025965 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025966 */
25967 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025968shortpath_for_partial(
25969 char_u **fnamep,
25970 char_u **bufp,
25971 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025972{
25973 int sepcount, len, tflen;
25974 char_u *p;
25975 char_u *pbuf, *tfname;
25976 int hasTilde;
25977
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025978 /* Count up the path separators from the RHS.. so we know which part
25979 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025980 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025981 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025982 if (vim_ispathsep(*p))
25983 ++sepcount;
25984
25985 /* Need full path first (use expand_env() to remove a "~/") */
25986 hasTilde = (**fnamep == '~');
25987 if (hasTilde)
25988 pbuf = tfname = expand_env_save(*fnamep);
25989 else
25990 pbuf = tfname = FullName_save(*fnamep, FALSE);
25991
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025992 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025993
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025994 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25995 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025996
25997 if (len == 0)
25998 {
25999 /* Don't have a valid filename, so shorten the rest of the
26000 * path if we can. This CAN give us invalid 8.3 filenames, but
26001 * there's not a lot of point in guessing what it might be.
26002 */
26003 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026004 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26005 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026006 }
26007
26008 /* Count the paths backward to find the beginning of the desired string. */
26009 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026010 {
26011#ifdef FEAT_MBYTE
26012 if (has_mbyte)
26013 p -= mb_head_off(tfname, p);
26014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026015 if (vim_ispathsep(*p))
26016 {
26017 if (sepcount == 0 || (hasTilde && sepcount == 1))
26018 break;
26019 else
26020 sepcount --;
26021 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026023 if (hasTilde)
26024 {
26025 --p;
26026 if (p >= tfname)
26027 *p = '~';
26028 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026029 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026030 }
26031 else
26032 ++p;
26033
26034 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26035 vim_free(*bufp);
26036 *fnamelen = (int)STRLEN(p);
26037 *bufp = pbuf;
26038 *fnamep = p;
26039
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026040 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026041}
26042#endif /* WIN3264 */
26043
26044/*
26045 * Adjust a filename, according to a string of modifiers.
26046 * *fnamep must be NUL terminated when called. When returning, the length is
26047 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026048 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026049 * When there is an error, *fnamep is set to NULL.
26050 */
26051 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026052modify_fname(
26053 char_u *src, /* string with modifiers */
26054 int *usedlen, /* characters after src that are used */
26055 char_u **fnamep, /* file name so far */
26056 char_u **bufp, /* buffer for allocated file name or NULL */
26057 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026058{
26059 int valid = 0;
26060 char_u *tail;
26061 char_u *s, *p, *pbuf;
26062 char_u dirname[MAXPATHL];
26063 int c;
26064 int has_fullname = 0;
26065#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026066 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026067 int has_shortname = 0;
26068#endif
26069
26070repeat:
26071 /* ":p" - full path/file_name */
26072 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26073 {
26074 has_fullname = 1;
26075
26076 valid |= VALID_PATH;
26077 *usedlen += 2;
26078
26079 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26080 if ((*fnamep)[0] == '~'
26081#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26082 && ((*fnamep)[1] == '/'
26083# ifdef BACKSLASH_IN_FILENAME
26084 || (*fnamep)[1] == '\\'
26085# endif
26086 || (*fnamep)[1] == NUL)
26087
26088#endif
26089 )
26090 {
26091 *fnamep = expand_env_save(*fnamep);
26092 vim_free(*bufp); /* free any allocated file name */
26093 *bufp = *fnamep;
26094 if (*fnamep == NULL)
26095 return -1;
26096 }
26097
26098 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026099 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026100 {
26101 if (vim_ispathsep(*p)
26102 && p[1] == '.'
26103 && (p[2] == NUL
26104 || vim_ispathsep(p[2])
26105 || (p[2] == '.'
26106 && (p[3] == NUL || vim_ispathsep(p[3])))))
26107 break;
26108 }
26109
26110 /* FullName_save() is slow, don't use it when not needed. */
26111 if (*p != NUL || !vim_isAbsName(*fnamep))
26112 {
26113 *fnamep = FullName_save(*fnamep, *p != NUL);
26114 vim_free(*bufp); /* free any allocated file name */
26115 *bufp = *fnamep;
26116 if (*fnamep == NULL)
26117 return -1;
26118 }
26119
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026120#ifdef WIN3264
26121# if _WIN32_WINNT >= 0x0500
26122 if (vim_strchr(*fnamep, '~') != NULL)
26123 {
26124 /* Expand 8.3 filename to full path. Needed to make sure the same
26125 * file does not have two different names.
26126 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26127 p = alloc(_MAX_PATH + 1);
26128 if (p != NULL)
26129 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026130 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026131 {
26132 vim_free(*bufp);
26133 *bufp = *fnamep = p;
26134 }
26135 else
26136 vim_free(p);
26137 }
26138 }
26139# endif
26140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026141 /* Append a path separator to a directory. */
26142 if (mch_isdir(*fnamep))
26143 {
26144 /* Make room for one or two extra characters. */
26145 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26146 vim_free(*bufp); /* free any allocated file name */
26147 *bufp = *fnamep;
26148 if (*fnamep == NULL)
26149 return -1;
26150 add_pathsep(*fnamep);
26151 }
26152 }
26153
26154 /* ":." - path relative to the current directory */
26155 /* ":~" - path relative to the home directory */
26156 /* ":8" - shortname path - postponed till after */
26157 while (src[*usedlen] == ':'
26158 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26159 {
26160 *usedlen += 2;
26161 if (c == '8')
26162 {
26163#ifdef WIN3264
26164 has_shortname = 1; /* Postpone this. */
26165#endif
26166 continue;
26167 }
26168 pbuf = NULL;
26169 /* Need full path first (use expand_env() to remove a "~/") */
26170 if (!has_fullname)
26171 {
26172 if (c == '.' && **fnamep == '~')
26173 p = pbuf = expand_env_save(*fnamep);
26174 else
26175 p = pbuf = FullName_save(*fnamep, FALSE);
26176 }
26177 else
26178 p = *fnamep;
26179
26180 has_fullname = 0;
26181
26182 if (p != NULL)
26183 {
26184 if (c == '.')
26185 {
26186 mch_dirname(dirname, MAXPATHL);
26187 s = shorten_fname(p, dirname);
26188 if (s != NULL)
26189 {
26190 *fnamep = s;
26191 if (pbuf != NULL)
26192 {
26193 vim_free(*bufp); /* free any allocated file name */
26194 *bufp = pbuf;
26195 pbuf = NULL;
26196 }
26197 }
26198 }
26199 else
26200 {
26201 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26202 /* Only replace it when it starts with '~' */
26203 if (*dirname == '~')
26204 {
26205 s = vim_strsave(dirname);
26206 if (s != NULL)
26207 {
26208 *fnamep = s;
26209 vim_free(*bufp);
26210 *bufp = s;
26211 }
26212 }
26213 }
26214 vim_free(pbuf);
26215 }
26216 }
26217
26218 tail = gettail(*fnamep);
26219 *fnamelen = (int)STRLEN(*fnamep);
26220
26221 /* ":h" - head, remove "/file_name", can be repeated */
26222 /* Don't remove the first "/" or "c:\" */
26223 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26224 {
26225 valid |= VALID_HEAD;
26226 *usedlen += 2;
26227 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026228 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026229 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026230 *fnamelen = (int)(tail - *fnamep);
26231#ifdef VMS
26232 if (*fnamelen > 0)
26233 *fnamelen += 1; /* the path separator is part of the path */
26234#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026235 if (*fnamelen == 0)
26236 {
26237 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26238 p = vim_strsave((char_u *)".");
26239 if (p == NULL)
26240 return -1;
26241 vim_free(*bufp);
26242 *bufp = *fnamep = tail = p;
26243 *fnamelen = 1;
26244 }
26245 else
26246 {
26247 while (tail > s && !after_pathsep(s, tail))
26248 mb_ptr_back(*fnamep, tail);
26249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026250 }
26251
26252 /* ":8" - shortname */
26253 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26254 {
26255 *usedlen += 2;
26256#ifdef WIN3264
26257 has_shortname = 1;
26258#endif
26259 }
26260
26261#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026262 /*
26263 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026264 */
26265 if (has_shortname)
26266 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026267 /* Copy the string if it is shortened by :h and when it wasn't copied
26268 * yet, because we are going to change it in place. Avoids changing
26269 * the buffer name for "%:8". */
26270 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026271 {
26272 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026273 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026274 return -1;
26275 vim_free(*bufp);
26276 *bufp = *fnamep = p;
26277 }
26278
26279 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026280 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026281 if (!has_fullname && !vim_isAbsName(*fnamep))
26282 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026283 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026284 return -1;
26285 }
26286 else
26287 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026288 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026289
Bram Moolenaardc935552011-08-17 15:23:23 +020026290 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026291 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026292 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026293 return -1;
26294
26295 if (l == 0)
26296 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026297 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026298 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026299 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026300 return -1;
26301 }
26302 *fnamelen = l;
26303 }
26304 }
26305#endif /* WIN3264 */
26306
26307 /* ":t" - tail, just the basename */
26308 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26309 {
26310 *usedlen += 2;
26311 *fnamelen -= (int)(tail - *fnamep);
26312 *fnamep = tail;
26313 }
26314
26315 /* ":e" - extension, can be repeated */
26316 /* ":r" - root, without extension, can be repeated */
26317 while (src[*usedlen] == ':'
26318 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26319 {
26320 /* find a '.' in the tail:
26321 * - for second :e: before the current fname
26322 * - otherwise: The last '.'
26323 */
26324 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26325 s = *fnamep - 2;
26326 else
26327 s = *fnamep + *fnamelen - 1;
26328 for ( ; s > tail; --s)
26329 if (s[0] == '.')
26330 break;
26331 if (src[*usedlen + 1] == 'e') /* :e */
26332 {
26333 if (s > tail)
26334 {
26335 *fnamelen += (int)(*fnamep - (s + 1));
26336 *fnamep = s + 1;
26337#ifdef VMS
26338 /* cut version from the extension */
26339 s = *fnamep + *fnamelen - 1;
26340 for ( ; s > *fnamep; --s)
26341 if (s[0] == ';')
26342 break;
26343 if (s > *fnamep)
26344 *fnamelen = s - *fnamep;
26345#endif
26346 }
26347 else if (*fnamep <= tail)
26348 *fnamelen = 0;
26349 }
26350 else /* :r */
26351 {
26352 if (s > tail) /* remove one extension */
26353 *fnamelen = (int)(s - *fnamep);
26354 }
26355 *usedlen += 2;
26356 }
26357
26358 /* ":s?pat?foo?" - substitute */
26359 /* ":gs?pat?foo?" - global substitute */
26360 if (src[*usedlen] == ':'
26361 && (src[*usedlen + 1] == 's'
26362 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26363 {
26364 char_u *str;
26365 char_u *pat;
26366 char_u *sub;
26367 int sep;
26368 char_u *flags;
26369 int didit = FALSE;
26370
26371 flags = (char_u *)"";
26372 s = src + *usedlen + 2;
26373 if (src[*usedlen + 1] == 'g')
26374 {
26375 flags = (char_u *)"g";
26376 ++s;
26377 }
26378
26379 sep = *s++;
26380 if (sep)
26381 {
26382 /* find end of pattern */
26383 p = vim_strchr(s, sep);
26384 if (p != NULL)
26385 {
26386 pat = vim_strnsave(s, (int)(p - s));
26387 if (pat != NULL)
26388 {
26389 s = p + 1;
26390 /* find end of substitution */
26391 p = vim_strchr(s, sep);
26392 if (p != NULL)
26393 {
26394 sub = vim_strnsave(s, (int)(p - s));
26395 str = vim_strnsave(*fnamep, *fnamelen);
26396 if (sub != NULL && str != NULL)
26397 {
26398 *usedlen = (int)(p + 1 - src);
26399 s = do_string_sub(str, pat, sub, flags);
26400 if (s != NULL)
26401 {
26402 *fnamep = s;
26403 *fnamelen = (int)STRLEN(s);
26404 vim_free(*bufp);
26405 *bufp = s;
26406 didit = TRUE;
26407 }
26408 }
26409 vim_free(sub);
26410 vim_free(str);
26411 }
26412 vim_free(pat);
26413 }
26414 }
26415 /* after using ":s", repeat all the modifiers */
26416 if (didit)
26417 goto repeat;
26418 }
26419 }
26420
Bram Moolenaar26df0922014-02-23 23:39:13 +010026421 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26422 {
26423 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
26424 if (p == NULL)
26425 return -1;
26426 vim_free(*bufp);
26427 *bufp = *fnamep = p;
26428 *fnamelen = (int)STRLEN(p);
26429 *usedlen += 2;
26430 }
26431
Bram Moolenaar071d4272004-06-13 20:20:40 +000026432 return valid;
26433}
26434
26435/*
26436 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26437 * "flags" can be "g" to do a global substitute.
26438 * Returns an allocated string, NULL for error.
26439 */
26440 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026441do_string_sub(
26442 char_u *str,
26443 char_u *pat,
26444 char_u *sub,
26445 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026446{
26447 int sublen;
26448 regmatch_T regmatch;
26449 int i;
26450 int do_all;
26451 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026452 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026453 garray_T ga;
26454 char_u *ret;
26455 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026456 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026457
26458 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26459 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026460 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026461
26462 ga_init2(&ga, 1, 200);
26463
26464 do_all = (flags[0] == 'g');
26465
26466 regmatch.rm_ic = p_ic;
26467 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26468 if (regmatch.regprog != NULL)
26469 {
26470 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026471 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026472 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26473 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026474 /* Skip empty match except for first match. */
26475 if (regmatch.startp[0] == regmatch.endp[0])
26476 {
26477 if (zero_width == regmatch.startp[0])
26478 {
26479 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026480 i = MB_PTR2LEN(tail);
26481 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26482 (size_t)i);
26483 ga.ga_len += i;
26484 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026485 continue;
26486 }
26487 zero_width = regmatch.startp[0];
26488 }
26489
Bram Moolenaar071d4272004-06-13 20:20:40 +000026490 /*
26491 * Get some space for a temporary buffer to do the substitution
26492 * into. It will contain:
26493 * - The text up to where the match is.
26494 * - The substituted text.
26495 * - The text after the match.
26496 */
26497 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026498 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026499 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26500 {
26501 ga_clear(&ga);
26502 break;
26503 }
26504
26505 /* copy the text up to where the match is */
26506 i = (int)(regmatch.startp[0] - tail);
26507 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26508 /* add the substituted text */
26509 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26510 + ga.ga_len + i, TRUE, TRUE, FALSE);
26511 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026512 tail = regmatch.endp[0];
26513 if (*tail == NUL)
26514 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026515 if (!do_all)
26516 break;
26517 }
26518
26519 if (ga.ga_data != NULL)
26520 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26521
Bram Moolenaar473de612013-06-08 18:19:48 +020026522 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026523 }
26524
26525 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26526 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026527 if (p_cpo == empty_option)
26528 p_cpo = save_cpo;
26529 else
26530 /* Darn, evaluating {sub} expression changed the value. */
26531 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026532
26533 return ret;
26534}
26535
26536#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */