blob: 5025a52c1978786c14556fccbaad8538a1327380 [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);
455static char_u *deref_func_name(char_u *name, int *lenp, 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, 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 Moolenaar81661fb2016-02-18 22:23:34 +0100504static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100505static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
506static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100507static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100508static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100509static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
510static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100511static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100512static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100513#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100514static void f_changenr(typval_T *argvars, typval_T *rettv);
515static void f_char2nr(typval_T *argvars, typval_T *rettv);
516static void f_cindent(typval_T *argvars, typval_T *rettv);
517static void f_clearmatches(typval_T *argvars, typval_T *rettv);
518static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000519#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100520static void f_complete(typval_T *argvars, typval_T *rettv);
521static void f_complete_add(typval_T *argvars, typval_T *rettv);
522static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000523#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100524static void f_confirm(typval_T *argvars, typval_T *rettv);
525static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000526#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100527static void f_cos(typval_T *argvars, typval_T *rettv);
528static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000529#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100530static void f_count(typval_T *argvars, typval_T *rettv);
531static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
532static void f_cursor(typval_T *argsvars, typval_T *rettv);
533static void f_deepcopy(typval_T *argvars, typval_T *rettv);
534static void f_delete(typval_T *argvars, typval_T *rettv);
535static void f_did_filetype(typval_T *argvars, typval_T *rettv);
536static void f_diff_filler(typval_T *argvars, typval_T *rettv);
537static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +0100538static void f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100539static void f_empty(typval_T *argvars, typval_T *rettv);
540static void f_escape(typval_T *argvars, typval_T *rettv);
541static void f_eval(typval_T *argvars, typval_T *rettv);
542static void f_eventhandler(typval_T *argvars, typval_T *rettv);
543static void f_executable(typval_T *argvars, typval_T *rettv);
544static void f_exepath(typval_T *argvars, typval_T *rettv);
545static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200546#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100547static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200548#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100549static void f_expand(typval_T *argvars, typval_T *rettv);
550static void f_extend(typval_T *argvars, typval_T *rettv);
551static void f_feedkeys(typval_T *argvars, typval_T *rettv);
552static void f_filereadable(typval_T *argvars, typval_T *rettv);
553static void f_filewritable(typval_T *argvars, typval_T *rettv);
554static void f_filter(typval_T *argvars, typval_T *rettv);
555static void f_finddir(typval_T *argvars, typval_T *rettv);
556static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000557#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100558static void f_float2nr(typval_T *argvars, typval_T *rettv);
559static void f_floor(typval_T *argvars, typval_T *rettv);
560static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000561#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100562static void f_fnameescape(typval_T *argvars, typval_T *rettv);
563static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
564static void f_foldclosed(typval_T *argvars, typval_T *rettv);
565static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
566static void f_foldlevel(typval_T *argvars, typval_T *rettv);
567static void f_foldtext(typval_T *argvars, typval_T *rettv);
568static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
569static void f_foreground(typval_T *argvars, typval_T *rettv);
570static void f_function(typval_T *argvars, typval_T *rettv);
571static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
572static void f_get(typval_T *argvars, typval_T *rettv);
573static void f_getbufline(typval_T *argvars, typval_T *rettv);
574static void f_getbufvar(typval_T *argvars, typval_T *rettv);
575static void f_getchar(typval_T *argvars, typval_T *rettv);
576static void f_getcharmod(typval_T *argvars, typval_T *rettv);
577static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
578static void f_getcmdline(typval_T *argvars, typval_T *rettv);
579static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
580static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
581static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
582static void f_getcwd(typval_T *argvars, typval_T *rettv);
583static void f_getfontname(typval_T *argvars, typval_T *rettv);
584static void f_getfperm(typval_T *argvars, typval_T *rettv);
585static void f_getfsize(typval_T *argvars, typval_T *rettv);
586static void f_getftime(typval_T *argvars, typval_T *rettv);
587static void f_getftype(typval_T *argvars, typval_T *rettv);
588static void f_getline(typval_T *argvars, typval_T *rettv);
589static void f_getmatches(typval_T *argvars, typval_T *rettv);
590static void f_getpid(typval_T *argvars, typval_T *rettv);
591static void f_getcurpos(typval_T *argvars, typval_T *rettv);
592static void f_getpos(typval_T *argvars, typval_T *rettv);
593static void f_getqflist(typval_T *argvars, typval_T *rettv);
594static void f_getreg(typval_T *argvars, typval_T *rettv);
595static void f_getregtype(typval_T *argvars, typval_T *rettv);
596static void f_gettabvar(typval_T *argvars, typval_T *rettv);
597static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
598static void f_getwinposx(typval_T *argvars, typval_T *rettv);
599static void f_getwinposy(typval_T *argvars, typval_T *rettv);
600static void f_getwinvar(typval_T *argvars, typval_T *rettv);
601static void f_glob(typval_T *argvars, typval_T *rettv);
602static void f_globpath(typval_T *argvars, typval_T *rettv);
603static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
604static void f_has(typval_T *argvars, typval_T *rettv);
605static void f_has_key(typval_T *argvars, typval_T *rettv);
606static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
607static void f_hasmapto(typval_T *argvars, typval_T *rettv);
608static void f_histadd(typval_T *argvars, typval_T *rettv);
609static void f_histdel(typval_T *argvars, typval_T *rettv);
610static void f_histget(typval_T *argvars, typval_T *rettv);
611static void f_histnr(typval_T *argvars, typval_T *rettv);
612static void f_hlID(typval_T *argvars, typval_T *rettv);
613static void f_hlexists(typval_T *argvars, typval_T *rettv);
614static void f_hostname(typval_T *argvars, typval_T *rettv);
615static void f_iconv(typval_T *argvars, typval_T *rettv);
616static void f_indent(typval_T *argvars, typval_T *rettv);
617static void f_index(typval_T *argvars, typval_T *rettv);
618static void f_input(typval_T *argvars, typval_T *rettv);
619static void f_inputdialog(typval_T *argvars, typval_T *rettv);
620static void f_inputlist(typval_T *argvars, typval_T *rettv);
621static void f_inputrestore(typval_T *argvars, typval_T *rettv);
622static void f_inputsave(typval_T *argvars, typval_T *rettv);
623static void f_inputsecret(typval_T *argvars, typval_T *rettv);
624static void f_insert(typval_T *argvars, typval_T *rettv);
625static void f_invert(typval_T *argvars, typval_T *rettv);
626static void f_isdirectory(typval_T *argvars, typval_T *rettv);
627static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100628#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
629static void f_isnan(typval_T *argvars, typval_T *rettv);
630#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100631static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100632#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100633static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8950a562016-03-12 15:22:55 +0100634static void f_job_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar65edff82016-02-21 16:40:11 +0100635static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100636static void f_job_start(typval_T *argvars, typval_T *rettv);
637static void f_job_stop(typval_T *argvars, typval_T *rettv);
638static void f_job_status(typval_T *argvars, typval_T *rettv);
639#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100640static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100641static void f_js_decode(typval_T *argvars, typval_T *rettv);
642static void f_js_encode(typval_T *argvars, typval_T *rettv);
643static void f_json_decode(typval_T *argvars, typval_T *rettv);
644static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100645static void f_keys(typval_T *argvars, typval_T *rettv);
646static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
647static void f_len(typval_T *argvars, typval_T *rettv);
648static void f_libcall(typval_T *argvars, typval_T *rettv);
649static void f_libcallnr(typval_T *argvars, typval_T *rettv);
650static void f_line(typval_T *argvars, typval_T *rettv);
651static void f_line2byte(typval_T *argvars, typval_T *rettv);
652static void f_lispindent(typval_T *argvars, typval_T *rettv);
653static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000654#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100655static void f_log(typval_T *argvars, typval_T *rettv);
656static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000657#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200658#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100659static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200660#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100661static void f_map(typval_T *argvars, typval_T *rettv);
662static void f_maparg(typval_T *argvars, typval_T *rettv);
663static void f_mapcheck(typval_T *argvars, typval_T *rettv);
664static void f_match(typval_T *argvars, typval_T *rettv);
665static void f_matchadd(typval_T *argvars, typval_T *rettv);
666static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
667static void f_matcharg(typval_T *argvars, typval_T *rettv);
668static void f_matchdelete(typval_T *argvars, typval_T *rettv);
669static void f_matchend(typval_T *argvars, typval_T *rettv);
670static void f_matchlist(typval_T *argvars, typval_T *rettv);
671static void f_matchstr(typval_T *argvars, typval_T *rettv);
672static void f_max(typval_T *argvars, typval_T *rettv);
673static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000674#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100675static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000676#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100677static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100678#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100679static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100680#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100681static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
682static void f_nr2char(typval_T *argvars, typval_T *rettv);
683static void f_or(typval_T *argvars, typval_T *rettv);
684static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100685#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100686static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100687#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000688#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100689static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000690#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100691static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
692static void f_printf(typval_T *argvars, typval_T *rettv);
693static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200694#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100695static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200696#endif
697#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100698static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200699#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100700static void f_range(typval_T *argvars, typval_T *rettv);
701static void f_readfile(typval_T *argvars, typval_T *rettv);
702static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100703#ifdef FEAT_FLOAT
704static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
705#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100706static void f_reltimestr(typval_T *argvars, typval_T *rettv);
707static void f_remote_expr(typval_T *argvars, typval_T *rettv);
708static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
709static void f_remote_peek(typval_T *argvars, typval_T *rettv);
710static void f_remote_read(typval_T *argvars, typval_T *rettv);
711static void f_remote_send(typval_T *argvars, typval_T *rettv);
712static void f_remove(typval_T *argvars, typval_T *rettv);
713static void f_rename(typval_T *argvars, typval_T *rettv);
714static void f_repeat(typval_T *argvars, typval_T *rettv);
715static void f_resolve(typval_T *argvars, typval_T *rettv);
716static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000717#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100718static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000719#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100720static void f_screenattr(typval_T *argvars, typval_T *rettv);
721static void f_screenchar(typval_T *argvars, typval_T *rettv);
722static void f_screencol(typval_T *argvars, typval_T *rettv);
723static void f_screenrow(typval_T *argvars, typval_T *rettv);
724static void f_search(typval_T *argvars, typval_T *rettv);
725static void f_searchdecl(typval_T *argvars, typval_T *rettv);
726static void f_searchpair(typval_T *argvars, typval_T *rettv);
727static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
728static void f_searchpos(typval_T *argvars, typval_T *rettv);
729static void f_server2client(typval_T *argvars, typval_T *rettv);
730static void f_serverlist(typval_T *argvars, typval_T *rettv);
731static void f_setbufvar(typval_T *argvars, typval_T *rettv);
732static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
733static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100734static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100735static void f_setline(typval_T *argvars, typval_T *rettv);
736static void f_setloclist(typval_T *argvars, typval_T *rettv);
737static void f_setmatches(typval_T *argvars, typval_T *rettv);
738static void f_setpos(typval_T *argvars, typval_T *rettv);
739static void f_setqflist(typval_T *argvars, typval_T *rettv);
740static void f_setreg(typval_T *argvars, typval_T *rettv);
741static void f_settabvar(typval_T *argvars, typval_T *rettv);
742static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
743static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100744#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100745static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100746#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100747static void f_shellescape(typval_T *argvars, typval_T *rettv);
748static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
749static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000750#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100751static void f_sin(typval_T *argvars, typval_T *rettv);
752static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000753#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100754static void f_sort(typval_T *argvars, typval_T *rettv);
755static void f_soundfold(typval_T *argvars, typval_T *rettv);
756static void f_spellbadword(typval_T *argvars, typval_T *rettv);
757static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
758static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000759#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100760static void f_sqrt(typval_T *argvars, typval_T *rettv);
761static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000762#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100763static void f_str2nr(typval_T *argvars, typval_T *rettv);
764static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000765#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100766static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000767#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100768static void f_stridx(typval_T *argvars, typval_T *rettv);
769static void f_string(typval_T *argvars, typval_T *rettv);
770static void f_strlen(typval_T *argvars, typval_T *rettv);
771static void f_strpart(typval_T *argvars, typval_T *rettv);
772static void f_strridx(typval_T *argvars, typval_T *rettv);
773static void f_strtrans(typval_T *argvars, typval_T *rettv);
774static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
775static void f_strwidth(typval_T *argvars, typval_T *rettv);
776static void f_submatch(typval_T *argvars, typval_T *rettv);
777static void f_substitute(typval_T *argvars, typval_T *rettv);
778static void f_synID(typval_T *argvars, typval_T *rettv);
779static void f_synIDattr(typval_T *argvars, typval_T *rettv);
780static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
781static void f_synstack(typval_T *argvars, typval_T *rettv);
782static void f_synconcealed(typval_T *argvars, typval_T *rettv);
783static void f_system(typval_T *argvars, typval_T *rettv);
784static void f_systemlist(typval_T *argvars, typval_T *rettv);
785static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
786static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
787static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
788static void f_taglist(typval_T *argvars, typval_T *rettv);
789static void f_tagfiles(typval_T *argvars, typval_T *rettv);
790static void f_tempname(typval_T *argvars, typval_T *rettv);
791static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200792#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100793static void f_tan(typval_T *argvars, typval_T *rettv);
794static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200795#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100796static void f_tolower(typval_T *argvars, typval_T *rettv);
797static void f_toupper(typval_T *argvars, typval_T *rettv);
798static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000799#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100800static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000801#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100802static void f_type(typval_T *argvars, typval_T *rettv);
803static void f_undofile(typval_T *argvars, typval_T *rettv);
804static void f_undotree(typval_T *argvars, typval_T *rettv);
805static void f_uniq(typval_T *argvars, typval_T *rettv);
806static void f_values(typval_T *argvars, typval_T *rettv);
807static void f_virtcol(typval_T *argvars, typval_T *rettv);
808static void f_visualmode(typval_T *argvars, typval_T *rettv);
809static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar86edef62016-03-13 18:07:30 +0100810static void f_win_getid(typval_T *argvars, typval_T *rettv);
811static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
812static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
813static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100814static void f_winbufnr(typval_T *argvars, typval_T *rettv);
815static void f_wincol(typval_T *argvars, typval_T *rettv);
816static void f_winheight(typval_T *argvars, typval_T *rettv);
817static void f_winline(typval_T *argvars, typval_T *rettv);
818static void f_winnr(typval_T *argvars, typval_T *rettv);
819static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
820static void f_winrestview(typval_T *argvars, typval_T *rettv);
821static void f_winsaveview(typval_T *argvars, typval_T *rettv);
822static void f_winwidth(typval_T *argvars, typval_T *rettv);
823static void f_writefile(typval_T *argvars, typval_T *rettv);
824static void f_wordcount(typval_T *argvars, typval_T *rettv);
825static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000826
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100827static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
828static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
829static int get_env_len(char_u **arg);
830static int get_id_len(char_u **arg);
831static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
832static char_u *find_name_end(char_u *arg, char_u **expr_start, char_u **expr_end, int flags);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000833#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
834#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
835 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100836static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
837static int eval_isnamec(int c);
838static int eval_isnamec1(int c);
839static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
840static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100841static typval_T *alloc_string_tv(char_u *string);
842static void init_tv(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100843#ifdef FEAT_FLOAT
844static float_T get_tv_float(typval_T *varp);
845#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100846static linenr_T get_tv_lnum(typval_T *argvars);
847static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100848static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
849static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
850static hashtab_T *find_var_ht(char_u *name, char_u **varname);
851static funccall_T *get_funccal(void);
852static void vars_clear_ext(hashtab_T *ht, int free_val);
853static void delete_var(hashtab_T *ht, hashitem_T *hi);
854static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
855static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
856static void set_var(char_u *name, typval_T *varp, int copy);
857static int var_check_ro(int flags, char_u *name, int use_gettext);
858static int var_check_fixed(int flags, char_u *name, int use_gettext);
859static int var_check_func_name(char_u *name, int new_var);
860static int valid_varname(char_u *varname);
861static int tv_check_lock(int lock, char_u *name, int use_gettext);
862static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
863static char_u *find_option_end(char_u **arg, int *opt_flags);
864static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd);
865static int eval_fname_script(char_u *p);
866static int eval_fname_sid(char_u *p);
867static void list_func_head(ufunc_T *fp, int indent);
868static ufunc_T *find_func(char_u *name);
869static int function_exists(char_u *name);
870static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000871#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100872static void func_do_profile(ufunc_T *fp);
873static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
874static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000875static int
876# ifdef __BORLANDC__
877 _RTLENTRYF
878# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100879 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000880static int
881# ifdef __BORLANDC__
882 _RTLENTRYF
883# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100884 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000885#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100886static int script_autoload(char_u *name, int reload);
887static char_u *autoload_name(char_u *name);
888static void cat_func_name(char_u *buf, ufunc_T *fp);
889static void func_free(ufunc_T *fp);
890static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
891static int can_free_funccal(funccall_T *fc, int copyID) ;
892static void free_funccal(funccall_T *fc, int free_val);
893static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
894static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
895static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
896static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
897static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
898static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
899static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
900static int write_list(FILE *fd, list_T *list, int binary);
901static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000902
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200903
904#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100905static int compare_func_name(const void *s1, const void *s2);
906static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200907#endif
908
Bram Moolenaar33570922005-01-25 22:26:29 +0000909/*
910 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000911 */
912 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100913eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000914{
Bram Moolenaar33570922005-01-25 22:26:29 +0000915 int i;
916 struct vimvar *p;
917
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200918 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
919 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200920 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000921 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000922 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000923
924 for (i = 0; i < VV_LEN; ++i)
925 {
926 p = &vimvars[i];
927 STRCPY(p->vv_di.di_key, p->vv_name);
928 if (p->vv_flags & VV_RO)
929 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
930 else if (p->vv_flags & VV_RO_SBX)
931 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
932 else
933 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000934
935 /* add to v: scope dict, unless the value is not always available */
936 if (p->vv_type != VAR_UNKNOWN)
937 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000938 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000939 /* add to compat scope dict */
940 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000941 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100942 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
943
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000944 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100945 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200946 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100947 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100948
949 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
950 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
951 set_vim_var_nr(VV_NONE, VVAL_NONE);
952 set_vim_var_nr(VV_NULL, VVAL_NULL);
953
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200954 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200955
956#ifdef EBCDIC
957 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100958 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200959 */
960 sortFunctions();
961#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000962}
963
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000964#if defined(EXITFREE) || defined(PROTO)
965 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100966eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000967{
968 int i;
969 struct vimvar *p;
970
971 for (i = 0; i < VV_LEN; ++i)
972 {
973 p = &vimvars[i];
974 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000975 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000976 vim_free(p->vv_str);
977 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000978 }
979 else if (p->vv_di.di_tv.v_type == VAR_LIST)
980 {
981 list_unref(p->vv_list);
982 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000983 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000984 }
985 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000986 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000987 hash_clear(&compat_hashtab);
988
Bram Moolenaard9fba312005-06-26 22:34:35 +0000989 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100990# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200991 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100992# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000993
994 /* global variables */
995 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000996
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000997 /* autoloaded script names */
998 ga_clear_strings(&ga_loaded);
999
Bram Moolenaarcca74132013-09-25 21:00:28 +02001000 /* Script-local variables. First clear all the variables and in a second
1001 * loop free the scriptvar_T, because a variable in one script might hold
1002 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001003 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001004 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001005 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001006 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001007 ga_clear(&ga_scripts);
1008
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001009 /* unreferenced lists and dicts */
1010 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001011
1012 /* functions */
1013 free_all_functions();
1014 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001015}
1016#endif
1017
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001018/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 * Return the name of the executed function.
1020 */
1021 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001022func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001024 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025}
1026
1027/*
1028 * Return the address holding the next breakpoint line for a funccall cookie.
1029 */
1030 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001031func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032{
Bram Moolenaar33570922005-01-25 22:26:29 +00001033 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034}
1035
1036/*
1037 * Return the address holding the debug tick for a funccall cookie.
1038 */
1039 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001040func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041{
Bram Moolenaar33570922005-01-25 22:26:29 +00001042 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043}
1044
1045/*
1046 * Return the nesting level for a funccall cookie.
1047 */
1048 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001049func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050{
Bram Moolenaar33570922005-01-25 22:26:29 +00001051 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052}
1053
1054/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001055funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001057/* pointer to list of previously used funccal, still around because some
1058 * item in it is still being used. */
1059funccall_T *previous_funccal = NULL;
1060
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061/*
1062 * Return TRUE when a function was ended by a ":return" command.
1063 */
1064 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001065current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066{
1067 return current_funccal->returned;
1068}
1069
1070
1071/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 * Set an internal variable to a string value. Creates the variable if it does
1073 * not already exist.
1074 */
1075 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001076set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001078 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001079 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080
1081 val = vim_strsave(value);
1082 if (val != NULL)
1083 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001084 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001085 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001087 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001088 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 }
1090 }
1091}
1092
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001093static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001094static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001095static char_u *redir_endp = NULL;
1096static char_u *redir_varname = NULL;
1097
1098/*
1099 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001100 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001101 * Returns OK if successfully completed the setup. FAIL otherwise.
1102 */
1103 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001104var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001105{
1106 int save_emsg;
1107 int err;
1108 typval_T tv;
1109
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001110 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001111 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001112 {
1113 EMSG(_(e_invarg));
1114 return FAIL;
1115 }
1116
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001117 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001118 redir_varname = vim_strsave(name);
1119 if (redir_varname == NULL)
1120 return FAIL;
1121
1122 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1123 if (redir_lval == NULL)
1124 {
1125 var_redir_stop();
1126 return FAIL;
1127 }
1128
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001129 /* The output is stored in growarray "redir_ga" until redirection ends. */
1130 ga_init2(&redir_ga, (int)sizeof(char), 500);
1131
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001132 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001133 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001134 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001135 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1136 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001137 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138 if (redir_endp != NULL && *redir_endp != NUL)
1139 /* Trailing characters are present after the variable name */
1140 EMSG(_(e_trailing));
1141 else
1142 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001143 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001144 var_redir_stop();
1145 return FAIL;
1146 }
1147
1148 /* check if we can write to the variable: set it to or append an empty
1149 * string */
1150 save_emsg = did_emsg;
1151 did_emsg = FALSE;
1152 tv.v_type = VAR_STRING;
1153 tv.vval.v_string = (char_u *)"";
1154 if (append)
1155 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1156 else
1157 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001158 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001159 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001160 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001161 if (err)
1162 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001163 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001164 var_redir_stop();
1165 return FAIL;
1166 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001167
1168 return OK;
1169}
1170
1171/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001172 * Append "value[value_len]" to the variable set by var_redir_start().
1173 * The actual appending is postponed until redirection ends, because the value
1174 * appended may in fact be the string we write to, changing it may cause freed
1175 * memory to be used:
1176 * :redir => foo
1177 * :let foo
1178 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001179 */
1180 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001181var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001182{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001183 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001184
1185 if (redir_lval == NULL)
1186 return;
1187
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001188 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001189 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001190 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001191 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001192
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001193 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001194 {
1195 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001196 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001197 }
1198 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001199 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001200}
1201
1202/*
1203 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001204 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001205 */
1206 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001207var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001208{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001209 typval_T tv;
1210
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001211 if (redir_lval != NULL)
1212 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001213 /* If there was no error: assign the text to the variable. */
1214 if (redir_endp != NULL)
1215 {
1216 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1217 tv.v_type = VAR_STRING;
1218 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001219 /* Call get_lval() again, if it's inside a Dict or List it may
1220 * have changed. */
1221 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001222 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001223 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1224 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1225 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001226 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001227
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001228 /* free the collected output */
1229 vim_free(redir_ga.ga_data);
1230 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001231
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001232 vim_free(redir_lval);
1233 redir_lval = NULL;
1234 }
1235 vim_free(redir_varname);
1236 redir_varname = NULL;
1237}
1238
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239# if defined(FEAT_MBYTE) || defined(PROTO)
1240 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001241eval_charconvert(
1242 char_u *enc_from,
1243 char_u *enc_to,
1244 char_u *fname_from,
1245 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246{
1247 int err = FALSE;
1248
1249 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1250 set_vim_var_string(VV_CC_TO, enc_to, -1);
1251 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1252 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1253 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1254 err = TRUE;
1255 set_vim_var_string(VV_CC_FROM, NULL, -1);
1256 set_vim_var_string(VV_CC_TO, NULL, -1);
1257 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1258 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1259
1260 if (err)
1261 return FAIL;
1262 return OK;
1263}
1264# endif
1265
1266# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1267 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001268eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269{
1270 int err = FALSE;
1271
1272 set_vim_var_string(VV_FNAME_IN, fname, -1);
1273 set_vim_var_string(VV_CMDARG, args, -1);
1274 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1275 err = TRUE;
1276 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1277 set_vim_var_string(VV_CMDARG, NULL, -1);
1278
1279 if (err)
1280 {
1281 mch_remove(fname);
1282 return FAIL;
1283 }
1284 return OK;
1285}
1286# endif
1287
1288# if defined(FEAT_DIFF) || defined(PROTO)
1289 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001290eval_diff(
1291 char_u *origfile,
1292 char_u *newfile,
1293 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294{
1295 int err = FALSE;
1296
1297 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1298 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1299 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1300 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1301 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1302 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1303 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1304}
1305
1306 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001307eval_patch(
1308 char_u *origfile,
1309 char_u *difffile,
1310 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311{
1312 int err;
1313
1314 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1315 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1316 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1317 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1318 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1319 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1320 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1321}
1322# endif
1323
1324/*
1325 * Top level evaluation function, returning a boolean.
1326 * Sets "error" to TRUE if there was an error.
1327 * Return TRUE or FALSE.
1328 */
1329 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001330eval_to_bool(
1331 char_u *arg,
1332 int *error,
1333 char_u **nextcmd,
1334 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335{
Bram Moolenaar33570922005-01-25 22:26:29 +00001336 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 int retval = FALSE;
1338
1339 if (skip)
1340 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001341 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 else
1344 {
1345 *error = FALSE;
1346 if (!skip)
1347 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001348 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001349 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 }
1351 }
1352 if (skip)
1353 --emsg_skip;
1354
1355 return retval;
1356}
1357
1358/*
1359 * Top level evaluation function, returning a string. If "skip" is TRUE,
1360 * only parsing to "nextcmd" is done, without reporting errors. Return
1361 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1362 */
1363 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001364eval_to_string_skip(
1365 char_u *arg,
1366 char_u **nextcmd,
1367 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368{
Bram Moolenaar33570922005-01-25 22:26:29 +00001369 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 char_u *retval;
1371
1372 if (skip)
1373 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001374 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 retval = NULL;
1376 else
1377 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001378 retval = vim_strsave(get_tv_string(&tv));
1379 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 }
1381 if (skip)
1382 --emsg_skip;
1383
1384 return retval;
1385}
1386
1387/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001388 * Skip over an expression at "*pp".
1389 * Return FAIL for an error, OK otherwise.
1390 */
1391 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001392skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001393{
Bram Moolenaar33570922005-01-25 22:26:29 +00001394 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001395
1396 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001397 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001398}
1399
1400/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001402 * When "convert" is TRUE convert a List into a sequence of lines and convert
1403 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 * Return pointer to allocated memory, or NULL for failure.
1405 */
1406 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001407eval_to_string(
1408 char_u *arg,
1409 char_u **nextcmd,
1410 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411{
Bram Moolenaar33570922005-01-25 22:26:29 +00001412 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001414 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001415#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001416 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001419 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 retval = NULL;
1421 else
1422 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001423 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001424 {
1425 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001426 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001427 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001428 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001429 if (tv.vval.v_list->lv_len > 0)
1430 ga_append(&ga, NL);
1431 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001432 ga_append(&ga, NUL);
1433 retval = (char_u *)ga.ga_data;
1434 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001435#ifdef FEAT_FLOAT
1436 else if (convert && tv.v_type == VAR_FLOAT)
1437 {
1438 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1439 retval = vim_strsave(numbuf);
1440 }
1441#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001442 else
1443 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001444 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 }
1446
1447 return retval;
1448}
1449
1450/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001451 * Call eval_to_string() without using current local variables and using
1452 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 */
1454 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001455eval_to_string_safe(
1456 char_u *arg,
1457 char_u **nextcmd,
1458 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459{
1460 char_u *retval;
1461 void *save_funccalp;
1462
1463 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001464 if (use_sandbox)
1465 ++sandbox;
1466 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001467 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001468 if (use_sandbox)
1469 --sandbox;
1470 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 restore_funccal(save_funccalp);
1472 return retval;
1473}
1474
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475/*
1476 * Top level evaluation function, returning a number.
1477 * Evaluates "expr" silently.
1478 * Returns -1 for an error.
1479 */
1480 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001481eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482{
Bram Moolenaar33570922005-01-25 22:26:29 +00001483 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001485 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486
1487 ++emsg_off;
1488
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001489 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 retval = -1;
1491 else
1492 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001493 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001494 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 }
1496 --emsg_off;
1497
1498 return retval;
1499}
1500
Bram Moolenaara40058a2005-07-11 22:42:07 +00001501/*
1502 * Prepare v: variable "idx" to be used.
1503 * Save the current typeval in "save_tv".
1504 * When not used yet add the variable to the v: hashtable.
1505 */
1506 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001507prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001508{
1509 *save_tv = vimvars[idx].vv_tv;
1510 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1511 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1512}
1513
1514/*
1515 * Restore v: variable "idx" to typeval "save_tv".
1516 * When no longer defined, remove the variable from the v: hashtable.
1517 */
1518 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001519restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001520{
1521 hashitem_T *hi;
1522
Bram Moolenaara40058a2005-07-11 22:42:07 +00001523 vimvars[idx].vv_tv = *save_tv;
1524 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1525 {
1526 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1527 if (HASHITEM_EMPTY(hi))
1528 EMSG2(_(e_intern2), "restore_vimvar()");
1529 else
1530 hash_remove(&vimvarht, hi);
1531 }
1532}
1533
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001534#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001535/*
1536 * Evaluate an expression to a list with suggestions.
1537 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001538 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001539 */
1540 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001541eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001542{
1543 typval_T save_val;
1544 typval_T rettv;
1545 list_T *list = NULL;
1546 char_u *p = skipwhite(expr);
1547
1548 /* Set "v:val" to the bad word. */
1549 prepare_vimvar(VV_VAL, &save_val);
1550 vimvars[VV_VAL].vv_type = VAR_STRING;
1551 vimvars[VV_VAL].vv_str = badword;
1552 if (p_verbose == 0)
1553 ++emsg_off;
1554
1555 if (eval1(&p, &rettv, TRUE) == OK)
1556 {
1557 if (rettv.v_type != VAR_LIST)
1558 clear_tv(&rettv);
1559 else
1560 list = rettv.vval.v_list;
1561 }
1562
1563 if (p_verbose == 0)
1564 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001565 restore_vimvar(VV_VAL, &save_val);
1566
1567 return list;
1568}
1569
1570/*
1571 * "list" is supposed to contain two items: a word and a number. Return the
1572 * word in "pp" and the number as the return value.
1573 * Return -1 if anything isn't right.
1574 * Used to get the good word and score from the eval_spell_expr() result.
1575 */
1576 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001577get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001578{
1579 listitem_T *li;
1580
1581 li = list->lv_first;
1582 if (li == NULL)
1583 return -1;
1584 *pp = get_tv_string(&li->li_tv);
1585
1586 li = li->li_next;
1587 if (li == NULL)
1588 return -1;
1589 return get_tv_number(&li->li_tv);
1590}
1591#endif
1592
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001593/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001594 * Top level evaluation function.
1595 * Returns an allocated typval_T with the result.
1596 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001597 */
1598 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001599eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001600{
1601 typval_T *tv;
1602
1603 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001604 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001605 {
1606 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001607 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001608 }
1609
1610 return tv;
1611}
1612
1613
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001615 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001616 * Uses argv[argc] for the function arguments. Only Number and String
1617 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001618 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001620 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001621call_vim_function(
1622 char_u *func,
1623 int argc,
1624 char_u **argv,
1625 int safe, /* use the sandbox */
1626 int str_arg_only, /* all arguments are strings */
1627 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628{
Bram Moolenaar33570922005-01-25 22:26:29 +00001629 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 long n;
1631 int len;
1632 int i;
1633 int doesrange;
1634 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001635 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001637 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001639 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640
1641 for (i = 0; i < argc; i++)
1642 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001643 /* Pass a NULL or empty argument as an empty string */
1644 if (argv[i] == NULL || *argv[i] == NUL)
1645 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001646 argvars[i].v_type = VAR_STRING;
1647 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001648 continue;
1649 }
1650
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001651 if (str_arg_only)
1652 len = 0;
1653 else
1654 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001655 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 if (len != 0 && len == (int)STRLEN(argv[i]))
1657 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001658 argvars[i].v_type = VAR_NUMBER;
1659 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 }
1661 else
1662 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001663 argvars[i].v_type = VAR_STRING;
1664 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 }
1666 }
1667
1668 if (safe)
1669 {
1670 save_funccalp = save_funccal();
1671 ++sandbox;
1672 }
1673
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001674 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1675 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001677 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 if (safe)
1679 {
1680 --sandbox;
1681 restore_funccal(save_funccalp);
1682 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001683 vim_free(argvars);
1684
1685 if (ret == FAIL)
1686 clear_tv(rettv);
1687
1688 return ret;
1689}
1690
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001691/*
1692 * Call vimL function "func" and return the result as a number.
1693 * Returns -1 when calling the function fails.
1694 * Uses argv[argc] for the function arguments.
1695 */
1696 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001697call_func_retnr(
1698 char_u *func,
1699 int argc,
1700 char_u **argv,
1701 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001702{
1703 typval_T rettv;
1704 long retval;
1705
1706 /* All arguments are passed as strings, no conversion to number. */
1707 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1708 return -1;
1709
1710 retval = get_tv_number_chk(&rettv, NULL);
1711 clear_tv(&rettv);
1712 return retval;
1713}
1714
1715#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1716 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1717
Bram Moolenaar4f688582007-07-24 12:34:30 +00001718# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001719/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001720 * Call vimL function "func" and return the result as a string.
1721 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001722 * Uses argv[argc] for the function arguments.
1723 */
1724 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001725call_func_retstr(
1726 char_u *func,
1727 int argc,
1728 char_u **argv,
1729 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001730{
1731 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001732 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001733
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001734 /* All arguments are passed as strings, no conversion to number. */
1735 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001736 return NULL;
1737
1738 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001739 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 return retval;
1741}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001742# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001743
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001744/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001745 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001746 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001747 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001748 */
1749 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001750call_func_retlist(
1751 char_u *func,
1752 int argc,
1753 char_u **argv,
1754 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001755{
1756 typval_T rettv;
1757
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001758 /* All arguments are passed as strings, no conversion to number. */
1759 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001760 return NULL;
1761
1762 if (rettv.v_type != VAR_LIST)
1763 {
1764 clear_tv(&rettv);
1765 return NULL;
1766 }
1767
1768 return rettv.vval.v_list;
1769}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770#endif
1771
1772/*
1773 * Save the current function call pointer, and set it to NULL.
1774 * Used when executing autocommands and for ":source".
1775 */
1776 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001777save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001779 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 current_funccal = NULL;
1782 return (void *)fc;
1783}
1784
1785 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001786restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001788 funccall_T *fc = (funccall_T *)vfc;
1789
1790 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791}
1792
Bram Moolenaar05159a02005-02-26 23:04:13 +00001793#if defined(FEAT_PROFILE) || defined(PROTO)
1794/*
1795 * Prepare profiling for entering a child or something else that is not
1796 * counted for the script/function itself.
1797 * Should always be called in pair with prof_child_exit().
1798 */
1799 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001800prof_child_enter(
1801 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001802{
1803 funccall_T *fc = current_funccal;
1804
1805 if (fc != NULL && fc->func->uf_profiling)
1806 profile_start(&fc->prof_child);
1807 script_prof_save(tm);
1808}
1809
1810/*
1811 * Take care of time spent in a child.
1812 * Should always be called after prof_child_enter().
1813 */
1814 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001815prof_child_exit(
1816 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001817{
1818 funccall_T *fc = current_funccal;
1819
1820 if (fc != NULL && fc->func->uf_profiling)
1821 {
1822 profile_end(&fc->prof_child);
1823 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1824 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1825 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1826 }
1827 script_prof_restore(tm);
1828}
1829#endif
1830
1831
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832#ifdef FEAT_FOLDING
1833/*
1834 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1835 * it in "*cp". Doesn't give error messages.
1836 */
1837 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001838eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839{
Bram Moolenaar33570922005-01-25 22:26:29 +00001840 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 int retval;
1842 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001843 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1844 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845
1846 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001847 if (use_sandbox)
1848 ++sandbox;
1849 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001851 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 retval = 0;
1853 else
1854 {
1855 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001856 if (tv.v_type == VAR_NUMBER)
1857 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001858 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 retval = 0;
1860 else
1861 {
1862 /* If the result is a string, check if there is a non-digit before
1863 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001864 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 if (!VIM_ISDIGIT(*s) && *s != '-')
1866 *cp = *s++;
1867 retval = atol((char *)s);
1868 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001869 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 }
1871 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001872 if (use_sandbox)
1873 --sandbox;
1874 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875
1876 return retval;
1877}
1878#endif
1879
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001881 * ":let" list all variable values
1882 * ":let var1 var2" list variable values
1883 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001884 * ":let var += expr" assignment command.
1885 * ":let var -= expr" assignment command.
1886 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001887 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 */
1889 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001890ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891{
1892 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001893 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001894 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001896 int var_count = 0;
1897 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001898 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001899 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001900 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901
Bram Moolenaardb552d602006-03-23 22:59:57 +00001902 argend = skip_var_list(arg, &var_count, &semicolon);
1903 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001904 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001905 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1906 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001907 expr = skipwhite(argend);
1908 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1909 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001911 /*
1912 * ":let" without "=": list variables
1913 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001914 if (*arg == '[')
1915 EMSG(_(e_invarg));
1916 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001917 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001918 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001919 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001920 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001921 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001922 list_glob_vars(&first);
1923 list_buf_vars(&first);
1924 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001925#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001926 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001927#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001928 list_script_vars(&first);
1929 list_func_vars(&first);
1930 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 eap->nextcmd = check_nextcmd(arg);
1933 }
1934 else
1935 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001936 op[0] = '=';
1937 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001938 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001939 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001940 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1941 op[0] = *expr; /* +=, -= or .= */
1942 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001943 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001944 else
1945 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001946
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 if (eap->skip)
1948 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001949 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 if (eap->skip)
1951 {
1952 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001953 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 --emsg_skip;
1955 }
1956 else if (i != FAIL)
1957 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001958 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001959 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001960 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 }
1962 }
1963}
1964
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001965/*
1966 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1967 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001968 * When "nextchars" is not NULL it points to a string with characters that
1969 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1970 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001971 * Returns OK or FAIL;
1972 */
1973 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001974ex_let_vars(
1975 char_u *arg_start,
1976 typval_T *tv,
1977 int copy, /* copy values from "tv", don't move */
1978 int semicolon, /* from skip_var_list() */
1979 int var_count, /* from skip_var_list() */
1980 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001981{
1982 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001983 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001984 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001985 listitem_T *item;
1986 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001987
1988 if (*arg != '[')
1989 {
1990 /*
1991 * ":let var = expr" or ":for var in list"
1992 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001993 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001994 return FAIL;
1995 return OK;
1996 }
1997
1998 /*
1999 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2000 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002001 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002002 {
2003 EMSG(_(e_listreq));
2004 return FAIL;
2005 }
2006
2007 i = list_len(l);
2008 if (semicolon == 0 && var_count < i)
2009 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002010 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002011 return FAIL;
2012 }
2013 if (var_count - semicolon > i)
2014 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002015 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002016 return FAIL;
2017 }
2018
2019 item = l->lv_first;
2020 while (*arg != ']')
2021 {
2022 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002023 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002024 item = item->li_next;
2025 if (arg == NULL)
2026 return FAIL;
2027
2028 arg = skipwhite(arg);
2029 if (*arg == ';')
2030 {
2031 /* Put the rest of the list (may be empty) in the var after ';'.
2032 * Create a new list for this. */
2033 l = list_alloc();
2034 if (l == NULL)
2035 return FAIL;
2036 while (item != NULL)
2037 {
2038 list_append_tv(l, &item->li_tv);
2039 item = item->li_next;
2040 }
2041
2042 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002043 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002044 ltv.vval.v_list = l;
2045 l->lv_refcount = 1;
2046
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002047 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2048 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002049 clear_tv(&ltv);
2050 if (arg == NULL)
2051 return FAIL;
2052 break;
2053 }
2054 else if (*arg != ',' && *arg != ']')
2055 {
2056 EMSG2(_(e_intern2), "ex_let_vars()");
2057 return FAIL;
2058 }
2059 }
2060
2061 return OK;
2062}
2063
2064/*
2065 * Skip over assignable variable "var" or list of variables "[var, var]".
2066 * Used for ":let varvar = expr" and ":for varvar in expr".
2067 * For "[var, var]" increment "*var_count" for each variable.
2068 * for "[var, var; var]" set "semicolon".
2069 * Return NULL for an error.
2070 */
2071 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002072skip_var_list(
2073 char_u *arg,
2074 int *var_count,
2075 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002076{
2077 char_u *p, *s;
2078
2079 if (*arg == '[')
2080 {
2081 /* "[var, var]": find the matching ']'. */
2082 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002083 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002084 {
2085 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2086 s = skip_var_one(p);
2087 if (s == p)
2088 {
2089 EMSG2(_(e_invarg2), p);
2090 return NULL;
2091 }
2092 ++*var_count;
2093
2094 p = skipwhite(s);
2095 if (*p == ']')
2096 break;
2097 else if (*p == ';')
2098 {
2099 if (*semicolon == 1)
2100 {
2101 EMSG(_("Double ; in list of variables"));
2102 return NULL;
2103 }
2104 *semicolon = 1;
2105 }
2106 else if (*p != ',')
2107 {
2108 EMSG2(_(e_invarg2), p);
2109 return NULL;
2110 }
2111 }
2112 return p + 1;
2113 }
2114 else
2115 return skip_var_one(arg);
2116}
2117
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002118/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002119 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002120 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002121 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002122 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002123skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002124{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002125 if (*arg == '@' && arg[1] != NUL)
2126 return arg + 2;
2127 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2128 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002129}
2130
Bram Moolenaara7043832005-01-21 11:56:39 +00002131/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002132 * List variables for hashtab "ht" with prefix "prefix".
2133 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002134 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002135 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002136list_hashtable_vars(
2137 hashtab_T *ht,
2138 char_u *prefix,
2139 int empty,
2140 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002141{
Bram Moolenaar33570922005-01-25 22:26:29 +00002142 hashitem_T *hi;
2143 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002144 int todo;
2145
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002146 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002147 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2148 {
2149 if (!HASHITEM_EMPTY(hi))
2150 {
2151 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002152 di = HI2DI(hi);
2153 if (empty || di->di_tv.v_type != VAR_STRING
2154 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002155 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002156 }
2157 }
2158}
2159
2160/*
2161 * List global variables.
2162 */
2163 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002164list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002165{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002166 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002167}
2168
2169/*
2170 * List buffer variables.
2171 */
2172 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002173list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002174{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002175 char_u numbuf[NUMBUFLEN];
2176
Bram Moolenaar429fa852013-04-15 12:27:36 +02002177 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002178 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002179
2180 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002181 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2182 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002183}
2184
2185/*
2186 * List window variables.
2187 */
2188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002189list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002190{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002191 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002192 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002193}
2194
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002195#ifdef FEAT_WINDOWS
2196/*
2197 * List tab page variables.
2198 */
2199 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002200list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002201{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002202 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002203 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002204}
2205#endif
2206
Bram Moolenaara7043832005-01-21 11:56:39 +00002207/*
2208 * List Vim variables.
2209 */
2210 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002211list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002212{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002213 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214}
2215
2216/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002217 * List script-local variables, if there is a script.
2218 */
2219 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002220list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002221{
2222 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002223 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2224 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002225}
2226
2227/*
2228 * List function variables, if there is a function.
2229 */
2230 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002231list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002232{
2233 if (current_funccal != NULL)
2234 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002235 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002236}
2237
2238/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002239 * List variables in "arg".
2240 */
2241 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002242list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002243{
2244 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002245 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002246 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002247 char_u *name_start;
2248 char_u *arg_subsc;
2249 char_u *tofree;
2250 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002251
2252 while (!ends_excmd(*arg) && !got_int)
2253 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002254 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002255 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002256 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002257 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2258 {
2259 emsg_severe = TRUE;
2260 EMSG(_(e_trailing));
2261 break;
2262 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002263 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002264 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002265 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002266 /* get_name_len() takes care of expanding curly braces */
2267 name_start = name = arg;
2268 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2269 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002270 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002271 /* This is mainly to keep test 49 working: when expanding
2272 * curly braces fails overrule the exception error message. */
2273 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002274 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002275 emsg_severe = TRUE;
2276 EMSG2(_(e_invarg2), arg);
2277 break;
2278 }
2279 error = TRUE;
2280 }
2281 else
2282 {
2283 if (tofree != NULL)
2284 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002285 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002286 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002287 else
2288 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002289 /* handle d.key, l[idx], f(expr) */
2290 arg_subsc = arg;
2291 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002292 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002293 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002294 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002295 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002296 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002297 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002298 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002299 case 'g': list_glob_vars(first); break;
2300 case 'b': list_buf_vars(first); break;
2301 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002302#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002303 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002304#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002305 case 'v': list_vim_vars(first); break;
2306 case 's': list_script_vars(first); break;
2307 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002308 default:
2309 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002310 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002311 }
2312 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002313 {
2314 char_u numbuf[NUMBUFLEN];
2315 char_u *tf;
2316 int c;
2317 char_u *s;
2318
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002319 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002320 c = *arg;
2321 *arg = NUL;
2322 list_one_var_a((char_u *)"",
2323 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002324 tv.v_type,
2325 s == NULL ? (char_u *)"" : s,
2326 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002327 *arg = c;
2328 vim_free(tf);
2329 }
2330 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002331 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002332 }
2333 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002334
2335 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002336 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002337
2338 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002339 }
2340
2341 return arg;
2342}
2343
2344/*
2345 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2346 * Returns a pointer to the char just after the var name.
2347 * Returns NULL if there is an error.
2348 */
2349 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002350ex_let_one(
2351 char_u *arg, /* points to variable name */
2352 typval_T *tv, /* value to assign to variable */
2353 int copy, /* copy value from "tv" */
2354 char_u *endchars, /* valid chars after variable name or NULL */
2355 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002356{
2357 int c1;
2358 char_u *name;
2359 char_u *p;
2360 char_u *arg_end = NULL;
2361 int len;
2362 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002363 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002364
2365 /*
2366 * ":let $VAR = expr": Set environment variable.
2367 */
2368 if (*arg == '$')
2369 {
2370 /* Find the end of the name. */
2371 ++arg;
2372 name = arg;
2373 len = get_env_len(&arg);
2374 if (len == 0)
2375 EMSG2(_(e_invarg2), name - 1);
2376 else
2377 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002378 if (op != NULL && (*op == '+' || *op == '-'))
2379 EMSG2(_(e_letwrong), op);
2380 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002381 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002382 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002383 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002384 {
2385 c1 = name[len];
2386 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002387 p = get_tv_string_chk(tv);
2388 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002389 {
2390 int mustfree = FALSE;
2391 char_u *s = vim_getenv(name, &mustfree);
2392
2393 if (s != NULL)
2394 {
2395 p = tofree = concat_str(s, p);
2396 if (mustfree)
2397 vim_free(s);
2398 }
2399 }
2400 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002401 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002402 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002403 if (STRICMP(name, "HOME") == 0)
2404 init_homedir();
2405 else if (didset_vim && STRICMP(name, "VIM") == 0)
2406 didset_vim = FALSE;
2407 else if (didset_vimruntime
2408 && STRICMP(name, "VIMRUNTIME") == 0)
2409 didset_vimruntime = FALSE;
2410 arg_end = arg;
2411 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002412 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002413 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002414 }
2415 }
2416 }
2417
2418 /*
2419 * ":let &option = expr": Set option value.
2420 * ":let &l:option = expr": Set local option value.
2421 * ":let &g:option = expr": Set global option value.
2422 */
2423 else if (*arg == '&')
2424 {
2425 /* Find the end of the name. */
2426 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002427 if (p == NULL || (endchars != NULL
2428 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002429 EMSG(_(e_letunexp));
2430 else
2431 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002432 long n;
2433 int opt_type;
2434 long numval;
2435 char_u *stringval = NULL;
2436 char_u *s;
2437
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002438 c1 = *p;
2439 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002440
2441 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002442 s = get_tv_string_chk(tv); /* != NULL if number or string */
2443 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002444 {
2445 opt_type = get_option_value(arg, &numval,
2446 &stringval, opt_flags);
2447 if ((opt_type == 1 && *op == '.')
2448 || (opt_type == 0 && *op != '.'))
2449 EMSG2(_(e_letwrong), op);
2450 else
2451 {
2452 if (opt_type == 1) /* number */
2453 {
2454 if (*op == '+')
2455 n = numval + n;
2456 else
2457 n = numval - n;
2458 }
2459 else if (opt_type == 0 && stringval != NULL) /* string */
2460 {
2461 s = concat_str(stringval, s);
2462 vim_free(stringval);
2463 stringval = s;
2464 }
2465 }
2466 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002467 if (s != NULL)
2468 {
2469 set_option_value(arg, n, s, opt_flags);
2470 arg_end = p;
2471 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002472 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002473 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002474 }
2475 }
2476
2477 /*
2478 * ":let @r = expr": Set register contents.
2479 */
2480 else if (*arg == '@')
2481 {
2482 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002483 if (op != NULL && (*op == '+' || *op == '-'))
2484 EMSG2(_(e_letwrong), op);
2485 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002486 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002487 EMSG(_(e_letunexp));
2488 else
2489 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002490 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002491 char_u *s;
2492
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002493 p = get_tv_string_chk(tv);
2494 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002495 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002496 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002497 if (s != NULL)
2498 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002499 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002500 vim_free(s);
2501 }
2502 }
2503 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002504 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002505 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002506 arg_end = arg + 1;
2507 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002508 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002509 }
2510 }
2511
2512 /*
2513 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002514 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002515 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002516 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002517 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002518 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002519
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002520 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002521 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002522 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002523 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2524 EMSG(_(e_letunexp));
2525 else
2526 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002527 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002528 arg_end = p;
2529 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002530 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002532 }
2533
2534 else
2535 EMSG2(_(e_invarg2), arg);
2536
2537 return arg_end;
2538}
2539
2540/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002541 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2542 */
2543 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002544check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002545{
2546 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2547 {
2548 EMSG2(_(e_readonlyvar), arg);
2549 return TRUE;
2550 }
2551 return FALSE;
2552}
2553
2554/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555 * Get an lval: variable, Dict item or List item that can be assigned a value
2556 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2557 * "name.key", "name.key[expr]" etc.
2558 * Indexing only works if "name" is an existing List or Dictionary.
2559 * "name" points to the start of the name.
2560 * If "rettv" is not NULL it points to the value to be assigned.
2561 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2562 * wrong; must end in space or cmd separator.
2563 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002564 * flags:
2565 * GLV_QUIET: do not give error messages
2566 * GLV_NO_AUTOLOAD: do not use script autoloading
2567 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002569 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002570 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002571 */
2572 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002573get_lval(
2574 char_u *name,
2575 typval_T *rettv,
2576 lval_T *lp,
2577 int unlet,
2578 int skip,
2579 int flags, /* GLV_ values */
2580 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002581{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002582 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 char_u *expr_start, *expr_end;
2584 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002585 dictitem_T *v;
2586 typval_T var1;
2587 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002588 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002589 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002590 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002591 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002592 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002593 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002594
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002596 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002597
2598 if (skip)
2599 {
2600 /* When skipping just find the end of the name. */
2601 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002602 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603 }
2604
2605 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002606 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 if (expr_start != NULL)
2608 {
2609 /* Don't expand the name when we already know there is an error. */
2610 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2611 && *p != '[' && *p != '.')
2612 {
2613 EMSG(_(e_trailing));
2614 return NULL;
2615 }
2616
2617 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2618 if (lp->ll_exp_name == NULL)
2619 {
2620 /* Report an invalid expression in braces, unless the
2621 * expression evaluation has been cancelled due to an
2622 * aborting error, an interrupt, or an exception. */
2623 if (!aborting() && !quiet)
2624 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002625 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002626 EMSG2(_(e_invarg2), name);
2627 return NULL;
2628 }
2629 }
2630 lp->ll_name = lp->ll_exp_name;
2631 }
2632 else
2633 lp->ll_name = name;
2634
2635 /* Without [idx] or .key we are done. */
2636 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2637 return p;
2638
2639 cc = *p;
2640 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002641 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 if (v == NULL && !quiet)
2643 EMSG2(_(e_undefvar), lp->ll_name);
2644 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002645 if (v == NULL)
2646 return NULL;
2647
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 /*
2649 * Loop until no more [idx] or .key is following.
2650 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002651 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002653 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2655 && !(lp->ll_tv->v_type == VAR_DICT
2656 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002657 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 if (!quiet)
2659 EMSG(_("E689: Can only index a List or Dictionary"));
2660 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002661 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002663 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 if (!quiet)
2665 EMSG(_("E708: [:] must come last"));
2666 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002667 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002668
Bram Moolenaar8c711452005-01-14 21:53:12 +00002669 len = -1;
2670 if (*p == '.')
2671 {
2672 key = p + 1;
2673 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2674 ;
2675 if (len == 0)
2676 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002677 if (!quiet)
2678 EMSG(_(e_emptykey));
2679 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002680 }
2681 p = key + len;
2682 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002683 else
2684 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002685 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002686 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002687 if (*p == ':')
2688 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002689 else
2690 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 empty1 = FALSE;
2692 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002694 if (get_tv_string_chk(&var1) == NULL)
2695 {
2696 /* not a number or string */
2697 clear_tv(&var1);
2698 return NULL;
2699 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700 }
2701
2702 /* Optionally get the second index [ :expr]. */
2703 if (*p == ':')
2704 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002707 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002708 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002709 if (!empty1)
2710 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002712 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 if (rettv != NULL && (rettv->v_type != VAR_LIST
2714 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 if (!quiet)
2717 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 if (!empty1)
2719 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 }
2722 p = skipwhite(p + 1);
2723 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002725 else
2726 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002727 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002728 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2729 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 if (!empty1)
2731 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002734 if (get_tv_string_chk(&var2) == NULL)
2735 {
2736 /* not a number or string */
2737 if (!empty1)
2738 clear_tv(&var1);
2739 clear_tv(&var2);
2740 return NULL;
2741 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002742 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002744 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002746 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002747
Bram Moolenaar8c711452005-01-14 21:53:12 +00002748 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002749 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 if (!quiet)
2751 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002752 if (!empty1)
2753 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002754 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002755 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002756 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 }
2758
2759 /* Skip to past ']'. */
2760 ++p;
2761 }
2762
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002764 {
2765 if (len == -1)
2766 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002767 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002768 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002769 if (*key == NUL)
2770 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 if (!quiet)
2772 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002773 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002774 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002775 }
2776 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002777 lp->ll_list = NULL;
2778 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002779 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002780
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002781 /* When assigning to a scope dictionary check that a function and
2782 * variable name is valid (only variable name unless it is l: or
2783 * g: dictionary). Disallow overwriting a builtin function. */
2784 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002785 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002786 int prevval;
2787 int wrong;
2788
2789 if (len != -1)
2790 {
2791 prevval = key[len];
2792 key[len] = NUL;
2793 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002794 else
2795 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002796 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2797 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002798 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002799 || !valid_varname(key);
2800 if (len != -1)
2801 key[len] = prevval;
2802 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002803 return NULL;
2804 }
2805
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002808 /* Can't add "v:" variable. */
2809 if (lp->ll_dict == &vimvardict)
2810 {
2811 EMSG2(_(e_illvar), name);
2812 return NULL;
2813 }
2814
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002815 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002816 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002817 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002818 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002819 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002820 if (len == -1)
2821 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002823 }
2824 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002826 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 if (len == -1)
2829 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002830 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002831 p = NULL;
2832 break;
2833 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002834 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002835 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002836 return NULL;
2837
Bram Moolenaar8c711452005-01-14 21:53:12 +00002838 if (len == -1)
2839 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002841 }
2842 else
2843 {
2844 /*
2845 * Get the number and item for the only or first index of the List.
2846 */
2847 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002848 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002849 else
2850 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002851 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002852 clear_tv(&var1);
2853 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002854 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 lp->ll_list = lp->ll_tv->vval.v_list;
2856 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2857 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002858 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002859 if (lp->ll_n1 < 0)
2860 {
2861 lp->ll_n1 = 0;
2862 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2863 }
2864 }
2865 if (lp->ll_li == NULL)
2866 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002867 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002868 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002869 if (!quiet)
2870 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002871 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002872 }
2873
2874 /*
2875 * May need to find the item or absolute index for the second
2876 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 * When no index given: "lp->ll_empty2" is TRUE.
2878 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002879 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002881 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002882 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002883 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002884 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002885 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002887 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002888 {
2889 if (!quiet)
2890 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002892 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002893 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002894 }
2895
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2897 if (lp->ll_n1 < 0)
2898 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2899 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002900 {
2901 if (!quiet)
2902 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002904 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002905 }
2906
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002907 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002908 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002909 }
2910
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 return p;
2912}
2913
2914/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002915 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002916 */
2917 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002918clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002919{
2920 vim_free(lp->ll_exp_name);
2921 vim_free(lp->ll_newkey);
2922}
2923
2924/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002925 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002926 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002927 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002928 */
2929 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002930set_var_lval(
2931 lval_T *lp,
2932 char_u *endp,
2933 typval_T *rettv,
2934 int copy,
2935 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002936{
2937 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002938 listitem_T *ri;
2939 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002940
2941 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002942 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002943 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002944 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002945 cc = *endp;
2946 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002947 if (op != NULL && *op != '=')
2948 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002949 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002950
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002951 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002952 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002953 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002954 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002955 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002956 if ((di == NULL
2957 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2958 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2959 FALSE)))
2960 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002961 set_var(lp->ll_name, &tv, FALSE);
2962 clear_tv(&tv);
2963 }
2964 }
2965 else
2966 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002967 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002968 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002969 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002970 else if (tv_check_lock(lp->ll_newkey == NULL
2971 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002972 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002973 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002974 else if (lp->ll_range)
2975 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002976 listitem_T *ll_li = lp->ll_li;
2977 int ll_n1 = lp->ll_n1;
2978
2979 /*
2980 * Check whether any of the list items is locked
2981 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002982 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002983 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002984 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002985 return;
2986 ri = ri->li_next;
2987 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2988 break;
2989 ll_li = ll_li->li_next;
2990 ++ll_n1;
2991 }
2992
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002993 /*
2994 * Assign the List values to the list items.
2995 */
2996 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002997 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002998 if (op != NULL && *op != '=')
2999 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3000 else
3001 {
3002 clear_tv(&lp->ll_li->li_tv);
3003 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3004 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003005 ri = ri->li_next;
3006 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3007 break;
3008 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003009 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003010 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003011 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003012 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003013 ri = NULL;
3014 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003015 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003016 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003017 lp->ll_li = lp->ll_li->li_next;
3018 ++lp->ll_n1;
3019 }
3020 if (ri != NULL)
3021 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003022 else if (lp->ll_empty2
3023 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003024 : lp->ll_n1 != lp->ll_n2)
3025 EMSG(_("E711: List value has not enough items"));
3026 }
3027 else
3028 {
3029 /*
3030 * Assign to a List or Dictionary item.
3031 */
3032 if (lp->ll_newkey != NULL)
3033 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003034 if (op != NULL && *op != '=')
3035 {
3036 EMSG2(_(e_letwrong), op);
3037 return;
3038 }
3039
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003040 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003041 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003042 if (di == NULL)
3043 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003044 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3045 {
3046 vim_free(di);
3047 return;
3048 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003049 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003050 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003051 else if (op != NULL && *op != '=')
3052 {
3053 tv_op(lp->ll_tv, rettv, op);
3054 return;
3055 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003056 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003057 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003058
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003059 /*
3060 * Assign the value to the variable or list item.
3061 */
3062 if (copy)
3063 copy_tv(rettv, lp->ll_tv);
3064 else
3065 {
3066 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003067 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003068 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003069 }
3070 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003071}
3072
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003073/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003074 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3075 * Returns OK or FAIL.
3076 */
3077 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003078tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003079{
3080 long n;
3081 char_u numbuf[NUMBUFLEN];
3082 char_u *s;
3083
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003084 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3085 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3086 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003087 {
3088 switch (tv1->v_type)
3089 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003090 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003091 case VAR_DICT:
3092 case VAR_FUNC:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003093 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003094 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003095 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003096 break;
3097
3098 case VAR_LIST:
3099 if (*op != '+' || tv2->v_type != VAR_LIST)
3100 break;
3101 /* List += List */
3102 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3103 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3104 return OK;
3105
3106 case VAR_NUMBER:
3107 case VAR_STRING:
3108 if (tv2->v_type == VAR_LIST)
3109 break;
3110 if (*op == '+' || *op == '-')
3111 {
3112 /* nr += nr or nr -= nr*/
3113 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003114#ifdef FEAT_FLOAT
3115 if (tv2->v_type == VAR_FLOAT)
3116 {
3117 float_T f = n;
3118
3119 if (*op == '+')
3120 f += tv2->vval.v_float;
3121 else
3122 f -= tv2->vval.v_float;
3123 clear_tv(tv1);
3124 tv1->v_type = VAR_FLOAT;
3125 tv1->vval.v_float = f;
3126 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003127 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003128#endif
3129 {
3130 if (*op == '+')
3131 n += get_tv_number(tv2);
3132 else
3133 n -= get_tv_number(tv2);
3134 clear_tv(tv1);
3135 tv1->v_type = VAR_NUMBER;
3136 tv1->vval.v_number = n;
3137 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003138 }
3139 else
3140 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003141 if (tv2->v_type == VAR_FLOAT)
3142 break;
3143
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003144 /* str .= str */
3145 s = get_tv_string(tv1);
3146 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3147 clear_tv(tv1);
3148 tv1->v_type = VAR_STRING;
3149 tv1->vval.v_string = s;
3150 }
3151 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003152
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003153 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003154#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003155 {
3156 float_T f;
3157
3158 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3159 && tv2->v_type != VAR_NUMBER
3160 && tv2->v_type != VAR_STRING))
3161 break;
3162 if (tv2->v_type == VAR_FLOAT)
3163 f = tv2->vval.v_float;
3164 else
3165 f = get_tv_number(tv2);
3166 if (*op == '+')
3167 tv1->vval.v_float += f;
3168 else
3169 tv1->vval.v_float -= f;
3170 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003171#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003172 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003173 }
3174 }
3175
3176 EMSG2(_(e_letwrong), op);
3177 return FAIL;
3178}
3179
3180/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003181 * Add a watcher to a list.
3182 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003183 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003184list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003185{
3186 lw->lw_next = l->lv_watch;
3187 l->lv_watch = lw;
3188}
3189
3190/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003191 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003192 * No warning when it isn't found...
3193 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003194 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003195list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003196{
Bram Moolenaar33570922005-01-25 22:26:29 +00003197 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003198
3199 lwp = &l->lv_watch;
3200 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3201 {
3202 if (lw == lwrem)
3203 {
3204 *lwp = lw->lw_next;
3205 break;
3206 }
3207 lwp = &lw->lw_next;
3208 }
3209}
3210
3211/*
3212 * Just before removing an item from a list: advance watchers to the next
3213 * item.
3214 */
3215 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003216list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003217{
Bram Moolenaar33570922005-01-25 22:26:29 +00003218 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003219
3220 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3221 if (lw->lw_item == item)
3222 lw->lw_item = item->li_next;
3223}
3224
3225/*
3226 * Evaluate the expression used in a ":for var in expr" command.
3227 * "arg" points to "var".
3228 * Set "*errp" to TRUE for an error, FALSE otherwise;
3229 * Return a pointer that holds the info. Null when there is an error.
3230 */
3231 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003232eval_for_line(
3233 char_u *arg,
3234 int *errp,
3235 char_u **nextcmdp,
3236 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003237{
Bram Moolenaar33570922005-01-25 22:26:29 +00003238 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003239 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003240 typval_T tv;
3241 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003242
3243 *errp = TRUE; /* default: there is an error */
3244
Bram Moolenaar33570922005-01-25 22:26:29 +00003245 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003246 if (fi == NULL)
3247 return NULL;
3248
3249 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3250 if (expr == NULL)
3251 return fi;
3252
3253 expr = skipwhite(expr);
3254 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3255 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003256 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003257 return fi;
3258 }
3259
3260 if (skip)
3261 ++emsg_skip;
3262 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3263 {
3264 *errp = FALSE;
3265 if (!skip)
3266 {
3267 l = tv.vval.v_list;
3268 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003269 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003270 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003271 clear_tv(&tv);
3272 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003273 else
3274 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003275 /* No need to increment the refcount, it's already set for the
3276 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003277 fi->fi_list = l;
3278 list_add_watch(l, &fi->fi_lw);
3279 fi->fi_lw.lw_item = l->lv_first;
3280 }
3281 }
3282 }
3283 if (skip)
3284 --emsg_skip;
3285
3286 return fi;
3287}
3288
3289/*
3290 * Use the first item in a ":for" list. Advance to the next.
3291 * Assign the values to the variable (list). "arg" points to the first one.
3292 * Return TRUE when a valid item was found, FALSE when at end of list or
3293 * something wrong.
3294 */
3295 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003296next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003297{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003298 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003299 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003300 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003301
3302 item = fi->fi_lw.lw_item;
3303 if (item == NULL)
3304 result = FALSE;
3305 else
3306 {
3307 fi->fi_lw.lw_item = item->li_next;
3308 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3309 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3310 }
3311 return result;
3312}
3313
3314/*
3315 * Free the structure used to store info used by ":for".
3316 */
3317 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003318free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003319{
Bram Moolenaar33570922005-01-25 22:26:29 +00003320 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003321
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003322 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003323 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003324 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003325 list_unref(fi->fi_list);
3326 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003327 vim_free(fi);
3328}
3329
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3331
3332 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003333set_context_for_expression(
3334 expand_T *xp,
3335 char_u *arg,
3336 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337{
3338 int got_eq = FALSE;
3339 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003340 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003342 if (cmdidx == CMD_let)
3343 {
3344 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003345 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003346 {
3347 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003348 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003349 {
3350 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003351 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003352 if (vim_iswhite(*p))
3353 break;
3354 }
3355 return;
3356 }
3357 }
3358 else
3359 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3360 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 while ((xp->xp_pattern = vim_strpbrk(arg,
3362 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3363 {
3364 c = *xp->xp_pattern;
3365 if (c == '&')
3366 {
3367 c = xp->xp_pattern[1];
3368 if (c == '&')
3369 {
3370 ++xp->xp_pattern;
3371 xp->xp_context = cmdidx != CMD_let || got_eq
3372 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3373 }
3374 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003375 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003377 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3378 xp->xp_pattern += 2;
3379
3380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 }
3382 else if (c == '$')
3383 {
3384 /* environment variable */
3385 xp->xp_context = EXPAND_ENV_VARS;
3386 }
3387 else if (c == '=')
3388 {
3389 got_eq = TRUE;
3390 xp->xp_context = EXPAND_EXPRESSION;
3391 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003392 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 && xp->xp_context == EXPAND_FUNCTIONS
3394 && vim_strchr(xp->xp_pattern, '(') == NULL)
3395 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003396 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 break;
3398 }
3399 else if (cmdidx != CMD_let || got_eq)
3400 {
3401 if (c == '"') /* string */
3402 {
3403 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3404 if (c == '\\' && xp->xp_pattern[1] != NUL)
3405 ++xp->xp_pattern;
3406 xp->xp_context = EXPAND_NOTHING;
3407 }
3408 else if (c == '\'') /* literal string */
3409 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003410 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3412 /* skip */ ;
3413 xp->xp_context = EXPAND_NOTHING;
3414 }
3415 else if (c == '|')
3416 {
3417 if (xp->xp_pattern[1] == '|')
3418 {
3419 ++xp->xp_pattern;
3420 xp->xp_context = EXPAND_EXPRESSION;
3421 }
3422 else
3423 xp->xp_context = EXPAND_COMMANDS;
3424 }
3425 else
3426 xp->xp_context = EXPAND_EXPRESSION;
3427 }
3428 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003429 /* Doesn't look like something valid, expand as an expression
3430 * anyway. */
3431 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 arg = xp->xp_pattern;
3433 if (*arg != NUL)
3434 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3435 /* skip */ ;
3436 }
3437 xp->xp_pattern = arg;
3438}
3439
3440#endif /* FEAT_CMDL_COMPL */
3441
3442/*
3443 * ":1,25call func(arg1, arg2)" function call.
3444 */
3445 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003446ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447{
3448 char_u *arg = eap->arg;
3449 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003451 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003453 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 linenr_T lnum;
3455 int doesrange;
3456 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003457 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003459 if (eap->skip)
3460 {
3461 /* trans_function_name() doesn't work well when skipping, use eval0()
3462 * instead to skip to any following command, e.g. for:
3463 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003464 ++emsg_skip;
3465 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3466 clear_tv(&rettv);
3467 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003468 return;
3469 }
3470
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003471 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003472 if (fudi.fd_newkey != NULL)
3473 {
3474 /* Still need to give an error message for missing key. */
3475 EMSG2(_(e_dictkey), fudi.fd_newkey);
3476 vim_free(fudi.fd_newkey);
3477 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003478 if (tofree == NULL)
3479 return;
3480
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003481 /* Increase refcount on dictionary, it could get deleted when evaluating
3482 * the arguments. */
3483 if (fudi.fd_dict != NULL)
3484 ++fudi.fd_dict->dv_refcount;
3485
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003486 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003487 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003488 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489
Bram Moolenaar532c7802005-01-27 14:44:31 +00003490 /* Skip white space to allow ":call func ()". Not good, but required for
3491 * backward compatibility. */
3492 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003493 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494
3495 if (*startarg != '(')
3496 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003497 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 goto end;
3499 }
3500
3501 /*
3502 * When skipping, evaluate the function once, to find the end of the
3503 * arguments.
3504 * When the function takes a range, this is discovered after the first
3505 * call, and the loop is broken.
3506 */
3507 if (eap->skip)
3508 {
3509 ++emsg_skip;
3510 lnum = eap->line2; /* do it once, also with an invalid range */
3511 }
3512 else
3513 lnum = eap->line1;
3514 for ( ; lnum <= eap->line2; ++lnum)
3515 {
3516 if (!eap->skip && eap->addr_count > 0)
3517 {
3518 curwin->w_cursor.lnum = lnum;
3519 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003520#ifdef FEAT_VIRTUALEDIT
3521 curwin->w_cursor.coladd = 0;
3522#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 }
3524 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003525 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003526 eap->line1, eap->line2, &doesrange,
3527 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 {
3529 failed = TRUE;
3530 break;
3531 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003532
3533 /* Handle a function returning a Funcref, Dictionary or List. */
3534 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3535 {
3536 failed = TRUE;
3537 break;
3538 }
3539
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003540 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 if (doesrange || eap->skip)
3542 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003543
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003545 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003546 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003547 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 if (aborting())
3549 break;
3550 }
3551 if (eap->skip)
3552 --emsg_skip;
3553
3554 if (!failed)
3555 {
3556 /* Check for trailing illegal characters and a following command. */
3557 if (!ends_excmd(*arg))
3558 {
3559 emsg_severe = TRUE;
3560 EMSG(_(e_trailing));
3561 }
3562 else
3563 eap->nextcmd = check_nextcmd(arg);
3564 }
3565
3566end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003567 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003568 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569}
3570
3571/*
3572 * ":unlet[!] var1 ... " command.
3573 */
3574 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003575ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003577 ex_unletlock(eap, eap->arg, 0);
3578}
3579
3580/*
3581 * ":lockvar" and ":unlockvar" commands
3582 */
3583 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003584ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003585{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003587 int deep = 2;
3588
3589 if (eap->forceit)
3590 deep = -1;
3591 else if (vim_isdigit(*arg))
3592 {
3593 deep = getdigits(&arg);
3594 arg = skipwhite(arg);
3595 }
3596
3597 ex_unletlock(eap, arg, deep);
3598}
3599
3600/*
3601 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3602 */
3603 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003604ex_unletlock(
3605 exarg_T *eap,
3606 char_u *argstart,
3607 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003608{
3609 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003612 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613
3614 do
3615 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003616 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003617 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003618 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003619 if (lv.ll_name == NULL)
3620 error = TRUE; /* error but continue parsing */
3621 if (name_end == NULL || (!vim_iswhite(*name_end)
3622 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003624 if (name_end != NULL)
3625 {
3626 emsg_severe = TRUE;
3627 EMSG(_(e_trailing));
3628 }
3629 if (!(eap->skip || error))
3630 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 break;
3632 }
3633
3634 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003635 {
3636 if (eap->cmdidx == CMD_unlet)
3637 {
3638 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3639 error = TRUE;
3640 }
3641 else
3642 {
3643 if (do_lock_var(&lv, name_end, deep,
3644 eap->cmdidx == CMD_lockvar) == FAIL)
3645 error = TRUE;
3646 }
3647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003649 if (!eap->skip)
3650 clear_lval(&lv);
3651
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 arg = skipwhite(name_end);
3653 } while (!ends_excmd(*arg));
3654
3655 eap->nextcmd = check_nextcmd(arg);
3656}
3657
Bram Moolenaar8c711452005-01-14 21:53:12 +00003658 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003659do_unlet_var(
3660 lval_T *lp,
3661 char_u *name_end,
3662 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003663{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003664 int ret = OK;
3665 int cc;
3666
3667 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003668 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003669 cc = *name_end;
3670 *name_end = NUL;
3671
3672 /* Normal name or expanded name. */
3673 if (check_changedtick(lp->ll_name))
3674 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003675 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003676 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003677 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003678 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003679 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003680 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003681 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003682 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003683 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003684 else if (lp->ll_range)
3685 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003686 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003687 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003688 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003689
3690 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3691 {
3692 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003693 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003694 return FAIL;
3695 ll_li = li;
3696 ++ll_n1;
3697 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003698
3699 /* Delete a range of List items. */
3700 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3701 {
3702 li = lp->ll_li->li_next;
3703 listitem_remove(lp->ll_list, lp->ll_li);
3704 lp->ll_li = li;
3705 ++lp->ll_n1;
3706 }
3707 }
3708 else
3709 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003710 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003711 /* unlet a List item. */
3712 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003713 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003714 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003715 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003716 }
3717
3718 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003719}
3720
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721/*
3722 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003723 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 */
3725 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003726do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727{
Bram Moolenaar33570922005-01-25 22:26:29 +00003728 hashtab_T *ht;
3729 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003730 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003731 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003732 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733
Bram Moolenaar33570922005-01-25 22:26:29 +00003734 ht = find_var_ht(name, &varname);
3735 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003737 if (ht == &globvarht)
3738 d = &globvardict;
3739 else if (current_funccal != NULL
3740 && ht == &current_funccal->l_vars.dv_hashtab)
3741 d = &current_funccal->l_vars;
3742 else if (ht == &compat_hashtab)
3743 d = &vimvardict;
3744 else
3745 {
3746 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3747 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3748 }
3749 if (d == NULL)
3750 {
3751 EMSG2(_(e_intern2), "do_unlet()");
3752 return FAIL;
3753 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003754 hi = hash_find(ht, varname);
3755 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003756 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003757 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003758 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003759 || var_check_ro(di->di_flags, name, FALSE)
3760 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003761 return FAIL;
3762
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003763 delete_var(ht, hi);
3764 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003767 if (forceit)
3768 return OK;
3769 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 return FAIL;
3771}
3772
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003773/*
3774 * Lock or unlock variable indicated by "lp".
3775 * "deep" is the levels to go (-1 for unlimited);
3776 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3777 */
3778 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003779do_lock_var(
3780 lval_T *lp,
3781 char_u *name_end,
3782 int deep,
3783 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003784{
3785 int ret = OK;
3786 int cc;
3787 dictitem_T *di;
3788
3789 if (deep == 0) /* nothing to do */
3790 return OK;
3791
3792 if (lp->ll_tv == NULL)
3793 {
3794 cc = *name_end;
3795 *name_end = NUL;
3796
3797 /* Normal name or expanded name. */
3798 if (check_changedtick(lp->ll_name))
3799 ret = FAIL;
3800 else
3801 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003802 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003803 if (di == NULL)
3804 ret = FAIL;
3805 else
3806 {
3807 if (lock)
3808 di->di_flags |= DI_FLAGS_LOCK;
3809 else
3810 di->di_flags &= ~DI_FLAGS_LOCK;
3811 item_lock(&di->di_tv, deep, lock);
3812 }
3813 }
3814 *name_end = cc;
3815 }
3816 else if (lp->ll_range)
3817 {
3818 listitem_T *li = lp->ll_li;
3819
3820 /* (un)lock a range of List items. */
3821 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3822 {
3823 item_lock(&li->li_tv, deep, lock);
3824 li = li->li_next;
3825 ++lp->ll_n1;
3826 }
3827 }
3828 else if (lp->ll_list != NULL)
3829 /* (un)lock a List item. */
3830 item_lock(&lp->ll_li->li_tv, deep, lock);
3831 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003832 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003833 item_lock(&lp->ll_di->di_tv, deep, lock);
3834
3835 return ret;
3836}
3837
3838/*
3839 * Lock or unlock an item. "deep" is nr of levels to go.
3840 */
3841 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003842item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003843{
3844 static int recurse = 0;
3845 list_T *l;
3846 listitem_T *li;
3847 dict_T *d;
3848 hashitem_T *hi;
3849 int todo;
3850
3851 if (recurse >= DICT_MAXNEST)
3852 {
3853 EMSG(_("E743: variable nested too deep for (un)lock"));
3854 return;
3855 }
3856 if (deep == 0)
3857 return;
3858 ++recurse;
3859
3860 /* lock/unlock the item itself */
3861 if (lock)
3862 tv->v_lock |= VAR_LOCKED;
3863 else
3864 tv->v_lock &= ~VAR_LOCKED;
3865
3866 switch (tv->v_type)
3867 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003868 case VAR_UNKNOWN:
3869 case VAR_NUMBER:
3870 case VAR_STRING:
3871 case VAR_FUNC:
3872 case VAR_FLOAT:
3873 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003874 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003875 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003876 break;
3877
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003878 case VAR_LIST:
3879 if ((l = tv->vval.v_list) != NULL)
3880 {
3881 if (lock)
3882 l->lv_lock |= VAR_LOCKED;
3883 else
3884 l->lv_lock &= ~VAR_LOCKED;
3885 if (deep < 0 || deep > 1)
3886 /* recursive: lock/unlock the items the List contains */
3887 for (li = l->lv_first; li != NULL; li = li->li_next)
3888 item_lock(&li->li_tv, deep - 1, lock);
3889 }
3890 break;
3891 case VAR_DICT:
3892 if ((d = tv->vval.v_dict) != NULL)
3893 {
3894 if (lock)
3895 d->dv_lock |= VAR_LOCKED;
3896 else
3897 d->dv_lock &= ~VAR_LOCKED;
3898 if (deep < 0 || deep > 1)
3899 {
3900 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003901 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003902 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3903 {
3904 if (!HASHITEM_EMPTY(hi))
3905 {
3906 --todo;
3907 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3908 }
3909 }
3910 }
3911 }
3912 }
3913 --recurse;
3914}
3915
Bram Moolenaara40058a2005-07-11 22:42:07 +00003916/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003917 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3918 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003919 */
3920 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003921tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003922{
3923 return (tv->v_lock & VAR_LOCKED)
3924 || (tv->v_type == VAR_LIST
3925 && tv->vval.v_list != NULL
3926 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3927 || (tv->v_type == VAR_DICT
3928 && tv->vval.v_dict != NULL
3929 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3930}
3931
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3933/*
3934 * Delete all "menutrans_" variables.
3935 */
3936 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003937del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938{
Bram Moolenaar33570922005-01-25 22:26:29 +00003939 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003940 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941
Bram Moolenaar33570922005-01-25 22:26:29 +00003942 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003943 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003944 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003945 {
3946 if (!HASHITEM_EMPTY(hi))
3947 {
3948 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003949 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3950 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003951 }
3952 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003953 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954}
3955#endif
3956
3957#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3958
3959/*
3960 * Local string buffer for the next two functions to store a variable name
3961 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3962 * get_user_var_name().
3963 */
3964
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003965static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966
3967static char_u *varnamebuf = NULL;
3968static int varnamebuflen = 0;
3969
3970/*
3971 * Function to concatenate a prefix and a variable name.
3972 */
3973 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003974cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975{
3976 int len;
3977
3978 len = (int)STRLEN(name) + 3;
3979 if (len > varnamebuflen)
3980 {
3981 vim_free(varnamebuf);
3982 len += 10; /* some additional space */
3983 varnamebuf = alloc(len);
3984 if (varnamebuf == NULL)
3985 {
3986 varnamebuflen = 0;
3987 return NULL;
3988 }
3989 varnamebuflen = len;
3990 }
3991 *varnamebuf = prefix;
3992 varnamebuf[1] = ':';
3993 STRCPY(varnamebuf + 2, name);
3994 return varnamebuf;
3995}
3996
3997/*
3998 * Function given to ExpandGeneric() to obtain the list of user defined
3999 * (global/buffer/window/built-in) variable names.
4000 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004002get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004004 static long_u gdone;
4005 static long_u bdone;
4006 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004007#ifdef FEAT_WINDOWS
4008 static long_u tdone;
4009#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004010 static int vidx;
4011 static hashitem_T *hi;
4012 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013
4014 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004015 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004016 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004017#ifdef FEAT_WINDOWS
4018 tdone = 0;
4019#endif
4020 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004021
4022 /* Global variables */
4023 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004025 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004026 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004027 else
4028 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004029 while (HASHITEM_EMPTY(hi))
4030 ++hi;
4031 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4032 return cat_prefix_varname('g', hi->hi_key);
4033 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004035
4036 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004037 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004038 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004040 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004041 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004042 else
4043 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004044 while (HASHITEM_EMPTY(hi))
4045 ++hi;
4046 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004048 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004050 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 return (char_u *)"b:changedtick";
4052 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004053
4054 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004055 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004056 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004058 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004059 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004060 else
4061 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004062 while (HASHITEM_EMPTY(hi))
4063 ++hi;
4064 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004066
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004067#ifdef FEAT_WINDOWS
4068 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004069 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004070 if (tdone < ht->ht_used)
4071 {
4072 if (tdone++ == 0)
4073 hi = ht->ht_array;
4074 else
4075 ++hi;
4076 while (HASHITEM_EMPTY(hi))
4077 ++hi;
4078 return cat_prefix_varname('t', hi->hi_key);
4079 }
4080#endif
4081
Bram Moolenaar33570922005-01-25 22:26:29 +00004082 /* v: variables */
4083 if (vidx < VV_LEN)
4084 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085
4086 vim_free(varnamebuf);
4087 varnamebuf = NULL;
4088 varnamebuflen = 0;
4089 return NULL;
4090}
4091
4092#endif /* FEAT_CMDL_COMPL */
4093
4094/*
4095 * types for expressions.
4096 */
4097typedef enum
4098{
4099 TYPE_UNKNOWN = 0
4100 , TYPE_EQUAL /* == */
4101 , TYPE_NEQUAL /* != */
4102 , TYPE_GREATER /* > */
4103 , TYPE_GEQUAL /* >= */
4104 , TYPE_SMALLER /* < */
4105 , TYPE_SEQUAL /* <= */
4106 , TYPE_MATCH /* =~ */
4107 , TYPE_NOMATCH /* !~ */
4108} exptype_T;
4109
4110/*
4111 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004112 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4114 */
4115
4116/*
4117 * Handle zero level expression.
4118 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004119 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004120 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 * Return OK or FAIL.
4122 */
4123 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004124eval0(
4125 char_u *arg,
4126 typval_T *rettv,
4127 char_u **nextcmd,
4128 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129{
4130 int ret;
4131 char_u *p;
4132
4133 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004134 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 if (ret == FAIL || !ends_excmd(*p))
4136 {
4137 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004138 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 /*
4140 * Report the invalid expression unless the expression evaluation has
4141 * been cancelled due to an aborting error, an interrupt, or an
4142 * exception.
4143 */
4144 if (!aborting())
4145 EMSG2(_(e_invexpr2), arg);
4146 ret = FAIL;
4147 }
4148 if (nextcmd != NULL)
4149 *nextcmd = check_nextcmd(p);
4150
4151 return ret;
4152}
4153
4154/*
4155 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004156 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 *
4158 * "arg" must point to the first non-white of the expression.
4159 * "arg" is advanced to the next non-white after the recognized expression.
4160 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004161 * Note: "rettv.v_lock" is not set.
4162 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 * Return OK or FAIL.
4164 */
4165 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004166eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167{
4168 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004169 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170
4171 /*
4172 * Get the first variable.
4173 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004174 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 return FAIL;
4176
4177 if ((*arg)[0] == '?')
4178 {
4179 result = FALSE;
4180 if (evaluate)
4181 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004182 int error = FALSE;
4183
4184 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004186 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004187 if (error)
4188 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 }
4190
4191 /*
4192 * Get the second variable.
4193 */
4194 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004195 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 return FAIL;
4197
4198 /*
4199 * Check for the ":".
4200 */
4201 if ((*arg)[0] != ':')
4202 {
4203 EMSG(_("E109: Missing ':' after '?'"));
4204 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004205 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 return FAIL;
4207 }
4208
4209 /*
4210 * Get the third variable.
4211 */
4212 *arg = skipwhite(*arg + 1);
4213 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4214 {
4215 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004216 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 return FAIL;
4218 }
4219 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004220 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 }
4222
4223 return OK;
4224}
4225
4226/*
4227 * Handle first level expression:
4228 * expr2 || expr2 || expr2 logical OR
4229 *
4230 * "arg" must point to the first non-white of the expression.
4231 * "arg" is advanced to the next non-white after the recognized expression.
4232 *
4233 * Return OK or FAIL.
4234 */
4235 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004236eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237{
Bram Moolenaar33570922005-01-25 22:26:29 +00004238 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 long result;
4240 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004241 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242
4243 /*
4244 * Get the first variable.
4245 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004246 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 return FAIL;
4248
4249 /*
4250 * Repeat until there is no following "||".
4251 */
4252 first = TRUE;
4253 result = FALSE;
4254 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4255 {
4256 if (evaluate && first)
4257 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004258 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004260 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004261 if (error)
4262 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 first = FALSE;
4264 }
4265
4266 /*
4267 * Get the second variable.
4268 */
4269 *arg = skipwhite(*arg + 2);
4270 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4271 return FAIL;
4272
4273 /*
4274 * Compute the result.
4275 */
4276 if (evaluate && !result)
4277 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004278 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004280 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004281 if (error)
4282 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 }
4284 if (evaluate)
4285 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004286 rettv->v_type = VAR_NUMBER;
4287 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 }
4289 }
4290
4291 return OK;
4292}
4293
4294/*
4295 * Handle second level expression:
4296 * expr3 && expr3 && expr3 logical AND
4297 *
4298 * "arg" must point to the first non-white of the expression.
4299 * "arg" is advanced to the next non-white after the recognized expression.
4300 *
4301 * Return OK or FAIL.
4302 */
4303 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004304eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305{
Bram Moolenaar33570922005-01-25 22:26:29 +00004306 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 long result;
4308 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004309 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310
4311 /*
4312 * Get the first variable.
4313 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004314 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 return FAIL;
4316
4317 /*
4318 * Repeat until there is no following "&&".
4319 */
4320 first = TRUE;
4321 result = TRUE;
4322 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4323 {
4324 if (evaluate && first)
4325 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004326 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004328 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004329 if (error)
4330 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 first = FALSE;
4332 }
4333
4334 /*
4335 * Get the second variable.
4336 */
4337 *arg = skipwhite(*arg + 2);
4338 if (eval4(arg, &var2, evaluate && result) == FAIL)
4339 return FAIL;
4340
4341 /*
4342 * Compute the result.
4343 */
4344 if (evaluate && result)
4345 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004346 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004348 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004349 if (error)
4350 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 }
4352 if (evaluate)
4353 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004354 rettv->v_type = VAR_NUMBER;
4355 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 }
4357 }
4358
4359 return OK;
4360}
4361
4362/*
4363 * Handle third level expression:
4364 * var1 == var2
4365 * var1 =~ var2
4366 * var1 != var2
4367 * var1 !~ var2
4368 * var1 > var2
4369 * var1 >= var2
4370 * var1 < var2
4371 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004372 * var1 is var2
4373 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 *
4375 * "arg" must point to the first non-white of the expression.
4376 * "arg" is advanced to the next non-white after the recognized expression.
4377 *
4378 * Return OK or FAIL.
4379 */
4380 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004381eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382{
Bram Moolenaar33570922005-01-25 22:26:29 +00004383 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 char_u *p;
4385 int i;
4386 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004387 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 int len = 2;
4389 long n1, n2;
4390 char_u *s1, *s2;
4391 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4392 regmatch_T regmatch;
4393 int ic;
4394 char_u *save_cpo;
4395
4396 /*
4397 * Get the first variable.
4398 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004399 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 return FAIL;
4401
4402 p = *arg;
4403 switch (p[0])
4404 {
4405 case '=': if (p[1] == '=')
4406 type = TYPE_EQUAL;
4407 else if (p[1] == '~')
4408 type = TYPE_MATCH;
4409 break;
4410 case '!': if (p[1] == '=')
4411 type = TYPE_NEQUAL;
4412 else if (p[1] == '~')
4413 type = TYPE_NOMATCH;
4414 break;
4415 case '>': if (p[1] != '=')
4416 {
4417 type = TYPE_GREATER;
4418 len = 1;
4419 }
4420 else
4421 type = TYPE_GEQUAL;
4422 break;
4423 case '<': if (p[1] != '=')
4424 {
4425 type = TYPE_SMALLER;
4426 len = 1;
4427 }
4428 else
4429 type = TYPE_SEQUAL;
4430 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004431 case 'i': if (p[1] == 's')
4432 {
4433 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4434 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004435 i = p[len];
4436 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004437 {
4438 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4439 type_is = TRUE;
4440 }
4441 }
4442 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 }
4444
4445 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004446 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 */
4448 if (type != TYPE_UNKNOWN)
4449 {
4450 /* extra question mark appended: ignore case */
4451 if (p[len] == '?')
4452 {
4453 ic = TRUE;
4454 ++len;
4455 }
4456 /* extra '#' appended: match case */
4457 else if (p[len] == '#')
4458 {
4459 ic = FALSE;
4460 ++len;
4461 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004462 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 else
4464 ic = p_ic;
4465
4466 /*
4467 * Get the second variable.
4468 */
4469 *arg = skipwhite(p + len);
4470 if (eval5(arg, &var2, evaluate) == FAIL)
4471 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004472 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 return FAIL;
4474 }
4475
4476 if (evaluate)
4477 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004478 if (type_is && rettv->v_type != var2.v_type)
4479 {
4480 /* For "is" a different type always means FALSE, for "notis"
4481 * it means TRUE. */
4482 n1 = (type == TYPE_NEQUAL);
4483 }
4484 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4485 {
4486 if (type_is)
4487 {
4488 n1 = (rettv->v_type == var2.v_type
4489 && rettv->vval.v_list == var2.vval.v_list);
4490 if (type == TYPE_NEQUAL)
4491 n1 = !n1;
4492 }
4493 else if (rettv->v_type != var2.v_type
4494 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4495 {
4496 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004497 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004498 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004499 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004500 clear_tv(rettv);
4501 clear_tv(&var2);
4502 return FAIL;
4503 }
4504 else
4505 {
4506 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004507 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4508 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004509 if (type == TYPE_NEQUAL)
4510 n1 = !n1;
4511 }
4512 }
4513
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004514 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4515 {
4516 if (type_is)
4517 {
4518 n1 = (rettv->v_type == var2.v_type
4519 && rettv->vval.v_dict == var2.vval.v_dict);
4520 if (type == TYPE_NEQUAL)
4521 n1 = !n1;
4522 }
4523 else if (rettv->v_type != var2.v_type
4524 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4525 {
4526 if (rettv->v_type != var2.v_type)
4527 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4528 else
4529 EMSG(_("E736: Invalid operation for Dictionary"));
4530 clear_tv(rettv);
4531 clear_tv(&var2);
4532 return FAIL;
4533 }
4534 else
4535 {
4536 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004537 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4538 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004539 if (type == TYPE_NEQUAL)
4540 n1 = !n1;
4541 }
4542 }
4543
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004544 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4545 {
4546 if (rettv->v_type != var2.v_type
4547 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4548 {
4549 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004550 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004551 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004552 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004553 clear_tv(rettv);
4554 clear_tv(&var2);
4555 return FAIL;
4556 }
4557 else
4558 {
4559 /* Compare two Funcrefs for being equal or unequal. */
4560 if (rettv->vval.v_string == NULL
4561 || var2.vval.v_string == NULL)
4562 n1 = FALSE;
4563 else
4564 n1 = STRCMP(rettv->vval.v_string,
4565 var2.vval.v_string) == 0;
4566 if (type == TYPE_NEQUAL)
4567 n1 = !n1;
4568 }
4569 }
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 {
5232 /* If "s" is the name of a variable of type VAR_FUNC
5233 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005234 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005235
5236 /* Invoke the function. */
5237 ret = get_func_tv(s, len, rettv, arg,
5238 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005239 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005240
5241 /* If evaluate is FALSE rettv->v_type was not set in
5242 * get_func_tv, but it's needed in handle_subscript() to parse
5243 * what follows. So set it here. */
5244 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5245 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005246 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005247 rettv->v_type = VAR_FUNC;
5248 }
5249
Bram Moolenaar8c711452005-01-14 21:53:12 +00005250 /* Stop the expression evaluation when immediately
5251 * aborting on error, or when an interrupt occurred or
5252 * an exception was thrown but not caught. */
5253 if (aborting())
5254 {
5255 if (ret == OK)
5256 clear_tv(rettv);
5257 ret = FAIL;
5258 }
5259 }
5260 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005261 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005262 else
5263 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005264 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005265 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005266 }
5267
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 *arg = skipwhite(*arg);
5269
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005270 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5271 * expr(expr). */
5272 if (ret == OK)
5273 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274
5275 /*
5276 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5277 */
5278 if (ret == OK && evaluate && end_leader > start_leader)
5279 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005280 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005281 int val = 0;
5282#ifdef FEAT_FLOAT
5283 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005284
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005285 if (rettv->v_type == VAR_FLOAT)
5286 f = rettv->vval.v_float;
5287 else
5288#endif
5289 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005290 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005292 clear_tv(rettv);
5293 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005295 else
5296 {
5297 while (end_leader > start_leader)
5298 {
5299 --end_leader;
5300 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005301 {
5302#ifdef FEAT_FLOAT
5303 if (rettv->v_type == VAR_FLOAT)
5304 f = !f;
5305 else
5306#endif
5307 val = !val;
5308 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005309 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005310 {
5311#ifdef FEAT_FLOAT
5312 if (rettv->v_type == VAR_FLOAT)
5313 f = -f;
5314 else
5315#endif
5316 val = -val;
5317 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005318 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005319#ifdef FEAT_FLOAT
5320 if (rettv->v_type == VAR_FLOAT)
5321 {
5322 clear_tv(rettv);
5323 rettv->vval.v_float = f;
5324 }
5325 else
5326#endif
5327 {
5328 clear_tv(rettv);
5329 rettv->v_type = VAR_NUMBER;
5330 rettv->vval.v_number = val;
5331 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 }
5334
5335 return ret;
5336}
5337
5338/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005339 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5340 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005341 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5342 */
5343 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005344eval_index(
5345 char_u **arg,
5346 typval_T *rettv,
5347 int evaluate,
5348 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005349{
5350 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005351 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005352 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005353 long len = -1;
5354 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005355 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005356 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005357
Bram Moolenaara03f2332016-02-06 18:09:59 +01005358 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005359 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005360 case VAR_FUNC:
5361 if (verbose)
5362 EMSG(_("E695: Cannot index a Funcref"));
5363 return FAIL;
5364 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005365#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005366 if (verbose)
5367 EMSG(_(e_float_as_string));
5368 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005369#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005370 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005371 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005372 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005373 if (verbose)
5374 EMSG(_("E909: Cannot index a special variable"));
5375 return FAIL;
5376 case VAR_UNKNOWN:
5377 if (evaluate)
5378 return FAIL;
5379 /* FALLTHROUGH */
5380
5381 case VAR_STRING:
5382 case VAR_NUMBER:
5383 case VAR_LIST:
5384 case VAR_DICT:
5385 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005386 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005387
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005388 init_tv(&var1);
5389 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005390 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005391 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005392 /*
5393 * dict.name
5394 */
5395 key = *arg + 1;
5396 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5397 ;
5398 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005399 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005400 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005401 }
5402 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005403 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005404 /*
5405 * something[idx]
5406 *
5407 * Get the (first) variable from inside the [].
5408 */
5409 *arg = skipwhite(*arg + 1);
5410 if (**arg == ':')
5411 empty1 = TRUE;
5412 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5413 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005414 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5415 {
5416 /* not a number or string */
5417 clear_tv(&var1);
5418 return FAIL;
5419 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005420
5421 /*
5422 * Get the second variable from inside the [:].
5423 */
5424 if (**arg == ':')
5425 {
5426 range = TRUE;
5427 *arg = skipwhite(*arg + 1);
5428 if (**arg == ']')
5429 empty2 = TRUE;
5430 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5431 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005432 if (!empty1)
5433 clear_tv(&var1);
5434 return FAIL;
5435 }
5436 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5437 {
5438 /* not a number or string */
5439 if (!empty1)
5440 clear_tv(&var1);
5441 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005442 return FAIL;
5443 }
5444 }
5445
5446 /* Check for the ']'. */
5447 if (**arg != ']')
5448 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005449 if (verbose)
5450 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005451 clear_tv(&var1);
5452 if (range)
5453 clear_tv(&var2);
5454 return FAIL;
5455 }
5456 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005457 }
5458
5459 if (evaluate)
5460 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005461 n1 = 0;
5462 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005463 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005464 n1 = get_tv_number(&var1);
5465 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005466 }
5467 if (range)
5468 {
5469 if (empty2)
5470 n2 = -1;
5471 else
5472 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005473 n2 = get_tv_number(&var2);
5474 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005475 }
5476 }
5477
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005478 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005479 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005480 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005481 case VAR_FUNC:
5482 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005483 case VAR_SPECIAL:
5484 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005485 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005486 break; /* not evaluating, skipping over subscript */
5487
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005488 case VAR_NUMBER:
5489 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005490 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005491 len = (long)STRLEN(s);
5492 if (range)
5493 {
5494 /* The resulting variable is a substring. If the indexes
5495 * are out of range the result is empty. */
5496 if (n1 < 0)
5497 {
5498 n1 = len + n1;
5499 if (n1 < 0)
5500 n1 = 0;
5501 }
5502 if (n2 < 0)
5503 n2 = len + n2;
5504 else if (n2 >= len)
5505 n2 = len;
5506 if (n1 >= len || n2 < 0 || n1 > n2)
5507 s = NULL;
5508 else
5509 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5510 }
5511 else
5512 {
5513 /* The resulting variable is a string of a single
5514 * character. If the index is too big or negative the
5515 * result is empty. */
5516 if (n1 >= len || n1 < 0)
5517 s = NULL;
5518 else
5519 s = vim_strnsave(s + n1, 1);
5520 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005521 clear_tv(rettv);
5522 rettv->v_type = VAR_STRING;
5523 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005524 break;
5525
5526 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005527 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005528 if (n1 < 0)
5529 n1 = len + n1;
5530 if (!empty1 && (n1 < 0 || n1 >= len))
5531 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005532 /* For a range we allow invalid values and return an empty
5533 * list. A list index out of range is an error. */
5534 if (!range)
5535 {
5536 if (verbose)
5537 EMSGN(_(e_listidx), n1);
5538 return FAIL;
5539 }
5540 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005541 }
5542 if (range)
5543 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005544 list_T *l;
5545 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005546
5547 if (n2 < 0)
5548 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005549 else if (n2 >= len)
5550 n2 = len - 1;
5551 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005552 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005553 l = list_alloc();
5554 if (l == NULL)
5555 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005556 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005557 n1 <= n2; ++n1)
5558 {
5559 if (list_append_tv(l, &item->li_tv) == FAIL)
5560 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005561 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005562 return FAIL;
5563 }
5564 item = item->li_next;
5565 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005566 clear_tv(rettv);
5567 rettv->v_type = VAR_LIST;
5568 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005569 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005570 }
5571 else
5572 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005573 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005574 clear_tv(rettv);
5575 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005576 }
5577 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005578
5579 case VAR_DICT:
5580 if (range)
5581 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005582 if (verbose)
5583 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005584 if (len == -1)
5585 clear_tv(&var1);
5586 return FAIL;
5587 }
5588 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005589 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005590
5591 if (len == -1)
5592 {
5593 key = get_tv_string(&var1);
5594 if (*key == NUL)
5595 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005596 if (verbose)
5597 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005598 clear_tv(&var1);
5599 return FAIL;
5600 }
5601 }
5602
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005603 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005604
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005605 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005606 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005607 if (len == -1)
5608 clear_tv(&var1);
5609 if (item == NULL)
5610 return FAIL;
5611
5612 copy_tv(&item->di_tv, &var1);
5613 clear_tv(rettv);
5614 *rettv = var1;
5615 }
5616 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005617 }
5618 }
5619
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005620 return OK;
5621}
5622
5623/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 * Get an option value.
5625 * "arg" points to the '&' or '+' before the option name.
5626 * "arg" is advanced to character after the option name.
5627 * Return OK or FAIL.
5628 */
5629 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005630get_option_tv(
5631 char_u **arg,
5632 typval_T *rettv, /* when NULL, only check if option exists */
5633 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634{
5635 char_u *option_end;
5636 long numval;
5637 char_u *stringval;
5638 int opt_type;
5639 int c;
5640 int working = (**arg == '+'); /* has("+option") */
5641 int ret = OK;
5642 int opt_flags;
5643
5644 /*
5645 * Isolate the option name and find its value.
5646 */
5647 option_end = find_option_end(arg, &opt_flags);
5648 if (option_end == NULL)
5649 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005650 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 EMSG2(_("E112: Option name missing: %s"), *arg);
5652 return FAIL;
5653 }
5654
5655 if (!evaluate)
5656 {
5657 *arg = option_end;
5658 return OK;
5659 }
5660
5661 c = *option_end;
5662 *option_end = NUL;
5663 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005664 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665
5666 if (opt_type == -3) /* invalid name */
5667 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005668 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 EMSG2(_("E113: Unknown option: %s"), *arg);
5670 ret = FAIL;
5671 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005672 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 {
5674 if (opt_type == -2) /* hidden string option */
5675 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005676 rettv->v_type = VAR_STRING;
5677 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 }
5679 else if (opt_type == -1) /* hidden number option */
5680 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005681 rettv->v_type = VAR_NUMBER;
5682 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 }
5684 else if (opt_type == 1) /* number option */
5685 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005686 rettv->v_type = VAR_NUMBER;
5687 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 }
5689 else /* string option */
5690 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005691 rettv->v_type = VAR_STRING;
5692 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 }
5694 }
5695 else if (working && (opt_type == -2 || opt_type == -1))
5696 ret = FAIL;
5697
5698 *option_end = c; /* put back for error messages */
5699 *arg = option_end;
5700
5701 return ret;
5702}
5703
5704/*
5705 * Allocate a variable for a string constant.
5706 * Return OK or FAIL.
5707 */
5708 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005709get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710{
5711 char_u *p;
5712 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 int extra = 0;
5714
5715 /*
5716 * Find the end of the string, skipping backslashed characters.
5717 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005718 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 {
5720 if (*p == '\\' && p[1] != NUL)
5721 {
5722 ++p;
5723 /* A "\<x>" form occupies at least 4 characters, and produces up
5724 * to 6 characters: reserve space for 2 extra */
5725 if (*p == '<')
5726 extra += 2;
5727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 }
5729
5730 if (*p != '"')
5731 {
5732 EMSG2(_("E114: Missing quote: %s"), *arg);
5733 return FAIL;
5734 }
5735
5736 /* If only parsing, set *arg and return here */
5737 if (!evaluate)
5738 {
5739 *arg = p + 1;
5740 return OK;
5741 }
5742
5743 /*
5744 * Copy the string into allocated memory, handling backslashed
5745 * characters.
5746 */
5747 name = alloc((unsigned)(p - *arg + extra));
5748 if (name == NULL)
5749 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005750 rettv->v_type = VAR_STRING;
5751 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752
Bram Moolenaar8c711452005-01-14 21:53:12 +00005753 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 {
5755 if (*p == '\\')
5756 {
5757 switch (*++p)
5758 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005759 case 'b': *name++ = BS; ++p; break;
5760 case 'e': *name++ = ESC; ++p; break;
5761 case 'f': *name++ = FF; ++p; break;
5762 case 'n': *name++ = NL; ++p; break;
5763 case 'r': *name++ = CAR; ++p; break;
5764 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765
5766 case 'X': /* hex: "\x1", "\x12" */
5767 case 'x':
5768 case 'u': /* Unicode: "\u0023" */
5769 case 'U':
5770 if (vim_isxdigit(p[1]))
5771 {
5772 int n, nr;
5773 int c = toupper(*p);
5774
5775 if (c == 'X')
5776 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005777 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005779 else
5780 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 nr = 0;
5782 while (--n >= 0 && vim_isxdigit(p[1]))
5783 {
5784 ++p;
5785 nr = (nr << 4) + hex2nr(*p);
5786 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005787 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788#ifdef FEAT_MBYTE
5789 /* For "\u" store the number according to
5790 * 'encoding'. */
5791 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005792 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 else
5794#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005795 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 break;
5798
5799 /* octal: "\1", "\12", "\123" */
5800 case '0':
5801 case '1':
5802 case '2':
5803 case '3':
5804 case '4':
5805 case '5':
5806 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005807 case '7': *name = *p++ - '0';
5808 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005810 *name = (*name << 3) + *p++ - '0';
5811 if (*p >= '0' && *p <= '7')
5812 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005814 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 break;
5816
5817 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005818 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 if (extra != 0)
5820 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005821 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 break;
5823 }
5824 /* FALLTHROUGH */
5825
Bram Moolenaar8c711452005-01-14 21:53:12 +00005826 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 break;
5828 }
5829 }
5830 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005831 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005834 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 *arg = p + 1;
5836
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 return OK;
5838}
5839
5840/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005841 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 * Return OK or FAIL.
5843 */
5844 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005845get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846{
5847 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005848 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005849 int reduce = 0;
5850
5851 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005852 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005853 */
5854 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5855 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005856 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005857 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005858 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005859 break;
5860 ++reduce;
5861 ++p;
5862 }
5863 }
5864
Bram Moolenaar8c711452005-01-14 21:53:12 +00005865 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005866 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005867 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005868 return FAIL;
5869 }
5870
Bram Moolenaar8c711452005-01-14 21:53:12 +00005871 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005872 if (!evaluate)
5873 {
5874 *arg = p + 1;
5875 return OK;
5876 }
5877
5878 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005879 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005880 */
5881 str = alloc((unsigned)((p - *arg) - reduce));
5882 if (str == NULL)
5883 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005884 rettv->v_type = VAR_STRING;
5885 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005886
Bram Moolenaar8c711452005-01-14 21:53:12 +00005887 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005888 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005889 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005890 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005891 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005892 break;
5893 ++p;
5894 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005895 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005896 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005897 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005898 *arg = p + 1;
5899
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005900 return OK;
5901}
5902
5903/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005904 * Allocate a variable for a List and fill it from "*arg".
5905 * Return OK or FAIL.
5906 */
5907 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005908get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005909{
Bram Moolenaar33570922005-01-25 22:26:29 +00005910 list_T *l = NULL;
5911 typval_T tv;
5912 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005913
5914 if (evaluate)
5915 {
5916 l = list_alloc();
5917 if (l == NULL)
5918 return FAIL;
5919 }
5920
5921 *arg = skipwhite(*arg + 1);
5922 while (**arg != ']' && **arg != NUL)
5923 {
5924 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5925 goto failret;
5926 if (evaluate)
5927 {
5928 item = listitem_alloc();
5929 if (item != NULL)
5930 {
5931 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005932 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005933 list_append(l, item);
5934 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005935 else
5936 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005937 }
5938
5939 if (**arg == ']')
5940 break;
5941 if (**arg != ',')
5942 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005943 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005944 goto failret;
5945 }
5946 *arg = skipwhite(*arg + 1);
5947 }
5948
5949 if (**arg != ']')
5950 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005951 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005952failret:
5953 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005954 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005955 return FAIL;
5956 }
5957
5958 *arg = skipwhite(*arg + 1);
5959 if (evaluate)
5960 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005961 rettv->v_type = VAR_LIST;
5962 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005963 ++l->lv_refcount;
5964 }
5965
5966 return OK;
5967}
5968
5969/*
5970 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005971 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005972 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005973 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005974list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005975{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005976 list_T *l;
5977
5978 l = (list_T *)alloc_clear(sizeof(list_T));
5979 if (l != NULL)
5980 {
5981 /* Prepend the list to the list of lists for garbage collection. */
5982 if (first_list != NULL)
5983 first_list->lv_used_prev = l;
5984 l->lv_used_prev = NULL;
5985 l->lv_used_next = first_list;
5986 first_list = l;
5987 }
5988 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005989}
5990
5991/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005992 * Allocate an empty list for a return value.
5993 * Returns OK or FAIL.
5994 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005995 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005996rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005997{
5998 list_T *l = list_alloc();
5999
6000 if (l == NULL)
6001 return FAIL;
6002
6003 rettv->vval.v_list = l;
6004 rettv->v_type = VAR_LIST;
6005 ++l->lv_refcount;
6006 return OK;
6007}
6008
6009/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006010 * Unreference a list: decrement the reference count and free it when it
6011 * becomes zero.
6012 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006013 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006014list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006015{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006016 if (l != NULL && --l->lv_refcount <= 0)
6017 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006018}
6019
6020/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006021 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006022 * Ignores the reference count.
6023 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006024 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006025list_free(
6026 list_T *l,
6027 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006028{
Bram Moolenaar33570922005-01-25 22:26:29 +00006029 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006030
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006031 /* Remove the list from the list of lists for garbage collection. */
6032 if (l->lv_used_prev == NULL)
6033 first_list = l->lv_used_next;
6034 else
6035 l->lv_used_prev->lv_used_next = l->lv_used_next;
6036 if (l->lv_used_next != NULL)
6037 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6038
Bram Moolenaard9fba312005-06-26 22:34:35 +00006039 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006040 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006041 /* Remove the item before deleting it. */
6042 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006043 if (recurse || (item->li_tv.v_type != VAR_LIST
6044 && item->li_tv.v_type != VAR_DICT))
6045 clear_tv(&item->li_tv);
6046 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006047 }
6048 vim_free(l);
6049}
6050
6051/*
6052 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006053 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006054 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006055 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006056listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006057{
Bram Moolenaar33570922005-01-25 22:26:29 +00006058 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006059}
6060
6061/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006062 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006063 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006064 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006065listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006066{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006067 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006068 vim_free(item);
6069}
6070
6071/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006072 * Remove a list item from a List and free it. Also clears the value.
6073 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006074 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006075listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006076{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006077 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006078 listitem_free(item);
6079}
6080
6081/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006082 * Get the number of items in a list.
6083 */
6084 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006085list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006086{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006087 if (l == NULL)
6088 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006089 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006090}
6091
6092/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006093 * Return TRUE when two lists have exactly the same values.
6094 */
6095 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006096list_equal(
6097 list_T *l1,
6098 list_T *l2,
6099 int ic, /* ignore case for strings */
6100 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006101{
Bram Moolenaar33570922005-01-25 22:26:29 +00006102 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006103
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006104 if (l1 == NULL || l2 == NULL)
6105 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006106 if (l1 == l2)
6107 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006108 if (list_len(l1) != list_len(l2))
6109 return FALSE;
6110
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006111 for (item1 = l1->lv_first, item2 = l2->lv_first;
6112 item1 != NULL && item2 != NULL;
6113 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006114 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006115 return FALSE;
6116 return item1 == NULL && item2 == NULL;
6117}
6118
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006119/*
6120 * Return the dictitem that an entry in a hashtable points to.
6121 */
6122 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006123dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006124{
6125 return HI2DI(hi);
6126}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006127
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006128/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006129 * Return TRUE when two dictionaries have exactly the same key/values.
6130 */
6131 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006132dict_equal(
6133 dict_T *d1,
6134 dict_T *d2,
6135 int ic, /* ignore case for strings */
6136 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006137{
Bram Moolenaar33570922005-01-25 22:26:29 +00006138 hashitem_T *hi;
6139 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006140 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006141
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006142 if (d1 == NULL || d2 == NULL)
6143 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006144 if (d1 == d2)
6145 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006146 if (dict_len(d1) != dict_len(d2))
6147 return FALSE;
6148
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006149 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006150 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006151 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006152 if (!HASHITEM_EMPTY(hi))
6153 {
6154 item2 = dict_find(d2, hi->hi_key, -1);
6155 if (item2 == NULL)
6156 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006157 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006158 return FALSE;
6159 --todo;
6160 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006161 }
6162 return TRUE;
6163}
6164
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006165static int tv_equal_recurse_limit;
6166
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006167/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006168 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006169 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006170 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006171 */
6172 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006173tv_equal(
6174 typval_T *tv1,
6175 typval_T *tv2,
6176 int ic, /* ignore case */
6177 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006178{
6179 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006180 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006181 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006182 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006183
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006184 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006185 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006186
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006187 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006188 * recursiveness to a limit. We guess they are equal then.
6189 * A fixed limit has the problem of still taking an awful long time.
6190 * Reduce the limit every time running into it. That should work fine for
6191 * deeply linked structures that are not recursively linked and catch
6192 * recursiveness quickly. */
6193 if (!recursive)
6194 tv_equal_recurse_limit = 1000;
6195 if (recursive_cnt >= tv_equal_recurse_limit)
6196 {
6197 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006198 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006199 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006200
6201 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006202 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006203 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006204 ++recursive_cnt;
6205 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6206 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006207 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006208
6209 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006210 ++recursive_cnt;
6211 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6212 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006213 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006214
6215 case VAR_FUNC:
6216 return (tv1->vval.v_string != NULL
6217 && tv2->vval.v_string != NULL
6218 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6219
6220 case VAR_NUMBER:
6221 return tv1->vval.v_number == tv2->vval.v_number;
6222
6223 case VAR_STRING:
6224 s1 = get_tv_string_buf(tv1, buf1);
6225 s2 = get_tv_string_buf(tv2, buf2);
6226 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006227
6228 case VAR_SPECIAL:
6229 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006230
6231 case VAR_FLOAT:
6232#ifdef FEAT_FLOAT
6233 return tv1->vval.v_float == tv2->vval.v_float;
6234#endif
6235 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006236#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006237 return tv1->vval.v_job == tv2->vval.v_job;
6238#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006239 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006240#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006241 return tv1->vval.v_channel == tv2->vval.v_channel;
6242#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006243 case VAR_UNKNOWN:
6244 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006245 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006246
Bram Moolenaara03f2332016-02-06 18:09:59 +01006247 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6248 * does not equal anything, not even itself. */
6249 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006250}
6251
6252/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006253 * Locate item with index "n" in list "l" and return it.
6254 * A negative index is counted from the end; -1 is the last item.
6255 * Returns NULL when "n" is out of range.
6256 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006257 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006258list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006259{
Bram Moolenaar33570922005-01-25 22:26:29 +00006260 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006261 long idx;
6262
6263 if (l == NULL)
6264 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006265
6266 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006267 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006268 n = l->lv_len + n;
6269
6270 /* Check for index out of range. */
6271 if (n < 0 || n >= l->lv_len)
6272 return NULL;
6273
6274 /* When there is a cached index may start search from there. */
6275 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006276 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006277 if (n < l->lv_idx / 2)
6278 {
6279 /* closest to the start of the list */
6280 item = l->lv_first;
6281 idx = 0;
6282 }
6283 else if (n > (l->lv_idx + l->lv_len) / 2)
6284 {
6285 /* closest to the end of the list */
6286 item = l->lv_last;
6287 idx = l->lv_len - 1;
6288 }
6289 else
6290 {
6291 /* closest to the cached index */
6292 item = l->lv_idx_item;
6293 idx = l->lv_idx;
6294 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006295 }
6296 else
6297 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006298 if (n < l->lv_len / 2)
6299 {
6300 /* closest to the start of the list */
6301 item = l->lv_first;
6302 idx = 0;
6303 }
6304 else
6305 {
6306 /* closest to the end of the list */
6307 item = l->lv_last;
6308 idx = l->lv_len - 1;
6309 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006310 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006311
6312 while (n > idx)
6313 {
6314 /* search forward */
6315 item = item->li_next;
6316 ++idx;
6317 }
6318 while (n < idx)
6319 {
6320 /* search backward */
6321 item = item->li_prev;
6322 --idx;
6323 }
6324
6325 /* cache the used index */
6326 l->lv_idx = idx;
6327 l->lv_idx_item = item;
6328
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006329 return item;
6330}
6331
6332/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006333 * Get list item "l[idx]" as a number.
6334 */
6335 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006336list_find_nr(
6337 list_T *l,
6338 long idx,
6339 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006340{
6341 listitem_T *li;
6342
6343 li = list_find(l, idx);
6344 if (li == NULL)
6345 {
6346 if (errorp != NULL)
6347 *errorp = TRUE;
6348 return -1L;
6349 }
6350 return get_tv_number_chk(&li->li_tv, errorp);
6351}
6352
6353/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006354 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6355 */
6356 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006357list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006358{
6359 listitem_T *li;
6360
6361 li = list_find(l, idx - 1);
6362 if (li == NULL)
6363 {
6364 EMSGN(_(e_listidx), idx);
6365 return NULL;
6366 }
6367 return get_tv_string(&li->li_tv);
6368}
6369
6370/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006371 * Locate "item" list "l" and return its index.
6372 * Returns -1 when "item" is not in the list.
6373 */
6374 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006375list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006376{
6377 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006378 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006379
6380 if (l == NULL)
6381 return -1;
6382 idx = 0;
6383 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6384 ++idx;
6385 if (li == NULL)
6386 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006387 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006388}
6389
6390/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006391 * Append item "item" to the end of list "l".
6392 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006393 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006394list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006395{
6396 if (l->lv_last == NULL)
6397 {
6398 /* empty list */
6399 l->lv_first = item;
6400 l->lv_last = item;
6401 item->li_prev = NULL;
6402 }
6403 else
6404 {
6405 l->lv_last->li_next = item;
6406 item->li_prev = l->lv_last;
6407 l->lv_last = item;
6408 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006409 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006410 item->li_next = NULL;
6411}
6412
6413/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006414 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006415 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006416 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006417 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006418list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006419{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006420 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006421
Bram Moolenaar05159a02005-02-26 23:04:13 +00006422 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006423 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006424 copy_tv(tv, &li->li_tv);
6425 list_append(l, li);
6426 return OK;
6427}
6428
6429/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006430 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006431 * Return FAIL when out of memory.
6432 */
6433 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006434list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006435{
6436 listitem_T *li = listitem_alloc();
6437
6438 if (li == NULL)
6439 return FAIL;
6440 li->li_tv.v_type = VAR_DICT;
6441 li->li_tv.v_lock = 0;
6442 li->li_tv.vval.v_dict = dict;
6443 list_append(list, li);
6444 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006445 return OK;
6446}
6447
6448/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006449 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006450 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006451 * Returns FAIL when out of memory.
6452 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006453 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006454list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006455{
6456 listitem_T *li = listitem_alloc();
6457
6458 if (li == NULL)
6459 return FAIL;
6460 list_append(l, li);
6461 li->li_tv.v_type = VAR_STRING;
6462 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006463 if (str == NULL)
6464 li->li_tv.vval.v_string = NULL;
6465 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006466 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006467 return FAIL;
6468 return OK;
6469}
6470
6471/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006472 * Append "n" to list "l".
6473 * Returns FAIL when out of memory.
6474 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006475 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006476list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006477{
6478 listitem_T *li;
6479
6480 li = listitem_alloc();
6481 if (li == NULL)
6482 return FAIL;
6483 li->li_tv.v_type = VAR_NUMBER;
6484 li->li_tv.v_lock = 0;
6485 li->li_tv.vval.v_number = n;
6486 list_append(l, li);
6487 return OK;
6488}
6489
6490/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006491 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006492 * If "item" is NULL append at the end.
6493 * Return FAIL when out of memory.
6494 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006495 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006496list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006497{
Bram Moolenaar33570922005-01-25 22:26:29 +00006498 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006499
6500 if (ni == NULL)
6501 return FAIL;
6502 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006503 list_insert(l, ni, item);
6504 return OK;
6505}
6506
6507 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006508list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006509{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006510 if (item == NULL)
6511 /* Append new item at end of list. */
6512 list_append(l, ni);
6513 else
6514 {
6515 /* Insert new item before existing item. */
6516 ni->li_prev = item->li_prev;
6517 ni->li_next = item;
6518 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006519 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006520 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006521 ++l->lv_idx;
6522 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006523 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006524 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006525 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006526 l->lv_idx_item = NULL;
6527 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006528 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006529 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006530 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006531}
6532
6533/*
6534 * Extend "l1" with "l2".
6535 * If "bef" is NULL append at the end, otherwise insert before this item.
6536 * Returns FAIL when out of memory.
6537 */
6538 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006539list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006540{
Bram Moolenaar33570922005-01-25 22:26:29 +00006541 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006542 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006543
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006544 /* We also quit the loop when we have inserted the original item count of
6545 * the list, avoid a hang when we extend a list with itself. */
6546 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006547 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6548 return FAIL;
6549 return OK;
6550}
6551
6552/*
6553 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6554 * Return FAIL when out of memory.
6555 */
6556 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006557list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006558{
Bram Moolenaar33570922005-01-25 22:26:29 +00006559 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006560
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006561 if (l1 == NULL || l2 == NULL)
6562 return FAIL;
6563
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006564 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006565 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006566 if (l == NULL)
6567 return FAIL;
6568 tv->v_type = VAR_LIST;
6569 tv->vval.v_list = l;
6570
6571 /* append all items from the second list */
6572 return list_extend(l, l2, NULL);
6573}
6574
6575/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006576 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006577 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006578 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006579 * Returns NULL when out of memory.
6580 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006581 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006582list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006583{
Bram Moolenaar33570922005-01-25 22:26:29 +00006584 list_T *copy;
6585 listitem_T *item;
6586 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006587
6588 if (orig == NULL)
6589 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006590
6591 copy = list_alloc();
6592 if (copy != NULL)
6593 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006594 if (copyID != 0)
6595 {
6596 /* Do this before adding the items, because one of the items may
6597 * refer back to this list. */
6598 orig->lv_copyID = copyID;
6599 orig->lv_copylist = copy;
6600 }
6601 for (item = orig->lv_first; item != NULL && !got_int;
6602 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006603 {
6604 ni = listitem_alloc();
6605 if (ni == NULL)
6606 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006607 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006608 {
6609 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6610 {
6611 vim_free(ni);
6612 break;
6613 }
6614 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006615 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006616 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006617 list_append(copy, ni);
6618 }
6619 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006620 if (item != NULL)
6621 {
6622 list_unref(copy);
6623 copy = NULL;
6624 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006625 }
6626
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006627 return copy;
6628}
6629
6630/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006631 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006632 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006633 * This used to be called list_remove, but that conflicts with a Sun header
6634 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006635 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006636 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006637vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006638{
Bram Moolenaar33570922005-01-25 22:26:29 +00006639 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006640
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006641 /* notify watchers */
6642 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006643 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006644 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006645 list_fix_watch(l, ip);
6646 if (ip == item2)
6647 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006648 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006649
6650 if (item2->li_next == NULL)
6651 l->lv_last = item->li_prev;
6652 else
6653 item2->li_next->li_prev = item->li_prev;
6654 if (item->li_prev == NULL)
6655 l->lv_first = item2->li_next;
6656 else
6657 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006658 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006659}
6660
6661/*
6662 * Return an allocated string with the string representation of a list.
6663 * May return NULL.
6664 */
6665 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006666list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006667{
6668 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006669
6670 if (tv->vval.v_list == NULL)
6671 return NULL;
6672 ga_init2(&ga, (int)sizeof(char), 80);
6673 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006674 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006675 {
6676 vim_free(ga.ga_data);
6677 return NULL;
6678 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006679 ga_append(&ga, ']');
6680 ga_append(&ga, NUL);
6681 return (char_u *)ga.ga_data;
6682}
6683
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006684typedef struct join_S {
6685 char_u *s;
6686 char_u *tofree;
6687} join_T;
6688
6689 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006690list_join_inner(
6691 garray_T *gap, /* to store the result in */
6692 list_T *l,
6693 char_u *sep,
6694 int echo_style,
6695 int copyID,
6696 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006697{
6698 int i;
6699 join_T *p;
6700 int len;
6701 int sumlen = 0;
6702 int first = TRUE;
6703 char_u *tofree;
6704 char_u numbuf[NUMBUFLEN];
6705 listitem_T *item;
6706 char_u *s;
6707
6708 /* Stringify each item in the list. */
6709 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6710 {
6711 if (echo_style)
6712 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6713 else
6714 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6715 if (s == NULL)
6716 return FAIL;
6717
6718 len = (int)STRLEN(s);
6719 sumlen += len;
6720
Bram Moolenaarcde88542015-08-11 19:14:00 +02006721 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006722 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6723 if (tofree != NULL || s != numbuf)
6724 {
6725 p->s = s;
6726 p->tofree = tofree;
6727 }
6728 else
6729 {
6730 p->s = vim_strnsave(s, len);
6731 p->tofree = p->s;
6732 }
6733
6734 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006735 if (did_echo_string_emsg) /* recursion error, bail out */
6736 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006737 }
6738
6739 /* Allocate result buffer with its total size, avoid re-allocation and
6740 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6741 if (join_gap->ga_len >= 2)
6742 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6743 if (ga_grow(gap, sumlen + 2) == FAIL)
6744 return FAIL;
6745
6746 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6747 {
6748 if (first)
6749 first = FALSE;
6750 else
6751 ga_concat(gap, sep);
6752 p = ((join_T *)join_gap->ga_data) + i;
6753
6754 if (p->s != NULL)
6755 ga_concat(gap, p->s);
6756 line_breakcheck();
6757 }
6758
6759 return OK;
6760}
6761
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006762/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006763 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006764 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006765 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006766 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006767 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006768list_join(
6769 garray_T *gap,
6770 list_T *l,
6771 char_u *sep,
6772 int echo_style,
6773 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006774{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006775 garray_T join_ga;
6776 int retval;
6777 join_T *p;
6778 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006779
Bram Moolenaard39a7512015-04-16 22:51:22 +02006780 if (l->lv_len < 1)
6781 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006782 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6783 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6784
6785 /* Dispose each item in join_ga. */
6786 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006787 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006788 p = (join_T *)join_ga.ga_data;
6789 for (i = 0; i < join_ga.ga_len; ++i)
6790 {
6791 vim_free(p->tofree);
6792 ++p;
6793 }
6794 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006795 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006796
6797 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006798}
6799
6800/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006801 * Return the next (unique) copy ID.
6802 * Used for serializing nested structures.
6803 */
6804 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006805get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006806{
6807 current_copyID += COPYID_INC;
6808 return current_copyID;
6809}
6810
6811/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006812 * Garbage collection for lists and dictionaries.
6813 *
6814 * We use reference counts to be able to free most items right away when they
6815 * are no longer used. But for composite items it's possible that it becomes
6816 * unused while the reference count is > 0: When there is a recursive
6817 * reference. Example:
6818 * :let l = [1, 2, 3]
6819 * :let d = {9: l}
6820 * :let l[1] = d
6821 *
6822 * Since this is quite unusual we handle this with garbage collection: every
6823 * once in a while find out which lists and dicts are not referenced from any
6824 * variable.
6825 *
6826 * Here is a good reference text about garbage collection (refers to Python
6827 * but it applies to all reference-counting mechanisms):
6828 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006829 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006830
6831/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006832 * Do garbage collection for lists and dicts.
6833 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006834 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006835 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006836garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006837{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006838 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006839 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006840 buf_T *buf;
6841 win_T *wp;
6842 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006843 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006844 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006845 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006846#ifdef FEAT_WINDOWS
6847 tabpage_T *tp;
6848#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006849
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006850 /* Only do this once. */
6851 want_garbage_collect = FALSE;
6852 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006853 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006854
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006855 /* We advance by two because we add one for items referenced through
6856 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006857 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006858
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006859 /*
6860 * 1. Go through all accessible variables and mark all lists and dicts
6861 * with copyID.
6862 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006863
6864 /* Don't free variables in the previous_funccal list unless they are only
6865 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006866 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006867 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6868 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006869 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6870 NULL);
6871 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6872 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006873 }
6874
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006875 /* script-local variables */
6876 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006877 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006878
6879 /* buffer-local variables */
6880 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006881 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6882 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006883
6884 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006885 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006886 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6887 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006888#ifdef FEAT_AUTOCMD
6889 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006890 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6891 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006892#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006893
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006894#ifdef FEAT_WINDOWS
6895 /* tabpage-local variables */
6896 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006897 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6898 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006899#endif
6900
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006901 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006902 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006903
6904 /* function-local variables */
6905 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6906 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006907 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6908 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006909 }
6910
Bram Moolenaard812df62008-11-09 12:46:09 +00006911 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006912 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006913
Bram Moolenaar1dced572012-04-05 16:54:08 +02006914#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006915 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006916#endif
6917
Bram Moolenaardb913952012-06-29 12:54:53 +02006918#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006919 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006920#endif
6921
6922#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006923 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006924#endif
6925
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006926#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006927 abort = abort || set_ref_in_channel(copyID);
6928#endif
6929
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006930 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006931 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006932 /*
6933 * 2. Free lists and dictionaries that are not referenced.
6934 */
6935 did_free = free_unref_items(copyID);
6936
6937 /*
6938 * 3. Check if any funccal can be freed now.
6939 */
6940 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006941 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006942 if (can_free_funccal(*pfc, copyID))
6943 {
6944 fc = *pfc;
6945 *pfc = fc->caller;
6946 free_funccal(fc, TRUE);
6947 did_free = TRUE;
6948 did_free_funccal = TRUE;
6949 }
6950 else
6951 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006952 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006953 if (did_free_funccal)
6954 /* When a funccal was freed some more items might be garbage
6955 * collected, so run again. */
6956 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006957 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006958 else if (p_verbose > 0)
6959 {
6960 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6961 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006962
6963 return did_free;
6964}
6965
6966/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01006967 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006968 */
6969 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006970free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006971{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006972 dict_T *dd, *dd_next;
6973 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006974 int did_free = FALSE;
6975
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006976 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006977 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006978 */
6979 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006980 {
6981 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006982 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006983 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006984 /* Free the Dictionary and ordinary items it contains, but don't
6985 * recurse into Lists and Dictionaries, they will be in the list
6986 * of dicts or list of lists. */
6987 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006988 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006989 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006990 dd = dd_next;
6991 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006992
6993 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006994 * Go through the list of lists and free items without the copyID.
6995 * But don't free a list that has a watcher (used in a for loop), these
6996 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006997 */
6998 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006999 {
7000 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007001 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7002 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007003 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007004 /* Free the List and ordinary items it contains, but don't recurse
7005 * into Lists and Dictionaries, they will be in the list of dicts
7006 * or list of lists. */
7007 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007008 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007009 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007010 ll = ll_next;
7011 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007012
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007013 return did_free;
7014}
7015
7016/*
7017 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007018 * "list_stack" is used to add lists to be marked. Can be NULL.
7019 *
7020 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007021 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007022 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007023set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007024{
7025 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007026 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007027 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007028 hashtab_T *cur_ht;
7029 ht_stack_T *ht_stack = NULL;
7030 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007031
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007032 cur_ht = ht;
7033 for (;;)
7034 {
7035 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007036 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007037 /* Mark each item in the hashtab. If the item contains a hashtab
7038 * it is added to ht_stack, if it contains a list it is added to
7039 * list_stack. */
7040 todo = (int)cur_ht->ht_used;
7041 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7042 if (!HASHITEM_EMPTY(hi))
7043 {
7044 --todo;
7045 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7046 &ht_stack, list_stack);
7047 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007048 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007049
7050 if (ht_stack == NULL)
7051 break;
7052
7053 /* take an item from the stack */
7054 cur_ht = ht_stack->ht;
7055 tempitem = ht_stack;
7056 ht_stack = ht_stack->prev;
7057 free(tempitem);
7058 }
7059
7060 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007061}
7062
7063/*
7064 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007065 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7066 *
7067 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007068 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007069 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007070set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007071{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007072 listitem_T *li;
7073 int abort = FALSE;
7074 list_T *cur_l;
7075 list_stack_T *list_stack = NULL;
7076 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007077
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007078 cur_l = l;
7079 for (;;)
7080 {
7081 if (!abort)
7082 /* Mark each item in the list. If the item contains a hashtab
7083 * it is added to ht_stack, if it contains a list it is added to
7084 * list_stack. */
7085 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7086 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7087 ht_stack, &list_stack);
7088 if (list_stack == NULL)
7089 break;
7090
7091 /* take an item from the stack */
7092 cur_l = list_stack->list;
7093 tempitem = list_stack;
7094 list_stack = list_stack->prev;
7095 free(tempitem);
7096 }
7097
7098 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007099}
7100
7101/*
7102 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007103 * "list_stack" is used to add lists to be marked. Can be NULL.
7104 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7105 *
7106 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007107 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007108 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007109set_ref_in_item(
7110 typval_T *tv,
7111 int copyID,
7112 ht_stack_T **ht_stack,
7113 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007114{
7115 dict_T *dd;
7116 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007117 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007118
Bram Moolenaara03f2332016-02-06 18:09:59 +01007119 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007120 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007121 dd = tv->vval.v_dict;
7122 if (dd != NULL && dd->dv_copyID != copyID)
7123 {
7124 /* Didn't see this dict yet. */
7125 dd->dv_copyID = copyID;
7126 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007127 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007128 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7129 }
7130 else
7131 {
7132 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7133 if (newitem == NULL)
7134 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007135 else
7136 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007137 newitem->ht = &dd->dv_hashtab;
7138 newitem->prev = *ht_stack;
7139 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007140 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007141 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007142 }
7143 }
7144 else if (tv->v_type == VAR_LIST)
7145 {
7146 ll = tv->vval.v_list;
7147 if (ll != NULL && ll->lv_copyID != copyID)
7148 {
7149 /* Didn't see this list yet. */
7150 ll->lv_copyID = copyID;
7151 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007152 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007153 abort = set_ref_in_list(ll, copyID, ht_stack);
7154 }
7155 else
7156 {
7157 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007158 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007159 if (newitem == NULL)
7160 abort = TRUE;
7161 else
7162 {
7163 newitem->list = ll;
7164 newitem->prev = *list_stack;
7165 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007166 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007167 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007168 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007169 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007170 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007171}
7172
7173/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007174 * Allocate an empty header for a dictionary.
7175 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007176 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007177dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007178{
Bram Moolenaar33570922005-01-25 22:26:29 +00007179 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007180
Bram Moolenaar33570922005-01-25 22:26:29 +00007181 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007182 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007183 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007184 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007185 if (first_dict != NULL)
7186 first_dict->dv_used_prev = d;
7187 d->dv_used_next = first_dict;
7188 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007189 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007190
Bram Moolenaar33570922005-01-25 22:26:29 +00007191 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007192 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007193 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007194 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007195 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007196 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007197 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007198}
7199
7200/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007201 * Allocate an empty dict for a return value.
7202 * Returns OK or FAIL.
7203 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007204 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007205rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007206{
7207 dict_T *d = dict_alloc();
7208
7209 if (d == NULL)
7210 return FAIL;
7211
7212 rettv->vval.v_dict = d;
7213 rettv->v_type = VAR_DICT;
7214 ++d->dv_refcount;
7215 return OK;
7216}
7217
7218
7219/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007220 * Unreference a Dictionary: decrement the reference count and free it when it
7221 * becomes zero.
7222 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007223 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007224dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007225{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007226 if (d != NULL && --d->dv_refcount <= 0)
7227 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007228}
7229
7230/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007231 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007232 * Ignores the reference count.
7233 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007234 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007235dict_free(
7236 dict_T *d,
7237 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007238{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007239 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007240 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007241 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007242
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007243 /* Remove the dict from the list of dicts for garbage collection. */
7244 if (d->dv_used_prev == NULL)
7245 first_dict = d->dv_used_next;
7246 else
7247 d->dv_used_prev->dv_used_next = d->dv_used_next;
7248 if (d->dv_used_next != NULL)
7249 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7250
7251 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007252 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007253 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007254 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007255 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007256 if (!HASHITEM_EMPTY(hi))
7257 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007258 /* Remove the item before deleting it, just in case there is
7259 * something recursive causing trouble. */
7260 di = HI2DI(hi);
7261 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007262 if (recurse || (di->di_tv.v_type != VAR_LIST
7263 && di->di_tv.v_type != VAR_DICT))
7264 clear_tv(&di->di_tv);
7265 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007266 --todo;
7267 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007268 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007269 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007270 vim_free(d);
7271}
7272
7273/*
7274 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007275 * The "key" is copied to the new item.
7276 * Note that the value of the item "di_tv" still needs to be initialized!
7277 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007278 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007279 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007280dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007281{
Bram Moolenaar33570922005-01-25 22:26:29 +00007282 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007283
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007284 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007285 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007286 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007287 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007288 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007289 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007290 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007291}
7292
7293/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007294 * Make a copy of a Dictionary item.
7295 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007296 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007297dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007298{
Bram Moolenaar33570922005-01-25 22:26:29 +00007299 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007300
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007301 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7302 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007303 if (di != NULL)
7304 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007305 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007306 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007307 copy_tv(&org->di_tv, &di->di_tv);
7308 }
7309 return di;
7310}
7311
7312/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007313 * Remove item "item" from Dictionary "dict" and free it.
7314 */
7315 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007316dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007317{
Bram Moolenaar33570922005-01-25 22:26:29 +00007318 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007319
Bram Moolenaar33570922005-01-25 22:26:29 +00007320 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007321 if (HASHITEM_EMPTY(hi))
7322 EMSG2(_(e_intern2), "dictitem_remove()");
7323 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007324 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007325 dictitem_free(item);
7326}
7327
7328/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007329 * Free a dict item. Also clears the value.
7330 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007331 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007332dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007333{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007334 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007335 if (item->di_flags & DI_FLAGS_ALLOC)
7336 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007337}
7338
7339/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007340 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7341 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007342 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007343 * Returns NULL when out of memory.
7344 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007345 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007346dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007347{
Bram Moolenaar33570922005-01-25 22:26:29 +00007348 dict_T *copy;
7349 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007350 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007351 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007352
7353 if (orig == NULL)
7354 return NULL;
7355
7356 copy = dict_alloc();
7357 if (copy != NULL)
7358 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007359 if (copyID != 0)
7360 {
7361 orig->dv_copyID = copyID;
7362 orig->dv_copydict = copy;
7363 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007364 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007365 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007366 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007367 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007368 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007369 --todo;
7370
7371 di = dictitem_alloc(hi->hi_key);
7372 if (di == NULL)
7373 break;
7374 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007375 {
7376 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7377 copyID) == FAIL)
7378 {
7379 vim_free(di);
7380 break;
7381 }
7382 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007383 else
7384 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7385 if (dict_add(copy, di) == FAIL)
7386 {
7387 dictitem_free(di);
7388 break;
7389 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007390 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007391 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007392
Bram Moolenaare9a41262005-01-15 22:18:47 +00007393 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007394 if (todo > 0)
7395 {
7396 dict_unref(copy);
7397 copy = NULL;
7398 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007399 }
7400
7401 return copy;
7402}
7403
7404/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007405 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007406 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007407 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007408 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007409dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007410{
Bram Moolenaar33570922005-01-25 22:26:29 +00007411 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007412}
7413
Bram Moolenaar8c711452005-01-14 21:53:12 +00007414/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007415 * Add a number or string entry to dictionary "d".
7416 * When "str" is NULL use number "nr", otherwise use "str".
7417 * Returns FAIL when out of memory and when key already exists.
7418 */
7419 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007420dict_add_nr_str(
7421 dict_T *d,
7422 char *key,
7423 long nr,
7424 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007425{
7426 dictitem_T *item;
7427
7428 item = dictitem_alloc((char_u *)key);
7429 if (item == NULL)
7430 return FAIL;
7431 item->di_tv.v_lock = 0;
7432 if (str == NULL)
7433 {
7434 item->di_tv.v_type = VAR_NUMBER;
7435 item->di_tv.vval.v_number = nr;
7436 }
7437 else
7438 {
7439 item->di_tv.v_type = VAR_STRING;
7440 item->di_tv.vval.v_string = vim_strsave(str);
7441 }
7442 if (dict_add(d, item) == FAIL)
7443 {
7444 dictitem_free(item);
7445 return FAIL;
7446 }
7447 return OK;
7448}
7449
7450/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007451 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007452 * Returns FAIL when out of memory and when key already exists.
7453 */
7454 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007455dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007456{
7457 dictitem_T *item;
7458
7459 item = dictitem_alloc((char_u *)key);
7460 if (item == NULL)
7461 return FAIL;
7462 item->di_tv.v_lock = 0;
7463 item->di_tv.v_type = VAR_LIST;
7464 item->di_tv.vval.v_list = list;
7465 if (dict_add(d, item) == FAIL)
7466 {
7467 dictitem_free(item);
7468 return FAIL;
7469 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007470 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007471 return OK;
7472}
7473
7474/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007475 * Get the number of items in a Dictionary.
7476 */
7477 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007478dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007479{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007480 if (d == NULL)
7481 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007482 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007483}
7484
7485/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007486 * Find item "key[len]" in Dictionary "d".
7487 * If "len" is negative use strlen(key).
7488 * Returns NULL when not found.
7489 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007490 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007491dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007492{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007493#define AKEYLEN 200
7494 char_u buf[AKEYLEN];
7495 char_u *akey;
7496 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007497 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007498
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007499 if (len < 0)
7500 akey = key;
7501 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007502 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007503 tofree = akey = vim_strnsave(key, len);
7504 if (akey == NULL)
7505 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007506 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007507 else
7508 {
7509 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007510 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007511 akey = buf;
7512 }
7513
Bram Moolenaar33570922005-01-25 22:26:29 +00007514 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007515 vim_free(tofree);
7516 if (HASHITEM_EMPTY(hi))
7517 return NULL;
7518 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007519}
7520
7521/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007522 * Get a string item from a dictionary.
7523 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007524 * Returns NULL if the entry doesn't exist or out of memory.
7525 */
7526 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007527get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007528{
7529 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007530 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007531
7532 di = dict_find(d, key, -1);
7533 if (di == NULL)
7534 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007535 s = get_tv_string(&di->di_tv);
7536 if (save && s != NULL)
7537 s = vim_strsave(s);
7538 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007539}
7540
7541/*
7542 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007543 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007544 */
7545 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007546get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007547{
7548 dictitem_T *di;
7549
7550 di = dict_find(d, key, -1);
7551 if (di == NULL)
7552 return 0;
7553 return get_tv_number(&di->di_tv);
7554}
7555
7556/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007557 * Return an allocated string with the string representation of a Dictionary.
7558 * May return NULL.
7559 */
7560 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007561dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007562{
7563 garray_T ga;
7564 int first = TRUE;
7565 char_u *tofree;
7566 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007567 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007568 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007569 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007570 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007571
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007572 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007573 return NULL;
7574 ga_init2(&ga, (int)sizeof(char), 80);
7575 ga_append(&ga, '{');
7576
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007577 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007578 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007579 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007580 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007581 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007582 --todo;
7583
7584 if (first)
7585 first = FALSE;
7586 else
7587 ga_concat(&ga, (char_u *)", ");
7588
7589 tofree = string_quote(hi->hi_key, FALSE);
7590 if (tofree != NULL)
7591 {
7592 ga_concat(&ga, tofree);
7593 vim_free(tofree);
7594 }
7595 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007596 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007597 if (s != NULL)
7598 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007599 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007600 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007601 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007602 line_breakcheck();
7603
Bram Moolenaar8c711452005-01-14 21:53:12 +00007604 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007605 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007606 if (todo > 0)
7607 {
7608 vim_free(ga.ga_data);
7609 return NULL;
7610 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007611
7612 ga_append(&ga, '}');
7613 ga_append(&ga, NUL);
7614 return (char_u *)ga.ga_data;
7615}
7616
7617/*
7618 * Allocate a variable for a Dictionary and fill it from "*arg".
7619 * Return OK or FAIL. Returns NOTDONE for {expr}.
7620 */
7621 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007622get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007623{
Bram Moolenaar33570922005-01-25 22:26:29 +00007624 dict_T *d = NULL;
7625 typval_T tvkey;
7626 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007627 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007628 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007629 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007630 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007631
7632 /*
7633 * First check if it's not a curly-braces thing: {expr}.
7634 * Must do this without evaluating, otherwise a function may be called
7635 * twice. Unfortunately this means we need to call eval1() twice for the
7636 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007637 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007638 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007639 if (*start != '}')
7640 {
7641 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7642 return FAIL;
7643 if (*start == '}')
7644 return NOTDONE;
7645 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007646
7647 if (evaluate)
7648 {
7649 d = dict_alloc();
7650 if (d == NULL)
7651 return FAIL;
7652 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007653 tvkey.v_type = VAR_UNKNOWN;
7654 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007655
7656 *arg = skipwhite(*arg + 1);
7657 while (**arg != '}' && **arg != NUL)
7658 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007659 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007660 goto failret;
7661 if (**arg != ':')
7662 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007663 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007664 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007665 goto failret;
7666 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007667 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007668 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007669 key = get_tv_string_buf_chk(&tvkey, buf);
7670 if (key == NULL || *key == NUL)
7671 {
7672 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7673 if (key != NULL)
7674 EMSG(_(e_emptykey));
7675 clear_tv(&tvkey);
7676 goto failret;
7677 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007678 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007679
7680 *arg = skipwhite(*arg + 1);
7681 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7682 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007683 if (evaluate)
7684 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007685 goto failret;
7686 }
7687 if (evaluate)
7688 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007689 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007690 if (item != NULL)
7691 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007692 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007693 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007694 clear_tv(&tv);
7695 goto failret;
7696 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007697 item = dictitem_alloc(key);
7698 clear_tv(&tvkey);
7699 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007700 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007701 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007702 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007703 if (dict_add(d, item) == FAIL)
7704 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007705 }
7706 }
7707
7708 if (**arg == '}')
7709 break;
7710 if (**arg != ',')
7711 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007712 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007713 goto failret;
7714 }
7715 *arg = skipwhite(*arg + 1);
7716 }
7717
7718 if (**arg != '}')
7719 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007720 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007721failret:
7722 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007723 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007724 return FAIL;
7725 }
7726
7727 *arg = skipwhite(*arg + 1);
7728 if (evaluate)
7729 {
7730 rettv->v_type = VAR_DICT;
7731 rettv->vval.v_dict = d;
7732 ++d->dv_refcount;
7733 }
7734
7735 return OK;
7736}
7737
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007738#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01007739#endif
7740
Bram Moolenaar17a13432016-01-24 14:22:10 +01007741 static char *
7742get_var_special_name(int nr)
7743{
7744 switch (nr)
7745 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007746 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01007747 case VVAL_TRUE: return "v:true";
7748 case VVAL_NONE: return "v:none";
7749 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007750 }
7751 EMSG2(_(e_intern2), "get_var_special_name()");
7752 return "42";
7753}
7754
Bram Moolenaar8c711452005-01-14 21:53:12 +00007755/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007756 * Return a string with the string representation of a variable.
7757 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007758 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007759 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007760 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007761 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007762 */
7763 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007764echo_string(
7765 typval_T *tv,
7766 char_u **tofree,
7767 char_u *numbuf,
7768 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007769{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007770 static int recurse = 0;
7771 char_u *r = NULL;
7772
Bram Moolenaar33570922005-01-25 22:26:29 +00007773 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007774 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007775 if (!did_echo_string_emsg)
7776 {
7777 /* Only give this message once for a recursive call to avoid
7778 * flooding the user with errors. And stop iterating over lists
7779 * and dicts. */
7780 did_echo_string_emsg = TRUE;
7781 EMSG(_("E724: variable nested too deep for displaying"));
7782 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007783 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007784 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007785 }
7786 ++recurse;
7787
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007788 switch (tv->v_type)
7789 {
7790 case VAR_FUNC:
7791 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007792 r = tv->vval.v_string;
7793 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007794
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007795 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007796 if (tv->vval.v_list == NULL)
7797 {
7798 *tofree = NULL;
7799 r = NULL;
7800 }
7801 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7802 {
7803 *tofree = NULL;
7804 r = (char_u *)"[...]";
7805 }
7806 else
7807 {
7808 tv->vval.v_list->lv_copyID = copyID;
7809 *tofree = list2string(tv, copyID);
7810 r = *tofree;
7811 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007812 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007813
Bram Moolenaar8c711452005-01-14 21:53:12 +00007814 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007815 if (tv->vval.v_dict == NULL)
7816 {
7817 *tofree = NULL;
7818 r = NULL;
7819 }
7820 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7821 {
7822 *tofree = NULL;
7823 r = (char_u *)"{...}";
7824 }
7825 else
7826 {
7827 tv->vval.v_dict->dv_copyID = copyID;
7828 *tofree = dict2string(tv, copyID);
7829 r = *tofree;
7830 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007831 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007832
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007833 case VAR_STRING:
7834 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007835 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007836 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007837 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007838 *tofree = NULL;
7839 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007840 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007841
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007842 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007843#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007844 *tofree = NULL;
7845 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7846 r = numbuf;
7847 break;
7848#endif
7849
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007850 case VAR_SPECIAL:
7851 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007852 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007853 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007854 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007855
Bram Moolenaar8502c702014-06-17 12:51:16 +02007856 if (--recurse == 0)
7857 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007858 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007859}
7860
7861/*
7862 * Return a string with the string representation of a variable.
7863 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7864 * "numbuf" is used for a number.
7865 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007866 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007867 */
7868 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007869tv2string(
7870 typval_T *tv,
7871 char_u **tofree,
7872 char_u *numbuf,
7873 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007874{
7875 switch (tv->v_type)
7876 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007877 case VAR_FUNC:
7878 *tofree = string_quote(tv->vval.v_string, TRUE);
7879 return *tofree;
7880 case VAR_STRING:
7881 *tofree = string_quote(tv->vval.v_string, FALSE);
7882 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007883 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01007884#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007885 *tofree = NULL;
7886 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7887 return numbuf;
7888#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007889 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007890 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007891 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007892 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007893 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007894 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007895 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007896 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007897 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007898 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007899}
7900
7901/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007902 * Return string "str" in ' quotes, doubling ' characters.
7903 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007904 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007905 */
7906 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007907string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007908{
Bram Moolenaar33570922005-01-25 22:26:29 +00007909 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007910 char_u *p, *r, *s;
7911
Bram Moolenaar33570922005-01-25 22:26:29 +00007912 len = (function ? 13 : 3);
7913 if (str != NULL)
7914 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007915 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007916 for (p = str; *p != NUL; mb_ptr_adv(p))
7917 if (*p == '\'')
7918 ++len;
7919 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007920 s = r = alloc(len);
7921 if (r != NULL)
7922 {
7923 if (function)
7924 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007925 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007926 r += 10;
7927 }
7928 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007929 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007930 if (str != NULL)
7931 for (p = str; *p != NUL; )
7932 {
7933 if (*p == '\'')
7934 *r++ = '\'';
7935 MB_COPY_CHAR(p, r);
7936 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007937 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007938 if (function)
7939 *r++ = ')';
7940 *r++ = NUL;
7941 }
7942 return s;
7943}
7944
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007945#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007946/*
7947 * Convert the string "text" to a floating point number.
7948 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7949 * this always uses a decimal point.
7950 * Returns the length of the text that was consumed.
7951 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007952 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007953string2float(
7954 char_u *text,
7955 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007956{
7957 char *s = (char *)text;
7958 float_T f;
7959
7960 f = strtod(s, &s);
7961 *value = f;
7962 return (int)((char_u *)s - text);
7963}
7964#endif
7965
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007966/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 * Get the value of an environment variable.
7968 * "arg" is pointing to the '$'. It is advanced to after the name.
7969 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007970 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 */
7972 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007973get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974{
7975 char_u *string = NULL;
7976 int len;
7977 int cc;
7978 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007979 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980
7981 ++*arg;
7982 name = *arg;
7983 len = get_env_len(arg);
7984 if (evaluate)
7985 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007986 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01007987 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007988
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007989 cc = name[len];
7990 name[len] = NUL;
7991 /* first try vim_getenv(), fast for normal environment vars */
7992 string = vim_getenv(name, &mustfree);
7993 if (string != NULL && *string != NUL)
7994 {
7995 if (!mustfree)
7996 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007998 else
7999 {
8000 if (mustfree)
8001 vim_free(string);
8002
8003 /* next try expanding things like $VIM and ${HOME} */
8004 string = expand_env_save(name - 1);
8005 if (string != NULL && *string == '$')
8006 {
8007 vim_free(string);
8008 string = NULL;
8009 }
8010 }
8011 name[len] = cc;
8012
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008013 rettv->v_type = VAR_STRING;
8014 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 }
8016
8017 return OK;
8018}
8019
8020/*
8021 * Array with names and number of arguments of all internal functions
8022 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8023 */
8024static struct fst
8025{
8026 char *f_name; /* function name */
8027 char f_min_argc; /* minimal number of arguments */
8028 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008029 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008030 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031} functions[] =
8032{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008033#ifdef FEAT_FLOAT
8034 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008035 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008036#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008037 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008038 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008039 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 {"append", 2, 2, f_append},
8041 {"argc", 0, 0, f_argc},
8042 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008043 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008044 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008045#ifdef FEAT_FLOAT
8046 {"asin", 1, 1, f_asin}, /* WJMc */
8047#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008048 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008049 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008050 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008051 {"assert_false", 1, 2, f_assert_false},
8052 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008053#ifdef FEAT_FLOAT
8054 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008055 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008056#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008058 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 {"bufexists", 1, 1, f_bufexists},
8060 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8061 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8062 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8063 {"buflisted", 1, 1, f_buflisted},
8064 {"bufloaded", 1, 1, f_bufloaded},
8065 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008066 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 {"bufwinnr", 1, 1, f_bufwinnr},
8068 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008069 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008070 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008071 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008072#ifdef FEAT_FLOAT
8073 {"ceil", 1, 1, f_ceil},
8074#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008075#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008076 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008077 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8078 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008079 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008080 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008081 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008082 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008083 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008084 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008085 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008086 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8087 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008088 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008089 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008090#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008091 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008092 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008094 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008096#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008097 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008098 {"complete_add", 1, 1, f_complete_add},
8099 {"complete_check", 0, 0, f_complete_check},
8100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008102 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008103#ifdef FEAT_FLOAT
8104 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008105 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008106#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008107 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008109 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008110 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008111 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008113 {"diff_filler", 1, 1, f_diff_filler},
8114 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008115 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008116 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008118 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 {"eventhandler", 0, 0, f_eventhandler},
8120 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008121 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008123#ifdef FEAT_FLOAT
8124 {"exp", 1, 1, f_exp},
8125#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008126 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008127 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008128 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8130 {"filereadable", 1, 1, f_filereadable},
8131 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008132 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008133 {"finddir", 1, 3, f_finddir},
8134 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008135#ifdef FEAT_FLOAT
8136 {"float2nr", 1, 1, f_float2nr},
8137 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008138 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008139#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008140 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 {"fnamemodify", 2, 2, f_fnamemodify},
8142 {"foldclosed", 1, 1, f_foldclosed},
8143 {"foldclosedend", 1, 1, f_foldclosedend},
8144 {"foldlevel", 1, 1, f_foldlevel},
8145 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008146 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008148 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008149 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008150 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008151 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008152 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 {"getchar", 0, 1, f_getchar},
8154 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008155 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 {"getcmdline", 0, 0, f_getcmdline},
8157 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008158 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008159 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008160 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008161 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008162 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008163 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 {"getfsize", 1, 1, f_getfsize},
8165 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008166 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008167 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008168 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008169 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008170 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008171 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008172 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008173 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008175 {"gettabvar", 2, 3, f_gettabvar},
8176 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 {"getwinposx", 0, 0, f_getwinposx},
8178 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008179 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008180 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008181 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008182 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008184 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008185 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008186 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8188 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8189 {"histadd", 2, 2, f_histadd},
8190 {"histdel", 1, 2, f_histdel},
8191 {"histget", 1, 2, f_histget},
8192 {"histnr", 1, 1, f_histnr},
8193 {"hlID", 1, 1, f_hlID},
8194 {"hlexists", 1, 1, f_hlexists},
8195 {"hostname", 0, 0, f_hostname},
8196 {"iconv", 3, 3, f_iconv},
8197 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008198 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008199 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008201 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 {"inputrestore", 0, 0, f_inputrestore},
8203 {"inputsave", 0, 0, f_inputsave},
8204 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008205 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008206 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008208 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008209#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8210 {"isnan", 1, 1, f_isnan},
8211#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008212 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008213#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008214 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008215 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008216 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008217 {"job_start", 1, 2, f_job_start},
8218 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008219 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008220#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008221 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008222 {"js_decode", 1, 1, f_js_decode},
8223 {"js_encode", 1, 1, f_js_encode},
8224 {"json_decode", 1, 1, f_json_decode},
8225 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008226 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008228 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 {"libcall", 3, 3, f_libcall},
8230 {"libcallnr", 3, 3, f_libcallnr},
8231 {"line", 1, 1, f_line},
8232 {"line2byte", 1, 1, f_line2byte},
8233 {"lispindent", 1, 1, f_lispindent},
8234 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008235#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008236 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008237 {"log10", 1, 1, f_log10},
8238#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008239#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008240 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008241#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008242 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008243 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008244 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008245 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008246 {"matchadd", 2, 5, f_matchadd},
8247 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008248 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008249 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008250 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008251 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008252 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008253 {"max", 1, 1, f_max},
8254 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008255#ifdef vim_mkdir
8256 {"mkdir", 1, 3, f_mkdir},
8257#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008258 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008259#ifdef FEAT_MZSCHEME
8260 {"mzeval", 1, 1, f_mzeval},
8261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008263 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008264 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008265 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008266#ifdef FEAT_PERL
8267 {"perleval", 1, 1, f_perleval},
8268#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008269#ifdef FEAT_FLOAT
8270 {"pow", 2, 2, f_pow},
8271#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008273 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008274 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008275#ifdef FEAT_PYTHON3
8276 {"py3eval", 1, 1, f_py3eval},
8277#endif
8278#ifdef FEAT_PYTHON
8279 {"pyeval", 1, 1, f_pyeval},
8280#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008281 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008282 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008283 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008284#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008285 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008286#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008287 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 {"remote_expr", 2, 3, f_remote_expr},
8289 {"remote_foreground", 1, 1, f_remote_foreground},
8290 {"remote_peek", 1, 2, f_remote_peek},
8291 {"remote_read", 1, 1, f_remote_read},
8292 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008293 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008295 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008297 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008298#ifdef FEAT_FLOAT
8299 {"round", 1, 1, f_round},
8300#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008301 {"screenattr", 2, 2, f_screenattr},
8302 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008303 {"screencol", 0, 0, f_screencol},
8304 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008305 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008306 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008307 {"searchpair", 3, 7, f_searchpair},
8308 {"searchpairpos", 3, 7, f_searchpairpos},
8309 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 {"server2client", 2, 2, f_server2client},
8311 {"serverlist", 0, 0, f_serverlist},
8312 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008313 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008315 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008317 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008318 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008319 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008320 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008322 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008323 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008325#ifdef FEAT_CRYPT
8326 {"sha256", 1, 1, f_sha256},
8327#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008328 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008329 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008331#ifdef FEAT_FLOAT
8332 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008333 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008334#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008335 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008336 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008337 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008338 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008339 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008340#ifdef FEAT_FLOAT
8341 {"sqrt", 1, 1, f_sqrt},
8342 {"str2float", 1, 1, f_str2float},
8343#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008344 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008345 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008346 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347#ifdef HAVE_STRFTIME
8348 {"strftime", 1, 2, f_strftime},
8349#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008350 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008351 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 {"strlen", 1, 1, f_strlen},
8353 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008354 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008356 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008357 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 {"substitute", 4, 4, f_substitute},
8359 {"synID", 3, 3, f_synID},
8360 {"synIDattr", 2, 3, f_synIDattr},
8361 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008362 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008363 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008364 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008365 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008366 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008367 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008368 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008369 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008370 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008371#ifdef FEAT_FLOAT
8372 {"tan", 1, 1, f_tan},
8373 {"tanh", 1, 1, f_tanh},
8374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008376 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 {"tolower", 1, 1, f_tolower},
8378 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008379 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008380#ifdef FEAT_FLOAT
8381 {"trunc", 1, 1, f_trunc},
8382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008384 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008385 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008386 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008387 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 {"virtcol", 1, 1, f_virtcol},
8389 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008390 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008391 {"win_getid", 0, 2, f_win_getid},
8392 {"win_gotoid", 1, 1, f_win_gotoid},
8393 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8394 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 {"winbufnr", 1, 1, f_winbufnr},
8396 {"wincol", 0, 0, f_wincol},
8397 {"winheight", 1, 1, f_winheight},
8398 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008399 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008401 {"winrestview", 1, 1, f_winrestview},
8402 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008404 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008405 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008406 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407};
8408
8409#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8410
8411/*
8412 * Function given to ExpandGeneric() to obtain the list of internal
8413 * or user defined function names.
8414 */
8415 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008416get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417{
8418 static int intidx = -1;
8419 char_u *name;
8420
8421 if (idx == 0)
8422 intidx = -1;
8423 if (intidx < 0)
8424 {
8425 name = get_user_func_name(xp, idx);
8426 if (name != NULL)
8427 return name;
8428 }
8429 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8430 {
8431 STRCPY(IObuff, functions[intidx].f_name);
8432 STRCAT(IObuff, "(");
8433 if (functions[intidx].f_max_argc == 0)
8434 STRCAT(IObuff, ")");
8435 return IObuff;
8436 }
8437
8438 return NULL;
8439}
8440
8441/*
8442 * Function given to ExpandGeneric() to obtain the list of internal or
8443 * user defined variable or function names.
8444 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008446get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447{
8448 static int intidx = -1;
8449 char_u *name;
8450
8451 if (idx == 0)
8452 intidx = -1;
8453 if (intidx < 0)
8454 {
8455 name = get_function_name(xp, idx);
8456 if (name != NULL)
8457 return name;
8458 }
8459 return get_user_var_name(xp, ++intidx);
8460}
8461
8462#endif /* FEAT_CMDL_COMPL */
8463
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008464#if defined(EBCDIC) || defined(PROTO)
8465/*
8466 * Compare struct fst by function name.
8467 */
8468 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008469compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008470{
8471 struct fst *p1 = (struct fst *)s1;
8472 struct fst *p2 = (struct fst *)s2;
8473
8474 return STRCMP(p1->f_name, p2->f_name);
8475}
8476
8477/*
8478 * Sort the function table by function name.
8479 * The sorting of the table above is ASCII dependant.
8480 * On machines using EBCDIC we have to sort it.
8481 */
8482 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008483sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008484{
8485 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8486
8487 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8488}
8489#endif
8490
8491
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492/*
8493 * Find internal function in table above.
8494 * Return index, or -1 if not found
8495 */
8496 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008497find_internal_func(
8498 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499{
8500 int first = 0;
8501 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8502 int cmp;
8503 int x;
8504
8505 /*
8506 * Find the function name in the table. Binary search.
8507 */
8508 while (first <= last)
8509 {
8510 x = first + ((unsigned)(last - first) >> 1);
8511 cmp = STRCMP(name, functions[x].f_name);
8512 if (cmp < 0)
8513 last = x - 1;
8514 else if (cmp > 0)
8515 first = x + 1;
8516 else
8517 return x;
8518 }
8519 return -1;
8520}
8521
8522/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008523 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8524 * name it contains, otherwise return "name".
8525 */
8526 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008527deref_func_name(char_u *name, int *lenp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008528{
Bram Moolenaar33570922005-01-25 22:26:29 +00008529 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008530 int cc;
8531
8532 cc = name[*lenp];
8533 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008534 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008535 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008536 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008537 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008538 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008539 {
8540 *lenp = 0;
8541 return (char_u *)""; /* just in case */
8542 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008543 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008544 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008545 }
8546
8547 return name;
8548}
8549
8550/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 * Allocate a variable for the result of a function.
8552 * Return OK or FAIL.
8553 */
8554 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008555get_func_tv(
8556 char_u *name, /* name of the function */
8557 int len, /* length of "name" */
8558 typval_T *rettv,
8559 char_u **arg, /* argument, pointing to the '(' */
8560 linenr_T firstline, /* first line of range */
8561 linenr_T lastline, /* last line of range */
8562 int *doesrange, /* return: function handled range */
8563 int evaluate,
8564 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565{
8566 char_u *argp;
8567 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008568 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 int argcount = 0; /* number of arguments found */
8570
8571 /*
8572 * Get the arguments.
8573 */
8574 argp = *arg;
8575 while (argcount < MAX_FUNC_ARGS)
8576 {
8577 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8578 if (*argp == ')' || *argp == ',' || *argp == NUL)
8579 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8581 {
8582 ret = FAIL;
8583 break;
8584 }
8585 ++argcount;
8586 if (*argp != ',')
8587 break;
8588 }
8589 if (*argp == ')')
8590 ++argp;
8591 else
8592 ret = FAIL;
8593
8594 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008595 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008596 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008598 {
8599 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008600 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008601 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008602 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604
8605 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008606 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607
8608 *arg = skipwhite(argp);
8609 return ret;
8610}
8611
8612
8613/*
8614 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008615 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008616 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008618 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008619call_func(
8620 char_u *funcname, /* name of the function */
8621 int len, /* length of "name" */
8622 typval_T *rettv, /* return value goes here */
8623 int argcount, /* number of "argvars" */
8624 typval_T *argvars, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008625 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008626 linenr_T firstline, /* first line of range */
8627 linenr_T lastline, /* last line of range */
8628 int *doesrange, /* return: function handled range */
8629 int evaluate,
8630 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631{
8632 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633#define ERROR_UNKNOWN 0
8634#define ERROR_TOOMANY 1
8635#define ERROR_TOOFEW 2
8636#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008637#define ERROR_DICT 4
8638#define ERROR_NONE 5
8639#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 int error = ERROR_NONE;
8641 int i;
8642 int llen;
8643 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644#define FLEN_FIXED 40
8645 char_u fname_buf[FLEN_FIXED + 1];
8646 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008647 char_u *name;
8648
8649 /* Make a copy of the name, if it comes from a funcref variable it could
8650 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008651 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008652 if (name == NULL)
8653 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654
8655 /*
8656 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8657 * Change <SNR>123_name() to K_SNR 123_name().
8658 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8659 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660 llen = eval_fname_script(name);
8661 if (llen > 0)
8662 {
8663 fname_buf[0] = K_SPECIAL;
8664 fname_buf[1] = KS_EXTRA;
8665 fname_buf[2] = (int)KE_SNR;
8666 i = 3;
8667 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8668 {
8669 if (current_SID <= 0)
8670 error = ERROR_SCRIPT;
8671 else
8672 {
8673 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8674 i = (int)STRLEN(fname_buf);
8675 }
8676 }
8677 if (i + STRLEN(name + llen) < FLEN_FIXED)
8678 {
8679 STRCPY(fname_buf + i, name + llen);
8680 fname = fname_buf;
8681 }
8682 else
8683 {
8684 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8685 if (fname == NULL)
8686 error = ERROR_OTHER;
8687 else
8688 {
8689 mch_memmove(fname, fname_buf, (size_t)i);
8690 STRCPY(fname + i, name + llen);
8691 }
8692 }
8693 }
8694 else
8695 fname = name;
8696
8697 *doesrange = FALSE;
8698
8699
8700 /* execute the function if no errors detected and executing */
8701 if (evaluate && error == ERROR_NONE)
8702 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008703 char_u *rfname = fname;
8704
8705 /* Ignore "g:" before a function name. */
8706 if (fname[0] == 'g' && fname[1] == ':')
8707 rfname = fname + 2;
8708
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008709 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8710 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 error = ERROR_UNKNOWN;
8712
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008713 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714 {
8715 /*
8716 * User defined function.
8717 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008718 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008719
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008721 /* Trigger FuncUndefined event, may load the function. */
8722 if (fp == NULL
8723 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008724 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008725 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008727 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008728 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 }
8730#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008731 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008732 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008733 {
8734 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008735 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008736 }
8737
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 if (fp != NULL)
8739 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008740 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008742 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008744 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008746 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008747 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 else
8749 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008750 int did_save_redo = FALSE;
8751
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 /*
8753 * Call the user function.
8754 * Save and restore search patterns, script variables and
8755 * redo buffer.
8756 */
8757 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008758#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008759 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008760#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008761 {
8762 saveRedobuff();
8763 did_save_redo = TRUE;
8764 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008765 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008766 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008767 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008768 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8769 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8770 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008771 /* Function was unreferenced while being used, free it
8772 * now. */
8773 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008774 if (did_save_redo)
8775 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 restore_search_patterns();
8777 error = ERROR_NONE;
8778 }
8779 }
8780 }
8781 else
8782 {
8783 /*
8784 * Find the function name in the table, call its implementation.
8785 */
8786 i = find_internal_func(fname);
8787 if (i >= 0)
8788 {
8789 if (argcount < functions[i].f_min_argc)
8790 error = ERROR_TOOFEW;
8791 else if (argcount > functions[i].f_max_argc)
8792 error = ERROR_TOOMANY;
8793 else
8794 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008795 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008796 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797 error = ERROR_NONE;
8798 }
8799 }
8800 }
8801 /*
8802 * The function call (or "FuncUndefined" autocommand sequence) might
8803 * have been aborted by an error, an interrupt, or an explicitly thrown
8804 * exception that has not been caught so far. This situation can be
8805 * tested for by calling aborting(). For an error in an internal
8806 * function or for the "E132" error in call_user_func(), however, the
8807 * throw point at which the "force_abort" flag (temporarily reset by
8808 * emsg()) is normally updated has not been reached yet. We need to
8809 * update that flag first to make aborting() reliable.
8810 */
8811 update_force_abort();
8812 }
8813 if (error == ERROR_NONE)
8814 ret = OK;
8815
8816 /*
8817 * Report an error unless the argument evaluation or function call has been
8818 * cancelled due to an aborting error, an interrupt, or an exception.
8819 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008820 if (!aborting())
8821 {
8822 switch (error)
8823 {
8824 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008825 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008826 break;
8827 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008828 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008829 break;
8830 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008831 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008832 name);
8833 break;
8834 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008835 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008836 name);
8837 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008838 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008839 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008840 name);
8841 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008842 }
8843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 if (fname != name && fname != fname_buf)
8846 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008847 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848
8849 return ret;
8850}
8851
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008852/*
8853 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008854 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008855 */
8856 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008857emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008858{
8859 char_u *p;
8860
8861 if (*name == K_SPECIAL)
8862 p = concat_str((char_u *)"<SNR>", name + 3);
8863 else
8864 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008865 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008866 if (p != name)
8867 vim_free(p);
8868}
8869
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008870/*
8871 * Return TRUE for a non-zero Number and a non-empty String.
8872 */
8873 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008874non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008875{
8876 return ((argvars[0].v_type == VAR_NUMBER
8877 && argvars[0].vval.v_number != 0)
8878 || (argvars[0].v_type == VAR_STRING
8879 && argvars[0].vval.v_string != NULL
8880 && *argvars[0].vval.v_string != NUL));
8881}
8882
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883/*********************************************
8884 * Implementation of the built-in functions
8885 */
8886
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008887#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008888static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008889
8890/*
8891 * Get the float value of "argvars[0]" into "f".
8892 * Returns FAIL when the argument is not a Number or Float.
8893 */
8894 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008895get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008896{
8897 if (argvars[0].v_type == VAR_FLOAT)
8898 {
8899 *f = argvars[0].vval.v_float;
8900 return OK;
8901 }
8902 if (argvars[0].v_type == VAR_NUMBER)
8903 {
8904 *f = (float_T)argvars[0].vval.v_number;
8905 return OK;
8906 }
8907 EMSG(_("E808: Number or Float required"));
8908 return FAIL;
8909}
8910
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008911/*
8912 * "abs(expr)" function
8913 */
8914 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008915f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008916{
8917 if (argvars[0].v_type == VAR_FLOAT)
8918 {
8919 rettv->v_type = VAR_FLOAT;
8920 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8921 }
8922 else
8923 {
8924 varnumber_T n;
8925 int error = FALSE;
8926
8927 n = get_tv_number_chk(&argvars[0], &error);
8928 if (error)
8929 rettv->vval.v_number = -1;
8930 else if (n > 0)
8931 rettv->vval.v_number = n;
8932 else
8933 rettv->vval.v_number = -n;
8934 }
8935}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008936
8937/*
8938 * "acos()" function
8939 */
8940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008941f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008942{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01008943 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008944
8945 rettv->v_type = VAR_FLOAT;
8946 if (get_float_arg(argvars, &f) == OK)
8947 rettv->vval.v_float = acos(f);
8948 else
8949 rettv->vval.v_float = 0.0;
8950}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008951#endif
8952
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008954 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 */
8956 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008957f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958{
Bram Moolenaar33570922005-01-25 22:26:29 +00008959 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008961 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008962 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008964 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02008965 && !tv_check_lock(l->lv_lock,
8966 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008967 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008968 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008969 }
8970 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008971 EMSG(_(e_listreq));
8972}
8973
8974/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008975 * "alloc_fail(id, countdown, repeat)" function
8976 */
8977 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008978f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008979{
8980 if (argvars[0].v_type != VAR_NUMBER
8981 || argvars[0].vval.v_number <= 0
8982 || argvars[1].v_type != VAR_NUMBER
8983 || argvars[1].vval.v_number < 0
8984 || argvars[2].v_type != VAR_NUMBER)
8985 EMSG(_(e_invarg));
8986 else
8987 {
8988 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01008989 if (alloc_fail_id >= aid_last)
8990 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008991 alloc_fail_countdown = argvars[1].vval.v_number;
8992 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01008993 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008994 }
8995}
8996
8997/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008998 * "and(expr, expr)" function
8999 */
9000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009001f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009002{
9003 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9004 & get_tv_number_chk(&argvars[1], NULL);
9005}
9006
9007/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009008 * "append(lnum, string/list)" function
9009 */
9010 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009011f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009012{
9013 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009014 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009015 list_T *l = NULL;
9016 listitem_T *li = NULL;
9017 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009018 long added = 0;
9019
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009020 /* When coming here from Insert mode, sync undo, so that this can be
9021 * undone separately from what was previously inserted. */
9022 if (u_sync_once == 2)
9023 {
9024 u_sync_once = 1; /* notify that u_sync() was called */
9025 u_sync(TRUE);
9026 }
9027
Bram Moolenaar0d660222005-01-07 21:51:51 +00009028 lnum = get_tv_lnum(argvars);
9029 if (lnum >= 0
9030 && lnum <= curbuf->b_ml.ml_line_count
9031 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009032 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009033 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009034 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009035 l = argvars[1].vval.v_list;
9036 if (l == NULL)
9037 return;
9038 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009039 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009040 for (;;)
9041 {
9042 if (l == NULL)
9043 tv = &argvars[1]; /* append a string */
9044 else if (li == NULL)
9045 break; /* end of list */
9046 else
9047 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009048 line = get_tv_string_chk(tv);
9049 if (line == NULL) /* type error */
9050 {
9051 rettv->vval.v_number = 1; /* Failed */
9052 break;
9053 }
9054 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009055 ++added;
9056 if (l == NULL)
9057 break;
9058 li = li->li_next;
9059 }
9060
9061 appended_lines_mark(lnum, added);
9062 if (curwin->w_cursor.lnum > lnum)
9063 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009065 else
9066 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067}
9068
9069/*
9070 * "argc()" function
9071 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009073f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009075 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076}
9077
9078/*
9079 * "argidx()" function
9080 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009082f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009084 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085}
9086
9087/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009088 * "arglistid()" function
9089 */
9090 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009091f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009092{
9093 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009094
9095 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009096 wp = find_tabwin(&argvars[0], &argvars[1]);
9097 if (wp != NULL)
9098 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009099}
9100
9101/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102 * "argv(nr)" function
9103 */
9104 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009105f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106{
9107 int idx;
9108
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009109 if (argvars[0].v_type != VAR_UNKNOWN)
9110 {
9111 idx = get_tv_number_chk(&argvars[0], NULL);
9112 if (idx >= 0 && idx < ARGCOUNT)
9113 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9114 else
9115 rettv->vval.v_string = NULL;
9116 rettv->v_type = VAR_STRING;
9117 }
9118 else if (rettv_list_alloc(rettv) == OK)
9119 for (idx = 0; idx < ARGCOUNT; ++idx)
9120 list_append_string(rettv->vval.v_list,
9121 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122}
9123
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009124static void prepare_assert_error(garray_T*gap);
9125static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9126static void assert_error(garray_T *gap);
9127static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009128
9129/*
9130 * Prepare "gap" for an assert error and add the sourcing position.
9131 */
9132 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009133prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009134{
9135 char buf[NUMBUFLEN];
9136
9137 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009138 if (sourcing_name != NULL)
9139 {
9140 ga_concat(gap, sourcing_name);
9141 if (sourcing_lnum > 0)
9142 ga_concat(gap, (char_u *)" ");
9143 }
9144 if (sourcing_lnum > 0)
9145 {
9146 sprintf(buf, "line %ld", (long)sourcing_lnum);
9147 ga_concat(gap, (char_u *)buf);
9148 }
9149 if (sourcing_name != NULL || sourcing_lnum > 0)
9150 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009151}
9152
9153/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009154 * Append "str" to "gap", escaping unprintable characters.
9155 * Changes NL to \n, CR to \r, etc.
9156 */
9157 static void
9158ga_concat_esc(garray_T *gap, char_u *str)
9159{
9160 char_u *p;
9161 char_u buf[NUMBUFLEN];
9162
9163 for (p = str; *p != NUL; ++p)
9164 switch (*p)
9165 {
9166 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9167 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9168 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9169 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9170 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9171 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9172 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9173 default:
9174 if (*p < ' ')
9175 {
9176 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9177 ga_concat(gap, buf);
9178 }
9179 else
9180 ga_append(gap, *p);
9181 break;
9182 }
9183}
9184
9185/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009186 * Fill "gap" with information about an assert error.
9187 */
9188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009189fill_assert_error(
9190 garray_T *gap,
9191 typval_T *opt_msg_tv,
9192 char_u *exp_str,
9193 typval_T *exp_tv,
9194 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009195{
9196 char_u numbuf[NUMBUFLEN];
9197 char_u *tofree;
9198
9199 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9200 {
9201 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9202 vim_free(tofree);
9203 }
9204 else
9205 {
9206 ga_concat(gap, (char_u *)"Expected ");
9207 if (exp_str == NULL)
9208 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009209 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009210 vim_free(tofree);
9211 }
9212 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009213 ga_concat_esc(gap, exp_str);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009214 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009215 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009216 vim_free(tofree);
9217 }
9218}
Bram Moolenaar43345542015-11-29 17:35:35 +01009219
9220/*
9221 * Add an assert error to v:errors.
9222 */
9223 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009224assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009225{
9226 struct vimvar *vp = &vimvars[VV_ERRORS];
9227
9228 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9229 /* Make sure v:errors is a list. */
9230 set_vim_var_list(VV_ERRORS, list_alloc());
9231 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9232}
9233
Bram Moolenaar43345542015-11-29 17:35:35 +01009234/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009235 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009236 */
9237 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009238f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009239{
9240 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009241
9242 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9243 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009244 prepare_assert_error(&ga);
9245 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9246 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009247 ga_clear(&ga);
9248 }
9249}
9250
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009251/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009252 * "assert_exception(string[, msg])" function
9253 */
9254 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009255f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009256{
9257 garray_T ga;
9258 char *error;
9259
9260 error = (char *)get_tv_string_chk(&argvars[0]);
9261 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9262 {
9263 prepare_assert_error(&ga);
9264 ga_concat(&ga, (char_u *)"v:exception is not set");
9265 assert_error(&ga);
9266 ga_clear(&ga);
9267 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009268 else if (error != NULL
9269 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009270 {
9271 prepare_assert_error(&ga);
9272 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9273 &vimvars[VV_EXCEPTION].vv_tv);
9274 assert_error(&ga);
9275 ga_clear(&ga);
9276 }
9277}
9278
9279/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009280 * "assert_fails(cmd [, error])" function
9281 */
9282 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009283f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009284{
9285 char_u *cmd = get_tv_string_chk(&argvars[0]);
9286 garray_T ga;
9287
9288 called_emsg = FALSE;
9289 suppress_errthrow = TRUE;
9290 emsg_silent = TRUE;
9291 do_cmdline_cmd(cmd);
9292 if (!called_emsg)
9293 {
9294 prepare_assert_error(&ga);
9295 ga_concat(&ga, (char_u *)"command did not fail: ");
9296 ga_concat(&ga, cmd);
9297 assert_error(&ga);
9298 ga_clear(&ga);
9299 }
9300 else if (argvars[1].v_type != VAR_UNKNOWN)
9301 {
9302 char_u buf[NUMBUFLEN];
9303 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9304
9305 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9306 {
9307 prepare_assert_error(&ga);
9308 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9309 &vimvars[VV_ERRMSG].vv_tv);
9310 assert_error(&ga);
9311 ga_clear(&ga);
9312 }
9313 }
9314
9315 called_emsg = FALSE;
9316 suppress_errthrow = FALSE;
9317 emsg_silent = FALSE;
9318 emsg_on_display = FALSE;
9319 set_vim_var_string(VV_ERRMSG, NULL, 0);
9320}
9321
9322/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009323 * Common for assert_true() and assert_false().
9324 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009325 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009326assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009327{
9328 int error = FALSE;
9329 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009330
Bram Moolenaar37127922016-02-06 20:29:28 +01009331 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009332 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009333 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009334 if (argvars[0].v_type != VAR_NUMBER
9335 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9336 || error)
9337 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009338 prepare_assert_error(&ga);
9339 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009340 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009341 NULL, &argvars[0]);
9342 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009343 ga_clear(&ga);
9344 }
9345}
9346
9347/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009348 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009349 */
9350 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009351f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009352{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009353 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009354}
9355
9356/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009357 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009358 */
9359 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009360f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009361{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009362 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009363}
9364
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009365#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009366/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009367 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009368 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009370f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009371{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009372 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009373
9374 rettv->v_type = VAR_FLOAT;
9375 if (get_float_arg(argvars, &f) == OK)
9376 rettv->vval.v_float = asin(f);
9377 else
9378 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009379}
9380
9381/*
9382 * "atan()" function
9383 */
9384 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009385f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009386{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009387 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009388
9389 rettv->v_type = VAR_FLOAT;
9390 if (get_float_arg(argvars, &f) == OK)
9391 rettv->vval.v_float = atan(f);
9392 else
9393 rettv->vval.v_float = 0.0;
9394}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009395
9396/*
9397 * "atan2()" function
9398 */
9399 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009400f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009401{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009402 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009403
9404 rettv->v_type = VAR_FLOAT;
9405 if (get_float_arg(argvars, &fx) == OK
9406 && get_float_arg(&argvars[1], &fy) == OK)
9407 rettv->vval.v_float = atan2(fx, fy);
9408 else
9409 rettv->vval.v_float = 0.0;
9410}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009411#endif
9412
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413/*
9414 * "browse(save, title, initdir, default)" function
9415 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009417f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418{
9419#ifdef FEAT_BROWSE
9420 int save;
9421 char_u *title;
9422 char_u *initdir;
9423 char_u *defname;
9424 char_u buf[NUMBUFLEN];
9425 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009426 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009428 save = get_tv_number_chk(&argvars[0], &error);
9429 title = get_tv_string_chk(&argvars[1]);
9430 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9431 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009433 if (error || title == NULL || initdir == NULL || defname == NULL)
9434 rettv->vval.v_string = NULL;
9435 else
9436 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009437 do_browse(save ? BROWSE_SAVE : 0,
9438 title, defname, NULL, initdir, NULL, curbuf);
9439#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009440 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009441#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009442 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009443}
9444
9445/*
9446 * "browsedir(title, initdir)" function
9447 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009448 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009449f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009450{
9451#ifdef FEAT_BROWSE
9452 char_u *title;
9453 char_u *initdir;
9454 char_u buf[NUMBUFLEN];
9455
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009456 title = get_tv_string_chk(&argvars[0]);
9457 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009458
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009459 if (title == NULL || initdir == NULL)
9460 rettv->vval.v_string = NULL;
9461 else
9462 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009463 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009465 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009467 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468}
9469
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009470static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009471
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472/*
9473 * Find a buffer by number or exact name.
9474 */
9475 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009476find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477{
9478 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009480 if (avar->v_type == VAR_NUMBER)
9481 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009482 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009484 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009485 if (buf == NULL)
9486 {
9487 /* No full path name match, try a match with a URL or a "nofile"
9488 * buffer, these don't use the full path. */
9489 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9490 if (buf->b_fname != NULL
9491 && (path_with_url(buf->b_fname)
9492#ifdef FEAT_QUICKFIX
9493 || bt_nofile(buf)
9494#endif
9495 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009496 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009497 break;
9498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499 }
9500 return buf;
9501}
9502
9503/*
9504 * "bufexists(expr)" function
9505 */
9506 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009507f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009509 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510}
9511
9512/*
9513 * "buflisted(expr)" function
9514 */
9515 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009516f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517{
9518 buf_T *buf;
9519
9520 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009521 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522}
9523
9524/*
9525 * "bufloaded(expr)" function
9526 */
9527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009528f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529{
9530 buf_T *buf;
9531
9532 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009533 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534}
9535
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01009536 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +01009537buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539 int save_magic;
9540 char_u *save_cpo;
9541 buf_T *buf;
9542
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9544 save_magic = p_magic;
9545 p_magic = TRUE;
9546 save_cpo = p_cpo;
9547 p_cpo = (char_u *)"";
9548
9549 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009550 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551
9552 p_magic = save_magic;
9553 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +01009554 return buf;
9555}
9556
9557/*
9558 * Get buffer by number or pattern.
9559 */
9560 static buf_T *
9561get_buf_tv(typval_T *tv, int curtab_only)
9562{
9563 char_u *name = tv->vval.v_string;
9564 buf_T *buf;
9565
9566 if (tv->v_type == VAR_NUMBER)
9567 return buflist_findnr((int)tv->vval.v_number);
9568 if (tv->v_type != VAR_STRING)
9569 return NULL;
9570 if (name == NULL || *name == NUL)
9571 return curbuf;
9572 if (name[0] == '$' && name[1] == NUL)
9573 return lastbuf;
9574
9575 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576
9577 /* If not found, try expanding the name, like done for bufexists(). */
9578 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009579 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580
9581 return buf;
9582}
9583
9584/*
9585 * "bufname(expr)" function
9586 */
9587 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009588f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589{
9590 buf_T *buf;
9591
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009592 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009594 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009595 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009597 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009599 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600 --emsg_off;
9601}
9602
9603/*
9604 * "bufnr(expr)" function
9605 */
9606 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009607f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608{
9609 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009610 int error = FALSE;
9611 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009613 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009615 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009616 --emsg_off;
9617
9618 /* If the buffer isn't found and the second argument is not zero create a
9619 * new buffer. */
9620 if (buf == NULL
9621 && argvars[1].v_type != VAR_UNKNOWN
9622 && get_tv_number_chk(&argvars[1], &error) != 0
9623 && !error
9624 && (name = get_tv_string_chk(&argvars[0])) != NULL
9625 && !error)
9626 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9627
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009629 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009631 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632}
9633
9634/*
9635 * "bufwinnr(nr)" function
9636 */
9637 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009638f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639{
9640#ifdef FEAT_WINDOWS
9641 win_T *wp;
9642 int winnr = 0;
9643#endif
9644 buf_T *buf;
9645
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009646 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009648 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649#ifdef FEAT_WINDOWS
9650 for (wp = firstwin; wp; wp = wp->w_next)
9651 {
9652 ++winnr;
9653 if (wp->w_buffer == buf)
9654 break;
9655 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009656 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009658 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659#endif
9660 --emsg_off;
9661}
9662
9663/*
9664 * "byte2line(byte)" function
9665 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009667f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668{
9669#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009670 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671#else
9672 long boff = 0;
9673
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009674 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009676 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009678 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679 (linenr_T)0, &boff);
9680#endif
9681}
9682
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009683 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009684byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009685{
9686#ifdef FEAT_MBYTE
9687 char_u *t;
9688#endif
9689 char_u *str;
9690 long idx;
9691
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009692 str = get_tv_string_chk(&argvars[0]);
9693 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009694 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009695 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009696 return;
9697
9698#ifdef FEAT_MBYTE
9699 t = str;
9700 for ( ; idx > 0; idx--)
9701 {
9702 if (*t == NUL) /* EOL reached */
9703 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009704 if (enc_utf8 && comp)
9705 t += utf_ptr2len(t);
9706 else
9707 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009708 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009709 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009710#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009711 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009712 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009713#endif
9714}
9715
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009716/*
9717 * "byteidx()" function
9718 */
9719 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009720f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009721{
9722 byteidx(argvars, rettv, FALSE);
9723}
9724
9725/*
9726 * "byteidxcomp()" function
9727 */
9728 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009729f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009730{
9731 byteidx(argvars, rettv, TRUE);
9732}
9733
Bram Moolenaardb913952012-06-29 12:54:53 +02009734 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009735func_call(
9736 char_u *name,
9737 typval_T *args,
9738 dict_T *selfdict,
9739 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009740{
9741 listitem_T *item;
9742 typval_T argv[MAX_FUNC_ARGS + 1];
9743 int argc = 0;
9744 int dummy;
9745 int r = 0;
9746
9747 for (item = args->vval.v_list->lv_first; item != NULL;
9748 item = item->li_next)
9749 {
9750 if (argc == MAX_FUNC_ARGS)
9751 {
9752 EMSG(_("E699: Too many arguments"));
9753 break;
9754 }
9755 /* Make a copy of each argument. This is needed to be able to set
9756 * v_lock to VAR_FIXED in the copy without changing the original list.
9757 */
9758 copy_tv(&item->li_tv, &argv[argc++]);
9759 }
9760
9761 if (item == NULL)
9762 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9763 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9764 &dummy, TRUE, selfdict);
9765
9766 /* Free the arguments. */
9767 while (argc > 0)
9768 clear_tv(&argv[--argc]);
9769
9770 return r;
9771}
9772
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009773/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009774 * "call(func, arglist)" function
9775 */
9776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009777f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009778{
9779 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009780 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009781
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009782 if (argvars[1].v_type != VAR_LIST)
9783 {
9784 EMSG(_(e_listreq));
9785 return;
9786 }
9787 if (argvars[1].vval.v_list == NULL)
9788 return;
9789
9790 if (argvars[0].v_type == VAR_FUNC)
9791 func = argvars[0].vval.v_string;
9792 else
9793 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009794 if (*func == NUL)
9795 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009796
Bram Moolenaare9a41262005-01-15 22:18:47 +00009797 if (argvars[2].v_type != VAR_UNKNOWN)
9798 {
9799 if (argvars[2].v_type != VAR_DICT)
9800 {
9801 EMSG(_(e_dictreq));
9802 return;
9803 }
9804 selfdict = argvars[2].vval.v_dict;
9805 }
9806
Bram Moolenaardb913952012-06-29 12:54:53 +02009807 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009808}
9809
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009810#ifdef FEAT_FLOAT
9811/*
9812 * "ceil({float})" function
9813 */
9814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009815f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009816{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009817 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009818
9819 rettv->v_type = VAR_FLOAT;
9820 if (get_float_arg(argvars, &f) == OK)
9821 rettv->vval.v_float = ceil(f);
9822 else
9823 rettv->vval.v_float = 0.0;
9824}
9825#endif
9826
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01009827#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009828/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009829 * "ch_close()" function
9830 */
9831 static void
9832f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
9833{
Bram Moolenaarf65333c2016-03-08 18:27:21 +01009834 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009835
9836 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +01009837 {
Bram Moolenaar8b374212016-02-24 20:43:06 +01009838 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +01009839 channel_clear(channel);
9840 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009841}
9842
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01009843/*
9844 * "ch_getbufnr()" function
9845 */
9846 static void
9847f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
9848{
Bram Moolenaarf65333c2016-03-08 18:27:21 +01009849 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01009850
9851 rettv->vval.v_number = -1;
9852 if (channel != NULL)
9853 {
9854 char_u *what = get_tv_string(&argvars[1]);
9855 int part;
9856
9857 if (STRCMP(what, "err") == 0)
9858 part = PART_ERR;
9859 else if (STRCMP(what, "out") == 0)
9860 part = PART_OUT;
9861 else if (STRCMP(what, "in") == 0)
9862 part = PART_IN;
9863 else
9864 part = PART_SOCK;
9865 if (channel->ch_part[part].ch_buffer != NULL)
9866 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
9867 }
9868}
9869
Bram Moolenaar02e83b42016-02-21 20:10:26 +01009870/*
9871 * "ch_getjob()" function
9872 */
9873 static void
9874f_ch_getjob(typval_T *argvars, typval_T *rettv)
9875{
Bram Moolenaarf65333c2016-03-08 18:27:21 +01009876 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar02e83b42016-02-21 20:10:26 +01009877
9878 if (channel != NULL)
9879 {
9880 rettv->v_type = VAR_JOB;
9881 rettv->vval.v_job = channel->ch_job;
9882 if (channel->ch_job != NULL)
9883 ++channel->ch_job->jv_refcount;
9884 }
9885}
Bram Moolenaar02e83b42016-02-21 20:10:26 +01009886
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009887/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +01009888 * "ch_log()" function
9889 */
9890 static void
9891f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
9892{
9893 char_u *msg = get_tv_string(&argvars[0]);
9894 channel_T *channel = NULL;
9895
9896 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf65333c2016-03-08 18:27:21 +01009897 channel = get_channel_arg(&argvars[1], TRUE);
Bram Moolenaar81661fb2016-02-18 22:23:34 +01009898
9899 ch_log(channel, (char *)msg);
9900}
9901
9902/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009903 * "ch_logfile()" function
9904 */
9905 static void
9906f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
9907{
9908 char_u *fname;
9909 char_u *opt = (char_u *)"";
9910 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009911
9912 fname = get_tv_string(&argvars[0]);
9913 if (argvars[1].v_type == VAR_STRING)
9914 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01009915 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009916}
Bram Moolenaarba093bc2016-02-16 19:37:29 +01009917
9918/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009919 * "ch_open()" function
9920 */
9921 static void
9922f_ch_open(typval_T *argvars, typval_T *rettv)
9923{
Bram Moolenaar77073442016-02-13 23:23:53 +01009924 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01009925 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +01009926}
9927
9928/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01009929 * "ch_read()" function
9930 */
9931 static void
9932f_ch_read(typval_T *argvars, typval_T *rettv)
9933{
9934 common_channel_read(argvars, rettv, FALSE);
9935}
9936
9937/*
9938 * "ch_readraw()" function
9939 */
9940 static void
9941f_ch_readraw(typval_T *argvars, typval_T *rettv)
9942{
9943 common_channel_read(argvars, rettv, TRUE);
9944}
9945
9946/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01009947 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009948 */
9949 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01009950f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
9951{
9952 ch_expr_common(argvars, rettv, TRUE);
9953}
9954
9955/*
9956 * "ch_sendexpr()" function
9957 */
9958 static void
9959f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
9960{
9961 ch_expr_common(argvars, rettv, FALSE);
9962}
9963
9964/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01009965 * "ch_evalraw()" function
9966 */
9967 static void
9968f_ch_evalraw(typval_T *argvars, typval_T *rettv)
9969{
9970 ch_raw_common(argvars, rettv, TRUE);
9971}
9972
9973/*
9974 * "ch_sendraw()" function
9975 */
9976 static void
9977f_ch_sendraw(typval_T *argvars, typval_T *rettv)
9978{
9979 ch_raw_common(argvars, rettv, FALSE);
9980}
9981
9982/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009983 * "ch_setoptions()" function
9984 */
9985 static void
9986f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
9987{
9988 channel_T *channel;
9989 jobopt_T opt;
9990
Bram Moolenaarf65333c2016-03-08 18:27:21 +01009991 channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009992 if (channel == NULL)
9993 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +01009994 clear_job_options(&opt);
9995 if (get_job_options(&argvars[1], &opt,
9996 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == FAIL)
Bram Moolenaar132006c2016-02-19 22:38:15 +01009997 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01009998 channel_set_options(channel, &opt);
9999}
10000
10001/*
10002 * "ch_status()" function
10003 */
10004 static void
10005f_ch_status(typval_T *argvars, typval_T *rettv)
10006{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010007 channel_T *channel;
10008
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010009 /* return an empty string by default */
10010 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010011 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010012
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010013 channel = get_channel_arg(&argvars[0], FALSE);
10014 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010015}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010016#endif
10017
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010018/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010019 * "changenr()" function
10020 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010021 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010022f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010023{
10024 rettv->vval.v_number = curbuf->b_u_seq_cur;
10025}
10026
10027/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028 * "char2nr(string)" function
10029 */
10030 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010031f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032{
10033#ifdef FEAT_MBYTE
10034 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010035 {
10036 int utf8 = 0;
10037
10038 if (argvars[1].v_type != VAR_UNKNOWN)
10039 utf8 = get_tv_number_chk(&argvars[1], NULL);
10040
10041 if (utf8)
10042 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10043 else
10044 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046 else
10047#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010048 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049}
10050
10051/*
10052 * "cindent(lnum)" function
10053 */
10054 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010055f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056{
10057#ifdef FEAT_CINDENT
10058 pos_T pos;
10059 linenr_T lnum;
10060
10061 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010062 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10064 {
10065 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010066 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067 curwin->w_cursor = pos;
10068 }
10069 else
10070#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010071 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072}
10073
10074/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010075 * "clearmatches()" function
10076 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010078f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010079{
10080#ifdef FEAT_SEARCH_EXTRA
10081 clear_matches(curwin);
10082#endif
10083}
10084
10085/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086 * "col(string)" function
10087 */
10088 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010089f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090{
10091 colnr_T col = 0;
10092 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010093 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010095 fp = var2fpos(&argvars[0], FALSE, &fnum);
10096 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097 {
10098 if (fp->col == MAXCOL)
10099 {
10100 /* '> can be MAXCOL, get the length of the line then */
10101 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010102 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103 else
10104 col = MAXCOL;
10105 }
10106 else
10107 {
10108 col = fp->col + 1;
10109#ifdef FEAT_VIRTUALEDIT
10110 /* col(".") when the cursor is on the NUL at the end of the line
10111 * because of "coladd" can be seen as an extra column. */
10112 if (virtual_active() && fp == &curwin->w_cursor)
10113 {
10114 char_u *p = ml_get_cursor();
10115
10116 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10117 curwin->w_virtcol - curwin->w_cursor.coladd))
10118 {
10119# ifdef FEAT_MBYTE
10120 int l;
10121
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010122 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123 col += l;
10124# else
10125 if (*p != NUL && p[1] == NUL)
10126 ++col;
10127# endif
10128 }
10129 }
10130#endif
10131 }
10132 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010133 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134}
10135
Bram Moolenaar572cb562005-08-05 21:35:02 +000010136#if defined(FEAT_INS_EXPAND)
10137/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010138 * "complete()" function
10139 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010141f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010142{
10143 int startcol;
10144
10145 if ((State & INSERT) == 0)
10146 {
10147 EMSG(_("E785: complete() can only be used in Insert mode"));
10148 return;
10149 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010150
10151 /* Check for undo allowed here, because if something was already inserted
10152 * the line was already saved for undo and this check isn't done. */
10153 if (!undo_allowed())
10154 return;
10155
Bram Moolenaarade00832006-03-10 21:46:58 +000010156 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10157 {
10158 EMSG(_(e_invarg));
10159 return;
10160 }
10161
10162 startcol = get_tv_number_chk(&argvars[0], NULL);
10163 if (startcol <= 0)
10164 return;
10165
10166 set_completion(startcol - 1, argvars[1].vval.v_list);
10167}
10168
10169/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010170 * "complete_add()" function
10171 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010172 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010173f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010174{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010175 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010176}
10177
10178/*
10179 * "complete_check()" function
10180 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010181 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010182f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010183{
10184 int saved = RedrawingDisabled;
10185
10186 RedrawingDisabled = 0;
10187 ins_compl_check_keys(0);
10188 rettv->vval.v_number = compl_interrupted;
10189 RedrawingDisabled = saved;
10190}
10191#endif
10192
Bram Moolenaar071d4272004-06-13 20:20:40 +000010193/*
10194 * "confirm(message, buttons[, default [, type]])" function
10195 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010197f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198{
10199#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10200 char_u *message;
10201 char_u *buttons = NULL;
10202 char_u buf[NUMBUFLEN];
10203 char_u buf2[NUMBUFLEN];
10204 int def = 1;
10205 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010206 char_u *typestr;
10207 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010209 message = get_tv_string_chk(&argvars[0]);
10210 if (message == NULL)
10211 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010212 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010213 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010214 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10215 if (buttons == NULL)
10216 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010217 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010219 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010220 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010222 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10223 if (typestr == NULL)
10224 error = TRUE;
10225 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010227 switch (TOUPPER_ASC(*typestr))
10228 {
10229 case 'E': type = VIM_ERROR; break;
10230 case 'Q': type = VIM_QUESTION; break;
10231 case 'I': type = VIM_INFO; break;
10232 case 'W': type = VIM_WARNING; break;
10233 case 'G': type = VIM_GENERIC; break;
10234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235 }
10236 }
10237 }
10238 }
10239
10240 if (buttons == NULL || *buttons == NUL)
10241 buttons = (char_u *)_("&Ok");
10242
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010243 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010244 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010245 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246#endif
10247}
10248
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010249/*
10250 * "copy()" function
10251 */
10252 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010253f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010254{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010255 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010256}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010257
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010258#ifdef FEAT_FLOAT
10259/*
10260 * "cos()" function
10261 */
10262 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010263f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010264{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010265 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010266
10267 rettv->v_type = VAR_FLOAT;
10268 if (get_float_arg(argvars, &f) == OK)
10269 rettv->vval.v_float = cos(f);
10270 else
10271 rettv->vval.v_float = 0.0;
10272}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010273
10274/*
10275 * "cosh()" function
10276 */
10277 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010278f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010279{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010280 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010281
10282 rettv->v_type = VAR_FLOAT;
10283 if (get_float_arg(argvars, &f) == OK)
10284 rettv->vval.v_float = cosh(f);
10285 else
10286 rettv->vval.v_float = 0.0;
10287}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010288#endif
10289
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010291 * "count()" function
10292 */
10293 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010294f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010295{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010296 long n = 0;
10297 int ic = FALSE;
10298
Bram Moolenaare9a41262005-01-15 22:18:47 +000010299 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010300 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010301 listitem_T *li;
10302 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010303 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010304
Bram Moolenaare9a41262005-01-15 22:18:47 +000010305 if ((l = argvars[0].vval.v_list) != NULL)
10306 {
10307 li = l->lv_first;
10308 if (argvars[2].v_type != VAR_UNKNOWN)
10309 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010310 int error = FALSE;
10311
10312 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010313 if (argvars[3].v_type != VAR_UNKNOWN)
10314 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010315 idx = get_tv_number_chk(&argvars[3], &error);
10316 if (!error)
10317 {
10318 li = list_find(l, idx);
10319 if (li == NULL)
10320 EMSGN(_(e_listidx), idx);
10321 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010322 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010323 if (error)
10324 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010325 }
10326
10327 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010328 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010329 ++n;
10330 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010331 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010332 else if (argvars[0].v_type == VAR_DICT)
10333 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010334 int todo;
10335 dict_T *d;
10336 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010337
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010338 if ((d = argvars[0].vval.v_dict) != NULL)
10339 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010340 int error = FALSE;
10341
Bram Moolenaare9a41262005-01-15 22:18:47 +000010342 if (argvars[2].v_type != VAR_UNKNOWN)
10343 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010344 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010345 if (argvars[3].v_type != VAR_UNKNOWN)
10346 EMSG(_(e_invarg));
10347 }
10348
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010349 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010350 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010351 {
10352 if (!HASHITEM_EMPTY(hi))
10353 {
10354 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010355 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010356 ++n;
10357 }
10358 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010359 }
10360 }
10361 else
10362 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010363 rettv->vval.v_number = n;
10364}
10365
10366/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010367 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10368 *
10369 * Checks the existence of a cscope connection.
10370 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010372f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010373{
10374#ifdef FEAT_CSCOPE
10375 int num = 0;
10376 char_u *dbpath = NULL;
10377 char_u *prepend = NULL;
10378 char_u buf[NUMBUFLEN];
10379
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010380 if (argvars[0].v_type != VAR_UNKNOWN
10381 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010383 num = (int)get_tv_number(&argvars[0]);
10384 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010385 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010386 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387 }
10388
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010389 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010390#endif
10391}
10392
10393/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010394 * "cursor(lnum, col)" function, or
10395 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010396 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010397 * Moves the cursor to the specified line and column.
10398 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010401f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402{
10403 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010404#ifdef FEAT_VIRTUALEDIT
10405 long coladd = 0;
10406#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010407 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010408
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010409 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010410 if (argvars[1].v_type == VAR_UNKNOWN)
10411 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010412 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010413 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010414
Bram Moolenaar493c1782014-05-28 14:34:46 +020010415 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010416 {
10417 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010418 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010419 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010420 line = pos.lnum;
10421 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010422#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010423 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010424#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010425 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010426 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010427 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010428 set_curswant = FALSE;
10429 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010430 }
10431 else
10432 {
10433 line = get_tv_lnum(argvars);
10434 col = get_tv_number_chk(&argvars[1], NULL);
10435#ifdef FEAT_VIRTUALEDIT
10436 if (argvars[2].v_type != VAR_UNKNOWN)
10437 coladd = get_tv_number_chk(&argvars[2], NULL);
10438#endif
10439 }
10440 if (line < 0 || col < 0
10441#ifdef FEAT_VIRTUALEDIT
10442 || coladd < 0
10443#endif
10444 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010445 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010446 if (line > 0)
10447 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448 if (col > 0)
10449 curwin->w_cursor.col = col - 1;
10450#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010451 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452#endif
10453
10454 /* Make sure the cursor is in a valid position. */
10455 check_cursor();
10456#ifdef FEAT_MBYTE
10457 /* Correct cursor for multi-byte character. */
10458 if (has_mbyte)
10459 mb_adjust_cursor();
10460#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010461
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010462 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010463 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464}
10465
10466/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010467 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468 */
10469 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010470f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010472 int noref = 0;
10473
10474 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010475 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010476 if (noref < 0 || noref > 1)
10477 EMSG(_(e_invarg));
10478 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010479 {
10480 current_copyID += COPYID_INC;
10481 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010483}
10484
10485/*
10486 * "delete()" function
10487 */
10488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010489f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010491 char_u nbuf[NUMBUFLEN];
10492 char_u *name;
10493 char_u *flags;
10494
10495 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010497 return;
10498
10499 name = get_tv_string(&argvars[0]);
10500 if (name == NULL || *name == NUL)
10501 {
10502 EMSG(_(e_invarg));
10503 return;
10504 }
10505
10506 if (argvars[1].v_type != VAR_UNKNOWN)
10507 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010509 flags = (char_u *)"";
10510
10511 if (*flags == NUL)
10512 /* delete a file */
10513 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10514 else if (STRCMP(flags, "d") == 0)
10515 /* delete an empty directory */
10516 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10517 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010518 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010519 rettv->vval.v_number = delete_recursive(name);
10520 else
10521 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010522}
10523
10524/*
10525 * "did_filetype()" function
10526 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010528f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529{
10530#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010531 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532#endif
10533}
10534
10535/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010536 * "diff_filler()" function
10537 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010538 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010539f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010540{
10541#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010542 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010543#endif
10544}
10545
10546/*
10547 * "diff_hlID()" function
10548 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010549 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010550f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010551{
10552#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010553 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010554 static linenr_T prev_lnum = 0;
10555 static int changedtick = 0;
10556 static int fnum = 0;
10557 static int change_start = 0;
10558 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010559 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010560 int filler_lines;
10561 int col;
10562
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010563 if (lnum < 0) /* ignore type error in {lnum} arg */
10564 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010565 if (lnum != prev_lnum
10566 || changedtick != curbuf->b_changedtick
10567 || fnum != curbuf->b_fnum)
10568 {
10569 /* New line, buffer, change: need to get the values. */
10570 filler_lines = diff_check(curwin, lnum);
10571 if (filler_lines < 0)
10572 {
10573 if (filler_lines == -1)
10574 {
10575 change_start = MAXCOL;
10576 change_end = -1;
10577 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10578 hlID = HLF_ADD; /* added line */
10579 else
10580 hlID = HLF_CHD; /* changed line */
10581 }
10582 else
10583 hlID = HLF_ADD; /* added line */
10584 }
10585 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010586 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010587 prev_lnum = lnum;
10588 changedtick = curbuf->b_changedtick;
10589 fnum = curbuf->b_fnum;
10590 }
10591
10592 if (hlID == HLF_CHD || hlID == HLF_TXD)
10593 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010594 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010595 if (col >= change_start && col <= change_end)
10596 hlID = HLF_TXD; /* changed text */
10597 else
10598 hlID = HLF_CHD; /* changed line */
10599 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010600 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010601#endif
10602}
10603
10604/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010010605 * "disable_char_avail_for_testing({expr})" function
10606 */
10607 static void
10608f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
10609{
10610 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
10611}
10612
10613/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010614 * "empty({expr})" function
10615 */
10616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010617f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010618{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010010619 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010620
10621 switch (argvars[0].v_type)
10622 {
10623 case VAR_STRING:
10624 case VAR_FUNC:
10625 n = argvars[0].vval.v_string == NULL
10626 || *argvars[0].vval.v_string == NUL;
10627 break;
10628 case VAR_NUMBER:
10629 n = argvars[0].vval.v_number == 0;
10630 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010631 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010010632#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010633 n = argvars[0].vval.v_float == 0.0;
10634 break;
10635#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010636 case VAR_LIST:
10637 n = argvars[0].vval.v_list == NULL
10638 || argvars[0].vval.v_list->lv_first == NULL;
10639 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010640 case VAR_DICT:
10641 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010642 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010643 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010644 case VAR_SPECIAL:
10645 n = argvars[0].vval.v_number != VVAL_TRUE;
10646 break;
10647
Bram Moolenaar835dc632016-02-07 14:27:38 +010010648 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010649#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010650 n = argvars[0].vval.v_job == NULL
10651 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
10652 break;
10653#endif
10654 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010655#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010656 n = argvars[0].vval.v_channel == NULL
10657 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010010658 break;
10659#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010010660 case VAR_UNKNOWN:
10661 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
10662 n = TRUE;
10663 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010664 }
10665
10666 rettv->vval.v_number = n;
10667}
10668
10669/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010670 * "escape({string}, {chars})" function
10671 */
10672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010673f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010674{
10675 char_u buf[NUMBUFLEN];
10676
Bram Moolenaar758711c2005-02-02 23:11:38 +000010677 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10678 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010679 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010680}
10681
10682/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010683 * "eval()" function
10684 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010685 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010686f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010687{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010688 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010689
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010690 s = get_tv_string_chk(&argvars[0]);
10691 if (s != NULL)
10692 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010693
Bram Moolenaar615b9972015-01-14 17:15:05 +010010694 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010695 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10696 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010697 if (p != NULL && !aborting())
10698 EMSG2(_(e_invexpr2), p);
10699 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010700 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010701 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010702 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010703 else if (*s != NUL)
10704 EMSG(_(e_trailing));
10705}
10706
10707/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010708 * "eventhandler()" function
10709 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010711f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010712{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010713 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714}
10715
10716/*
10717 * "executable()" function
10718 */
10719 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010720f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010721{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010722 char_u *name = get_tv_string(&argvars[0]);
10723
10724 /* Check in $PATH and also check directly if there is a directory name. */
10725 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10726 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010727}
10728
10729/*
10730 * "exepath()" function
10731 */
10732 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010733f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010734{
10735 char_u *p = NULL;
10736
Bram Moolenaarb5971142015-03-21 17:32:19 +010010737 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010738 rettv->v_type = VAR_STRING;
10739 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740}
10741
10742/*
10743 * "exists()" function
10744 */
10745 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010746f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010747{
10748 char_u *p;
10749 char_u *name;
10750 int n = FALSE;
10751 int len = 0;
10752
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010753 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010754 if (*p == '$') /* environment variable */
10755 {
10756 /* first try "normal" environment variables (fast) */
10757 if (mch_getenv(p + 1) != NULL)
10758 n = TRUE;
10759 else
10760 {
10761 /* try expanding things like $VIM and ${HOME} */
10762 p = expand_env_save(p);
10763 if (p != NULL && *p != '$')
10764 n = TRUE;
10765 vim_free(p);
10766 }
10767 }
10768 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010769 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010770 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010771 if (*skipwhite(p) != NUL)
10772 n = FALSE; /* trailing garbage */
10773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010774 else if (*p == '*') /* internal or user defined function */
10775 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010776 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010777 }
10778 else if (*p == ':')
10779 {
10780 n = cmd_exists(p + 1);
10781 }
10782 else if (*p == '#')
10783 {
10784#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010785 if (p[1] == '#')
10786 n = autocmd_supported(p + 2);
10787 else
10788 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010789#endif
10790 }
10791 else /* internal variable */
10792 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010793 char_u *tofree;
10794 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010795
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010796 /* get_name_len() takes care of expanding curly braces */
10797 name = p;
10798 len = get_name_len(&p, &tofree, TRUE, FALSE);
10799 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010800 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010801 if (tofree != NULL)
10802 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010803 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010804 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010805 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010806 /* handle d.key, l[idx], f(expr) */
10807 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10808 if (n)
10809 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810 }
10811 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010812 if (*p != NUL)
10813 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010815 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816 }
10817
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010818 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010819}
10820
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010821#ifdef FEAT_FLOAT
10822/*
10823 * "exp()" function
10824 */
10825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010826f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010827{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010828 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010829
10830 rettv->v_type = VAR_FLOAT;
10831 if (get_float_arg(argvars, &f) == OK)
10832 rettv->vval.v_float = exp(f);
10833 else
10834 rettv->vval.v_float = 0.0;
10835}
10836#endif
10837
Bram Moolenaar071d4272004-06-13 20:20:40 +000010838/*
10839 * "expand()" function
10840 */
10841 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010842f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843{
10844 char_u *s;
10845 int len;
10846 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010847 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010848 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010849 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010850 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010852 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010853 if (argvars[1].v_type != VAR_UNKNOWN
10854 && argvars[2].v_type != VAR_UNKNOWN
10855 && get_tv_number_chk(&argvars[2], &error)
10856 && !error)
10857 {
10858 rettv->v_type = VAR_LIST;
10859 rettv->vval.v_list = NULL;
10860 }
10861
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010862 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010863 if (*s == '%' || *s == '#' || *s == '<')
10864 {
10865 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010866 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010868 if (rettv->v_type == VAR_LIST)
10869 {
10870 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10871 list_append_string(rettv->vval.v_list, result, -1);
10872 else
10873 vim_free(result);
10874 }
10875 else
10876 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877 }
10878 else
10879 {
10880 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010881 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010882 if (argvars[1].v_type != VAR_UNKNOWN
10883 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010884 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010885 if (!error)
10886 {
10887 ExpandInit(&xpc);
10888 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010889 if (p_wic)
10890 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010891 if (rettv->v_type == VAR_STRING)
10892 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10893 options, WILD_ALL);
10894 else if (rettv_list_alloc(rettv) != FAIL)
10895 {
10896 int i;
10897
10898 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10899 for (i = 0; i < xpc.xp_numfiles; i++)
10900 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10901 ExpandCleanup(&xpc);
10902 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010903 }
10904 else
10905 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906 }
10907}
10908
10909/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010910 * Go over all entries in "d2" and add them to "d1".
10911 * When "action" is "error" then a duplicate key is an error.
10912 * When "action" is "force" then a duplicate key is overwritten.
10913 * Otherwise duplicate keys are ignored ("action" is "keep").
10914 */
10915 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010916dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020010917{
10918 dictitem_T *di1;
10919 hashitem_T *hi2;
10920 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010921 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020010922
10923 todo = (int)d2->dv_hashtab.ht_used;
10924 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10925 {
10926 if (!HASHITEM_EMPTY(hi2))
10927 {
10928 --todo;
10929 di1 = dict_find(d1, hi2->hi_key, -1);
10930 if (d1->dv_scope != 0)
10931 {
10932 /* Disallow replacing a builtin function in l: and g:.
10933 * Check the key to be valid when adding to any
10934 * scope. */
10935 if (d1->dv_scope == VAR_DEF_SCOPE
10936 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10937 && var_check_func_name(hi2->hi_key,
10938 di1 == NULL))
10939 break;
10940 if (!valid_varname(hi2->hi_key))
10941 break;
10942 }
10943 if (di1 == NULL)
10944 {
10945 di1 = dictitem_copy(HI2DI(hi2));
10946 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10947 dictitem_free(di1);
10948 }
10949 else if (*action == 'e')
10950 {
10951 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10952 break;
10953 }
10954 else if (*action == 'f' && HI2DI(hi2) != di1)
10955 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020010956 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
10957 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020010958 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020010959 clear_tv(&di1->di_tv);
10960 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10961 }
10962 }
10963 }
10964}
10965
10966/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010967 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010968 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010969 */
10970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010971f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010972{
Bram Moolenaar77354e72015-04-21 16:49:05 +020010973 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010974
Bram Moolenaare9a41262005-01-15 22:18:47 +000010975 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010976 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010977 list_T *l1, *l2;
10978 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010979 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010980 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010981
Bram Moolenaare9a41262005-01-15 22:18:47 +000010982 l1 = argvars[0].vval.v_list;
10983 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020010984 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010985 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010986 {
10987 if (argvars[2].v_type != VAR_UNKNOWN)
10988 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010989 before = get_tv_number_chk(&argvars[2], &error);
10990 if (error)
10991 return; /* type error; errmsg already given */
10992
Bram Moolenaar758711c2005-02-02 23:11:38 +000010993 if (before == l1->lv_len)
10994 item = NULL;
10995 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010996 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010997 item = list_find(l1, before);
10998 if (item == NULL)
10999 {
11000 EMSGN(_(e_listidx), before);
11001 return;
11002 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011003 }
11004 }
11005 else
11006 item = NULL;
11007 list_extend(l1, l2, item);
11008
Bram Moolenaare9a41262005-01-15 22:18:47 +000011009 copy_tv(&argvars[0], rettv);
11010 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011011 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011012 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11013 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011014 dict_T *d1, *d2;
11015 char_u *action;
11016 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011017
11018 d1 = argvars[0].vval.v_dict;
11019 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011020 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011021 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011022 {
11023 /* Check the third argument. */
11024 if (argvars[2].v_type != VAR_UNKNOWN)
11025 {
11026 static char *(av[]) = {"keep", "force", "error"};
11027
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011028 action = get_tv_string_chk(&argvars[2]);
11029 if (action == NULL)
11030 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011031 for (i = 0; i < 3; ++i)
11032 if (STRCMP(action, av[i]) == 0)
11033 break;
11034 if (i == 3)
11035 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011036 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011037 return;
11038 }
11039 }
11040 else
11041 action = (char_u *)"force";
11042
Bram Moolenaara9922d62013-05-30 13:01:18 +020011043 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011044
Bram Moolenaare9a41262005-01-15 22:18:47 +000011045 copy_tv(&argvars[0], rettv);
11046 }
11047 }
11048 else
11049 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011050}
11051
11052/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011053 * "feedkeys()" function
11054 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011055 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011056f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011057{
11058 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011059 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011060 char_u *keys, *flags;
11061 char_u nbuf[NUMBUFLEN];
11062 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011063 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011064 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011065
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011066 /* This is not allowed in the sandbox. If the commands would still be
11067 * executed in the sandbox it would be OK, but it probably happens later,
11068 * when "sandbox" is no longer set. */
11069 if (check_secure())
11070 return;
11071
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011072 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011073
11074 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011075 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011076 flags = get_tv_string_buf(&argvars[1], nbuf);
11077 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011078 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011079 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011080 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011081 case 'n': remap = FALSE; break;
11082 case 'm': remap = TRUE; break;
11083 case 't': typed = TRUE; break;
11084 case 'i': insert = TRUE; break;
11085 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011086 }
11087 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011088 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011089
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011090 if (*keys != NUL || execute)
11091 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011092 /* Need to escape K_SPECIAL and CSI before putting the string in the
11093 * typeahead buffer. */
11094 keys_esc = vim_strsave_escape_csi(keys);
11095 if (keys_esc != NULL)
11096 {
11097 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011098 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011099 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011100 if (vgetc_busy)
11101 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011102 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011103 {
11104 int save_msg_scroll = msg_scroll;
11105
11106 /* Avoid a 1 second delay when the keys start Insert mode. */
11107 msg_scroll = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011108 exec_normal(TRUE);
Bram Moolenaar9e496852016-03-11 19:31:47 +010011109 msg_scroll |= save_msg_scroll;
11110 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011111 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011112 }
11113}
11114
11115/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011116 * "filereadable()" function
11117 */
11118 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011119f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011120{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011121 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011122 char_u *p;
11123 int n;
11124
Bram Moolenaarc236c162008-07-13 17:41:49 +000011125#ifndef O_NONBLOCK
11126# define O_NONBLOCK 0
11127#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011128 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011129 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11130 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131 {
11132 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011133 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011134 }
11135 else
11136 n = FALSE;
11137
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011138 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011139}
11140
11141/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011142 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011143 * rights to write into.
11144 */
11145 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011146f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011147{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011148 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011149}
11150
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011151 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011152findfilendir(
11153 typval_T *argvars UNUSED,
11154 typval_T *rettv,
11155 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011156{
11157#ifdef FEAT_SEARCHPATH
11158 char_u *fname;
11159 char_u *fresult = NULL;
11160 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11161 char_u *p;
11162 char_u pathbuf[NUMBUFLEN];
11163 int count = 1;
11164 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011165 int error = FALSE;
11166#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011167
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011168 rettv->vval.v_string = NULL;
11169 rettv->v_type = VAR_STRING;
11170
11171#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011172 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011173
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011174 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011175 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011176 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11177 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011178 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011179 else
11180 {
11181 if (*p != NUL)
11182 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011183
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011184 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011185 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011186 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011187 }
11188
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011189 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11190 error = TRUE;
11191
11192 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011193 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011194 do
11195 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011196 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011197 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011198 fresult = find_file_in_path_option(first ? fname : NULL,
11199 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011200 0, first, path,
11201 find_what,
11202 curbuf->b_ffname,
11203 find_what == FINDFILE_DIR
11204 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011205 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011206
11207 if (fresult != NULL && rettv->v_type == VAR_LIST)
11208 list_append_string(rettv->vval.v_list, fresult, -1);
11209
11210 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011211 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011212
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011213 if (rettv->v_type == VAR_STRING)
11214 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011215#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011216}
11217
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011218static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11219static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011220
11221/*
11222 * Implementation of map() and filter().
11223 */
11224 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011225filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011226{
11227 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011228 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011229 listitem_T *li, *nli;
11230 list_T *l = NULL;
11231 dictitem_T *di;
11232 hashtab_T *ht;
11233 hashitem_T *hi;
11234 dict_T *d = NULL;
11235 typval_T save_val;
11236 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011237 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011238 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011239 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011240 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011241 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011242 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011243 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011244
Bram Moolenaare9a41262005-01-15 22:18:47 +000011245 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011246 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011247 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011248 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011249 return;
11250 }
11251 else if (argvars[0].v_type == VAR_DICT)
11252 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011253 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011254 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011255 return;
11256 }
11257 else
11258 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011259 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011260 return;
11261 }
11262
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011263 expr = get_tv_string_buf_chk(&argvars[1], buf);
11264 /* On type errors, the preceding call has already displayed an error
11265 * message. Avoid a misleading error message for an empty string that
11266 * was not passed as argument. */
11267 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011268 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011269 prepare_vimvar(VV_VAL, &save_val);
11270 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011271
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011272 /* We reset "did_emsg" to be able to detect whether an error
11273 * occurred during evaluation of the expression. */
11274 save_did_emsg = did_emsg;
11275 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011276
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011277 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011278 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011279 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011280 vimvars[VV_KEY].vv_type = VAR_STRING;
11281
11282 ht = &d->dv_hashtab;
11283 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011284 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011285 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011286 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011287 if (!HASHITEM_EMPTY(hi))
11288 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011289 int r;
11290
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011291 --todo;
11292 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011293 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011294 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11295 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011296 break;
11297 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011298 r = filter_map_one(&di->di_tv, expr, map, &rem);
11299 clear_tv(&vimvars[VV_KEY].vv_tv);
11300 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011301 break;
11302 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011303 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011304 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11305 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011306 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011307 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011308 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011309 }
11310 }
11311 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011312 }
11313 else
11314 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011315 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11316
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011317 for (li = l->lv_first; li != NULL; li = nli)
11318 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011319 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011320 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011321 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011322 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011323 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011324 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011325 break;
11326 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011327 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011328 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011329 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011330 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011331
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011332 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011333 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011334
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011335 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011336 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011337
11338 copy_tv(&argvars[0], rettv);
11339}
11340
11341 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011342filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011343{
Bram Moolenaar33570922005-01-25 22:26:29 +000011344 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011345 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011346 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011347
Bram Moolenaar33570922005-01-25 22:26:29 +000011348 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011349 s = expr;
11350 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011351 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011352 if (*s != NUL) /* check for trailing chars after expr */
11353 {
11354 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011355 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011356 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011357 }
11358 if (map)
11359 {
11360 /* map(): replace the list item value */
11361 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011362 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011363 *tv = rettv;
11364 }
11365 else
11366 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011367 int error = FALSE;
11368
Bram Moolenaare9a41262005-01-15 22:18:47 +000011369 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011370 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011371 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011372 /* On type error, nothing has been removed; return FAIL to stop the
11373 * loop. The error message was given by get_tv_number_chk(). */
11374 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011375 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011376 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011377 retval = OK;
11378theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011379 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011380 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011381}
11382
11383/*
11384 * "filter()" function
11385 */
11386 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011387f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011388{
11389 filter_map(argvars, rettv, FALSE);
11390}
11391
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011392/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011393 * "finddir({fname}[, {path}[, {count}]])" function
11394 */
11395 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011396f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011397{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011398 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011399}
11400
11401/*
11402 * "findfile({fname}[, {path}[, {count}]])" function
11403 */
11404 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011405f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011406{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011407 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011408}
11409
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011410#ifdef FEAT_FLOAT
11411/*
11412 * "float2nr({float})" function
11413 */
11414 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011415f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011416{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011417 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011418
11419 if (get_float_arg(argvars, &f) == OK)
11420 {
11421 if (f < -0x7fffffff)
11422 rettv->vval.v_number = -0x7fffffff;
11423 else if (f > 0x7fffffff)
11424 rettv->vval.v_number = 0x7fffffff;
11425 else
11426 rettv->vval.v_number = (varnumber_T)f;
11427 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011428}
11429
11430/*
11431 * "floor({float})" function
11432 */
11433 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011434f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011435{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011436 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011437
11438 rettv->v_type = VAR_FLOAT;
11439 if (get_float_arg(argvars, &f) == OK)
11440 rettv->vval.v_float = floor(f);
11441 else
11442 rettv->vval.v_float = 0.0;
11443}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011444
11445/*
11446 * "fmod()" function
11447 */
11448 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011449f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011450{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011451 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011452
11453 rettv->v_type = VAR_FLOAT;
11454 if (get_float_arg(argvars, &fx) == OK
11455 && get_float_arg(&argvars[1], &fy) == OK)
11456 rettv->vval.v_float = fmod(fx, fy);
11457 else
11458 rettv->vval.v_float = 0.0;
11459}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011460#endif
11461
Bram Moolenaar0d660222005-01-07 21:51:51 +000011462/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011463 * "fnameescape({string})" function
11464 */
11465 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011466f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011467{
11468 rettv->vval.v_string = vim_strsave_fnameescape(
11469 get_tv_string(&argvars[0]), FALSE);
11470 rettv->v_type = VAR_STRING;
11471}
11472
11473/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011474 * "fnamemodify({fname}, {mods})" function
11475 */
11476 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011477f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011478{
11479 char_u *fname;
11480 char_u *mods;
11481 int usedlen = 0;
11482 int len;
11483 char_u *fbuf = NULL;
11484 char_u buf[NUMBUFLEN];
11485
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011486 fname = get_tv_string_chk(&argvars[0]);
11487 mods = get_tv_string_buf_chk(&argvars[1], buf);
11488 if (fname == NULL || mods == NULL)
11489 fname = NULL;
11490 else
11491 {
11492 len = (int)STRLEN(fname);
11493 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011495
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011496 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011497 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011498 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011499 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011500 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011501 vim_free(fbuf);
11502}
11503
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011504static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011505
11506/*
11507 * "foldclosed()" function
11508 */
11509 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011510foldclosed_both(
11511 typval_T *argvars UNUSED,
11512 typval_T *rettv,
11513 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011514{
11515#ifdef FEAT_FOLDING
11516 linenr_T lnum;
11517 linenr_T first, last;
11518
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011519 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011520 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11521 {
11522 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11523 {
11524 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011525 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011526 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011527 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011528 return;
11529 }
11530 }
11531#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011532 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011533}
11534
11535/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011536 * "foldclosed()" function
11537 */
11538 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011539f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011540{
11541 foldclosed_both(argvars, rettv, FALSE);
11542}
11543
11544/*
11545 * "foldclosedend()" function
11546 */
11547 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011548f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011549{
11550 foldclosed_both(argvars, rettv, TRUE);
11551}
11552
11553/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011554 * "foldlevel()" function
11555 */
11556 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011557f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011558{
11559#ifdef FEAT_FOLDING
11560 linenr_T lnum;
11561
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011562 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011563 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011564 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011565#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011566}
11567
11568/*
11569 * "foldtext()" function
11570 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011571 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011572f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011573{
11574#ifdef FEAT_FOLDING
11575 linenr_T lnum;
11576 char_u *s;
11577 char_u *r;
11578 int len;
11579 char *txt;
11580#endif
11581
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011582 rettv->v_type = VAR_STRING;
11583 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011584#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011585 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11586 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11587 <= curbuf->b_ml.ml_line_count
11588 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011589 {
11590 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011591 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11592 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011593 {
11594 if (!linewhite(lnum))
11595 break;
11596 ++lnum;
11597 }
11598
11599 /* Find interesting text in this line. */
11600 s = skipwhite(ml_get(lnum));
11601 /* skip C comment-start */
11602 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011603 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011604 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011605 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011606 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011607 {
11608 s = skipwhite(ml_get(lnum + 1));
11609 if (*s == '*')
11610 s = skipwhite(s + 1);
11611 }
11612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011613 txt = _("+-%s%3ld lines: ");
11614 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011615 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011616 + 20 /* for %3ld */
11617 + STRLEN(s))); /* concatenated */
11618 if (r != NULL)
11619 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011620 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11621 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11622 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011623 len = (int)STRLEN(r);
11624 STRCAT(r, s);
11625 /* remove 'foldmarker' and 'commentstring' */
11626 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011627 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011628 }
11629 }
11630#endif
11631}
11632
11633/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011634 * "foldtextresult(lnum)" function
11635 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011636 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011637f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011638{
11639#ifdef FEAT_FOLDING
11640 linenr_T lnum;
11641 char_u *text;
11642 char_u buf[51];
11643 foldinfo_T foldinfo;
11644 int fold_count;
11645#endif
11646
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011647 rettv->v_type = VAR_STRING;
11648 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011649#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011650 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011651 /* treat illegal types and illegal string values for {lnum} the same */
11652 if (lnum < 0)
11653 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011654 fold_count = foldedCount(curwin, lnum, &foldinfo);
11655 if (fold_count > 0)
11656 {
11657 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11658 &foldinfo, buf);
11659 if (text == buf)
11660 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011661 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011662 }
11663#endif
11664}
11665
11666/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011667 * "foreground()" function
11668 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011669 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011670f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011671{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011672#ifdef FEAT_GUI
11673 if (gui.in_use)
11674 gui_mch_set_foreground();
11675#else
11676# ifdef WIN32
11677 win32_set_foreground();
11678# endif
11679#endif
11680}
11681
11682/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011683 * "function()" function
11684 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011685 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011686f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011687{
11688 char_u *s;
11689
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011690 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011691 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011692 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011693 /* Don't check an autoload name for existence here. */
11694 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011695 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011696 else
11697 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011698 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011699 {
11700 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011701 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011702
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011703 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11704 * also be called from another script. Using trans_function_name()
11705 * would also work, but some plugins depend on the name being
11706 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011707 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011708 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011709 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011710 if (rettv->vval.v_string != NULL)
11711 {
11712 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011713 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011714 }
11715 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011716 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011717 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011718 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011719 }
11720}
11721
11722/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011723 * "garbagecollect()" function
11724 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011725 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011726f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011727{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011728 /* This is postponed until we are back at the toplevel, because we may be
11729 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11730 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011731
11732 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11733 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011734}
11735
11736/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011737 * "get()" function
11738 */
11739 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011740f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011741{
Bram Moolenaar33570922005-01-25 22:26:29 +000011742 listitem_T *li;
11743 list_T *l;
11744 dictitem_T *di;
11745 dict_T *d;
11746 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011747
Bram Moolenaare9a41262005-01-15 22:18:47 +000011748 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011749 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011750 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011751 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011752 int error = FALSE;
11753
11754 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11755 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011756 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011757 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011758 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011759 else if (argvars[0].v_type == VAR_DICT)
11760 {
11761 if ((d = argvars[0].vval.v_dict) != NULL)
11762 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011763 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011764 if (di != NULL)
11765 tv = &di->di_tv;
11766 }
11767 }
11768 else
11769 EMSG2(_(e_listdictarg), "get()");
11770
11771 if (tv == NULL)
11772 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011773 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011774 copy_tv(&argvars[2], rettv);
11775 }
11776 else
11777 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011778}
11779
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011780static 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 +000011781
11782/*
11783 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011784 * Return a range (from start to end) of lines in rettv from the specified
11785 * buffer.
11786 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011787 */
11788 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011789get_buffer_lines(
11790 buf_T *buf,
11791 linenr_T start,
11792 linenr_T end,
11793 int retlist,
11794 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011795{
11796 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011797
Bram Moolenaar959a1432013-12-14 12:17:38 +010011798 rettv->v_type = VAR_STRING;
11799 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011800 if (retlist && rettv_list_alloc(rettv) == FAIL)
11801 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011802
11803 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11804 return;
11805
11806 if (!retlist)
11807 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011808 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11809 p = ml_get_buf(buf, start, FALSE);
11810 else
11811 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011812 rettv->vval.v_string = vim_strsave(p);
11813 }
11814 else
11815 {
11816 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011817 return;
11818
11819 if (start < 1)
11820 start = 1;
11821 if (end > buf->b_ml.ml_line_count)
11822 end = buf->b_ml.ml_line_count;
11823 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011824 if (list_append_string(rettv->vval.v_list,
11825 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011826 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011827 }
11828}
11829
11830/*
11831 * "getbufline()" function
11832 */
11833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011834f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011835{
11836 linenr_T lnum;
11837 linenr_T end;
11838 buf_T *buf;
11839
11840 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11841 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011842 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011843 --emsg_off;
11844
Bram Moolenaar661b1822005-07-28 22:36:45 +000011845 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011846 if (argvars[2].v_type == VAR_UNKNOWN)
11847 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011848 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011849 end = get_tv_lnum_buf(&argvars[2], buf);
11850
Bram Moolenaar342337a2005-07-21 21:11:17 +000011851 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011852}
11853
Bram Moolenaar0d660222005-01-07 21:51:51 +000011854/*
11855 * "getbufvar()" function
11856 */
11857 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011858f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011859{
11860 buf_T *buf;
11861 buf_T *save_curbuf;
11862 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011863 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011864 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011865
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011866 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11867 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011868 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011869 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011870
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011871 rettv->v_type = VAR_STRING;
11872 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011873
11874 if (buf != NULL && varname != NULL)
11875 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011876 /* set curbuf to be our buf, temporarily */
11877 save_curbuf = curbuf;
11878 curbuf = buf;
11879
Bram Moolenaar0d660222005-01-07 21:51:51 +000011880 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011881 {
11882 if (get_option_tv(&varname, rettv, TRUE) == OK)
11883 done = TRUE;
11884 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011885 else if (STRCMP(varname, "changedtick") == 0)
11886 {
11887 rettv->v_type = VAR_NUMBER;
11888 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011889 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011890 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011891 else
11892 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011893 /* Look up the variable. */
11894 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11895 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11896 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011897 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011898 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011899 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011900 done = TRUE;
11901 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011902 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011903
11904 /* restore previous notion of curbuf */
11905 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011906 }
11907
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011908 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11909 /* use the default value */
11910 copy_tv(&argvars[2], rettv);
11911
Bram Moolenaar0d660222005-01-07 21:51:51 +000011912 --emsg_off;
11913}
11914
11915/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011916 * "getchar()" function
11917 */
11918 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011919f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011920{
11921 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011922 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011923
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011924 /* Position the cursor. Needed after a message that ends in a space. */
11925 windgoto(msg_row, msg_col);
11926
Bram Moolenaar071d4272004-06-13 20:20:40 +000011927 ++no_mapping;
11928 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011929 for (;;)
11930 {
11931 if (argvars[0].v_type == VAR_UNKNOWN)
11932 /* getchar(): blocking wait. */
11933 n = safe_vgetc();
11934 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11935 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011936 n = vpeekc_any();
11937 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011938 /* illegal argument or getchar(0) and no char avail: return zero */
11939 n = 0;
11940 else
11941 /* getchar(0) and char avail: return char */
11942 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011943
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011944 if (n == K_IGNORE)
11945 continue;
11946 break;
11947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011948 --no_mapping;
11949 --allow_keys;
11950
Bram Moolenaar219b8702006-11-01 14:32:36 +000011951 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11952 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11953 vimvars[VV_MOUSE_COL].vv_nr = 0;
11954
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011955 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011956 if (IS_SPECIAL(n) || mod_mask != 0)
11957 {
11958 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11959 int i = 0;
11960
11961 /* Turn a special key into three bytes, plus modifier. */
11962 if (mod_mask != 0)
11963 {
11964 temp[i++] = K_SPECIAL;
11965 temp[i++] = KS_MODIFIER;
11966 temp[i++] = mod_mask;
11967 }
11968 if (IS_SPECIAL(n))
11969 {
11970 temp[i++] = K_SPECIAL;
11971 temp[i++] = K_SECOND(n);
11972 temp[i++] = K_THIRD(n);
11973 }
11974#ifdef FEAT_MBYTE
11975 else if (has_mbyte)
11976 i += (*mb_char2bytes)(n, temp + i);
11977#endif
11978 else
11979 temp[i++] = n;
11980 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011981 rettv->v_type = VAR_STRING;
11982 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011983
11984#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011985 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011986 {
11987 int row = mouse_row;
11988 int col = mouse_col;
11989 win_T *win;
11990 linenr_T lnum;
11991# ifdef FEAT_WINDOWS
11992 win_T *wp;
11993# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011994 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011995
11996 if (row >= 0 && col >= 0)
11997 {
11998 /* Find the window at the mouse coordinates and compute the
11999 * text position. */
12000 win = mouse_find_win(&row, &col);
12001 (void)mouse_comp_pos(win, &row, &col, &lnum);
12002# ifdef FEAT_WINDOWS
12003 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012004 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012005# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012006 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012007 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12008 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12009 }
12010 }
12011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012012 }
12013}
12014
12015/*
12016 * "getcharmod()" function
12017 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012018 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012019f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012020{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012021 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012022}
12023
12024/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012025 * "getcharsearch()" function
12026 */
12027 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012028f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012029{
12030 if (rettv_dict_alloc(rettv) != FAIL)
12031 {
12032 dict_T *dict = rettv->vval.v_dict;
12033
12034 dict_add_nr_str(dict, "char", 0L, last_csearch());
12035 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12036 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12037 }
12038}
12039
12040/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012041 * "getcmdline()" function
12042 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012043 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012044f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012045{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012046 rettv->v_type = VAR_STRING;
12047 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012048}
12049
12050/*
12051 * "getcmdpos()" function
12052 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012053 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012054f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012055{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012056 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012057}
12058
12059/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012060 * "getcmdtype()" function
12061 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012062 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012063f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012064{
12065 rettv->v_type = VAR_STRING;
12066 rettv->vval.v_string = alloc(2);
12067 if (rettv->vval.v_string != NULL)
12068 {
12069 rettv->vval.v_string[0] = get_cmdline_type();
12070 rettv->vval.v_string[1] = NUL;
12071 }
12072}
12073
12074/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012075 * "getcmdwintype()" function
12076 */
12077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012078f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012079{
12080 rettv->v_type = VAR_STRING;
12081 rettv->vval.v_string = NULL;
12082#ifdef FEAT_CMDWIN
12083 rettv->vval.v_string = alloc(2);
12084 if (rettv->vval.v_string != NULL)
12085 {
12086 rettv->vval.v_string[0] = cmdwin_type;
12087 rettv->vval.v_string[1] = NUL;
12088 }
12089#endif
12090}
12091
12092/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012093 * "getcwd()" function
12094 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012095 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012096f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012097{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012098 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012099 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012100
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012101 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012102 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012103
12104 wp = find_tabwin(&argvars[0], &argvars[1]);
12105 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012106 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012107 if (wp->w_localdir != NULL)
12108 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12109 else if(globaldir != NULL)
12110 rettv->vval.v_string = vim_strsave(globaldir);
12111 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012112 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012113 cwd = alloc(MAXPATHL);
12114 if (cwd != NULL)
12115 {
12116 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12117 rettv->vval.v_string = vim_strsave(cwd);
12118 vim_free(cwd);
12119 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012120 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012121#ifdef BACKSLASH_IN_FILENAME
12122 if (rettv->vval.v_string != NULL)
12123 slash_adjust(rettv->vval.v_string);
12124#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012125 }
12126}
12127
12128/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012129 * "getfontname()" function
12130 */
12131 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012132f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012133{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012134 rettv->v_type = VAR_STRING;
12135 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012136#ifdef FEAT_GUI
12137 if (gui.in_use)
12138 {
12139 GuiFont font;
12140 char_u *name = NULL;
12141
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012142 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012143 {
12144 /* Get the "Normal" font. Either the name saved by
12145 * hl_set_font_name() or from the font ID. */
12146 font = gui.norm_font;
12147 name = hl_get_font_name();
12148 }
12149 else
12150 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012151 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012152 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12153 return;
12154 font = gui_mch_get_font(name, FALSE);
12155 if (font == NOFONT)
12156 return; /* Invalid font name, return empty string. */
12157 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012158 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012159 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012160 gui_mch_free_font(font);
12161 }
12162#endif
12163}
12164
12165/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012166 * "getfperm({fname})" function
12167 */
12168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012169f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012170{
12171 char_u *fname;
12172 struct stat st;
12173 char_u *perm = NULL;
12174 char_u flags[] = "rwx";
12175 int i;
12176
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012177 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012178
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012179 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012180 if (mch_stat((char *)fname, &st) >= 0)
12181 {
12182 perm = vim_strsave((char_u *)"---------");
12183 if (perm != NULL)
12184 {
12185 for (i = 0; i < 9; i++)
12186 {
12187 if (st.st_mode & (1 << (8 - i)))
12188 perm[i] = flags[i % 3];
12189 }
12190 }
12191 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012192 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012193}
12194
12195/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012196 * "getfsize({fname})" function
12197 */
12198 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012199f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012200{
12201 char_u *fname;
12202 struct stat st;
12203
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012204 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012205
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012206 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012207
12208 if (mch_stat((char *)fname, &st) >= 0)
12209 {
12210 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012211 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012212 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012213 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012214 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012215
12216 /* non-perfect check for overflow */
12217 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12218 rettv->vval.v_number = -2;
12219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012220 }
12221 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012222 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012223}
12224
12225/*
12226 * "getftime({fname})" function
12227 */
12228 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012229f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012230{
12231 char_u *fname;
12232 struct stat st;
12233
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012234 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012235
12236 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012237 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012238 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012239 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012240}
12241
12242/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012243 * "getftype({fname})" function
12244 */
12245 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012246f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012247{
12248 char_u *fname;
12249 struct stat st;
12250 char_u *type = NULL;
12251 char *t;
12252
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012253 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012254
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012255 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012256 if (mch_lstat((char *)fname, &st) >= 0)
12257 {
12258#ifdef S_ISREG
12259 if (S_ISREG(st.st_mode))
12260 t = "file";
12261 else if (S_ISDIR(st.st_mode))
12262 t = "dir";
12263# ifdef S_ISLNK
12264 else if (S_ISLNK(st.st_mode))
12265 t = "link";
12266# endif
12267# ifdef S_ISBLK
12268 else if (S_ISBLK(st.st_mode))
12269 t = "bdev";
12270# endif
12271# ifdef S_ISCHR
12272 else if (S_ISCHR(st.st_mode))
12273 t = "cdev";
12274# endif
12275# ifdef S_ISFIFO
12276 else if (S_ISFIFO(st.st_mode))
12277 t = "fifo";
12278# endif
12279# ifdef S_ISSOCK
12280 else if (S_ISSOCK(st.st_mode))
12281 t = "fifo";
12282# endif
12283 else
12284 t = "other";
12285#else
12286# ifdef S_IFMT
12287 switch (st.st_mode & S_IFMT)
12288 {
12289 case S_IFREG: t = "file"; break;
12290 case S_IFDIR: t = "dir"; break;
12291# ifdef S_IFLNK
12292 case S_IFLNK: t = "link"; break;
12293# endif
12294# ifdef S_IFBLK
12295 case S_IFBLK: t = "bdev"; break;
12296# endif
12297# ifdef S_IFCHR
12298 case S_IFCHR: t = "cdev"; break;
12299# endif
12300# ifdef S_IFIFO
12301 case S_IFIFO: t = "fifo"; break;
12302# endif
12303# ifdef S_IFSOCK
12304 case S_IFSOCK: t = "socket"; break;
12305# endif
12306 default: t = "other";
12307 }
12308# else
12309 if (mch_isdir(fname))
12310 t = "dir";
12311 else
12312 t = "file";
12313# endif
12314#endif
12315 type = vim_strsave((char_u *)t);
12316 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012317 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012318}
12319
12320/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012321 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012322 */
12323 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012324f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012325{
12326 linenr_T lnum;
12327 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012328 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012329
12330 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012331 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012332 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012333 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012334 retlist = FALSE;
12335 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012336 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012337 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012338 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012339 retlist = TRUE;
12340 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012341
Bram Moolenaar342337a2005-07-21 21:11:17 +000012342 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012343}
12344
12345/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012346 * "getmatches()" function
12347 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012348 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012349f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012350{
12351#ifdef FEAT_SEARCH_EXTRA
12352 dict_T *dict;
12353 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012354 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012355
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012356 if (rettv_list_alloc(rettv) == OK)
12357 {
12358 while (cur != NULL)
12359 {
12360 dict = dict_alloc();
12361 if (dict == NULL)
12362 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012363 if (cur->match.regprog == NULL)
12364 {
12365 /* match added with matchaddpos() */
12366 for (i = 0; i < MAXPOSMATCH; ++i)
12367 {
12368 llpos_T *llpos;
12369 char buf[6];
12370 list_T *l;
12371
12372 llpos = &cur->pos.pos[i];
12373 if (llpos->lnum == 0)
12374 break;
12375 l = list_alloc();
12376 if (l == NULL)
12377 break;
12378 list_append_number(l, (varnumber_T)llpos->lnum);
12379 if (llpos->col > 0)
12380 {
12381 list_append_number(l, (varnumber_T)llpos->col);
12382 list_append_number(l, (varnumber_T)llpos->len);
12383 }
12384 sprintf(buf, "pos%d", i + 1);
12385 dict_add_list(dict, buf, l);
12386 }
12387 }
12388 else
12389 {
12390 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12391 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012392 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012393 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12394 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012395# ifdef FEAT_CONCEAL
12396 if (cur->conceal_char)
12397 {
12398 char_u buf[MB_MAXBYTES + 1];
12399
12400 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12401 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12402 }
12403# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012404 list_append_dict(rettv->vval.v_list, dict);
12405 cur = cur->next;
12406 }
12407 }
12408#endif
12409}
12410
12411/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012412 * "getpid()" function
12413 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012414 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012415f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012416{
12417 rettv->vval.v_number = mch_get_pid();
12418}
12419
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012420static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012421
12422/*
12423 * "getcurpos()" function
12424 */
12425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012426f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012427{
12428 getpos_both(argvars, rettv, TRUE);
12429}
12430
Bram Moolenaar18081e32008-02-20 19:11:07 +000012431/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012432 * "getpos(string)" function
12433 */
12434 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012435f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012436{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012437 getpos_both(argvars, rettv, FALSE);
12438}
12439
12440 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012441getpos_both(
12442 typval_T *argvars,
12443 typval_T *rettv,
12444 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012445{
Bram Moolenaara5525202006-03-02 22:52:09 +000012446 pos_T *fp;
12447 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012448 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012449
12450 if (rettv_list_alloc(rettv) == OK)
12451 {
12452 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012453 if (getcurpos)
12454 fp = &curwin->w_cursor;
12455 else
12456 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012457 if (fnum != -1)
12458 list_append_number(l, (varnumber_T)fnum);
12459 else
12460 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012461 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12462 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012463 list_append_number(l, (fp != NULL)
12464 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012465 : (varnumber_T)0);
12466 list_append_number(l,
12467#ifdef FEAT_VIRTUALEDIT
12468 (fp != NULL) ? (varnumber_T)fp->coladd :
12469#endif
12470 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012471 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012472 {
12473 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010012474 list_append_number(l, curwin->w_curswant == MAXCOL ?
12475 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012476 }
Bram Moolenaara5525202006-03-02 22:52:09 +000012477 }
12478 else
12479 rettv->vval.v_number = FALSE;
12480}
12481
12482/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012483 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012484 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012485 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012486f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012487{
12488#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012489 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012490#endif
12491
Bram Moolenaar2641f772005-03-25 21:58:17 +000012492#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012493 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012494 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012495 wp = NULL;
12496 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12497 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012498 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012499 if (wp == NULL)
12500 return;
12501 }
12502
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012503 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012504 }
12505#endif
12506}
12507
12508/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012509 * "getreg()" function
12510 */
12511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012512f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012513{
12514 char_u *strregname;
12515 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012516 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012517 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012518 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012519
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012520 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012521 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012522 strregname = get_tv_string_chk(&argvars[0]);
12523 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012524 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012525 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012526 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012527 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12528 return_list = get_tv_number_chk(&argvars[2], &error);
12529 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012531 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012532 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012533
12534 if (error)
12535 return;
12536
Bram Moolenaar071d4272004-06-13 20:20:40 +000012537 regname = (strregname == NULL ? '"' : *strregname);
12538 if (regname == 0)
12539 regname = '"';
12540
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012541 if (return_list)
12542 {
12543 rettv->v_type = VAR_LIST;
12544 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12545 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012546 if (rettv->vval.v_list != NULL)
12547 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012548 }
12549 else
12550 {
12551 rettv->v_type = VAR_STRING;
12552 rettv->vval.v_string = get_reg_contents(regname,
12553 arg2 ? GREG_EXPR_SRC : 0);
12554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012555}
12556
12557/*
12558 * "getregtype()" function
12559 */
12560 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012561f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012562{
12563 char_u *strregname;
12564 int regname;
12565 char_u buf[NUMBUFLEN + 2];
12566 long reglen = 0;
12567
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012568 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012569 {
12570 strregname = get_tv_string_chk(&argvars[0]);
12571 if (strregname == NULL) /* type error; errmsg already given */
12572 {
12573 rettv->v_type = VAR_STRING;
12574 rettv->vval.v_string = NULL;
12575 return;
12576 }
12577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012578 else
12579 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012580 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012581
12582 regname = (strregname == NULL ? '"' : *strregname);
12583 if (regname == 0)
12584 regname = '"';
12585
12586 buf[0] = NUL;
12587 buf[1] = NUL;
12588 switch (get_reg_type(regname, &reglen))
12589 {
12590 case MLINE: buf[0] = 'V'; break;
12591 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012592 case MBLOCK:
12593 buf[0] = Ctrl_V;
12594 sprintf((char *)buf + 1, "%ld", reglen + 1);
12595 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012596 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012597 rettv->v_type = VAR_STRING;
12598 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012599}
12600
12601/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012602 * "gettabvar()" function
12603 */
12604 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012605f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012606{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012607 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012608 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012609 dictitem_T *v;
12610 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012611 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012612
12613 rettv->v_type = VAR_STRING;
12614 rettv->vval.v_string = NULL;
12615
12616 varname = get_tv_string_chk(&argvars[1]);
12617 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12618 if (tp != NULL && varname != NULL)
12619 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012620 /* Set tp to be our tabpage, temporarily. Also set the window to the
12621 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012622 if (switch_win(&oldcurwin, &oldtabpage,
12623 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012624 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012625 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012626 /* look up the variable */
12627 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12628 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12629 if (v != NULL)
12630 {
12631 copy_tv(&v->di_tv, rettv);
12632 done = TRUE;
12633 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012634 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012635
12636 /* restore previous notion of curwin */
12637 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012638 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012639
12640 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012641 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012642}
12643
12644/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012645 * "gettabwinvar()" function
12646 */
12647 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012648f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012649{
12650 getwinvar(argvars, rettv, 1);
12651}
12652
12653/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012654 * "getwinposx()" function
12655 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012656 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012657f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012658{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012659 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012660#ifdef FEAT_GUI
12661 if (gui.in_use)
12662 {
12663 int x, y;
12664
12665 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012666 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012667 }
12668#endif
12669}
12670
12671/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010012672 * "win_getid()" function
12673 */
12674 static void
12675f_win_getid(typval_T *argvars, typval_T *rettv)
12676{
12677 rettv->vval.v_number = win_getid(argvars);
12678}
12679
12680/*
12681 * "win_gotoid()" function
12682 */
12683 static void
12684f_win_gotoid(typval_T *argvars, typval_T *rettv)
12685{
12686 rettv->vval.v_number = win_gotoid(argvars);
12687}
12688
12689/*
12690 * "win_id2tabwin()" function
12691 */
12692 static void
12693f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
12694{
12695 if (rettv_list_alloc(rettv) != FAIL)
12696 win_id2tabwin(argvars, rettv->vval.v_list);
12697}
12698
12699/*
12700 * "win_id2win()" function
12701 */
12702 static void
12703f_win_id2win(typval_T *argvars, typval_T *rettv)
12704{
12705 rettv->vval.v_number = win_id2win(argvars);
12706}
12707
12708/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012709 * "getwinposy()" function
12710 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012711 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012712f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012713{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012714 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012715#ifdef FEAT_GUI
12716 if (gui.in_use)
12717 {
12718 int x, y;
12719
12720 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012721 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012722 }
12723#endif
12724}
12725
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012726/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012727 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012728 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012729 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012730find_win_by_nr(
12731 typval_T *vp,
12732 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012733{
12734#ifdef FEAT_WINDOWS
12735 win_T *wp;
12736#endif
12737 int nr;
12738
12739 nr = get_tv_number_chk(vp, NULL);
12740
12741#ifdef FEAT_WINDOWS
12742 if (nr < 0)
12743 return NULL;
12744 if (nr == 0)
12745 return curwin;
12746
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012747 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12748 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012749 if (--nr <= 0)
12750 break;
12751 return wp;
12752#else
12753 if (nr == 0 || nr == 1)
12754 return curwin;
12755 return NULL;
12756#endif
12757}
12758
Bram Moolenaar071d4272004-06-13 20:20:40 +000012759/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010012760 * Find window specified by "wvp" in tabpage "tvp".
12761 */
12762 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010012763find_tabwin(
12764 typval_T *wvp, /* VAR_UNKNOWN for current window */
12765 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010012766{
12767 win_T *wp = NULL;
12768 tabpage_T *tp = NULL;
12769 long n;
12770
12771 if (wvp->v_type != VAR_UNKNOWN)
12772 {
12773 if (tvp->v_type != VAR_UNKNOWN)
12774 {
12775 n = get_tv_number(tvp);
12776 if (n >= 0)
12777 tp = find_tabpage(n);
12778 }
12779 else
12780 tp = curtab;
12781
12782 if (tp != NULL)
12783 wp = find_win_by_nr(wvp, tp);
12784 }
12785 else
12786 wp = curwin;
12787
12788 return wp;
12789}
12790
12791/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012792 * "getwinvar()" function
12793 */
12794 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012795f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012796{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012797 getwinvar(argvars, rettv, 0);
12798}
12799
12800/*
12801 * getwinvar() and gettabwinvar()
12802 */
12803 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012804getwinvar(
12805 typval_T *argvars,
12806 typval_T *rettv,
12807 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012808{
Bram Moolenaarba117c22015-09-29 16:53:22 +020012809 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012810 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012811 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012812 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012813 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020012814#ifdef FEAT_WINDOWS
12815 win_T *oldcurwin;
12816 tabpage_T *oldtabpage;
12817 int need_switch_win;
12818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012819
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012820#ifdef FEAT_WINDOWS
12821 if (off == 1)
12822 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12823 else
12824 tp = curtab;
12825#endif
12826 win = find_win_by_nr(&argvars[off], tp);
12827 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012828 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012829
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012830 rettv->v_type = VAR_STRING;
12831 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012832
12833 if (win != NULL && varname != NULL)
12834 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020012835#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020012836 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020012837 * otherwise the window is not valid. Only do this when needed,
12838 * autocommands get blocked. */
12839 need_switch_win = !(tp == curtab && win == curwin);
12840 if (!need_switch_win
12841 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
12842#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012843 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012844 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012845 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012846 if (get_option_tv(&varname, rettv, 1) == OK)
12847 done = TRUE;
12848 }
12849 else
12850 {
12851 /* Look up the variable. */
12852 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12853 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12854 varname, FALSE);
12855 if (v != NULL)
12856 {
12857 copy_tv(&v->di_tv, rettv);
12858 done = TRUE;
12859 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012861 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012862
Bram Moolenaarba117c22015-09-29 16:53:22 +020012863#ifdef FEAT_WINDOWS
12864 if (need_switch_win)
12865 /* restore previous notion of curwin */
12866 restore_win(oldcurwin, oldtabpage, TRUE);
12867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012868 }
12869
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012870 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12871 /* use the default return value */
12872 copy_tv(&argvars[off + 2], rettv);
12873
Bram Moolenaar071d4272004-06-13 20:20:40 +000012874 --emsg_off;
12875}
12876
12877/*
12878 * "glob()" function
12879 */
12880 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012881f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012882{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012883 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012884 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012885 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012886
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012887 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012888 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012889 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012890 if (argvars[1].v_type != VAR_UNKNOWN)
12891 {
12892 if (get_tv_number_chk(&argvars[1], &error))
12893 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012894 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012895 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012896 if (get_tv_number_chk(&argvars[2], &error))
12897 {
12898 rettv->v_type = VAR_LIST;
12899 rettv->vval.v_list = NULL;
12900 }
12901 if (argvars[3].v_type != VAR_UNKNOWN
12902 && get_tv_number_chk(&argvars[3], &error))
12903 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012904 }
12905 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012906 if (!error)
12907 {
12908 ExpandInit(&xpc);
12909 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012910 if (p_wic)
12911 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012912 if (rettv->v_type == VAR_STRING)
12913 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012914 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012915 else if (rettv_list_alloc(rettv) != FAIL)
12916 {
12917 int i;
12918
12919 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12920 NULL, options, WILD_ALL_KEEP);
12921 for (i = 0; i < xpc.xp_numfiles; i++)
12922 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12923
12924 ExpandCleanup(&xpc);
12925 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012926 }
12927 else
12928 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012929}
12930
12931/*
12932 * "globpath()" function
12933 */
12934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012935f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012936{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012937 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012938 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012939 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012940 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012941 garray_T ga;
12942 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012943
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012944 /* When the optional second argument is non-zero, don't remove matches
12945 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012946 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012947 if (argvars[2].v_type != VAR_UNKNOWN)
12948 {
12949 if (get_tv_number_chk(&argvars[2], &error))
12950 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010012951 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012952 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010012953 if (get_tv_number_chk(&argvars[3], &error))
12954 {
12955 rettv->v_type = VAR_LIST;
12956 rettv->vval.v_list = NULL;
12957 }
12958 if (argvars[4].v_type != VAR_UNKNOWN
12959 && get_tv_number_chk(&argvars[4], &error))
12960 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012961 }
12962 }
12963 if (file != NULL && !error)
12964 {
12965 ga_init2(&ga, (int)sizeof(char_u *), 10);
12966 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12967 if (rettv->v_type == VAR_STRING)
12968 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12969 else if (rettv_list_alloc(rettv) != FAIL)
12970 for (i = 0; i < ga.ga_len; ++i)
12971 list_append_string(rettv->vval.v_list,
12972 ((char_u **)(ga.ga_data))[i], -1);
12973 ga_clear_strings(&ga);
12974 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012975 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012976 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012977}
12978
12979/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012980 * "glob2regpat()" function
12981 */
12982 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012983f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012984{
12985 char_u *pat = get_tv_string_chk(&argvars[0]);
12986
12987 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010012988 rettv->vval.v_string = (pat == NULL)
12989 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010012990}
12991
12992/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012993 * "has()" function
12994 */
12995 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012996f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012997{
12998 int i;
12999 char_u *name;
13000 int n = FALSE;
13001 static char *(has_list[]) =
13002 {
13003#ifdef AMIGA
13004 "amiga",
13005# ifdef FEAT_ARP
13006 "arp",
13007# endif
13008#endif
13009#ifdef __BEOS__
13010 "beos",
13011#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013012#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013013 "mac",
13014#endif
13015#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013016 "macunix", /* built with 'darwin' enabled */
13017#endif
13018#if defined(__APPLE__) && __APPLE__ == 1
13019 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013020#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013021#ifdef __QNX__
13022 "qnx",
13023#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013024#ifdef UNIX
13025 "unix",
13026#endif
13027#ifdef VMS
13028 "vms",
13029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013030#ifdef WIN32
13031 "win32",
13032#endif
13033#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13034 "win32unix",
13035#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013036#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013037 "win64",
13038#endif
13039#ifdef EBCDIC
13040 "ebcdic",
13041#endif
13042#ifndef CASE_INSENSITIVE_FILENAME
13043 "fname_case",
13044#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013045#ifdef HAVE_ACL
13046 "acl",
13047#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013048#ifdef FEAT_ARABIC
13049 "arabic",
13050#endif
13051#ifdef FEAT_AUTOCMD
13052 "autocmd",
13053#endif
13054#ifdef FEAT_BEVAL
13055 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013056# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13057 "balloon_multiline",
13058# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013059#endif
13060#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13061 "builtin_terms",
13062# ifdef ALL_BUILTIN_TCAPS
13063 "all_builtin_terms",
13064# endif
13065#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013066#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13067 || defined(FEAT_GUI_W32) \
13068 || defined(FEAT_GUI_MOTIF))
13069 "browsefilter",
13070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013071#ifdef FEAT_BYTEOFF
13072 "byte_offset",
13073#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013074#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013075 "channel",
13076#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013077#ifdef FEAT_CINDENT
13078 "cindent",
13079#endif
13080#ifdef FEAT_CLIENTSERVER
13081 "clientserver",
13082#endif
13083#ifdef FEAT_CLIPBOARD
13084 "clipboard",
13085#endif
13086#ifdef FEAT_CMDL_COMPL
13087 "cmdline_compl",
13088#endif
13089#ifdef FEAT_CMDHIST
13090 "cmdline_hist",
13091#endif
13092#ifdef FEAT_COMMENTS
13093 "comments",
13094#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013095#ifdef FEAT_CONCEAL
13096 "conceal",
13097#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013098#ifdef FEAT_CRYPT
13099 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013100 "crypt-blowfish",
13101 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013102#endif
13103#ifdef FEAT_CSCOPE
13104 "cscope",
13105#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013106#ifdef FEAT_CURSORBIND
13107 "cursorbind",
13108#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013109#ifdef CURSOR_SHAPE
13110 "cursorshape",
13111#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013112#ifdef DEBUG
13113 "debug",
13114#endif
13115#ifdef FEAT_CON_DIALOG
13116 "dialog_con",
13117#endif
13118#ifdef FEAT_GUI_DIALOG
13119 "dialog_gui",
13120#endif
13121#ifdef FEAT_DIFF
13122 "diff",
13123#endif
13124#ifdef FEAT_DIGRAPHS
13125 "digraphs",
13126#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013127#ifdef FEAT_DIRECTX
13128 "directx",
13129#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013130#ifdef FEAT_DND
13131 "dnd",
13132#endif
13133#ifdef FEAT_EMACS_TAGS
13134 "emacs_tags",
13135#endif
13136 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013137 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013138#ifdef FEAT_SEARCH_EXTRA
13139 "extra_search",
13140#endif
13141#ifdef FEAT_FKMAP
13142 "farsi",
13143#endif
13144#ifdef FEAT_SEARCHPATH
13145 "file_in_path",
13146#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013147#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013148 "filterpipe",
13149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013150#ifdef FEAT_FIND_ID
13151 "find_in_path",
13152#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013153#ifdef FEAT_FLOAT
13154 "float",
13155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013156#ifdef FEAT_FOLDING
13157 "folding",
13158#endif
13159#ifdef FEAT_FOOTER
13160 "footer",
13161#endif
13162#if !defined(USE_SYSTEM) && defined(UNIX)
13163 "fork",
13164#endif
13165#ifdef FEAT_GETTEXT
13166 "gettext",
13167#endif
13168#ifdef FEAT_GUI
13169 "gui",
13170#endif
13171#ifdef FEAT_GUI_ATHENA
13172# ifdef FEAT_GUI_NEXTAW
13173 "gui_neXtaw",
13174# else
13175 "gui_athena",
13176# endif
13177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013178#ifdef FEAT_GUI_GTK
13179 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013180# ifdef USE_GTK3
13181 "gui_gtk3",
13182# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013183 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013184# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013185#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013186#ifdef FEAT_GUI_GNOME
13187 "gui_gnome",
13188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013189#ifdef FEAT_GUI_MAC
13190 "gui_mac",
13191#endif
13192#ifdef FEAT_GUI_MOTIF
13193 "gui_motif",
13194#endif
13195#ifdef FEAT_GUI_PHOTON
13196 "gui_photon",
13197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013198#ifdef FEAT_GUI_W32
13199 "gui_win32",
13200#endif
13201#ifdef FEAT_HANGULIN
13202 "hangul_input",
13203#endif
13204#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13205 "iconv",
13206#endif
13207#ifdef FEAT_INS_EXPAND
13208 "insert_expand",
13209#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013210#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010013211 "job",
13212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013213#ifdef FEAT_JUMPLIST
13214 "jumplist",
13215#endif
13216#ifdef FEAT_KEYMAP
13217 "keymap",
13218#endif
13219#ifdef FEAT_LANGMAP
13220 "langmap",
13221#endif
13222#ifdef FEAT_LIBCALL
13223 "libcall",
13224#endif
13225#ifdef FEAT_LINEBREAK
13226 "linebreak",
13227#endif
13228#ifdef FEAT_LISP
13229 "lispindent",
13230#endif
13231#ifdef FEAT_LISTCMDS
13232 "listcmds",
13233#endif
13234#ifdef FEAT_LOCALMAP
13235 "localmap",
13236#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013237#ifdef FEAT_LUA
13238# ifndef DYNAMIC_LUA
13239 "lua",
13240# endif
13241#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013242#ifdef FEAT_MENU
13243 "menu",
13244#endif
13245#ifdef FEAT_SESSION
13246 "mksession",
13247#endif
13248#ifdef FEAT_MODIFY_FNAME
13249 "modify_fname",
13250#endif
13251#ifdef FEAT_MOUSE
13252 "mouse",
13253#endif
13254#ifdef FEAT_MOUSESHAPE
13255 "mouseshape",
13256#endif
13257#if defined(UNIX) || defined(VMS)
13258# ifdef FEAT_MOUSE_DEC
13259 "mouse_dec",
13260# endif
13261# ifdef FEAT_MOUSE_GPM
13262 "mouse_gpm",
13263# endif
13264# ifdef FEAT_MOUSE_JSB
13265 "mouse_jsbterm",
13266# endif
13267# ifdef FEAT_MOUSE_NET
13268 "mouse_netterm",
13269# endif
13270# ifdef FEAT_MOUSE_PTERM
13271 "mouse_pterm",
13272# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013273# ifdef FEAT_MOUSE_SGR
13274 "mouse_sgr",
13275# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013276# ifdef FEAT_SYSMOUSE
13277 "mouse_sysmouse",
13278# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013279# ifdef FEAT_MOUSE_URXVT
13280 "mouse_urxvt",
13281# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013282# ifdef FEAT_MOUSE_XTERM
13283 "mouse_xterm",
13284# endif
13285#endif
13286#ifdef FEAT_MBYTE
13287 "multi_byte",
13288#endif
13289#ifdef FEAT_MBYTE_IME
13290 "multi_byte_ime",
13291#endif
13292#ifdef FEAT_MULTI_LANG
13293 "multi_lang",
13294#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013295#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013296#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013297 "mzscheme",
13298#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013299#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013300#ifdef FEAT_OLE
13301 "ole",
13302#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013303 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013304#ifdef FEAT_PATH_EXTRA
13305 "path_extra",
13306#endif
13307#ifdef FEAT_PERL
13308#ifndef DYNAMIC_PERL
13309 "perl",
13310#endif
13311#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013312#ifdef FEAT_PERSISTENT_UNDO
13313 "persistent_undo",
13314#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013315#ifdef FEAT_PYTHON
13316#ifndef DYNAMIC_PYTHON
13317 "python",
13318#endif
13319#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013320#ifdef FEAT_PYTHON3
13321#ifndef DYNAMIC_PYTHON3
13322 "python3",
13323#endif
13324#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013325#ifdef FEAT_POSTSCRIPT
13326 "postscript",
13327#endif
13328#ifdef FEAT_PRINTER
13329 "printer",
13330#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013331#ifdef FEAT_PROFILE
13332 "profile",
13333#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013334#ifdef FEAT_RELTIME
13335 "reltime",
13336#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013337#ifdef FEAT_QUICKFIX
13338 "quickfix",
13339#endif
13340#ifdef FEAT_RIGHTLEFT
13341 "rightleft",
13342#endif
13343#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13344 "ruby",
13345#endif
13346#ifdef FEAT_SCROLLBIND
13347 "scrollbind",
13348#endif
13349#ifdef FEAT_CMDL_INFO
13350 "showcmd",
13351 "cmdline_info",
13352#endif
13353#ifdef FEAT_SIGNS
13354 "signs",
13355#endif
13356#ifdef FEAT_SMARTINDENT
13357 "smartindent",
13358#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013359#ifdef STARTUPTIME
13360 "startuptime",
13361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013362#ifdef FEAT_STL_OPT
13363 "statusline",
13364#endif
13365#ifdef FEAT_SUN_WORKSHOP
13366 "sun_workshop",
13367#endif
13368#ifdef FEAT_NETBEANS_INTG
13369 "netbeans_intg",
13370#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013371#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013372 "spell",
13373#endif
13374#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013375 "syntax",
13376#endif
13377#if defined(USE_SYSTEM) || !defined(UNIX)
13378 "system",
13379#endif
13380#ifdef FEAT_TAG_BINS
13381 "tag_binary",
13382#endif
13383#ifdef FEAT_TAG_OLDSTATIC
13384 "tag_old_static",
13385#endif
13386#ifdef FEAT_TAG_ANYWHITE
13387 "tag_any_white",
13388#endif
13389#ifdef FEAT_TCL
13390# ifndef DYNAMIC_TCL
13391 "tcl",
13392# endif
13393#endif
13394#ifdef TERMINFO
13395 "terminfo",
13396#endif
13397#ifdef FEAT_TERMRESPONSE
13398 "termresponse",
13399#endif
13400#ifdef FEAT_TEXTOBJ
13401 "textobjects",
13402#endif
13403#ifdef HAVE_TGETENT
13404 "tgetent",
13405#endif
13406#ifdef FEAT_TITLE
13407 "title",
13408#endif
13409#ifdef FEAT_TOOLBAR
13410 "toolbar",
13411#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013412#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13413 "unnamedplus",
13414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013415#ifdef FEAT_USR_CMDS
13416 "user-commands", /* was accidentally included in 5.4 */
13417 "user_commands",
13418#endif
13419#ifdef FEAT_VIMINFO
13420 "viminfo",
13421#endif
13422#ifdef FEAT_VERTSPLIT
13423 "vertsplit",
13424#endif
13425#ifdef FEAT_VIRTUALEDIT
13426 "virtualedit",
13427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013428 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013429#ifdef FEAT_VISUALEXTRA
13430 "visualextra",
13431#endif
13432#ifdef FEAT_VREPLACE
13433 "vreplace",
13434#endif
13435#ifdef FEAT_WILDIGN
13436 "wildignore",
13437#endif
13438#ifdef FEAT_WILDMENU
13439 "wildmenu",
13440#endif
13441#ifdef FEAT_WINDOWS
13442 "windows",
13443#endif
13444#ifdef FEAT_WAK
13445 "winaltkeys",
13446#endif
13447#ifdef FEAT_WRITEBACKUP
13448 "writebackup",
13449#endif
13450#ifdef FEAT_XIM
13451 "xim",
13452#endif
13453#ifdef FEAT_XFONTSET
13454 "xfontset",
13455#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013456#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013457 "xpm",
13458 "xpm_w32", /* for backward compatibility */
13459#else
13460# if defined(HAVE_XPM)
13461 "xpm",
13462# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013463#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013464#ifdef USE_XSMP
13465 "xsmp",
13466#endif
13467#ifdef USE_XSMP_INTERACT
13468 "xsmp_interact",
13469#endif
13470#ifdef FEAT_XCLIPBOARD
13471 "xterm_clipboard",
13472#endif
13473#ifdef FEAT_XTERM_SAVE
13474 "xterm_save",
13475#endif
13476#if defined(UNIX) && defined(FEAT_X11)
13477 "X11",
13478#endif
13479 NULL
13480 };
13481
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013482 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013483 for (i = 0; has_list[i] != NULL; ++i)
13484 if (STRICMP(name, has_list[i]) == 0)
13485 {
13486 n = TRUE;
13487 break;
13488 }
13489
13490 if (n == FALSE)
13491 {
13492 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013493 {
13494 if (name[5] == '-'
13495 && STRLEN(name) > 11
13496 && vim_isdigit(name[6])
13497 && vim_isdigit(name[8])
13498 && vim_isdigit(name[10]))
13499 {
13500 int major = atoi((char *)name + 6);
13501 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013502
13503 /* Expect "patch-9.9.01234". */
13504 n = (major < VIM_VERSION_MAJOR
13505 || (major == VIM_VERSION_MAJOR
13506 && (minor < VIM_VERSION_MINOR
13507 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013508 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013509 }
13510 else
13511 n = has_patch(atoi((char *)name + 5));
13512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013513 else if (STRICMP(name, "vim_starting") == 0)
13514 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013515#ifdef FEAT_MBYTE
13516 else if (STRICMP(name, "multi_byte_encoding") == 0)
13517 n = has_mbyte;
13518#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013519#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13520 else if (STRICMP(name, "balloon_multiline") == 0)
13521 n = multiline_balloon_available();
13522#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013523#ifdef DYNAMIC_TCL
13524 else if (STRICMP(name, "tcl") == 0)
13525 n = tcl_enabled(FALSE);
13526#endif
13527#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13528 else if (STRICMP(name, "iconv") == 0)
13529 n = iconv_enabled(FALSE);
13530#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013531#ifdef DYNAMIC_LUA
13532 else if (STRICMP(name, "lua") == 0)
13533 n = lua_enabled(FALSE);
13534#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013535#ifdef DYNAMIC_MZSCHEME
13536 else if (STRICMP(name, "mzscheme") == 0)
13537 n = mzscheme_enabled(FALSE);
13538#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013539#ifdef DYNAMIC_RUBY
13540 else if (STRICMP(name, "ruby") == 0)
13541 n = ruby_enabled(FALSE);
13542#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013543#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013544#ifdef DYNAMIC_PYTHON
13545 else if (STRICMP(name, "python") == 0)
13546 n = python_enabled(FALSE);
13547#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013548#endif
13549#ifdef FEAT_PYTHON3
13550#ifdef DYNAMIC_PYTHON3
13551 else if (STRICMP(name, "python3") == 0)
13552 n = python3_enabled(FALSE);
13553#endif
13554#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013555#ifdef DYNAMIC_PERL
13556 else if (STRICMP(name, "perl") == 0)
13557 n = perl_enabled(FALSE);
13558#endif
13559#ifdef FEAT_GUI
13560 else if (STRICMP(name, "gui_running") == 0)
13561 n = (gui.in_use || gui.starting);
13562# ifdef FEAT_GUI_W32
13563 else if (STRICMP(name, "gui_win32s") == 0)
13564 n = gui_is_win32s();
13565# endif
13566# ifdef FEAT_BROWSE
13567 else if (STRICMP(name, "browse") == 0)
13568 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13569# endif
13570#endif
13571#ifdef FEAT_SYN_HL
13572 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013573 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013574#endif
13575#if defined(WIN3264)
13576 else if (STRICMP(name, "win95") == 0)
13577 n = mch_windows95();
13578#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013579#ifdef FEAT_NETBEANS_INTG
13580 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013581 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013582#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013583 }
13584
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013585 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013586}
13587
13588/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013589 * "has_key()" function
13590 */
13591 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013592f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013593{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013594 if (argvars[0].v_type != VAR_DICT)
13595 {
13596 EMSG(_(e_dictreq));
13597 return;
13598 }
13599 if (argvars[0].vval.v_dict == NULL)
13600 return;
13601
13602 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013603 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013604}
13605
13606/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013607 * "haslocaldir()" function
13608 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013609 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013610f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013611{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013612 win_T *wp = NULL;
13613
13614 wp = find_tabwin(&argvars[0], &argvars[1]);
13615 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013616}
13617
13618/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013619 * "hasmapto()" function
13620 */
13621 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013622f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013623{
13624 char_u *name;
13625 char_u *mode;
13626 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013627 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013628
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013629 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013630 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013631 mode = (char_u *)"nvo";
13632 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013633 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013634 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013635 if (argvars[2].v_type != VAR_UNKNOWN)
13636 abbr = get_tv_number(&argvars[2]);
13637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013638
Bram Moolenaar2c932302006-03-18 21:42:09 +000013639 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013640 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013641 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013642 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013643}
13644
13645/*
13646 * "histadd()" function
13647 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013648 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013649f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013650{
13651#ifdef FEAT_CMDHIST
13652 int histype;
13653 char_u *str;
13654 char_u buf[NUMBUFLEN];
13655#endif
13656
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013657 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013658 if (check_restricted() || check_secure())
13659 return;
13660#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013661 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13662 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013663 if (histype >= 0)
13664 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013665 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013666 if (*str != NUL)
13667 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013668 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013669 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013670 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013671 return;
13672 }
13673 }
13674#endif
13675}
13676
13677/*
13678 * "histdel()" function
13679 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013680 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013681f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013682{
13683#ifdef FEAT_CMDHIST
13684 int n;
13685 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013686 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013687
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013688 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13689 if (str == NULL)
13690 n = 0;
13691 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013692 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013693 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013694 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013695 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013696 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013697 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013698 else
13699 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013700 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013701 get_tv_string_buf(&argvars[1], buf));
13702 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013703#endif
13704}
13705
13706/*
13707 * "histget()" function
13708 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013710f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013711{
13712#ifdef FEAT_CMDHIST
13713 int type;
13714 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013715 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013716
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013717 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13718 if (str == NULL)
13719 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013720 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013721 {
13722 type = get_histtype(str);
13723 if (argvars[1].v_type == VAR_UNKNOWN)
13724 idx = get_history_idx(type);
13725 else
13726 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13727 /* -1 on type error */
13728 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013730#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013731 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013732#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013733 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013734}
13735
13736/*
13737 * "histnr()" function
13738 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013739 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013740f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013741{
13742 int i;
13743
13744#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013745 char_u *history = get_tv_string_chk(&argvars[0]);
13746
13747 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013748 if (i >= HIST_CMD && i < HIST_COUNT)
13749 i = get_history_idx(i);
13750 else
13751#endif
13752 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013753 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013754}
13755
13756/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013757 * "highlightID(name)" function
13758 */
13759 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013760f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013761{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013762 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013763}
13764
13765/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013766 * "highlight_exists()" function
13767 */
13768 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013769f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013770{
13771 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13772}
13773
13774/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013775 * "hostname()" function
13776 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013777 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013778f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013779{
13780 char_u hostname[256];
13781
13782 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013783 rettv->v_type = VAR_STRING;
13784 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013785}
13786
13787/*
13788 * iconv() function
13789 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013790 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013791f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013792{
13793#ifdef FEAT_MBYTE
13794 char_u buf1[NUMBUFLEN];
13795 char_u buf2[NUMBUFLEN];
13796 char_u *from, *to, *str;
13797 vimconv_T vimconv;
13798#endif
13799
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013800 rettv->v_type = VAR_STRING;
13801 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013802
13803#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013804 str = get_tv_string(&argvars[0]);
13805 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13806 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013807 vimconv.vc_type = CONV_NONE;
13808 convert_setup(&vimconv, from, to);
13809
13810 /* If the encodings are equal, no conversion needed. */
13811 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013812 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013813 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013814 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013815
13816 convert_setup(&vimconv, NULL, NULL);
13817 vim_free(from);
13818 vim_free(to);
13819#endif
13820}
13821
13822/*
13823 * "indent()" function
13824 */
13825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013826f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013827{
13828 linenr_T lnum;
13829
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013830 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013831 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013832 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013833 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013834 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013835}
13836
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013837/*
13838 * "index()" function
13839 */
13840 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013841f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013842{
Bram Moolenaar33570922005-01-25 22:26:29 +000013843 list_T *l;
13844 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013845 long idx = 0;
13846 int ic = FALSE;
13847
13848 rettv->vval.v_number = -1;
13849 if (argvars[0].v_type != VAR_LIST)
13850 {
13851 EMSG(_(e_listreq));
13852 return;
13853 }
13854 l = argvars[0].vval.v_list;
13855 if (l != NULL)
13856 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013857 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013858 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013859 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013860 int error = FALSE;
13861
Bram Moolenaar758711c2005-02-02 23:11:38 +000013862 /* Start at specified item. Use the cached index that list_find()
13863 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013864 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013865 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013866 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013867 ic = get_tv_number_chk(&argvars[3], &error);
13868 if (error)
13869 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013870 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013871
Bram Moolenaar758711c2005-02-02 23:11:38 +000013872 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013873 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013874 {
13875 rettv->vval.v_number = idx;
13876 break;
13877 }
13878 }
13879}
13880
Bram Moolenaar071d4272004-06-13 20:20:40 +000013881static int inputsecret_flag = 0;
13882
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013883static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013884
Bram Moolenaar071d4272004-06-13 20:20:40 +000013885/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013886 * This function is used by f_input() and f_inputdialog() functions. The third
13887 * argument to f_input() specifies the type of completion to use at the
13888 * prompt. The third argument to f_inputdialog() specifies the value to return
13889 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013890 */
13891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013892get_user_input(
13893 typval_T *argvars,
13894 typval_T *rettv,
13895 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013896{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013897 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013898 char_u *p = NULL;
13899 int c;
13900 char_u buf[NUMBUFLEN];
13901 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013902 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013903 int xp_type = EXPAND_NOTHING;
13904 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013905
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013906 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013907 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013908
13909#ifdef NO_CONSOLE_INPUT
13910 /* While starting up, there is no place to enter text. */
13911 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013912 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013913#endif
13914
13915 cmd_silent = FALSE; /* Want to see the prompt. */
13916 if (prompt != NULL)
13917 {
13918 /* Only the part of the message after the last NL is considered as
13919 * prompt for the command line */
13920 p = vim_strrchr(prompt, '\n');
13921 if (p == NULL)
13922 p = prompt;
13923 else
13924 {
13925 ++p;
13926 c = *p;
13927 *p = NUL;
13928 msg_start();
13929 msg_clr_eos();
13930 msg_puts_attr(prompt, echo_attr);
13931 msg_didout = FALSE;
13932 msg_starthere();
13933 *p = c;
13934 }
13935 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013936
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013937 if (argvars[1].v_type != VAR_UNKNOWN)
13938 {
13939 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13940 if (defstr != NULL)
13941 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013942
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013943 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013944 {
13945 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013946 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013947 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013948
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013949 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013950 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013951
Bram Moolenaar4463f292005-09-25 22:20:24 +000013952 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13953 if (xp_name == NULL)
13954 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013955
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013956 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013957
Bram Moolenaar4463f292005-09-25 22:20:24 +000013958 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13959 &xp_arg) == FAIL)
13960 return;
13961 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013962 }
13963
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013964 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013965 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013966 int save_ex_normal_busy = ex_normal_busy;
13967 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013968 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013969 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13970 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013971 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013972 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013973 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013974 && argvars[1].v_type != VAR_UNKNOWN
13975 && argvars[2].v_type != VAR_UNKNOWN)
13976 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13977 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013978
13979 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013980
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013981 /* since the user typed this, no need to wait for return */
13982 need_wait_return = FALSE;
13983 msg_didout = FALSE;
13984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013985 cmd_silent = cmd_silent_save;
13986}
13987
13988/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013989 * "input()" function
13990 * Also handles inputsecret() when inputsecret is set.
13991 */
13992 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013993f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013994{
13995 get_user_input(argvars, rettv, FALSE);
13996}
13997
13998/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013999 * "inputdialog()" function
14000 */
14001 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014002f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014003{
14004#if defined(FEAT_GUI_TEXTDIALOG)
14005 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14006 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14007 {
14008 char_u *message;
14009 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014010 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014011
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014012 message = get_tv_string_chk(&argvars[0]);
14013 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014014 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014015 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014016 else
14017 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014018 if (message != NULL && defstr != NULL
14019 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014020 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014021 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014022 else
14023 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014024 if (message != NULL && defstr != NULL
14025 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014026 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014027 rettv->vval.v_string = vim_strsave(
14028 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014029 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014030 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014031 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014032 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014033 }
14034 else
14035#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014036 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014037}
14038
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014039/*
14040 * "inputlist()" function
14041 */
14042 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014043f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014044{
14045 listitem_T *li;
14046 int selected;
14047 int mouse_used;
14048
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014049#ifdef NO_CONSOLE_INPUT
14050 /* While starting up, there is no place to enter text. */
14051 if (no_console_input())
14052 return;
14053#endif
14054 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14055 {
14056 EMSG2(_(e_listarg), "inputlist()");
14057 return;
14058 }
14059
14060 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014061 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014062 lines_left = Rows; /* avoid more prompt */
14063 msg_scroll = TRUE;
14064 msg_clr_eos();
14065
14066 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14067 {
14068 msg_puts(get_tv_string(&li->li_tv));
14069 msg_putchar('\n');
14070 }
14071
14072 /* Ask for choice. */
14073 selected = prompt_for_number(&mouse_used);
14074 if (mouse_used)
14075 selected -= lines_left;
14076
14077 rettv->vval.v_number = selected;
14078}
14079
14080
Bram Moolenaar071d4272004-06-13 20:20:40 +000014081static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14082
14083/*
14084 * "inputrestore()" function
14085 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014086 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014087f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014088{
14089 if (ga_userinput.ga_len > 0)
14090 {
14091 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014092 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14093 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014094 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014095 }
14096 else if (p_verbose > 1)
14097 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014098 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014099 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014100 }
14101}
14102
14103/*
14104 * "inputsave()" function
14105 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014106 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014107f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014108{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014109 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014110 if (ga_grow(&ga_userinput, 1) == OK)
14111 {
14112 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14113 + ga_userinput.ga_len);
14114 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014115 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014116 }
14117 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014118 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014119}
14120
14121/*
14122 * "inputsecret()" function
14123 */
14124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014125f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014126{
14127 ++cmdline_star;
14128 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014129 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014130 --cmdline_star;
14131 --inputsecret_flag;
14132}
14133
14134/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014135 * "insert()" function
14136 */
14137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014138f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014139{
14140 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014141 listitem_T *item;
14142 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014143 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014144
14145 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014146 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014147 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014148 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014149 {
14150 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014151 before = get_tv_number_chk(&argvars[2], &error);
14152 if (error)
14153 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014154
Bram Moolenaar758711c2005-02-02 23:11:38 +000014155 if (before == l->lv_len)
14156 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014157 else
14158 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014159 item = list_find(l, before);
14160 if (item == NULL)
14161 {
14162 EMSGN(_(e_listidx), before);
14163 l = NULL;
14164 }
14165 }
14166 if (l != NULL)
14167 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014168 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014169 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014170 }
14171 }
14172}
14173
14174/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014175 * "invert(expr)" function
14176 */
14177 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014178f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014179{
14180 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14181}
14182
14183/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014184 * "isdirectory()" function
14185 */
14186 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014187f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014188{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014189 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014190}
14191
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014192/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014193 * "islocked()" function
14194 */
14195 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014196f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014197{
14198 lval_T lv;
14199 char_u *end;
14200 dictitem_T *di;
14201
14202 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014203 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14204 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014205 if (end != NULL && lv.ll_name != NULL)
14206 {
14207 if (*end != NUL)
14208 EMSG(_(e_trailing));
14209 else
14210 {
14211 if (lv.ll_tv == NULL)
14212 {
14213 if (check_changedtick(lv.ll_name))
14214 rettv->vval.v_number = 1; /* always locked */
14215 else
14216 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014217 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014218 if (di != NULL)
14219 {
14220 /* Consider a variable locked when:
14221 * 1. the variable itself is locked
14222 * 2. the value of the variable is locked.
14223 * 3. the List or Dict value is locked.
14224 */
14225 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14226 || tv_islocked(&di->di_tv));
14227 }
14228 }
14229 }
14230 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014231 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014232 else if (lv.ll_newkey != NULL)
14233 EMSG2(_(e_dictkey), lv.ll_newkey);
14234 else if (lv.ll_list != NULL)
14235 /* List item. */
14236 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14237 else
14238 /* Dictionary item. */
14239 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14240 }
14241 }
14242
14243 clear_lval(&lv);
14244}
14245
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014246#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14247/*
14248 * "isnan()" function
14249 */
14250 static void
14251f_isnan(typval_T *argvars, typval_T *rettv)
14252{
14253 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14254 && isnan(argvars[0].vval.v_float);
14255}
14256#endif
14257
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014258static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014259
14260/*
14261 * Turn a dict into a list:
14262 * "what" == 0: list of keys
14263 * "what" == 1: list of values
14264 * "what" == 2: list of items
14265 */
14266 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014267dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014268{
Bram Moolenaar33570922005-01-25 22:26:29 +000014269 list_T *l2;
14270 dictitem_T *di;
14271 hashitem_T *hi;
14272 listitem_T *li;
14273 listitem_T *li2;
14274 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014275 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014276
Bram Moolenaar8c711452005-01-14 21:53:12 +000014277 if (argvars[0].v_type != VAR_DICT)
14278 {
14279 EMSG(_(e_dictreq));
14280 return;
14281 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014282 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014283 return;
14284
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014285 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014286 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014287
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014288 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014289 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014290 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014291 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014292 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014293 --todo;
14294 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014295
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014296 li = listitem_alloc();
14297 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014298 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014299 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014300
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014301 if (what == 0)
14302 {
14303 /* keys() */
14304 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014305 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014306 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14307 }
14308 else if (what == 1)
14309 {
14310 /* values() */
14311 copy_tv(&di->di_tv, &li->li_tv);
14312 }
14313 else
14314 {
14315 /* items() */
14316 l2 = list_alloc();
14317 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014318 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014319 li->li_tv.vval.v_list = l2;
14320 if (l2 == NULL)
14321 break;
14322 ++l2->lv_refcount;
14323
14324 li2 = listitem_alloc();
14325 if (li2 == NULL)
14326 break;
14327 list_append(l2, li2);
14328 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014329 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014330 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14331
14332 li2 = listitem_alloc();
14333 if (li2 == NULL)
14334 break;
14335 list_append(l2, li2);
14336 copy_tv(&di->di_tv, &li2->li_tv);
14337 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014338 }
14339 }
14340}
14341
14342/*
14343 * "items(dict)" function
14344 */
14345 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014346f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014347{
14348 dict_list(argvars, rettv, 2);
14349}
14350
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014351#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014352/*
14353 * Get the job from the argument.
14354 * Returns NULL if the job is invalid.
14355 */
14356 static job_T *
14357get_job_arg(typval_T *tv)
14358{
14359 job_T *job;
14360
14361 if (tv->v_type != VAR_JOB)
14362 {
14363 EMSG2(_(e_invarg2), get_tv_string(tv));
14364 return NULL;
14365 }
14366 job = tv->vval.v_job;
14367
14368 if (job == NULL)
14369 EMSG(_("E916: not a valid job"));
14370 return job;
14371}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014372
Bram Moolenaar835dc632016-02-07 14:27:38 +010014373/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014374 * "job_getchannel()" function
14375 */
14376 static void
14377f_job_getchannel(typval_T *argvars, typval_T *rettv)
14378{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014379 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014380
Bram Moolenaar65edff82016-02-21 16:40:11 +010014381 if (job != NULL)
14382 {
Bram Moolenaar77073442016-02-13 23:23:53 +010014383 rettv->v_type = VAR_CHANNEL;
14384 rettv->vval.v_channel = job->jv_channel;
14385 if (job->jv_channel != NULL)
14386 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014387 }
14388}
14389
14390/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010014391 * "job_info()" function
14392 */
14393 static void
14394f_job_info(typval_T *argvars, typval_T *rettv)
14395{
14396 job_T *job = get_job_arg(&argvars[0]);
14397
14398 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
14399 job_info(job, rettv->vval.v_dict);
14400}
14401
14402/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010014403 * "job_setoptions()" function
14404 */
14405 static void
14406f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
14407{
14408 job_T *job = get_job_arg(&argvars[0]);
14409 jobopt_T opt;
14410
14411 if (job == NULL)
14412 return;
14413 clear_job_options(&opt);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014414 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014415 return;
14416 job_set_options(job, &opt);
14417}
14418
14419/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014420 * "job_start()" function
14421 */
14422 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010014423f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014424{
Bram Moolenaar835dc632016-02-07 14:27:38 +010014425 rettv->v_type = VAR_JOB;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014426 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014427}
14428
14429/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014430 * "job_status()" function
14431 */
14432 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014433f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014434{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014435 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014436
Bram Moolenaar65edff82016-02-21 16:40:11 +010014437 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014438 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010014439 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010014440 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010014441 }
14442}
14443
14444/*
14445 * "job_stop()" function
14446 */
14447 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014448f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014449{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014450 job_T *job = get_job_arg(&argvars[0]);
14451
14452 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014453 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014454}
14455#endif
14456
Bram Moolenaar071d4272004-06-13 20:20:40 +000014457/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014458 * "join()" function
14459 */
14460 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014461f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014462{
14463 garray_T ga;
14464 char_u *sep;
14465
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014466 if (argvars[0].v_type != VAR_LIST)
14467 {
14468 EMSG(_(e_listreq));
14469 return;
14470 }
14471 if (argvars[0].vval.v_list == NULL)
14472 return;
14473 if (argvars[1].v_type == VAR_UNKNOWN)
14474 sep = (char_u *)" ";
14475 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014476 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014477
14478 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014479
14480 if (sep != NULL)
14481 {
14482 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014483 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014484 ga_append(&ga, NUL);
14485 rettv->vval.v_string = (char_u *)ga.ga_data;
14486 }
14487 else
14488 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014489}
14490
14491/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014492 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014493 */
14494 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014495f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014496{
14497 js_read_T reader;
14498
14499 reader.js_buf = get_tv_string(&argvars[0]);
14500 reader.js_fill = NULL;
14501 reader.js_used = 0;
14502 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
14503 EMSG(_(e_invarg));
14504}
14505
14506/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014507 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014508 */
14509 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014510f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014511{
14512 rettv->v_type = VAR_STRING;
14513 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
14514}
14515
14516/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014517 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014518 */
14519 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014520f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014521{
14522 js_read_T reader;
14523
14524 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010014525 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014526 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014527 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014528 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014529}
14530
14531/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014532 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014533 */
14534 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014535f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014536{
14537 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014538 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014539}
14540
14541/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014542 * "keys()" function
14543 */
14544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014545f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014546{
14547 dict_list(argvars, rettv, 0);
14548}
14549
14550/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014551 * "last_buffer_nr()" function.
14552 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014554f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014555{
14556 int n = 0;
14557 buf_T *buf;
14558
14559 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14560 if (n < buf->b_fnum)
14561 n = buf->b_fnum;
14562
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014563 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014564}
14565
14566/*
14567 * "len()" function
14568 */
14569 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014570f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014571{
14572 switch (argvars[0].v_type)
14573 {
14574 case VAR_STRING:
14575 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014576 rettv->vval.v_number = (varnumber_T)STRLEN(
14577 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014578 break;
14579 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014580 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014581 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014582 case VAR_DICT:
14583 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14584 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010014585 case VAR_UNKNOWN:
14586 case VAR_SPECIAL:
14587 case VAR_FLOAT:
14588 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010014589 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010014590 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014591 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014592 break;
14593 }
14594}
14595
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014596static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014597
14598 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014599libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014600{
14601#ifdef FEAT_LIBCALL
14602 char_u *string_in;
14603 char_u **string_result;
14604 int nr_result;
14605#endif
14606
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014607 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014608 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014609 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014610
14611 if (check_restricted() || check_secure())
14612 return;
14613
14614#ifdef FEAT_LIBCALL
14615 /* The first two args must be strings, otherwise its meaningless */
14616 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14617 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014618 string_in = NULL;
14619 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014620 string_in = argvars[2].vval.v_string;
14621 if (type == VAR_NUMBER)
14622 string_result = NULL;
14623 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014624 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014625 if (mch_libcall(argvars[0].vval.v_string,
14626 argvars[1].vval.v_string,
14627 string_in,
14628 argvars[2].vval.v_number,
14629 string_result,
14630 &nr_result) == OK
14631 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014632 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014633 }
14634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014635}
14636
14637/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014638 * "libcall()" function
14639 */
14640 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014641f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014642{
14643 libcall_common(argvars, rettv, VAR_STRING);
14644}
14645
14646/*
14647 * "libcallnr()" function
14648 */
14649 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014650f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014651{
14652 libcall_common(argvars, rettv, VAR_NUMBER);
14653}
14654
14655/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014656 * "line(string)" function
14657 */
14658 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014659f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014660{
14661 linenr_T lnum = 0;
14662 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014663 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014664
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014665 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014666 if (fp != NULL)
14667 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014668 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014669}
14670
14671/*
14672 * "line2byte(lnum)" function
14673 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014674 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014675f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014676{
14677#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014678 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014679#else
14680 linenr_T lnum;
14681
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014682 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014683 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014684 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014685 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014686 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14687 if (rettv->vval.v_number >= 0)
14688 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014689#endif
14690}
14691
14692/*
14693 * "lispindent(lnum)" function
14694 */
14695 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014696f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014697{
14698#ifdef FEAT_LISP
14699 pos_T pos;
14700 linenr_T lnum;
14701
14702 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014703 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014704 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14705 {
14706 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014707 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014708 curwin->w_cursor = pos;
14709 }
14710 else
14711#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014712 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014713}
14714
14715/*
14716 * "localtime()" function
14717 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014718 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014719f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014720{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014721 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014722}
14723
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014724static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014725
14726 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014727get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014728{
14729 char_u *keys;
14730 char_u *which;
14731 char_u buf[NUMBUFLEN];
14732 char_u *keys_buf = NULL;
14733 char_u *rhs;
14734 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014735 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014736 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014737 mapblock_T *mp;
14738 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014739
14740 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014741 rettv->v_type = VAR_STRING;
14742 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014743
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014744 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014745 if (*keys == NUL)
14746 return;
14747
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014748 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014749 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014750 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014751 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014752 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014753 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014754 if (argvars[3].v_type != VAR_UNKNOWN)
14755 get_dict = get_tv_number(&argvars[3]);
14756 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014758 else
14759 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014760 if (which == NULL)
14761 return;
14762
Bram Moolenaar071d4272004-06-13 20:20:40 +000014763 mode = get_map_mode(&which, 0);
14764
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014765 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014766 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014767 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014768
14769 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014770 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014771 /* Return a string. */
14772 if (rhs != NULL)
14773 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014774
Bram Moolenaarbd743252010-10-20 21:23:33 +020014775 }
14776 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14777 {
14778 /* Return a dictionary. */
14779 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14780 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14781 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014782
Bram Moolenaarbd743252010-10-20 21:23:33 +020014783 dict_add_nr_str(dict, "lhs", 0L, lhs);
14784 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14785 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14786 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14787 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14788 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14789 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014790 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014791 dict_add_nr_str(dict, "mode", 0L, mapmode);
14792
14793 vim_free(lhs);
14794 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014795 }
14796}
14797
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014798#ifdef FEAT_FLOAT
14799/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014800 * "log()" function
14801 */
14802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014803f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014804{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010014805 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014806
14807 rettv->v_type = VAR_FLOAT;
14808 if (get_float_arg(argvars, &f) == OK)
14809 rettv->vval.v_float = log(f);
14810 else
14811 rettv->vval.v_float = 0.0;
14812}
14813
14814/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014815 * "log10()" function
14816 */
14817 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014818f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014819{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010014820 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014821
14822 rettv->v_type = VAR_FLOAT;
14823 if (get_float_arg(argvars, &f) == OK)
14824 rettv->vval.v_float = log10(f);
14825 else
14826 rettv->vval.v_float = 0.0;
14827}
14828#endif
14829
Bram Moolenaar1dced572012-04-05 16:54:08 +020014830#ifdef FEAT_LUA
14831/*
14832 * "luaeval()" function
14833 */
14834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014835f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020014836{
14837 char_u *str;
14838 char_u buf[NUMBUFLEN];
14839
14840 str = get_tv_string_buf(&argvars[0], buf);
14841 do_luaeval(str, argvars + 1, rettv);
14842}
14843#endif
14844
Bram Moolenaar071d4272004-06-13 20:20:40 +000014845/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014846 * "map()" function
14847 */
14848 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014849f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014850{
14851 filter_map(argvars, rettv, TRUE);
14852}
14853
14854/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014855 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014856 */
14857 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014858f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014859{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014860 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014861}
14862
14863/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014864 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014865 */
14866 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014867f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014868{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014869 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014870}
14871
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014872static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014873
14874 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014875find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014876{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014877 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014878 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014879 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014880 char_u *pat;
14881 regmatch_T regmatch;
14882 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014883 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014884 char_u *save_cpo;
14885 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014886 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014887 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014888 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014889 list_T *l = NULL;
14890 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014891 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014892 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014893
14894 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14895 save_cpo = p_cpo;
14896 p_cpo = (char_u *)"";
14897
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014898 rettv->vval.v_number = -1;
14899 if (type == 3)
14900 {
14901 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014902 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014903 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014904 }
14905 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014906 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014907 rettv->v_type = VAR_STRING;
14908 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014910
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014911 if (argvars[0].v_type == VAR_LIST)
14912 {
14913 if ((l = argvars[0].vval.v_list) == NULL)
14914 goto theend;
14915 li = l->lv_first;
14916 }
14917 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014918 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014919 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014920 len = (long)STRLEN(str);
14921 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014922
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014923 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14924 if (pat == NULL)
14925 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014926
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014927 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014928 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014929 int error = FALSE;
14930
14931 start = get_tv_number_chk(&argvars[2], &error);
14932 if (error)
14933 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014934 if (l != NULL)
14935 {
14936 li = list_find(l, start);
14937 if (li == NULL)
14938 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014939 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014940 }
14941 else
14942 {
14943 if (start < 0)
14944 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014945 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014946 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014947 /* When "count" argument is there ignore matches before "start",
14948 * otherwise skip part of the string. Differs when pattern is "^"
14949 * or "\<". */
14950 if (argvars[3].v_type != VAR_UNKNOWN)
14951 startcol = start;
14952 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014953 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014954 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014955 len -= start;
14956 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014957 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014958
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014959 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014960 nth = get_tv_number_chk(&argvars[3], &error);
14961 if (error)
14962 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014963 }
14964
14965 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14966 if (regmatch.regprog != NULL)
14967 {
14968 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014969
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014970 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014971 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014972 if (l != NULL)
14973 {
14974 if (li == NULL)
14975 {
14976 match = FALSE;
14977 break;
14978 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014979 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014980 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014981 if (str == NULL)
14982 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014983 }
14984
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014985 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014986
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014987 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014988 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014989 if (l == NULL && !match)
14990 break;
14991
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014992 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014993 if (l != NULL)
14994 {
14995 li = li->li_next;
14996 ++idx;
14997 }
14998 else
14999 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015000#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015001 startcol = (colnr_T)(regmatch.startp[0]
15002 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015003#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015004 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015005#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015006 if (startcol > (colnr_T)len
15007 || str + startcol <= regmatch.startp[0])
15008 {
15009 match = FALSE;
15010 break;
15011 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015012 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015013 }
15014
15015 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015016 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015017 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015018 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015019 int i;
15020
15021 /* return list with matched string and submatches */
15022 for (i = 0; i < NSUBEXP; ++i)
15023 {
15024 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015025 {
15026 if (list_append_string(rettv->vval.v_list,
15027 (char_u *)"", 0) == FAIL)
15028 break;
15029 }
15030 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015031 regmatch.startp[i],
15032 (int)(regmatch.endp[i] - regmatch.startp[i]))
15033 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015034 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015035 }
15036 }
15037 else if (type == 2)
15038 {
15039 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015040 if (l != NULL)
15041 copy_tv(&li->li_tv, rettv);
15042 else
15043 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015044 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015045 }
15046 else if (l != NULL)
15047 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015048 else
15049 {
15050 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015051 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015052 (varnumber_T)(regmatch.startp[0] - str);
15053 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015054 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015055 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015056 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015057 }
15058 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015059 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015060 }
15061
15062theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015063 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015064 p_cpo = save_cpo;
15065}
15066
15067/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015068 * "match()" function
15069 */
15070 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015071f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015072{
15073 find_some_match(argvars, rettv, 1);
15074}
15075
15076/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015077 * "matchadd()" function
15078 */
15079 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015080f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015081{
15082#ifdef FEAT_SEARCH_EXTRA
15083 char_u buf[NUMBUFLEN];
15084 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15085 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15086 int prio = 10; /* default priority */
15087 int id = -1;
15088 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015089 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015090
15091 rettv->vval.v_number = -1;
15092
15093 if (grp == NULL || pat == NULL)
15094 return;
15095 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015096 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015097 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015098 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015099 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015100 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015101 if (argvars[4].v_type != VAR_UNKNOWN)
15102 {
15103 if (argvars[4].v_type != VAR_DICT)
15104 {
15105 EMSG(_(e_dictreq));
15106 return;
15107 }
15108 if (dict_find(argvars[4].vval.v_dict,
15109 (char_u *)"conceal", -1) != NULL)
15110 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15111 (char_u *)"conceal", FALSE);
15112 }
15113 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015114 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015115 if (error == TRUE)
15116 return;
15117 if (id >= 1 && id <= 3)
15118 {
15119 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15120 return;
15121 }
15122
Bram Moolenaar6561d522015-07-21 15:48:27 +020015123 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15124 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015125#endif
15126}
15127
15128/*
15129 * "matchaddpos()" function
15130 */
15131 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015132f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015133{
15134#ifdef FEAT_SEARCH_EXTRA
15135 char_u buf[NUMBUFLEN];
15136 char_u *group;
15137 int prio = 10;
15138 int id = -1;
15139 int error = FALSE;
15140 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015141 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015142
15143 rettv->vval.v_number = -1;
15144
15145 group = get_tv_string_buf_chk(&argvars[0], buf);
15146 if (group == NULL)
15147 return;
15148
15149 if (argvars[1].v_type != VAR_LIST)
15150 {
15151 EMSG2(_(e_listarg), "matchaddpos()");
15152 return;
15153 }
15154 l = argvars[1].vval.v_list;
15155 if (l == NULL)
15156 return;
15157
15158 if (argvars[2].v_type != VAR_UNKNOWN)
15159 {
15160 prio = get_tv_number_chk(&argvars[2], &error);
15161 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015162 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015163 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015164 if (argvars[4].v_type != VAR_UNKNOWN)
15165 {
15166 if (argvars[4].v_type != VAR_DICT)
15167 {
15168 EMSG(_(e_dictreq));
15169 return;
15170 }
15171 if (dict_find(argvars[4].vval.v_dict,
15172 (char_u *)"conceal", -1) != NULL)
15173 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15174 (char_u *)"conceal", FALSE);
15175 }
15176 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015177 }
15178 if (error == TRUE)
15179 return;
15180
15181 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15182 if (id == 1 || id == 2)
15183 {
15184 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15185 return;
15186 }
15187
Bram Moolenaar6561d522015-07-21 15:48:27 +020015188 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15189 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015190#endif
15191}
15192
15193/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015194 * "matcharg()" function
15195 */
15196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015197f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015198{
15199 if (rettv_list_alloc(rettv) == OK)
15200 {
15201#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015202 int id = get_tv_number(&argvars[0]);
15203 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015204
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015205 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015206 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015207 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15208 {
15209 list_append_string(rettv->vval.v_list,
15210 syn_id2name(m->hlg_id), -1);
15211 list_append_string(rettv->vval.v_list, m->pattern, -1);
15212 }
15213 else
15214 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015215 list_append_string(rettv->vval.v_list, NULL, -1);
15216 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015217 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015218 }
15219#endif
15220 }
15221}
15222
15223/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015224 * "matchdelete()" function
15225 */
15226 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015227f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015228{
15229#ifdef FEAT_SEARCH_EXTRA
15230 rettv->vval.v_number = match_delete(curwin,
15231 (int)get_tv_number(&argvars[0]), TRUE);
15232#endif
15233}
15234
15235/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015236 * "matchend()" function
15237 */
15238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015239f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015240{
15241 find_some_match(argvars, rettv, 0);
15242}
15243
15244/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015245 * "matchlist()" function
15246 */
15247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015248f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015249{
15250 find_some_match(argvars, rettv, 3);
15251}
15252
15253/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015254 * "matchstr()" function
15255 */
15256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015257f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015258{
15259 find_some_match(argvars, rettv, 2);
15260}
15261
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015262static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015263
15264 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015265max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015266{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015267 long n = 0;
15268 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015269 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015270
15271 if (argvars[0].v_type == VAR_LIST)
15272 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015273 list_T *l;
15274 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015275
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015276 l = argvars[0].vval.v_list;
15277 if (l != NULL)
15278 {
15279 li = l->lv_first;
15280 if (li != NULL)
15281 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015282 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015283 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015284 {
15285 li = li->li_next;
15286 if (li == NULL)
15287 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015288 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015289 if (domax ? i > n : i < n)
15290 n = i;
15291 }
15292 }
15293 }
15294 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015295 else if (argvars[0].v_type == VAR_DICT)
15296 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015297 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015298 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015299 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015300 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015301
15302 d = argvars[0].vval.v_dict;
15303 if (d != NULL)
15304 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015305 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015306 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015307 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015308 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015309 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015310 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015311 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015312 if (first)
15313 {
15314 n = i;
15315 first = FALSE;
15316 }
15317 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015318 n = i;
15319 }
15320 }
15321 }
15322 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015323 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015324 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015325 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015326}
15327
15328/*
15329 * "max()" function
15330 */
15331 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015332f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015333{
15334 max_min(argvars, rettv, TRUE);
15335}
15336
15337/*
15338 * "min()" function
15339 */
15340 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015341f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015342{
15343 max_min(argvars, rettv, FALSE);
15344}
15345
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015346static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015347
15348/*
15349 * Create the directory in which "dir" is located, and higher levels when
15350 * needed.
15351 */
15352 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015353mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015354{
15355 char_u *p;
15356 char_u *updir;
15357 int r = FAIL;
15358
15359 /* Get end of directory name in "dir".
15360 * We're done when it's "/" or "c:/". */
15361 p = gettail_sep(dir);
15362 if (p <= get_past_head(dir))
15363 return OK;
15364
15365 /* If the directory exists we're done. Otherwise: create it.*/
15366 updir = vim_strnsave(dir, (int)(p - dir));
15367 if (updir == NULL)
15368 return FAIL;
15369 if (mch_isdir(updir))
15370 r = OK;
15371 else if (mkdir_recurse(updir, prot) == OK)
15372 r = vim_mkdir_emsg(updir, prot);
15373 vim_free(updir);
15374 return r;
15375}
15376
15377#ifdef vim_mkdir
15378/*
15379 * "mkdir()" function
15380 */
15381 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015382f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015383{
15384 char_u *dir;
15385 char_u buf[NUMBUFLEN];
15386 int prot = 0755;
15387
15388 rettv->vval.v_number = FAIL;
15389 if (check_restricted() || check_secure())
15390 return;
15391
15392 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015393 if (*dir == NUL)
15394 rettv->vval.v_number = FAIL;
15395 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015396 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015397 if (*gettail(dir) == NUL)
15398 /* remove trailing slashes */
15399 *gettail_sep(dir) = NUL;
15400
15401 if (argvars[1].v_type != VAR_UNKNOWN)
15402 {
15403 if (argvars[2].v_type != VAR_UNKNOWN)
15404 prot = get_tv_number_chk(&argvars[2], NULL);
15405 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15406 mkdir_recurse(dir, prot);
15407 }
15408 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015409 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015410}
15411#endif
15412
Bram Moolenaar0d660222005-01-07 21:51:51 +000015413/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015414 * "mode()" function
15415 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015416 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015417f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015418{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015419 char_u buf[3];
15420
15421 buf[1] = NUL;
15422 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015423
Bram Moolenaar071d4272004-06-13 20:20:40 +000015424 if (VIsual_active)
15425 {
15426 if (VIsual_select)
15427 buf[0] = VIsual_mode + 's' - 'v';
15428 else
15429 buf[0] = VIsual_mode;
15430 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015431 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015432 || State == CONFIRM)
15433 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015434 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015435 if (State == ASKMORE)
15436 buf[1] = 'm';
15437 else if (State == CONFIRM)
15438 buf[1] = '?';
15439 }
15440 else if (State == EXTERNCMD)
15441 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015442 else if (State & INSERT)
15443 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015444#ifdef FEAT_VREPLACE
15445 if (State & VREPLACE_FLAG)
15446 {
15447 buf[0] = 'R';
15448 buf[1] = 'v';
15449 }
15450 else
15451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015452 if (State & REPLACE_FLAG)
15453 buf[0] = 'R';
15454 else
15455 buf[0] = 'i';
15456 }
15457 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015458 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015459 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015460 if (exmode_active)
15461 buf[1] = 'v';
15462 }
15463 else if (exmode_active)
15464 {
15465 buf[0] = 'c';
15466 buf[1] = 'e';
15467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015468 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015469 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015470 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015471 if (finish_op)
15472 buf[1] = 'o';
15473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015474
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015475 /* Clear out the minor mode when the argument is not a non-zero number or
15476 * non-empty string. */
15477 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015478 buf[1] = NUL;
15479
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015480 rettv->vval.v_string = vim_strsave(buf);
15481 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015482}
15483
Bram Moolenaar429fa852013-04-15 12:27:36 +020015484#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015485/*
15486 * "mzeval()" function
15487 */
15488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015489f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015490{
15491 char_u *str;
15492 char_u buf[NUMBUFLEN];
15493
15494 str = get_tv_string_buf(&argvars[0], buf);
15495 do_mzeval(str, rettv);
15496}
Bram Moolenaar75676462013-01-30 14:55:42 +010015497
15498 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015499mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015500{
15501 typval_T argvars[3];
15502
15503 argvars[0].v_type = VAR_STRING;
15504 argvars[0].vval.v_string = name;
15505 copy_tv(args, &argvars[1]);
15506 argvars[2].v_type = VAR_UNKNOWN;
15507 f_call(argvars, rettv);
15508 clear_tv(&argvars[1]);
15509}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015510#endif
15511
Bram Moolenaar071d4272004-06-13 20:20:40 +000015512/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015513 * "nextnonblank()" function
15514 */
15515 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015516f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015517{
15518 linenr_T lnum;
15519
15520 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15521 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015522 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015523 {
15524 lnum = 0;
15525 break;
15526 }
15527 if (*skipwhite(ml_get(lnum)) != NUL)
15528 break;
15529 }
15530 rettv->vval.v_number = lnum;
15531}
15532
15533/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015534 * "nr2char()" function
15535 */
15536 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015537f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015538{
15539 char_u buf[NUMBUFLEN];
15540
15541#ifdef FEAT_MBYTE
15542 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015543 {
15544 int utf8 = 0;
15545
15546 if (argvars[1].v_type != VAR_UNKNOWN)
15547 utf8 = get_tv_number_chk(&argvars[1], NULL);
15548 if (utf8)
15549 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15550 else
15551 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015553 else
15554#endif
15555 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015556 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015557 buf[1] = NUL;
15558 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015559 rettv->v_type = VAR_STRING;
15560 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015561}
15562
15563/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015564 * "or(expr, expr)" function
15565 */
15566 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015567f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015568{
15569 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15570 | get_tv_number_chk(&argvars[1], NULL);
15571}
15572
15573/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015574 * "pathshorten()" function
15575 */
15576 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015577f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015578{
15579 char_u *p;
15580
15581 rettv->v_type = VAR_STRING;
15582 p = get_tv_string_chk(&argvars[0]);
15583 if (p == NULL)
15584 rettv->vval.v_string = NULL;
15585 else
15586 {
15587 p = vim_strsave(p);
15588 rettv->vval.v_string = p;
15589 if (p != NULL)
15590 shorten_dir(p);
15591 }
15592}
15593
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015594#ifdef FEAT_PERL
15595/*
15596 * "perleval()" function
15597 */
15598 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015599f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015600{
15601 char_u *str;
15602 char_u buf[NUMBUFLEN];
15603
15604 str = get_tv_string_buf(&argvars[0], buf);
15605 do_perleval(str, rettv);
15606}
15607#endif
15608
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015609#ifdef FEAT_FLOAT
15610/*
15611 * "pow()" function
15612 */
15613 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015614f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015615{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015616 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015617
15618 rettv->v_type = VAR_FLOAT;
15619 if (get_float_arg(argvars, &fx) == OK
15620 && get_float_arg(&argvars[1], &fy) == OK)
15621 rettv->vval.v_float = pow(fx, fy);
15622 else
15623 rettv->vval.v_float = 0.0;
15624}
15625#endif
15626
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015627/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015628 * "prevnonblank()" function
15629 */
15630 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015631f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015632{
15633 linenr_T lnum;
15634
15635 lnum = get_tv_lnum(argvars);
15636 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15637 lnum = 0;
15638 else
15639 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15640 --lnum;
15641 rettv->vval.v_number = lnum;
15642}
15643
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015644/* This dummy va_list is here because:
15645 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15646 * - locally in the function results in a "used before set" warning
15647 * - using va_start() to initialize it gives "function with fixed args" error */
15648static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015649
Bram Moolenaar8c711452005-01-14 21:53:12 +000015650/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015651 * "printf()" function
15652 */
15653 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015654f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015655{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015656 char_u buf[NUMBUFLEN];
15657 int len;
15658 char_u *s;
15659 int saved_did_emsg = did_emsg;
15660 char *fmt;
15661
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015662 rettv->v_type = VAR_STRING;
15663 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015664
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015665 /* Get the required length, allocate the buffer and do it for real. */
15666 did_emsg = FALSE;
15667 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
15668 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
15669 if (!did_emsg)
15670 {
15671 s = alloc(len + 1);
15672 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015673 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015674 rettv->vval.v_string = s;
15675 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015676 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015677 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015678 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015679}
15680
15681/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015682 * "pumvisible()" function
15683 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015684 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015685f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015686{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015687#ifdef FEAT_INS_EXPAND
15688 if (pum_visible())
15689 rettv->vval.v_number = 1;
15690#endif
15691}
15692
Bram Moolenaardb913952012-06-29 12:54:53 +020015693#ifdef FEAT_PYTHON3
15694/*
15695 * "py3eval()" function
15696 */
15697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015698f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015699{
15700 char_u *str;
15701 char_u buf[NUMBUFLEN];
15702
15703 str = get_tv_string_buf(&argvars[0], buf);
15704 do_py3eval(str, rettv);
15705}
15706#endif
15707
15708#ifdef FEAT_PYTHON
15709/*
15710 * "pyeval()" function
15711 */
15712 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015713f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015714{
15715 char_u *str;
15716 char_u buf[NUMBUFLEN];
15717
15718 str = get_tv_string_buf(&argvars[0], buf);
15719 do_pyeval(str, rettv);
15720}
15721#endif
15722
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015723/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015724 * "range()" function
15725 */
15726 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015727f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015728{
15729 long start;
15730 long end;
15731 long stride = 1;
15732 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015733 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015734
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015735 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015736 if (argvars[1].v_type == VAR_UNKNOWN)
15737 {
15738 end = start - 1;
15739 start = 0;
15740 }
15741 else
15742 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015743 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015744 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015745 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015746 }
15747
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015748 if (error)
15749 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015750 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015751 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015752 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015753 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015754 else
15755 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015756 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015757 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015758 if (list_append_number(rettv->vval.v_list,
15759 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015760 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015761 }
15762}
15763
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015764/*
15765 * "readfile()" function
15766 */
15767 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015768f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015769{
15770 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015771 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015772 char_u *fname;
15773 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015774 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15775 int io_size = sizeof(buf);
15776 int readlen; /* size of last fread() */
15777 char_u *prev = NULL; /* previously read bytes, if any */
15778 long prevlen = 0; /* length of data in prev */
15779 long prevsize = 0; /* size of prev buffer */
15780 long maxline = MAXLNUM;
15781 long cnt = 0;
15782 char_u *p; /* position in buf */
15783 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015784
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015785 if (argvars[1].v_type != VAR_UNKNOWN)
15786 {
15787 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15788 binary = TRUE;
15789 if (argvars[2].v_type != VAR_UNKNOWN)
15790 maxline = get_tv_number(&argvars[2]);
15791 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015792
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015793 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015794 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015795
15796 /* Always open the file in binary mode, library functions have a mind of
15797 * their own about CR-LF conversion. */
15798 fname = get_tv_string(&argvars[0]);
15799 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15800 {
15801 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15802 return;
15803 }
15804
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015805 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015806 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015807 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015808
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015809 /* This for loop processes what was read, but is also entered at end
15810 * of file so that either:
15811 * - an incomplete line gets written
15812 * - a "binary" file gets an empty line at the end if it ends in a
15813 * newline. */
15814 for (p = buf, start = buf;
15815 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15816 ++p)
15817 {
15818 if (*p == '\n' || readlen <= 0)
15819 {
15820 listitem_T *li;
15821 char_u *s = NULL;
15822 long_u len = p - start;
15823
15824 /* Finished a line. Remove CRs before NL. */
15825 if (readlen > 0 && !binary)
15826 {
15827 while (len > 0 && start[len - 1] == '\r')
15828 --len;
15829 /* removal may cross back to the "prev" string */
15830 if (len == 0)
15831 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15832 --prevlen;
15833 }
15834 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015835 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015836 else
15837 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015838 /* Change "prev" buffer to be the right size. This way
15839 * the bytes are only copied once, and very long lines are
15840 * allocated only once. */
15841 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015842 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015843 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015844 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015845 prev = NULL; /* the list will own the string */
15846 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015847 }
15848 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015849 if (s == NULL)
15850 {
15851 do_outofmem_msg((long_u) prevlen + len + 1);
15852 failed = TRUE;
15853 break;
15854 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015855
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015856 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015857 {
15858 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015859 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015860 break;
15861 }
15862 li->li_tv.v_type = VAR_STRING;
15863 li->li_tv.v_lock = 0;
15864 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015865 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015866
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015867 start = p + 1; /* step over newline */
15868 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015869 break;
15870 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015871 else if (*p == NUL)
15872 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015873#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015874 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15875 * when finding the BF and check the previous two bytes. */
15876 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015877 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015878 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15879 * + 1, these may be in the "prev" string. */
15880 char_u back1 = p >= buf + 1 ? p[-1]
15881 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15882 char_u back2 = p >= buf + 2 ? p[-2]
15883 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15884 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015885
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015886 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015887 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015888 char_u *dest = p - 2;
15889
15890 /* Usually a BOM is at the beginning of a file, and so at
15891 * the beginning of a line; then we can just step over it.
15892 */
15893 if (start == dest)
15894 start = p + 1;
15895 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015896 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015897 /* have to shuffle buf to close gap */
15898 int adjust_prevlen = 0;
15899
15900 if (dest < buf)
15901 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015902 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015903 dest = buf;
15904 }
15905 if (readlen > p - buf + 1)
15906 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15907 readlen -= 3 - adjust_prevlen;
15908 prevlen -= adjust_prevlen;
15909 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015910 }
15911 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015912 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015913#endif
15914 } /* for */
15915
15916 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15917 break;
15918 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015919 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015920 /* There's part of a line in buf, store it in "prev". */
15921 if (p - start + prevlen >= prevsize)
15922 {
15923 /* need bigger "prev" buffer */
15924 char_u *newprev;
15925
15926 /* A common use case is ordinary text files and "prev" gets a
15927 * fragment of a line, so the first allocation is made
15928 * small, to avoid repeatedly 'allocing' large and
15929 * 'reallocing' small. */
15930 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015931 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015932 else
15933 {
15934 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015935 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015936 prevsize = grow50pc > growmin ? grow50pc : growmin;
15937 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015938 newprev = prev == NULL ? alloc(prevsize)
15939 : vim_realloc(prev, prevsize);
15940 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015941 {
15942 do_outofmem_msg((long_u)prevsize);
15943 failed = TRUE;
15944 break;
15945 }
15946 prev = newprev;
15947 }
15948 /* Add the line part to end of "prev". */
15949 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015950 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015951 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015952 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015953
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015954 /*
15955 * For a negative line count use only the lines at the end of the file,
15956 * free the rest.
15957 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015958 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015959 while (cnt > -maxline)
15960 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015961 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015962 --cnt;
15963 }
15964
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015965 if (failed)
15966 {
15967 list_free(rettv->vval.v_list, TRUE);
15968 /* readfile doc says an empty list is returned on error */
15969 rettv->vval.v_list = list_alloc();
15970 }
15971
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015972 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015973 fclose(fd);
15974}
15975
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015976#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015977static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015978
15979/*
15980 * Convert a List to proftime_T.
15981 * Return FAIL when there is something wrong.
15982 */
15983 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015984list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015985{
15986 long n1, n2;
15987 int error = FALSE;
15988
15989 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15990 || arg->vval.v_list->lv_len != 2)
15991 return FAIL;
15992 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15993 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15994# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015995 tm->HighPart = n1;
15996 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015997# else
15998 tm->tv_sec = n1;
15999 tm->tv_usec = n2;
16000# endif
16001 return error ? FAIL : OK;
16002}
16003#endif /* FEAT_RELTIME */
16004
16005/*
16006 * "reltime()" function
16007 */
16008 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016009f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016010{
16011#ifdef FEAT_RELTIME
16012 proftime_T res;
16013 proftime_T start;
16014
16015 if (argvars[0].v_type == VAR_UNKNOWN)
16016 {
16017 /* No arguments: get current time. */
16018 profile_start(&res);
16019 }
16020 else if (argvars[1].v_type == VAR_UNKNOWN)
16021 {
16022 if (list2proftime(&argvars[0], &res) == FAIL)
16023 return;
16024 profile_end(&res);
16025 }
16026 else
16027 {
16028 /* Two arguments: compute the difference. */
16029 if (list2proftime(&argvars[0], &start) == FAIL
16030 || list2proftime(&argvars[1], &res) == FAIL)
16031 return;
16032 profile_sub(&res, &start);
16033 }
16034
16035 if (rettv_list_alloc(rettv) == OK)
16036 {
16037 long n1, n2;
16038
16039# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016040 n1 = res.HighPart;
16041 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016042# else
16043 n1 = res.tv_sec;
16044 n2 = res.tv_usec;
16045# endif
16046 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16047 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16048 }
16049#endif
16050}
16051
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016052#ifdef FEAT_FLOAT
16053/*
16054 * "reltimefloat()" function
16055 */
16056 static void
16057f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16058{
16059# ifdef FEAT_RELTIME
16060 proftime_T tm;
16061# endif
16062
16063 rettv->v_type = VAR_FLOAT;
16064 rettv->vval.v_float = 0;
16065# ifdef FEAT_RELTIME
16066 if (list2proftime(&argvars[0], &tm) == OK)
16067 rettv->vval.v_float = profile_float(&tm);
16068# endif
16069}
16070#endif
16071
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016072/*
16073 * "reltimestr()" function
16074 */
16075 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016076f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016077{
16078#ifdef FEAT_RELTIME
16079 proftime_T tm;
16080#endif
16081
16082 rettv->v_type = VAR_STRING;
16083 rettv->vval.v_string = NULL;
16084#ifdef FEAT_RELTIME
16085 if (list2proftime(&argvars[0], &tm) == OK)
16086 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16087#endif
16088}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016089
Bram Moolenaar0d660222005-01-07 21:51:51 +000016090#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016091static void make_connection(void);
16092static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016093
16094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016095make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016096{
16097 if (X_DISPLAY == NULL
16098# ifdef FEAT_GUI
16099 && !gui.in_use
16100# endif
16101 )
16102 {
16103 x_force_connect = TRUE;
16104 setup_term_clip();
16105 x_force_connect = FALSE;
16106 }
16107}
16108
16109 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016110check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016111{
16112 make_connection();
16113 if (X_DISPLAY == NULL)
16114 {
16115 EMSG(_("E240: No connection to Vim server"));
16116 return FAIL;
16117 }
16118 return OK;
16119}
16120#endif
16121
16122#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016123static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016124
16125 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016126remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016127{
16128 char_u *server_name;
16129 char_u *keys;
16130 char_u *r = NULL;
16131 char_u buf[NUMBUFLEN];
16132# ifdef WIN32
16133 HWND w;
16134# else
16135 Window w;
16136# endif
16137
16138 if (check_restricted() || check_secure())
16139 return;
16140
16141# ifdef FEAT_X11
16142 if (check_connection() == FAIL)
16143 return;
16144# endif
16145
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016146 server_name = get_tv_string_chk(&argvars[0]);
16147 if (server_name == NULL)
16148 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016149 keys = get_tv_string_buf(&argvars[1], buf);
16150# ifdef WIN32
16151 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16152# else
16153 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16154 < 0)
16155# endif
16156 {
16157 if (r != NULL)
16158 EMSG(r); /* sending worked but evaluation failed */
16159 else
16160 EMSG2(_("E241: Unable to send to %s"), server_name);
16161 return;
16162 }
16163
16164 rettv->vval.v_string = r;
16165
16166 if (argvars[2].v_type != VAR_UNKNOWN)
16167 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016168 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016169 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016170 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016171
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016172 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016173 v.di_tv.v_type = VAR_STRING;
16174 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016175 idvar = get_tv_string_chk(&argvars[2]);
16176 if (idvar != NULL)
16177 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016178 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016179 }
16180}
16181#endif
16182
16183/*
16184 * "remote_expr()" function
16185 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016186 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016187f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016188{
16189 rettv->v_type = VAR_STRING;
16190 rettv->vval.v_string = NULL;
16191#ifdef FEAT_CLIENTSERVER
16192 remote_common(argvars, rettv, TRUE);
16193#endif
16194}
16195
16196/*
16197 * "remote_foreground()" function
16198 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016199 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016200f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016201{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016202#ifdef FEAT_CLIENTSERVER
16203# ifdef WIN32
16204 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016205 {
16206 char_u *server_name = get_tv_string_chk(&argvars[0]);
16207
16208 if (server_name != NULL)
16209 serverForeground(server_name);
16210 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016211# else
16212 /* Send a foreground() expression to the server. */
16213 argvars[1].v_type = VAR_STRING;
16214 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16215 argvars[2].v_type = VAR_UNKNOWN;
16216 remote_common(argvars, rettv, TRUE);
16217 vim_free(argvars[1].vval.v_string);
16218# endif
16219#endif
16220}
16221
Bram Moolenaar0d660222005-01-07 21:51:51 +000016222 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016223f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016224{
16225#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016226 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016227 char_u *s = NULL;
16228# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016229 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016230# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016231 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016232
16233 if (check_restricted() || check_secure())
16234 {
16235 rettv->vval.v_number = -1;
16236 return;
16237 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016238 serverid = get_tv_string_chk(&argvars[0]);
16239 if (serverid == NULL)
16240 {
16241 rettv->vval.v_number = -1;
16242 return; /* type error; errmsg already given */
16243 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016244# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016245 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016246 if (n == 0)
16247 rettv->vval.v_number = -1;
16248 else
16249 {
16250 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16251 rettv->vval.v_number = (s != NULL);
16252 }
16253# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016254 if (check_connection() == FAIL)
16255 return;
16256
16257 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016258 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016259# endif
16260
16261 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16262 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016263 char_u *retvar;
16264
Bram Moolenaar33570922005-01-25 22:26:29 +000016265 v.di_tv.v_type = VAR_STRING;
16266 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016267 retvar = get_tv_string_chk(&argvars[1]);
16268 if (retvar != NULL)
16269 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016270 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016271 }
16272#else
16273 rettv->vval.v_number = -1;
16274#endif
16275}
16276
Bram Moolenaar0d660222005-01-07 21:51:51 +000016277 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016278f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016279{
16280 char_u *r = NULL;
16281
16282#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016283 char_u *serverid = get_tv_string_chk(&argvars[0]);
16284
16285 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016286 {
16287# ifdef WIN32
16288 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016289 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016290
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016291 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016292 if (n != 0)
16293 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16294 if (r == NULL)
16295# else
16296 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016297 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016298# endif
16299 EMSG(_("E277: Unable to read a server reply"));
16300 }
16301#endif
16302 rettv->v_type = VAR_STRING;
16303 rettv->vval.v_string = r;
16304}
16305
16306/*
16307 * "remote_send()" function
16308 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016310f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016311{
16312 rettv->v_type = VAR_STRING;
16313 rettv->vval.v_string = NULL;
16314#ifdef FEAT_CLIENTSERVER
16315 remote_common(argvars, rettv, FALSE);
16316#endif
16317}
16318
16319/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016320 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016321 */
16322 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016323f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016324{
Bram Moolenaar33570922005-01-25 22:26:29 +000016325 list_T *l;
16326 listitem_T *item, *item2;
16327 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016328 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016329 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016330 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016331 dict_T *d;
16332 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016333 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016334
Bram Moolenaar8c711452005-01-14 21:53:12 +000016335 if (argvars[0].v_type == VAR_DICT)
16336 {
16337 if (argvars[2].v_type != VAR_UNKNOWN)
16338 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016339 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016340 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016341 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016342 key = get_tv_string_chk(&argvars[1]);
16343 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016344 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016345 di = dict_find(d, key, -1);
16346 if (di == NULL)
16347 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016348 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16349 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016350 {
16351 *rettv = di->di_tv;
16352 init_tv(&di->di_tv);
16353 dictitem_remove(d, di);
16354 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016355 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016356 }
16357 }
16358 else if (argvars[0].v_type != VAR_LIST)
16359 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016360 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016361 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016362 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016363 int error = FALSE;
16364
16365 idx = get_tv_number_chk(&argvars[1], &error);
16366 if (error)
16367 ; /* type error: do nothing, errmsg already given */
16368 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016369 EMSGN(_(e_listidx), idx);
16370 else
16371 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016372 if (argvars[2].v_type == VAR_UNKNOWN)
16373 {
16374 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016375 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016376 *rettv = item->li_tv;
16377 vim_free(item);
16378 }
16379 else
16380 {
16381 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016382 end = get_tv_number_chk(&argvars[2], &error);
16383 if (error)
16384 ; /* type error: do nothing */
16385 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016386 EMSGN(_(e_listidx), end);
16387 else
16388 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016389 int cnt = 0;
16390
16391 for (li = item; li != NULL; li = li->li_next)
16392 {
16393 ++cnt;
16394 if (li == item2)
16395 break;
16396 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016397 if (li == NULL) /* didn't find "item2" after "item" */
16398 EMSG(_(e_invrange));
16399 else
16400 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016401 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016402 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016403 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016404 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016405 l->lv_first = item;
16406 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016407 item->li_prev = NULL;
16408 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016409 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016410 }
16411 }
16412 }
16413 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016414 }
16415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016416}
16417
16418/*
16419 * "rename({from}, {to})" function
16420 */
16421 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016422f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016423{
16424 char_u buf[NUMBUFLEN];
16425
16426 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016427 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016428 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016429 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16430 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016431}
16432
16433/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016434 * "repeat()" function
16435 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016436 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016437f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016438{
16439 char_u *p;
16440 int n;
16441 int slen;
16442 int len;
16443 char_u *r;
16444 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016445
16446 n = get_tv_number(&argvars[1]);
16447 if (argvars[0].v_type == VAR_LIST)
16448 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016449 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016450 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016451 if (list_extend(rettv->vval.v_list,
16452 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016453 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016454 }
16455 else
16456 {
16457 p = get_tv_string(&argvars[0]);
16458 rettv->v_type = VAR_STRING;
16459 rettv->vval.v_string = NULL;
16460
16461 slen = (int)STRLEN(p);
16462 len = slen * n;
16463 if (len <= 0)
16464 return;
16465
16466 r = alloc(len + 1);
16467 if (r != NULL)
16468 {
16469 for (i = 0; i < n; i++)
16470 mch_memmove(r + i * slen, p, (size_t)slen);
16471 r[len] = NUL;
16472 }
16473
16474 rettv->vval.v_string = r;
16475 }
16476}
16477
16478/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016479 * "resolve()" function
16480 */
16481 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016482f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016483{
16484 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016485#ifdef HAVE_READLINK
16486 char_u *buf = NULL;
16487#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016488
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016489 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016490#ifdef FEAT_SHORTCUT
16491 {
16492 char_u *v = NULL;
16493
16494 v = mch_resolve_shortcut(p);
16495 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016496 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016497 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016498 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016499 }
16500#else
16501# ifdef HAVE_READLINK
16502 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016503 char_u *cpy;
16504 int len;
16505 char_u *remain = NULL;
16506 char_u *q;
16507 int is_relative_to_current = FALSE;
16508 int has_trailing_pathsep = FALSE;
16509 int limit = 100;
16510
16511 p = vim_strsave(p);
16512
16513 if (p[0] == '.' && (vim_ispathsep(p[1])
16514 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16515 is_relative_to_current = TRUE;
16516
16517 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016518 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016519 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016520 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016521 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016523
16524 q = getnextcomp(p);
16525 if (*q != NUL)
16526 {
16527 /* Separate the first path component in "p", and keep the
16528 * remainder (beginning with the path separator). */
16529 remain = vim_strsave(q - 1);
16530 q[-1] = NUL;
16531 }
16532
Bram Moolenaard9462e32011-04-11 21:35:11 +020016533 buf = alloc(MAXPATHL + 1);
16534 if (buf == NULL)
16535 goto fail;
16536
Bram Moolenaar071d4272004-06-13 20:20:40 +000016537 for (;;)
16538 {
16539 for (;;)
16540 {
16541 len = readlink((char *)p, (char *)buf, MAXPATHL);
16542 if (len <= 0)
16543 break;
16544 buf[len] = NUL;
16545
16546 if (limit-- == 0)
16547 {
16548 vim_free(p);
16549 vim_free(remain);
16550 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016551 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016552 goto fail;
16553 }
16554
16555 /* Ensure that the result will have a trailing path separator
16556 * if the argument has one. */
16557 if (remain == NULL && has_trailing_pathsep)
16558 add_pathsep(buf);
16559
16560 /* Separate the first path component in the link value and
16561 * concatenate the remainders. */
16562 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16563 if (*q != NUL)
16564 {
16565 if (remain == NULL)
16566 remain = vim_strsave(q - 1);
16567 else
16568 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016569 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016570 if (cpy != NULL)
16571 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016572 vim_free(remain);
16573 remain = cpy;
16574 }
16575 }
16576 q[-1] = NUL;
16577 }
16578
16579 q = gettail(p);
16580 if (q > p && *q == NUL)
16581 {
16582 /* Ignore trailing path separator. */
16583 q[-1] = NUL;
16584 q = gettail(p);
16585 }
16586 if (q > p && !mch_isFullName(buf))
16587 {
16588 /* symlink is relative to directory of argument */
16589 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16590 if (cpy != NULL)
16591 {
16592 STRCPY(cpy, p);
16593 STRCPY(gettail(cpy), buf);
16594 vim_free(p);
16595 p = cpy;
16596 }
16597 }
16598 else
16599 {
16600 vim_free(p);
16601 p = vim_strsave(buf);
16602 }
16603 }
16604
16605 if (remain == NULL)
16606 break;
16607
16608 /* Append the first path component of "remain" to "p". */
16609 q = getnextcomp(remain + 1);
16610 len = q - remain - (*q != NUL);
16611 cpy = vim_strnsave(p, STRLEN(p) + len);
16612 if (cpy != NULL)
16613 {
16614 STRNCAT(cpy, remain, len);
16615 vim_free(p);
16616 p = cpy;
16617 }
16618 /* Shorten "remain". */
16619 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016620 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016621 else
16622 {
16623 vim_free(remain);
16624 remain = NULL;
16625 }
16626 }
16627
16628 /* If the result is a relative path name, make it explicitly relative to
16629 * the current directory if and only if the argument had this form. */
16630 if (!vim_ispathsep(*p))
16631 {
16632 if (is_relative_to_current
16633 && *p != NUL
16634 && !(p[0] == '.'
16635 && (p[1] == NUL
16636 || vim_ispathsep(p[1])
16637 || (p[1] == '.'
16638 && (p[2] == NUL
16639 || vim_ispathsep(p[2]))))))
16640 {
16641 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016642 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016643 if (cpy != NULL)
16644 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016645 vim_free(p);
16646 p = cpy;
16647 }
16648 }
16649 else if (!is_relative_to_current)
16650 {
16651 /* Strip leading "./". */
16652 q = p;
16653 while (q[0] == '.' && vim_ispathsep(q[1]))
16654 q += 2;
16655 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016656 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016657 }
16658 }
16659
16660 /* Ensure that the result will have no trailing path separator
16661 * if the argument had none. But keep "/" or "//". */
16662 if (!has_trailing_pathsep)
16663 {
16664 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016665 if (after_pathsep(p, q))
16666 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016667 }
16668
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016669 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016670 }
16671# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016672 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016673# endif
16674#endif
16675
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016676 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016677
16678#ifdef HAVE_READLINK
16679fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016680 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016681#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016682 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016683}
16684
16685/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016686 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016687 */
16688 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016689f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016690{
Bram Moolenaar33570922005-01-25 22:26:29 +000016691 list_T *l;
16692 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016693
Bram Moolenaar0d660222005-01-07 21:51:51 +000016694 if (argvars[0].v_type != VAR_LIST)
16695 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016696 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016697 && !tv_check_lock(l->lv_lock,
16698 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016699 {
16700 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016701 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016702 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016703 while (li != NULL)
16704 {
16705 ni = li->li_prev;
16706 list_append(l, li);
16707 li = ni;
16708 }
16709 rettv->vval.v_list = l;
16710 rettv->v_type = VAR_LIST;
16711 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016712 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016714}
16715
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016716#define SP_NOMOVE 0x01 /* don't move cursor */
16717#define SP_REPEAT 0x02 /* repeat to find outer pair */
16718#define SP_RETCOUNT 0x04 /* return matchcount */
16719#define SP_SETPCMARK 0x08 /* set previous context mark */
16720#define SP_START 0x10 /* accept match at start position */
16721#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16722#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016723#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016724
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016725static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016726
16727/*
16728 * Get flags for a search function.
16729 * Possibly sets "p_ws".
16730 * Returns BACKWARD, FORWARD or zero (for an error).
16731 */
16732 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016733get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016734{
16735 int dir = FORWARD;
16736 char_u *flags;
16737 char_u nbuf[NUMBUFLEN];
16738 int mask;
16739
16740 if (varp->v_type != VAR_UNKNOWN)
16741 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016742 flags = get_tv_string_buf_chk(varp, nbuf);
16743 if (flags == NULL)
16744 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016745 while (*flags != NUL)
16746 {
16747 switch (*flags)
16748 {
16749 case 'b': dir = BACKWARD; break;
16750 case 'w': p_ws = TRUE; break;
16751 case 'W': p_ws = FALSE; break;
16752 default: mask = 0;
16753 if (flagsp != NULL)
16754 switch (*flags)
16755 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016756 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016757 case 'e': mask = SP_END; break;
16758 case 'm': mask = SP_RETCOUNT; break;
16759 case 'n': mask = SP_NOMOVE; break;
16760 case 'p': mask = SP_SUBPAT; break;
16761 case 'r': mask = SP_REPEAT; break;
16762 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016763 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016764 }
16765 if (mask == 0)
16766 {
16767 EMSG2(_(e_invarg2), flags);
16768 dir = 0;
16769 }
16770 else
16771 *flagsp |= mask;
16772 }
16773 if (dir == 0)
16774 break;
16775 ++flags;
16776 }
16777 }
16778 return dir;
16779}
16780
Bram Moolenaar071d4272004-06-13 20:20:40 +000016781/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016782 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016783 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016784 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016785search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016786{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016787 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016788 char_u *pat;
16789 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016790 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016791 int save_p_ws = p_ws;
16792 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016793 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016794 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016795 proftime_T tm;
16796#ifdef FEAT_RELTIME
16797 long time_limit = 0;
16798#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016799 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016800 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016801
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016802 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016803 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016804 if (dir == 0)
16805 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016806 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016807 if (flags & SP_START)
16808 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016809 if (flags & SP_END)
16810 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010016811 if (flags & SP_COLUMN)
16812 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016813
Bram Moolenaar76929292008-01-06 19:07:36 +000016814 /* Optional arguments: line number to stop searching and timeout. */
16815 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016816 {
16817 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16818 if (lnum_stop < 0)
16819 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016820#ifdef FEAT_RELTIME
16821 if (argvars[3].v_type != VAR_UNKNOWN)
16822 {
16823 time_limit = get_tv_number_chk(&argvars[3], NULL);
16824 if (time_limit < 0)
16825 goto theend;
16826 }
16827#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016828 }
16829
Bram Moolenaar76929292008-01-06 19:07:36 +000016830#ifdef FEAT_RELTIME
16831 /* Set the time limit, if there is one. */
16832 profile_setlimit(time_limit, &tm);
16833#endif
16834
Bram Moolenaar231334e2005-07-25 20:46:57 +000016835 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016836 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016837 * Check to make sure only those flags are set.
16838 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16839 * flags cannot be set. Check for that condition also.
16840 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016841 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016842 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016843 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016844 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016845 goto theend;
16846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016847
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016848 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016849 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016850 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016851 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016852 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016853 if (flags & SP_SUBPAT)
16854 retval = subpatnum;
16855 else
16856 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016857 if (flags & SP_SETPCMARK)
16858 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016859 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016860 if (match_pos != NULL)
16861 {
16862 /* Store the match cursor position */
16863 match_pos->lnum = pos.lnum;
16864 match_pos->col = pos.col + 1;
16865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016866 /* "/$" will put the cursor after the end of the line, may need to
16867 * correct that here */
16868 check_cursor();
16869 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016870
16871 /* If 'n' flag is used: restore cursor position. */
16872 if (flags & SP_NOMOVE)
16873 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016874 else
16875 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016876theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016877 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016878
16879 return retval;
16880}
16881
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016882#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016883
16884/*
16885 * round() is not in C90, use ceil() or floor() instead.
16886 */
16887 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010016888vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016889{
16890 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16891}
16892
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016893/*
16894 * "round({float})" function
16895 */
16896 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016897f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016898{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016899 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016900
16901 rettv->v_type = VAR_FLOAT;
16902 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016903 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016904 else
16905 rettv->vval.v_float = 0.0;
16906}
16907#endif
16908
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016909/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016910 * "screenattr()" function
16911 */
16912 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010016913f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020016914{
16915 int row;
16916 int col;
16917 int c;
16918
16919 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16920 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16921 if (row < 0 || row >= screen_Rows
16922 || col < 0 || col >= screen_Columns)
16923 c = -1;
16924 else
16925 c = ScreenAttrs[LineOffset[row] + col];
16926 rettv->vval.v_number = c;
16927}
16928
16929/*
16930 * "screenchar()" function
16931 */
16932 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010016933f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020016934{
16935 int row;
16936 int col;
16937 int off;
16938 int c;
16939
16940 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16941 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16942 if (row < 0 || row >= screen_Rows
16943 || col < 0 || col >= screen_Columns)
16944 c = -1;
16945 else
16946 {
16947 off = LineOffset[row] + col;
16948#ifdef FEAT_MBYTE
16949 if (enc_utf8 && ScreenLinesUC[off] != 0)
16950 c = ScreenLinesUC[off];
16951 else
16952#endif
16953 c = ScreenLines[off];
16954 }
16955 rettv->vval.v_number = c;
16956}
16957
16958/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016959 * "screencol()" function
16960 *
16961 * First column is 1 to be consistent with virtcol().
16962 */
16963 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016964f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016965{
16966 rettv->vval.v_number = screen_screencol() + 1;
16967}
16968
16969/*
16970 * "screenrow()" function
16971 */
16972 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016973f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016974{
16975 rettv->vval.v_number = screen_screenrow() + 1;
16976}
16977
16978/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016979 * "search()" function
16980 */
16981 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016982f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016983{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016984 int flags = 0;
16985
16986 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016987}
16988
Bram Moolenaar071d4272004-06-13 20:20:40 +000016989/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016990 * "searchdecl()" function
16991 */
16992 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016993f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016994{
16995 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016996 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016997 int error = FALSE;
16998 char_u *name;
16999
17000 rettv->vval.v_number = 1; /* default: FAIL */
17001
17002 name = get_tv_string_chk(&argvars[0]);
17003 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017004 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017005 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017006 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17007 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17008 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017009 if (!error && name != NULL)
17010 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017011 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017012}
17013
17014/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017015 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017016 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017017 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017018searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017019{
17020 char_u *spat, *mpat, *epat;
17021 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017022 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017023 int dir;
17024 int flags = 0;
17025 char_u nbuf1[NUMBUFLEN];
17026 char_u nbuf2[NUMBUFLEN];
17027 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017028 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017029 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017030 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017031
Bram Moolenaar071d4272004-06-13 20:20:40 +000017032 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017033 spat = get_tv_string_chk(&argvars[0]);
17034 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17035 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17036 if (spat == NULL || mpat == NULL || epat == NULL)
17037 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017038
Bram Moolenaar071d4272004-06-13 20:20:40 +000017039 /* Handle the optional fourth argument: flags */
17040 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017041 if (dir == 0)
17042 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017043
17044 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017045 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17046 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017047 if ((flags & (SP_END | SP_SUBPAT)) != 0
17048 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017049 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017050 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017051 goto theend;
17052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017053
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017054 /* Using 'r' implies 'W', otherwise it doesn't work. */
17055 if (flags & SP_REPEAT)
17056 p_ws = FALSE;
17057
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017058 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017059 if (argvars[3].v_type == VAR_UNKNOWN
17060 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017061 skip = (char_u *)"";
17062 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017063 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017064 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017065 if (argvars[5].v_type != VAR_UNKNOWN)
17066 {
17067 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17068 if (lnum_stop < 0)
17069 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017070#ifdef FEAT_RELTIME
17071 if (argvars[6].v_type != VAR_UNKNOWN)
17072 {
17073 time_limit = get_tv_number_chk(&argvars[6], NULL);
17074 if (time_limit < 0)
17075 goto theend;
17076 }
17077#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017078 }
17079 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017080 if (skip == NULL)
17081 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017082
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017083 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017084 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017085
17086theend:
17087 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017088
17089 return retval;
17090}
17091
17092/*
17093 * "searchpair()" function
17094 */
17095 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017096f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017097{
17098 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17099}
17100
17101/*
17102 * "searchpairpos()" function
17103 */
17104 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017105f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017106{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017107 pos_T match_pos;
17108 int lnum = 0;
17109 int col = 0;
17110
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017111 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017112 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017113
17114 if (searchpair_cmn(argvars, &match_pos) > 0)
17115 {
17116 lnum = match_pos.lnum;
17117 col = match_pos.col;
17118 }
17119
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017120 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17121 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017122}
17123
17124/*
17125 * Search for a start/middle/end thing.
17126 * Used by searchpair(), see its documentation for the details.
17127 * Returns 0 or -1 for no match,
17128 */
17129 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017130do_searchpair(
17131 char_u *spat, /* start pattern */
17132 char_u *mpat, /* middle pattern */
17133 char_u *epat, /* end pattern */
17134 int dir, /* BACKWARD or FORWARD */
17135 char_u *skip, /* skip expression */
17136 int flags, /* SP_SETPCMARK and other SP_ values */
17137 pos_T *match_pos,
17138 linenr_T lnum_stop, /* stop at this line if not zero */
17139 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017140{
17141 char_u *save_cpo;
17142 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17143 long retval = 0;
17144 pos_T pos;
17145 pos_T firstpos;
17146 pos_T foundpos;
17147 pos_T save_cursor;
17148 pos_T save_pos;
17149 int n;
17150 int r;
17151 int nest = 1;
17152 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017153 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017154 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017155
17156 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17157 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017158 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017159
Bram Moolenaar76929292008-01-06 19:07:36 +000017160#ifdef FEAT_RELTIME
17161 /* Set the time limit, if there is one. */
17162 profile_setlimit(time_limit, &tm);
17163#endif
17164
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017165 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17166 * start/middle/end (pat3, for the top pair). */
17167 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17168 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17169 if (pat2 == NULL || pat3 == NULL)
17170 goto theend;
17171 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17172 if (*mpat == NUL)
17173 STRCPY(pat3, pat2);
17174 else
17175 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17176 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017177 if (flags & SP_START)
17178 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017179
Bram Moolenaar071d4272004-06-13 20:20:40 +000017180 save_cursor = curwin->w_cursor;
17181 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017182 clearpos(&firstpos);
17183 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017184 pat = pat3;
17185 for (;;)
17186 {
17187 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017188 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017189 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17190 /* didn't find it or found the first match again: FAIL */
17191 break;
17192
17193 if (firstpos.lnum == 0)
17194 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017195 if (equalpos(pos, foundpos))
17196 {
17197 /* Found the same position again. Can happen with a pattern that
17198 * has "\zs" at the end and searching backwards. Advance one
17199 * character and try again. */
17200 if (dir == BACKWARD)
17201 decl(&pos);
17202 else
17203 incl(&pos);
17204 }
17205 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017206
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017207 /* clear the start flag to avoid getting stuck here */
17208 options &= ~SEARCH_START;
17209
Bram Moolenaar071d4272004-06-13 20:20:40 +000017210 /* If the skip pattern matches, ignore this match. */
17211 if (*skip != NUL)
17212 {
17213 save_pos = curwin->w_cursor;
17214 curwin->w_cursor = pos;
17215 r = eval_to_bool(skip, &err, NULL, FALSE);
17216 curwin->w_cursor = save_pos;
17217 if (err)
17218 {
17219 /* Evaluating {skip} caused an error, break here. */
17220 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017221 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017222 break;
17223 }
17224 if (r)
17225 continue;
17226 }
17227
17228 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17229 {
17230 /* Found end when searching backwards or start when searching
17231 * forward: nested pair. */
17232 ++nest;
17233 pat = pat2; /* nested, don't search for middle */
17234 }
17235 else
17236 {
17237 /* Found end when searching forward or start when searching
17238 * backward: end of (nested) pair; or found middle in outer pair. */
17239 if (--nest == 1)
17240 pat = pat3; /* outer level, search for middle */
17241 }
17242
17243 if (nest == 0)
17244 {
17245 /* Found the match: return matchcount or line number. */
17246 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017247 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017248 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017249 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017250 if (flags & SP_SETPCMARK)
17251 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017252 curwin->w_cursor = pos;
17253 if (!(flags & SP_REPEAT))
17254 break;
17255 nest = 1; /* search for next unmatched */
17256 }
17257 }
17258
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017259 if (match_pos != NULL)
17260 {
17261 /* Store the match cursor position */
17262 match_pos->lnum = curwin->w_cursor.lnum;
17263 match_pos->col = curwin->w_cursor.col + 1;
17264 }
17265
Bram Moolenaar071d4272004-06-13 20:20:40 +000017266 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017267 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017268 curwin->w_cursor = save_cursor;
17269
17270theend:
17271 vim_free(pat2);
17272 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017273 if (p_cpo == empty_option)
17274 p_cpo = save_cpo;
17275 else
17276 /* Darn, evaluating the {skip} expression changed the value. */
17277 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017278
17279 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017280}
17281
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017282/*
17283 * "searchpos()" function
17284 */
17285 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017286f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017287{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017288 pos_T match_pos;
17289 int lnum = 0;
17290 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017291 int n;
17292 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017293
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017294 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017295 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017296
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017297 n = search_cmn(argvars, &match_pos, &flags);
17298 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017299 {
17300 lnum = match_pos.lnum;
17301 col = match_pos.col;
17302 }
17303
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017304 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17305 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017306 if (flags & SP_SUBPAT)
17307 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017308}
17309
Bram Moolenaar0d660222005-01-07 21:51:51 +000017310 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017311f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017312{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017313#ifdef FEAT_CLIENTSERVER
17314 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017315 char_u *server = get_tv_string_chk(&argvars[0]);
17316 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017317
Bram Moolenaar0d660222005-01-07 21:51:51 +000017318 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017319 if (server == NULL || reply == NULL)
17320 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017321 if (check_restricted() || check_secure())
17322 return;
17323# ifdef FEAT_X11
17324 if (check_connection() == FAIL)
17325 return;
17326# endif
17327
17328 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017329 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017330 EMSG(_("E258: Unable to send to client"));
17331 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017332 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017333 rettv->vval.v_number = 0;
17334#else
17335 rettv->vval.v_number = -1;
17336#endif
17337}
17338
Bram Moolenaar0d660222005-01-07 21:51:51 +000017339 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017340f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017341{
17342 char_u *r = NULL;
17343
17344#ifdef FEAT_CLIENTSERVER
17345# ifdef WIN32
17346 r = serverGetVimNames();
17347# else
17348 make_connection();
17349 if (X_DISPLAY != NULL)
17350 r = serverGetVimNames(X_DISPLAY);
17351# endif
17352#endif
17353 rettv->v_type = VAR_STRING;
17354 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017355}
17356
17357/*
17358 * "setbufvar()" function
17359 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017360 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017361f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017362{
17363 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017364 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017365 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017366 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017367 char_u nbuf[NUMBUFLEN];
17368
17369 if (check_restricted() || check_secure())
17370 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017371 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17372 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017373 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017374 varp = &argvars[2];
17375
17376 if (buf != NULL && varname != NULL && varp != NULL)
17377 {
17378 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017379 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017380
17381 if (*varname == '&')
17382 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017383 long numval;
17384 char_u *strval;
17385 int error = FALSE;
17386
Bram Moolenaar071d4272004-06-13 20:20:40 +000017387 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017388 numval = get_tv_number_chk(varp, &error);
17389 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017390 if (!error && strval != NULL)
17391 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017392 }
17393 else
17394 {
17395 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17396 if (bufvarname != NULL)
17397 {
17398 STRCPY(bufvarname, "b:");
17399 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017400 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017401 vim_free(bufvarname);
17402 }
17403 }
17404
17405 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017406 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017408}
17409
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017410 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017411f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017412{
17413 dict_T *d;
17414 dictitem_T *di;
17415 char_u *csearch;
17416
17417 if (argvars[0].v_type != VAR_DICT)
17418 {
17419 EMSG(_(e_dictreq));
17420 return;
17421 }
17422
17423 if ((d = argvars[0].vval.v_dict) != NULL)
17424 {
17425 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17426 if (csearch != NULL)
17427 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017428#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017429 if (enc_utf8)
17430 {
17431 int pcc[MAX_MCO];
17432 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017433
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017434 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17435 }
17436 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017437#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017438 set_last_csearch(PTR2CHAR(csearch),
17439 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017440 }
17441
17442 di = dict_find(d, (char_u *)"forward", -1);
17443 if (di != NULL)
17444 set_csearch_direction(get_tv_number(&di->di_tv)
17445 ? FORWARD : BACKWARD);
17446
17447 di = dict_find(d, (char_u *)"until", -1);
17448 if (di != NULL)
17449 set_csearch_until(!!get_tv_number(&di->di_tv));
17450 }
17451}
17452
Bram Moolenaar071d4272004-06-13 20:20:40 +000017453/*
17454 * "setcmdpos()" function
17455 */
17456 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017457f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017458{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017459 int pos = (int)get_tv_number(&argvars[0]) - 1;
17460
17461 if (pos >= 0)
17462 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017463}
17464
17465/*
Bram Moolenaar80492532016-03-08 17:08:53 +010017466 * "setfperm({fname}, {mode})" function
17467 */
17468 static void
17469f_setfperm(typval_T *argvars, typval_T *rettv)
17470{
17471 char_u *fname;
17472 char_u modebuf[NUMBUFLEN];
17473 char_u *mode_str;
17474 int i;
17475 int mask;
17476 int mode = 0;
17477
17478 rettv->vval.v_number = 0;
17479 fname = get_tv_string_chk(&argvars[0]);
17480 if (fname == NULL)
17481 return;
17482 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
17483 if (mode_str == NULL)
17484 return;
17485 if (STRLEN(mode_str) != 9)
17486 {
17487 EMSG2(_(e_invarg2), mode_str);
17488 return;
17489 }
17490
17491 mask = 1;
17492 for (i = 8; i >= 0; --i)
17493 {
17494 if (mode_str[i] != '-')
17495 mode |= mask;
17496 mask = mask << 1;
17497 }
17498 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
17499}
17500
17501/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017502 * "setline()" function
17503 */
17504 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017505f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017506{
17507 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017508 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017509 list_T *l = NULL;
17510 listitem_T *li = NULL;
17511 long added = 0;
17512 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017513
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017514 lnum = get_tv_lnum(&argvars[0]);
17515 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017516 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017517 l = argvars[1].vval.v_list;
17518 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017519 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017520 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017521 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017522
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017523 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017524 for (;;)
17525 {
17526 if (l != NULL)
17527 {
17528 /* list argument, get next string */
17529 if (li == NULL)
17530 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017531 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017532 li = li->li_next;
17533 }
17534
17535 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017536 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017537 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017538
17539 /* When coming here from Insert mode, sync undo, so that this can be
17540 * undone separately from what was previously inserted. */
17541 if (u_sync_once == 2)
17542 {
17543 u_sync_once = 1; /* notify that u_sync() was called */
17544 u_sync(TRUE);
17545 }
17546
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017547 if (lnum <= curbuf->b_ml.ml_line_count)
17548 {
17549 /* existing line, replace it */
17550 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17551 {
17552 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017553 if (lnum == curwin->w_cursor.lnum)
17554 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017555 rettv->vval.v_number = 0; /* OK */
17556 }
17557 }
17558 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17559 {
17560 /* lnum is one past the last line, append the line */
17561 ++added;
17562 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17563 rettv->vval.v_number = 0; /* OK */
17564 }
17565
17566 if (l == NULL) /* only one string argument */
17567 break;
17568 ++lnum;
17569 }
17570
17571 if (added > 0)
17572 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017573}
17574
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017575static 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 +000017576
Bram Moolenaar071d4272004-06-13 20:20:40 +000017577/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017578 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017579 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017580 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017581set_qf_ll_list(
17582 win_T *wp UNUSED,
17583 typval_T *list_arg UNUSED,
17584 typval_T *action_arg UNUSED,
17585 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017586{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017587#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017588 char_u *act;
17589 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017590#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017591
Bram Moolenaar2641f772005-03-25 21:58:17 +000017592 rettv->vval.v_number = -1;
17593
17594#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017595 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017596 EMSG(_(e_listreq));
17597 else
17598 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017599 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017600
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017601 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017602 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017603 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017604 if (act == NULL)
17605 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017606 if (*act == 'a' || *act == 'r')
17607 action = *act;
17608 }
17609
Bram Moolenaar81484f42012-12-05 15:16:47 +010017610 if (l != NULL && set_errorlist(wp, l, action,
17611 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017612 rettv->vval.v_number = 0;
17613 }
17614#endif
17615}
17616
17617/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017618 * "setloclist()" function
17619 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017620 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017621f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017622{
17623 win_T *win;
17624
17625 rettv->vval.v_number = -1;
17626
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017627 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017628 if (win != NULL)
17629 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17630}
17631
17632/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017633 * "setmatches()" function
17634 */
17635 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017636f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017637{
17638#ifdef FEAT_SEARCH_EXTRA
17639 list_T *l;
17640 listitem_T *li;
17641 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017642 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017643
17644 rettv->vval.v_number = -1;
17645 if (argvars[0].v_type != VAR_LIST)
17646 {
17647 EMSG(_(e_listreq));
17648 return;
17649 }
17650 if ((l = argvars[0].vval.v_list) != NULL)
17651 {
17652
17653 /* To some extent make sure that we are dealing with a list from
17654 * "getmatches()". */
17655 li = l->lv_first;
17656 while (li != NULL)
17657 {
17658 if (li->li_tv.v_type != VAR_DICT
17659 || (d = li->li_tv.vval.v_dict) == NULL)
17660 {
17661 EMSG(_(e_invarg));
17662 return;
17663 }
17664 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017665 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17666 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017667 && dict_find(d, (char_u *)"priority", -1) != NULL
17668 && dict_find(d, (char_u *)"id", -1) != NULL))
17669 {
17670 EMSG(_(e_invarg));
17671 return;
17672 }
17673 li = li->li_next;
17674 }
17675
17676 clear_matches(curwin);
17677 li = l->lv_first;
17678 while (li != NULL)
17679 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017680 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017681 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017682 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017683 char_u *group;
17684 int priority;
17685 int id;
17686 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017687
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017688 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017689 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17690 {
17691 if (s == NULL)
17692 {
17693 s = list_alloc();
17694 if (s == NULL)
17695 return;
17696 }
17697
17698 /* match from matchaddpos() */
17699 for (i = 1; i < 9; i++)
17700 {
17701 sprintf((char *)buf, (char *)"pos%d", i);
17702 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17703 {
17704 if (di->di_tv.v_type != VAR_LIST)
17705 return;
17706
17707 list_append_tv(s, &di->di_tv);
17708 s->lv_refcount++;
17709 }
17710 else
17711 break;
17712 }
17713 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020017714
17715 group = get_dict_string(d, (char_u *)"group", FALSE);
17716 priority = (int)get_dict_number(d, (char_u *)"priority");
17717 id = (int)get_dict_number(d, (char_u *)"id");
17718 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
17719 ? get_dict_string(d, (char_u *)"conceal", FALSE)
17720 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017721 if (i == 0)
17722 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017723 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017724 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020017725 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017726 }
17727 else
17728 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020017729 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017730 list_unref(s);
17731 s = NULL;
17732 }
17733
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017734 li = li->li_next;
17735 }
17736 rettv->vval.v_number = 0;
17737 }
17738#endif
17739}
17740
17741/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017742 * "setpos()" function
17743 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017744 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017745f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017746{
17747 pos_T pos;
17748 int fnum;
17749 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017750 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017751
Bram Moolenaar08250432008-02-13 11:42:46 +000017752 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017753 name = get_tv_string_chk(argvars);
17754 if (name != NULL)
17755 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017756 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017757 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017758 if (--pos.col < 0)
17759 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017760 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017761 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017762 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017763 if (fnum == curbuf->b_fnum)
17764 {
17765 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017766 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017767 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017768 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010017769 curwin->w_set_curswant = FALSE;
17770 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017771 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017772 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017773 }
17774 else
17775 EMSG(_(e_invarg));
17776 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017777 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17778 {
17779 /* set mark */
17780 if (setmark_pos(name[1], &pos, fnum) == OK)
17781 rettv->vval.v_number = 0;
17782 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017783 else
17784 EMSG(_(e_invarg));
17785 }
17786 }
17787}
17788
17789/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017790 * "setqflist()" function
17791 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017792 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017793f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017794{
17795 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17796}
17797
17798/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017799 * "setreg()" function
17800 */
17801 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017802f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017803{
17804 int regname;
17805 char_u *strregname;
17806 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017807 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017808 int append;
17809 char_u yank_type;
17810 long block_len;
17811
17812 block_len = -1;
17813 yank_type = MAUTO;
17814 append = FALSE;
17815
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017816 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017817 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017818
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017819 if (strregname == NULL)
17820 return; /* type error; errmsg already given */
17821 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017822 if (regname == 0 || regname == '@')
17823 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017824
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017825 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017826 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017827 stropt = get_tv_string_chk(&argvars[2]);
17828 if (stropt == NULL)
17829 return; /* type error */
17830 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017831 switch (*stropt)
17832 {
17833 case 'a': case 'A': /* append */
17834 append = TRUE;
17835 break;
17836 case 'v': case 'c': /* character-wise selection */
17837 yank_type = MCHAR;
17838 break;
17839 case 'V': case 'l': /* line-wise selection */
17840 yank_type = MLINE;
17841 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017842 case 'b': case Ctrl_V: /* block-wise selection */
17843 yank_type = MBLOCK;
17844 if (VIM_ISDIGIT(stropt[1]))
17845 {
17846 ++stropt;
17847 block_len = getdigits(&stropt) - 1;
17848 --stropt;
17849 }
17850 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017851 }
17852 }
17853
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017854 if (argvars[1].v_type == VAR_LIST)
17855 {
17856 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017857 char_u **allocval;
17858 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017859 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017860 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017861 int len = argvars[1].vval.v_list->lv_len;
17862 listitem_T *li;
17863
Bram Moolenaar7d647822014-04-05 21:28:56 +020017864 /* First half: use for pointers to result lines; second half: use for
17865 * pointers to allocated copies. */
17866 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017867 if (lstval == NULL)
17868 return;
17869 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017870 allocval = lstval + len + 2;
17871 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017872
17873 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17874 li = li->li_next)
17875 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017876 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017877 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017878 goto free_lstval;
17879 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017880 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017881 /* Need to make a copy, next get_tv_string_buf_chk() will
17882 * overwrite the string. */
17883 strval = vim_strsave(buf);
17884 if (strval == NULL)
17885 goto free_lstval;
17886 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017887 }
17888 *curval++ = strval;
17889 }
17890 *curval++ = NULL;
17891
17892 write_reg_contents_lst(regname, lstval, -1,
17893 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017894free_lstval:
17895 while (curallocval > allocval)
17896 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017897 vim_free(lstval);
17898 }
17899 else
17900 {
17901 strval = get_tv_string_chk(&argvars[1]);
17902 if (strval == NULL)
17903 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017904 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017905 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017906 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017907 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017908}
17909
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017910/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017911 * "settabvar()" function
17912 */
17913 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017914f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017915{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017916#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017917 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017918 tabpage_T *tp;
17919#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017920 char_u *varname, *tabvarname;
17921 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017922
17923 rettv->vval.v_number = 0;
17924
17925 if (check_restricted() || check_secure())
17926 return;
17927
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017928#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017929 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017930#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017931 varname = get_tv_string_chk(&argvars[1]);
17932 varp = &argvars[2];
17933
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017934 if (varname != NULL && varp != NULL
17935#ifdef FEAT_WINDOWS
17936 && tp != NULL
17937#endif
17938 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017939 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017940#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017941 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017942 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017943#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017944
17945 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17946 if (tabvarname != NULL)
17947 {
17948 STRCPY(tabvarname, "t:");
17949 STRCPY(tabvarname + 2, varname);
17950 set_var(tabvarname, varp, TRUE);
17951 vim_free(tabvarname);
17952 }
17953
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017954#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017955 /* Restore current tabpage */
17956 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017957 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017958#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017959 }
17960}
17961
17962/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017963 * "settabwinvar()" function
17964 */
17965 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017966f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017967{
17968 setwinvar(argvars, rettv, 1);
17969}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017970
17971/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017972 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017973 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017974 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017975f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017976{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017977 setwinvar(argvars, rettv, 0);
17978}
17979
17980/*
17981 * "setwinvar()" and "settabwinvar()" functions
17982 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017983
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017984 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017985setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017986{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017987 win_T *win;
17988#ifdef FEAT_WINDOWS
17989 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017990 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020017991 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017992#endif
17993 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017994 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017995 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017996 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017997
17998 if (check_restricted() || check_secure())
17999 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018000
18001#ifdef FEAT_WINDOWS
18002 if (off == 1)
18003 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18004 else
18005 tp = curtab;
18006#endif
18007 win = find_win_by_nr(&argvars[off], tp);
18008 varname = get_tv_string_chk(&argvars[off + 1]);
18009 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018010
18011 if (win != NULL && varname != NULL && varp != NULL)
18012 {
18013#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018014 need_switch_win = !(tp == curtab && win == curwin);
18015 if (!need_switch_win
18016 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018017#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018018 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018019 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018020 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018021 long numval;
18022 char_u *strval;
18023 int error = FALSE;
18024
18025 ++varname;
18026 numval = get_tv_number_chk(varp, &error);
18027 strval = get_tv_string_buf_chk(varp, nbuf);
18028 if (!error && strval != NULL)
18029 set_option_value(varname, numval, strval, OPT_LOCAL);
18030 }
18031 else
18032 {
18033 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18034 if (winvarname != NULL)
18035 {
18036 STRCPY(winvarname, "w:");
18037 STRCPY(winvarname + 2, varname);
18038 set_var(winvarname, varp, TRUE);
18039 vim_free(winvarname);
18040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018041 }
18042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018043#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018044 if (need_switch_win)
18045 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018046#endif
18047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018048}
18049
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018050#ifdef FEAT_CRYPT
18051/*
18052 * "sha256({string})" function
18053 */
18054 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018055f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018056{
18057 char_u *p;
18058
18059 p = get_tv_string(&argvars[0]);
18060 rettv->vval.v_string = vim_strsave(
18061 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18062 rettv->v_type = VAR_STRING;
18063}
18064#endif /* FEAT_CRYPT */
18065
Bram Moolenaar071d4272004-06-13 20:20:40 +000018066/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018067 * "shellescape({string})" function
18068 */
18069 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018070f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018071{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018072 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018073 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018074 rettv->v_type = VAR_STRING;
18075}
18076
18077/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018078 * shiftwidth() function
18079 */
18080 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018081f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018082{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018083 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018084}
18085
18086/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018087 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018088 */
18089 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018090f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018091{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018092 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018093
Bram Moolenaar0d660222005-01-07 21:51:51 +000018094 p = get_tv_string(&argvars[0]);
18095 rettv->vval.v_string = vim_strsave(p);
18096 simplify_filename(rettv->vval.v_string); /* simplify in place */
18097 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018098}
18099
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018100#ifdef FEAT_FLOAT
18101/*
18102 * "sin()" function
18103 */
18104 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018105f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018106{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018107 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018108
18109 rettv->v_type = VAR_FLOAT;
18110 if (get_float_arg(argvars, &f) == OK)
18111 rettv->vval.v_float = sin(f);
18112 else
18113 rettv->vval.v_float = 0.0;
18114}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018115
18116/*
18117 * "sinh()" function
18118 */
18119 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018120f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018121{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018122 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018123
18124 rettv->v_type = VAR_FLOAT;
18125 if (get_float_arg(argvars, &f) == OK)
18126 rettv->vval.v_float = sinh(f);
18127 else
18128 rettv->vval.v_float = 0.0;
18129}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018130#endif
18131
Bram Moolenaar0d660222005-01-07 21:51:51 +000018132static int
18133#ifdef __BORLANDC__
18134 _RTLENTRYF
18135#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018136 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018137static int
18138#ifdef __BORLANDC__
18139 _RTLENTRYF
18140#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018141 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018142
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018143/* struct used in the array that's given to qsort() */
18144typedef struct
18145{
18146 listitem_T *item;
18147 int idx;
18148} sortItem_T;
18149
Bram Moolenaar0b962472016-02-22 22:51:33 +010018150/* struct storing information about current sort */
18151typedef struct
18152{
18153 int item_compare_ic;
18154 int item_compare_numeric;
18155 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018156#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018157 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018158#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018159 char_u *item_compare_func;
18160 dict_T *item_compare_selfdict;
18161 int item_compare_func_err;
18162 int item_compare_keep_zero;
18163} sortinfo_T;
18164static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018165static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018166#define ITEM_COMPARE_FAIL 999
18167
Bram Moolenaar071d4272004-06-13 20:20:40 +000018168/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018169 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018170 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018171 static int
18172#ifdef __BORLANDC__
18173_RTLENTRYF
18174#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018175item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018176{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018177 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018178 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018179 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018180 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018181 int res;
18182 char_u numbuf1[NUMBUFLEN];
18183 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018184
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018185 si1 = (sortItem_T *)s1;
18186 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018187 tv1 = &si1->item->li_tv;
18188 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018189
Bram Moolenaar0b962472016-02-22 22:51:33 +010018190 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018191 {
18192 long v1 = get_tv_number(tv1);
18193 long v2 = get_tv_number(tv2);
18194
18195 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18196 }
18197
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018198#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018199 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018200 {
18201 float_T v1 = get_tv_float(tv1);
18202 float_T v2 = get_tv_float(tv2);
18203
18204 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18205 }
18206#endif
18207
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018208 /* tv2string() puts quotes around a string and allocates memory. Don't do
18209 * that for string variables. Use a single quote when comparing with a
18210 * non-string to do what the docs promise. */
18211 if (tv1->v_type == VAR_STRING)
18212 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018213 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018214 p1 = (char_u *)"'";
18215 else
18216 p1 = tv1->vval.v_string;
18217 }
18218 else
18219 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18220 if (tv2->v_type == VAR_STRING)
18221 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018222 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018223 p2 = (char_u *)"'";
18224 else
18225 p2 = tv2->vval.v_string;
18226 }
18227 else
18228 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018229 if (p1 == NULL)
18230 p1 = (char_u *)"";
18231 if (p2 == NULL)
18232 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010018233 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018234 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018235 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018236 res = STRICMP(p1, p2);
18237 else
18238 res = STRCMP(p1, p2);
18239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018240 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018241 {
18242 double n1, n2;
18243 n1 = strtod((char *)p1, (char **)&p1);
18244 n2 = strtod((char *)p2, (char **)&p2);
18245 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18246 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018247
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018248 /* When the result would be zero, compare the item indexes. Makes the
18249 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018250 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018251 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018252
Bram Moolenaar0d660222005-01-07 21:51:51 +000018253 vim_free(tofree1);
18254 vim_free(tofree2);
18255 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018256}
18257
18258 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018259#ifdef __BORLANDC__
18260_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018261#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018262item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018263{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018264 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018265 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018266 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018267 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018268 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018269
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018270 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018271 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018272 return 0;
18273
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018274 si1 = (sortItem_T *)s1;
18275 si2 = (sortItem_T *)s2;
18276
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018277 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018278 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018279 copy_tv(&si1->item->li_tv, &argv[0]);
18280 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018281
18282 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018283 res = call_func(sortinfo->item_compare_func,
Bram Moolenaar4e221c92016-02-23 13:20:22 +010018284 (int)STRLEN(sortinfo->item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018285 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar0b962472016-02-22 22:51:33 +010018286 sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018287 clear_tv(&argv[0]);
18288 clear_tv(&argv[1]);
18289
18290 if (res == FAIL)
18291 res = ITEM_COMPARE_FAIL;
18292 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010018293 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
18294 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018295 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018296 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018297
18298 /* When the result would be zero, compare the pointers themselves. Makes
18299 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018300 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018301 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018302
Bram Moolenaar0d660222005-01-07 21:51:51 +000018303 return res;
18304}
18305
18306/*
18307 * "sort({list})" function
18308 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018310do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018311{
Bram Moolenaar33570922005-01-25 22:26:29 +000018312 list_T *l;
18313 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018314 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018315 sortinfo_T *old_sortinfo;
18316 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018317 long len;
18318 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018319
Bram Moolenaar0b962472016-02-22 22:51:33 +010018320 /* Pointer to current info struct used in compare function. Save and
18321 * restore the current one for nested calls. */
18322 old_sortinfo = sortinfo;
18323 sortinfo = &info;
18324
Bram Moolenaar0d660222005-01-07 21:51:51 +000018325 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018326 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018327 else
18328 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018329 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018330 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018331 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18332 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010018333 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018334 rettv->vval.v_list = l;
18335 rettv->v_type = VAR_LIST;
18336 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018337
Bram Moolenaar0d660222005-01-07 21:51:51 +000018338 len = list_len(l);
18339 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018340 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018341
Bram Moolenaar0b962472016-02-22 22:51:33 +010018342 info.item_compare_ic = FALSE;
18343 info.item_compare_numeric = FALSE;
18344 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018345#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018346 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018347#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018348 info.item_compare_func = NULL;
18349 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018350 if (argvars[1].v_type != VAR_UNKNOWN)
18351 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018352 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018353 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018354 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018355 else
18356 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018357 int error = FALSE;
18358
18359 i = get_tv_number_chk(&argvars[1], &error);
18360 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018361 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018362 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018363 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010018364 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018365 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010018366 else if (i != 0)
18367 {
18368 EMSG(_(e_invarg));
18369 goto theend;
18370 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018371 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018372 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010018373 if (*info.item_compare_func == NUL)
18374 {
18375 /* empty string means default sort */
18376 info.item_compare_func = NULL;
18377 }
18378 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018379 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018380 info.item_compare_func = NULL;
18381 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018382 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018383 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018384 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018385 info.item_compare_func = NULL;
18386 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018387 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018388#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018389 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018390 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018391 info.item_compare_func = NULL;
18392 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018393 }
18394#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018395 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018396 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018397 info.item_compare_func = NULL;
18398 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018399 }
18400 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018401 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018402
18403 if (argvars[2].v_type != VAR_UNKNOWN)
18404 {
18405 /* optional third argument: {dict} */
18406 if (argvars[2].v_type != VAR_DICT)
18407 {
18408 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010018409 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018410 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018411 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018412 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018414
Bram Moolenaar0d660222005-01-07 21:51:51 +000018415 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018416 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018417 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018418 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018419
Bram Moolenaar327aa022014-03-25 18:24:23 +010018420 i = 0;
18421 if (sort)
18422 {
18423 /* sort(): ptrs will be the list to sort */
18424 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018425 {
18426 ptrs[i].item = li;
18427 ptrs[i].idx = i;
18428 ++i;
18429 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018430
Bram Moolenaar0b962472016-02-22 22:51:33 +010018431 info.item_compare_func_err = FALSE;
18432 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018433 /* test the compare function */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018434 if (info.item_compare_func != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010018435 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018436 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018437 EMSG(_("E702: Sort compare function failed"));
18438 else
18439 {
18440 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018441 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010018442 info.item_compare_func == NULL
18443 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018444
Bram Moolenaar0b962472016-02-22 22:51:33 +010018445 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018446 {
18447 /* Clear the List and append the items in sorted order. */
18448 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18449 l->lv_len = 0;
18450 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018451 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018452 }
18453 }
18454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018455 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018456 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018457 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018458
18459 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018460 info.item_compare_func_err = FALSE;
18461 info.item_compare_keep_zero = TRUE;
18462 item_compare_func_ptr = info.item_compare_func
Bram Moolenaar327aa022014-03-25 18:24:23 +010018463 ? item_compare2 : item_compare;
18464
18465 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18466 li = li->li_next)
18467 {
18468 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18469 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018470 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018471 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018472 {
18473 EMSG(_("E882: Uniq compare function failed"));
18474 break;
18475 }
18476 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018477
Bram Moolenaar0b962472016-02-22 22:51:33 +010018478 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018479 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018480 while (--i >= 0)
18481 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018482 li = ptrs[i].item->li_next;
18483 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018484 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018485 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018486 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018487 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018488 list_fix_watch(l, li);
18489 listitem_free(li);
18490 l->lv_len--;
18491 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018492 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018493 }
18494
18495 vim_free(ptrs);
18496 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018497theend:
18498 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018499}
18500
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018501/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018502 * "sort({list})" function
18503 */
18504 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018505f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018506{
18507 do_sort_uniq(argvars, rettv, TRUE);
18508}
18509
18510/*
18511 * "uniq({list})" function
18512 */
18513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018514f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018515{
18516 do_sort_uniq(argvars, rettv, FALSE);
18517}
18518
18519/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018520 * "soundfold({word})" function
18521 */
18522 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018523f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018524{
18525 char_u *s;
18526
18527 rettv->v_type = VAR_STRING;
18528 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018529#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018530 rettv->vval.v_string = eval_soundfold(s);
18531#else
18532 rettv->vval.v_string = vim_strsave(s);
18533#endif
18534}
18535
18536/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018537 * "spellbadword()" function
18538 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018539 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018540f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018541{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018542 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018543 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018544 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018545
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018546 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018547 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018548
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018549#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018550 if (argvars[0].v_type == VAR_UNKNOWN)
18551 {
18552 /* Find the start and length of the badly spelled word. */
18553 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18554 if (len != 0)
18555 word = ml_get_cursor();
18556 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018557 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018558 {
18559 char_u *str = get_tv_string_chk(&argvars[0]);
18560 int capcol = -1;
18561
18562 if (str != NULL)
18563 {
18564 /* Check the argument for spelling. */
18565 while (*str != NUL)
18566 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018567 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018568 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018569 {
18570 word = str;
18571 break;
18572 }
18573 str += len;
18574 }
18575 }
18576 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018577#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018578
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018579 list_append_string(rettv->vval.v_list, word, len);
18580 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018581 attr == HLF_SPB ? "bad" :
18582 attr == HLF_SPR ? "rare" :
18583 attr == HLF_SPL ? "local" :
18584 attr == HLF_SPC ? "caps" :
18585 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018586}
18587
18588/*
18589 * "spellsuggest()" function
18590 */
18591 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018592f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018593{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018594#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018595 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018596 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018597 int maxcount;
18598 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018599 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018600 listitem_T *li;
18601 int need_capital = FALSE;
18602#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018603
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018604 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018605 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018606
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018607#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018608 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018609 {
18610 str = get_tv_string(&argvars[0]);
18611 if (argvars[1].v_type != VAR_UNKNOWN)
18612 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018613 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018614 if (maxcount <= 0)
18615 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018616 if (argvars[2].v_type != VAR_UNKNOWN)
18617 {
18618 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18619 if (typeerr)
18620 return;
18621 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018622 }
18623 else
18624 maxcount = 25;
18625
Bram Moolenaar4770d092006-01-12 23:22:24 +000018626 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018627
18628 for (i = 0; i < ga.ga_len; ++i)
18629 {
18630 str = ((char_u **)ga.ga_data)[i];
18631
18632 li = listitem_alloc();
18633 if (li == NULL)
18634 vim_free(str);
18635 else
18636 {
18637 li->li_tv.v_type = VAR_STRING;
18638 li->li_tv.v_lock = 0;
18639 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018640 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018641 }
18642 }
18643 ga_clear(&ga);
18644 }
18645#endif
18646}
18647
Bram Moolenaar0d660222005-01-07 21:51:51 +000018648 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018649f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018650{
18651 char_u *str;
18652 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018653 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018654 regmatch_T regmatch;
18655 char_u patbuf[NUMBUFLEN];
18656 char_u *save_cpo;
18657 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018658 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018659 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018660 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018661
18662 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18663 save_cpo = p_cpo;
18664 p_cpo = (char_u *)"";
18665
18666 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018667 if (argvars[1].v_type != VAR_UNKNOWN)
18668 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018669 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18670 if (pat == NULL)
18671 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018672 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018673 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018674 }
18675 if (pat == NULL || *pat == NUL)
18676 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018677
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018678 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018679 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018680 if (typeerr)
18681 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018682
Bram Moolenaar0d660222005-01-07 21:51:51 +000018683 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18684 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018685 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018686 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018687 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018688 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018689 if (*str == NUL)
18690 match = FALSE; /* empty item at the end */
18691 else
18692 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018693 if (match)
18694 end = regmatch.startp[0];
18695 else
18696 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018697 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
18698 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000018699 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018700 if (list_append_string(rettv->vval.v_list, str,
18701 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018702 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018703 }
18704 if (!match)
18705 break;
18706 /* Advance to just after the match. */
18707 if (regmatch.endp[0] > str)
18708 col = 0;
18709 else
18710 {
18711 /* Don't get stuck at the same match. */
18712#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018713 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018714#else
18715 col = 1;
18716#endif
18717 }
18718 str = regmatch.endp[0];
18719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018720
Bram Moolenaar473de612013-06-08 18:19:48 +020018721 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018723
Bram Moolenaar0d660222005-01-07 21:51:51 +000018724 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018725}
18726
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018727#ifdef FEAT_FLOAT
18728/*
18729 * "sqrt()" function
18730 */
18731 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018732f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018733{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018734 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018735
18736 rettv->v_type = VAR_FLOAT;
18737 if (get_float_arg(argvars, &f) == OK)
18738 rettv->vval.v_float = sqrt(f);
18739 else
18740 rettv->vval.v_float = 0.0;
18741}
18742
18743/*
18744 * "str2float()" function
18745 */
18746 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018747f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018748{
18749 char_u *p = skipwhite(get_tv_string(&argvars[0]));
18750
18751 if (*p == '+')
18752 p = skipwhite(p + 1);
18753 (void)string2float(p, &rettv->vval.v_float);
18754 rettv->v_type = VAR_FLOAT;
18755}
18756#endif
18757
Bram Moolenaar2c932302006-03-18 21:42:09 +000018758/*
18759 * "str2nr()" function
18760 */
18761 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018762f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018763{
18764 int base = 10;
18765 char_u *p;
18766 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018767 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000018768
18769 if (argvars[1].v_type != VAR_UNKNOWN)
18770 {
18771 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018772 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000018773 {
18774 EMSG(_(e_invarg));
18775 return;
18776 }
18777 }
18778
18779 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018780 if (*p == '+')
18781 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010018782 switch (base)
18783 {
18784 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
18785 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
18786 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
18787 default: what = 0;
18788 }
18789 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018790 rettv->vval.v_number = n;
18791}
18792
Bram Moolenaar071d4272004-06-13 20:20:40 +000018793#ifdef HAVE_STRFTIME
18794/*
18795 * "strftime({format}[, {time}])" function
18796 */
18797 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018798f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018799{
18800 char_u result_buf[256];
18801 struct tm *curtime;
18802 time_t seconds;
18803 char_u *p;
18804
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018805 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018806
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018807 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018808 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018809 seconds = time(NULL);
18810 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018811 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018812 curtime = localtime(&seconds);
18813 /* MSVC returns NULL for an invalid value of seconds. */
18814 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018815 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018816 else
18817 {
18818# ifdef FEAT_MBYTE
18819 vimconv_T conv;
18820 char_u *enc;
18821
18822 conv.vc_type = CONV_NONE;
18823 enc = enc_locale();
18824 convert_setup(&conv, p_enc, enc);
18825 if (conv.vc_type != CONV_NONE)
18826 p = string_convert(&conv, p, NULL);
18827# endif
18828 if (p != NULL)
18829 (void)strftime((char *)result_buf, sizeof(result_buf),
18830 (char *)p, curtime);
18831 else
18832 result_buf[0] = NUL;
18833
18834# ifdef FEAT_MBYTE
18835 if (conv.vc_type != CONV_NONE)
18836 vim_free(p);
18837 convert_setup(&conv, enc, p_enc);
18838 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018839 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018840 else
18841# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018842 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018843
18844# ifdef FEAT_MBYTE
18845 /* Release conversion descriptors */
18846 convert_setup(&conv, NULL, NULL);
18847 vim_free(enc);
18848# endif
18849 }
18850}
18851#endif
18852
18853/*
18854 * "stridx()" function
18855 */
18856 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018857f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018858{
18859 char_u buf[NUMBUFLEN];
18860 char_u *needle;
18861 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018862 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018863 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018864 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018865
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018866 needle = get_tv_string_chk(&argvars[1]);
18867 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018868 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018869 if (needle == NULL || haystack == NULL)
18870 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018871
Bram Moolenaar33570922005-01-25 22:26:29 +000018872 if (argvars[2].v_type != VAR_UNKNOWN)
18873 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018874 int error = FALSE;
18875
18876 start_idx = get_tv_number_chk(&argvars[2], &error);
18877 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018878 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018879 if (start_idx >= 0)
18880 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018881 }
18882
18883 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18884 if (pos != NULL)
18885 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018886}
18887
18888/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018889 * "string()" function
18890 */
18891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018892f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018893{
18894 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018895 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018896
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018897 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018898 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018899 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018900 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018901 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018902}
18903
18904/*
18905 * "strlen()" function
18906 */
18907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018908f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018909{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018910 rettv->vval.v_number = (varnumber_T)(STRLEN(
18911 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018912}
18913
18914/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018915 * "strchars()" function
18916 */
18917 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018918f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020018919{
18920 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018921 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020018922#ifdef FEAT_MBYTE
18923 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018924 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020018925#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020018926
18927 if (argvars[1].v_type != VAR_UNKNOWN)
18928 skipcc = get_tv_number_chk(&argvars[1], NULL);
18929 if (skipcc < 0 || skipcc > 1)
18930 EMSG(_(e_invarg));
18931 else
18932 {
18933#ifdef FEAT_MBYTE
18934 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
18935 while (*s != NUL)
18936 {
18937 func_mb_ptr2char_adv(&s);
18938 ++len;
18939 }
18940 rettv->vval.v_number = len;
18941#else
18942 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18943#endif
18944 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020018945}
18946
18947/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018948 * "strdisplaywidth()" function
18949 */
18950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018951f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020018952{
18953 char_u *s = get_tv_string(&argvars[0]);
18954 int col = 0;
18955
18956 if (argvars[1].v_type != VAR_UNKNOWN)
18957 col = get_tv_number(&argvars[1]);
18958
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018959 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018960}
18961
18962/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018963 * "strwidth()" function
18964 */
18965 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018966f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020018967{
18968 char_u *s = get_tv_string(&argvars[0]);
18969
18970 rettv->vval.v_number = (varnumber_T)(
18971#ifdef FEAT_MBYTE
18972 mb_string2cells(s, -1)
18973#else
18974 STRLEN(s)
18975#endif
18976 );
18977}
18978
18979/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018980 * "strpart()" function
18981 */
18982 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018983f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018984{
18985 char_u *p;
18986 int n;
18987 int len;
18988 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018989 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018990
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018991 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018992 slen = (int)STRLEN(p);
18993
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018994 n = get_tv_number_chk(&argvars[1], &error);
18995 if (error)
18996 len = 0;
18997 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018998 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018999 else
19000 len = slen - n; /* default len: all bytes that are available. */
19001
19002 /*
19003 * Only return the overlap between the specified part and the actual
19004 * string.
19005 */
19006 if (n < 0)
19007 {
19008 len += n;
19009 n = 0;
19010 }
19011 else if (n > slen)
19012 n = slen;
19013 if (len < 0)
19014 len = 0;
19015 else if (n + len > slen)
19016 len = slen - n;
19017
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019018 rettv->v_type = VAR_STRING;
19019 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019020}
19021
19022/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019023 * "strridx()" function
19024 */
19025 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019026f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019027{
19028 char_u buf[NUMBUFLEN];
19029 char_u *needle;
19030 char_u *haystack;
19031 char_u *rest;
19032 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019033 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019034
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019035 needle = get_tv_string_chk(&argvars[1]);
19036 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019037
19038 rettv->vval.v_number = -1;
19039 if (needle == NULL || haystack == NULL)
19040 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019041
19042 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019043 if (argvars[2].v_type != VAR_UNKNOWN)
19044 {
19045 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019046 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019047 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019048 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019049 }
19050 else
19051 end_idx = haystack_len;
19052
Bram Moolenaar0d660222005-01-07 21:51:51 +000019053 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019054 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019055 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019056 lastmatch = haystack + end_idx;
19057 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019058 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019059 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019060 for (rest = haystack; *rest != '\0'; ++rest)
19061 {
19062 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019063 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019064 break;
19065 lastmatch = rest;
19066 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019067 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019068
19069 if (lastmatch == NULL)
19070 rettv->vval.v_number = -1;
19071 else
19072 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19073}
19074
19075/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019076 * "strtrans()" function
19077 */
19078 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019079f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019080{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019081 rettv->v_type = VAR_STRING;
19082 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019083}
19084
19085/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019086 * "submatch()" function
19087 */
19088 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019089f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019090{
Bram Moolenaar41571762014-04-02 19:00:58 +020019091 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019092 int no;
19093 int retList = 0;
19094
19095 no = (int)get_tv_number_chk(&argvars[0], &error);
19096 if (error)
19097 return;
19098 error = FALSE;
19099 if (argvars[1].v_type != VAR_UNKNOWN)
19100 retList = get_tv_number_chk(&argvars[1], &error);
19101 if (error)
19102 return;
19103
19104 if (retList == 0)
19105 {
19106 rettv->v_type = VAR_STRING;
19107 rettv->vval.v_string = reg_submatch(no);
19108 }
19109 else
19110 {
19111 rettv->v_type = VAR_LIST;
19112 rettv->vval.v_list = reg_submatch_list(no);
19113 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019114}
19115
19116/*
19117 * "substitute()" function
19118 */
19119 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019120f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019121{
19122 char_u patbuf[NUMBUFLEN];
19123 char_u subbuf[NUMBUFLEN];
19124 char_u flagsbuf[NUMBUFLEN];
19125
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019126 char_u *str = get_tv_string_chk(&argvars[0]);
19127 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19128 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19129 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19130
Bram Moolenaar0d660222005-01-07 21:51:51 +000019131 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019132 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19133 rettv->vval.v_string = NULL;
19134 else
19135 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019136}
19137
19138/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019139 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019140 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019142f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019143{
19144 int id = 0;
19145#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019146 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019147 long col;
19148 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019149 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019150
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019151 lnum = get_tv_lnum(argvars); /* -1 on type error */
19152 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19153 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019154
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019155 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019156 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019157 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019158#endif
19159
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019160 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019161}
19162
19163/*
19164 * "synIDattr(id, what [, mode])" function
19165 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019166 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019167f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019168{
19169 char_u *p = NULL;
19170#ifdef FEAT_SYN_HL
19171 int id;
19172 char_u *what;
19173 char_u *mode;
19174 char_u modebuf[NUMBUFLEN];
19175 int modec;
19176
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019177 id = get_tv_number(&argvars[0]);
19178 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019179 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019180 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019181 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019182 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019183 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019184 modec = 0; /* replace invalid with current */
19185 }
19186 else
19187 {
19188#ifdef FEAT_GUI
19189 if (gui.in_use)
19190 modec = 'g';
19191 else
19192#endif
19193 if (t_colors > 1)
19194 modec = 'c';
19195 else
19196 modec = 't';
19197 }
19198
19199
19200 switch (TOLOWER_ASC(what[0]))
19201 {
19202 case 'b':
19203 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19204 p = highlight_color(id, what, modec);
19205 else /* bold */
19206 p = highlight_has_attr(id, HL_BOLD, modec);
19207 break;
19208
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019209 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019210 p = highlight_color(id, what, modec);
19211 break;
19212
19213 case 'i':
19214 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19215 p = highlight_has_attr(id, HL_INVERSE, modec);
19216 else /* italic */
19217 p = highlight_has_attr(id, HL_ITALIC, modec);
19218 break;
19219
19220 case 'n': /* name */
19221 p = get_highlight_name(NULL, id - 1);
19222 break;
19223
19224 case 'r': /* reverse */
19225 p = highlight_has_attr(id, HL_INVERSE, modec);
19226 break;
19227
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019228 case 's':
19229 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19230 p = highlight_color(id, what, modec);
19231 else /* standout */
19232 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019233 break;
19234
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019235 case 'u':
19236 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19237 /* underline */
19238 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19239 else
19240 /* undercurl */
19241 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019242 break;
19243 }
19244
19245 if (p != NULL)
19246 p = vim_strsave(p);
19247#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019248 rettv->v_type = VAR_STRING;
19249 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019250}
19251
19252/*
19253 * "synIDtrans(id)" function
19254 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019255 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019256f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019257{
19258 int id;
19259
19260#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019261 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019262
19263 if (id > 0)
19264 id = syn_get_final_id(id);
19265 else
19266#endif
19267 id = 0;
19268
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019269 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019270}
19271
19272/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019273 * "synconcealed(lnum, col)" function
19274 */
19275 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019276f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019277{
19278#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19279 long lnum;
19280 long col;
19281 int syntax_flags = 0;
19282 int cchar;
19283 int matchid = 0;
19284 char_u str[NUMBUFLEN];
19285#endif
19286
19287 rettv->v_type = VAR_LIST;
19288 rettv->vval.v_list = NULL;
19289
19290#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19291 lnum = get_tv_lnum(argvars); /* -1 on type error */
19292 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19293
19294 vim_memset(str, NUL, sizeof(str));
19295
19296 if (rettv_list_alloc(rettv) != FAIL)
19297 {
19298 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19299 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19300 && curwin->w_p_cole > 0)
19301 {
19302 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19303 syntax_flags = get_syntax_info(&matchid);
19304
19305 /* get the conceal character */
19306 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19307 {
19308 cchar = syn_get_sub_char();
19309 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19310 cchar = lcs_conceal;
19311 if (cchar != NUL)
19312 {
19313# ifdef FEAT_MBYTE
19314 if (has_mbyte)
19315 (*mb_char2bytes)(cchar, str);
19316 else
19317# endif
19318 str[0] = cchar;
19319 }
19320 }
19321 }
19322
19323 list_append_number(rettv->vval.v_list,
19324 (syntax_flags & HL_CONCEAL) != 0);
19325 /* -1 to auto-determine strlen */
19326 list_append_string(rettv->vval.v_list, str, -1);
19327 list_append_number(rettv->vval.v_list, matchid);
19328 }
19329#endif
19330}
19331
19332/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019333 * "synstack(lnum, col)" function
19334 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019335 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019336f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019337{
19338#ifdef FEAT_SYN_HL
19339 long lnum;
19340 long col;
19341 int i;
19342 int id;
19343#endif
19344
19345 rettv->v_type = VAR_LIST;
19346 rettv->vval.v_list = NULL;
19347
19348#ifdef FEAT_SYN_HL
19349 lnum = get_tv_lnum(argvars); /* -1 on type error */
19350 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19351
19352 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019353 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019354 && rettv_list_alloc(rettv) != FAIL)
19355 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019356 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019357 for (i = 0; ; ++i)
19358 {
19359 id = syn_get_stack_item(i);
19360 if (id < 0)
19361 break;
19362 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19363 break;
19364 }
19365 }
19366#endif
19367}
19368
Bram Moolenaar071d4272004-06-13 20:20:40 +000019369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019370get_cmd_output_as_rettv(
19371 typval_T *argvars,
19372 typval_T *rettv,
19373 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019374{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019375 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019376 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019377 char_u *infile = NULL;
19378 char_u buf[NUMBUFLEN];
19379 int err = FALSE;
19380 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019381 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019382 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019383
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019384 rettv->v_type = VAR_STRING;
19385 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019386 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019387 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019388
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019389 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019390 {
19391 /*
19392 * Write the string to a temp file, to be used for input of the shell
19393 * command.
19394 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019395 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019396 {
19397 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019398 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019399 }
19400
19401 fd = mch_fopen((char *)infile, WRITEBIN);
19402 if (fd == NULL)
19403 {
19404 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019405 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019406 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019407 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019408 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019409 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19410 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019411 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019412 else
19413 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019414 size_t len;
19415
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019416 p = get_tv_string_buf_chk(&argvars[1], buf);
19417 if (p == NULL)
19418 {
19419 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019420 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019421 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019422 len = STRLEN(p);
19423 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019424 err = TRUE;
19425 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019426 if (fclose(fd) != 0)
19427 err = TRUE;
19428 if (err)
19429 {
19430 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019431 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019432 }
19433 }
19434
Bram Moolenaar52a72462014-08-29 15:53:52 +020019435 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19436 * echoes typeahead, that messes up the display. */
19437 if (!msg_silent)
19438 flags += SHELL_COOKED;
19439
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019440 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019441 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019442 int len;
19443 listitem_T *li;
19444 char_u *s = NULL;
19445 char_u *start;
19446 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019447 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019448
Bram Moolenaar52a72462014-08-29 15:53:52 +020019449 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019450 if (res == NULL)
19451 goto errret;
19452
19453 list = list_alloc();
19454 if (list == NULL)
19455 goto errret;
19456
19457 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019458 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019459 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019460 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019461 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019462 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019463
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019464 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019465 if (s == NULL)
19466 goto errret;
19467
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019468 for (p = s; start < end; ++p, ++start)
19469 *p = *start == NUL ? NL : *start;
19470 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019471
19472 li = listitem_alloc();
19473 if (li == NULL)
19474 {
19475 vim_free(s);
19476 goto errret;
19477 }
19478 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019479 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019480 li->li_tv.vval.v_string = s;
19481 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019482 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019483
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019484 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019485 rettv->v_type = VAR_LIST;
19486 rettv->vval.v_list = list;
19487 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019488 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019489 else
19490 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019491 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019492#ifdef USE_CR
19493 /* translate <CR> into <NL> */
19494 if (res != NULL)
19495 {
19496 char_u *s;
19497
19498 for (s = res; *s; ++s)
19499 {
19500 if (*s == CAR)
19501 *s = NL;
19502 }
19503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019504#else
19505# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019506 /* translate <CR><NL> into <NL> */
19507 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019508 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019509 char_u *s, *d;
19510
19511 d = res;
19512 for (s = res; *s; ++s)
19513 {
19514 if (s[0] == CAR && s[1] == NL)
19515 ++s;
19516 *d++ = *s;
19517 }
19518 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019520# endif
19521#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019522 rettv->vval.v_string = res;
19523 res = NULL;
19524 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019525
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019526errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019527 if (infile != NULL)
19528 {
19529 mch_remove(infile);
19530 vim_free(infile);
19531 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019532 if (res != NULL)
19533 vim_free(res);
19534 if (list != NULL)
19535 list_free(list, TRUE);
19536}
19537
19538/*
19539 * "system()" function
19540 */
19541 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019542f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019543{
19544 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19545}
19546
19547/*
19548 * "systemlist()" function
19549 */
19550 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019551f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019552{
19553 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019554}
19555
19556/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019557 * "tabpagebuflist()" function
19558 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019559 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019560f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019561{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019562#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019563 tabpage_T *tp;
19564 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019565
19566 if (argvars[0].v_type == VAR_UNKNOWN)
19567 wp = firstwin;
19568 else
19569 {
19570 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19571 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019572 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019573 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019574 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019575 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019576 for (; wp != NULL; wp = wp->w_next)
19577 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019578 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019579 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019580 }
19581#endif
19582}
19583
19584
19585/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019586 * "tabpagenr()" function
19587 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019588 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019589f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019590{
19591 int nr = 1;
19592#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019593 char_u *arg;
19594
19595 if (argvars[0].v_type != VAR_UNKNOWN)
19596 {
19597 arg = get_tv_string_chk(&argvars[0]);
19598 nr = 0;
19599 if (arg != NULL)
19600 {
19601 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019602 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019603 else
19604 EMSG2(_(e_invexpr2), arg);
19605 }
19606 }
19607 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019608 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019609#endif
19610 rettv->vval.v_number = nr;
19611}
19612
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019613
19614#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019615static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019616
19617/*
19618 * Common code for tabpagewinnr() and winnr().
19619 */
19620 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019621get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019622{
19623 win_T *twin;
19624 int nr = 1;
19625 win_T *wp;
19626 char_u *arg;
19627
19628 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19629 if (argvar->v_type != VAR_UNKNOWN)
19630 {
19631 arg = get_tv_string_chk(argvar);
19632 if (arg == NULL)
19633 nr = 0; /* type error; errmsg already given */
19634 else if (STRCMP(arg, "$") == 0)
19635 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19636 else if (STRCMP(arg, "#") == 0)
19637 {
19638 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19639 if (twin == NULL)
19640 nr = 0;
19641 }
19642 else
19643 {
19644 EMSG2(_(e_invexpr2), arg);
19645 nr = 0;
19646 }
19647 }
19648
19649 if (nr > 0)
19650 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19651 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019652 {
19653 if (wp == NULL)
19654 {
19655 /* didn't find it in this tabpage */
19656 nr = 0;
19657 break;
19658 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019659 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019660 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019661 return nr;
19662}
19663#endif
19664
19665/*
19666 * "tabpagewinnr()" function
19667 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019668 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019669f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019670{
19671 int nr = 1;
19672#ifdef FEAT_WINDOWS
19673 tabpage_T *tp;
19674
19675 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19676 if (tp == NULL)
19677 nr = 0;
19678 else
19679 nr = get_winnr(tp, &argvars[1]);
19680#endif
19681 rettv->vval.v_number = nr;
19682}
19683
19684
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019685/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019686 * "tagfiles()" function
19687 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019688 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019689f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019690{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019691 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019692 tagname_T tn;
19693 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019694
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019695 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019696 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020019697 fname = alloc(MAXPATHL);
19698 if (fname == NULL)
19699 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019700
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019701 for (first = TRUE; ; first = FALSE)
19702 if (get_tagfname(&tn, first, fname) == FAIL
19703 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019704 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019705 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020019706 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019707}
19708
19709/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000019710 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019711 */
19712 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019713f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019714{
19715 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019716
19717 tag_pattern = get_tv_string(&argvars[0]);
19718
19719 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019720 if (*tag_pattern == NUL)
19721 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019722
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019723 if (rettv_list_alloc(rettv) == OK)
19724 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019725}
19726
19727/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019728 * "tempname()" function
19729 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019731f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019732{
19733 static int x = 'A';
19734
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019735 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019736 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019737
19738 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
19739 * names. Skip 'I' and 'O', they are used for shell redirection. */
19740 do
19741 {
19742 if (x == 'Z')
19743 x = '0';
19744 else if (x == '9')
19745 x = 'A';
19746 else
19747 {
19748#ifdef EBCDIC
19749 if (x == 'I')
19750 x = 'J';
19751 else if (x == 'R')
19752 x = 'S';
19753 else
19754#endif
19755 ++x;
19756 }
19757 } while (x == 'I' || x == 'O');
19758}
19759
19760/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019761 * "test(list)" function: Just checking the walls...
19762 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019763 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019764f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000019765{
19766 /* Used for unit testing. Change the code below to your liking. */
19767#if 0
19768 listitem_T *li;
19769 list_T *l;
19770 char_u *bad, *good;
19771
19772 if (argvars[0].v_type != VAR_LIST)
19773 return;
19774 l = argvars[0].vval.v_list;
19775 if (l == NULL)
19776 return;
19777 li = l->lv_first;
19778 if (li == NULL)
19779 return;
19780 bad = get_tv_string(&li->li_tv);
19781 li = li->li_next;
19782 if (li == NULL)
19783 return;
19784 good = get_tv_string(&li->li_tv);
19785 rettv->vval.v_number = test_edit_score(bad, good);
19786#endif
19787}
19788
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019789#ifdef FEAT_FLOAT
19790/*
19791 * "tan()" function
19792 */
19793 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019794f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019795{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019796 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019797
19798 rettv->v_type = VAR_FLOAT;
19799 if (get_float_arg(argvars, &f) == OK)
19800 rettv->vval.v_float = tan(f);
19801 else
19802 rettv->vval.v_float = 0.0;
19803}
19804
19805/*
19806 * "tanh()" function
19807 */
19808 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019809f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019810{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019811 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019812
19813 rettv->v_type = VAR_FLOAT;
19814 if (get_float_arg(argvars, &f) == OK)
19815 rettv->vval.v_float = tanh(f);
19816 else
19817 rettv->vval.v_float = 0.0;
19818}
19819#endif
19820
Bram Moolenaard52d9742005-08-21 22:20:28 +000019821/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019822 * "tolower(string)" function
19823 */
19824 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019825f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019826{
19827 char_u *p;
19828
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019829 p = vim_strsave(get_tv_string(&argvars[0]));
19830 rettv->v_type = VAR_STRING;
19831 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019832
19833 if (p != NULL)
19834 while (*p != NUL)
19835 {
19836#ifdef FEAT_MBYTE
19837 int l;
19838
19839 if (enc_utf8)
19840 {
19841 int c, lc;
19842
19843 c = utf_ptr2char(p);
19844 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019845 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019846 /* TODO: reallocate string when byte count changes. */
19847 if (utf_char2len(lc) == l)
19848 utf_char2bytes(lc, p);
19849 p += l;
19850 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019851 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019852 p += l; /* skip multi-byte character */
19853 else
19854#endif
19855 {
19856 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19857 ++p;
19858 }
19859 }
19860}
19861
19862/*
19863 * "toupper(string)" function
19864 */
19865 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019866f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019867{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019868 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019869 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019870}
19871
19872/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019873 * "tr(string, fromstr, tostr)" function
19874 */
19875 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019876f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019877{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019878 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019879 char_u *fromstr;
19880 char_u *tostr;
19881 char_u *p;
19882#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019883 int inlen;
19884 int fromlen;
19885 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019886 int idx;
19887 char_u *cpstr;
19888 int cplen;
19889 int first = TRUE;
19890#endif
19891 char_u buf[NUMBUFLEN];
19892 char_u buf2[NUMBUFLEN];
19893 garray_T ga;
19894
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019895 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019896 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19897 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019898
19899 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019900 rettv->v_type = VAR_STRING;
19901 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019902 if (fromstr == NULL || tostr == NULL)
19903 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019904 ga_init2(&ga, (int)sizeof(char), 80);
19905
19906#ifdef FEAT_MBYTE
19907 if (!has_mbyte)
19908#endif
19909 /* not multi-byte: fromstr and tostr must be the same length */
19910 if (STRLEN(fromstr) != STRLEN(tostr))
19911 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019912#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019913error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019914#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019915 EMSG2(_(e_invarg2), fromstr);
19916 ga_clear(&ga);
19917 return;
19918 }
19919
19920 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019921 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019922 {
19923#ifdef FEAT_MBYTE
19924 if (has_mbyte)
19925 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019926 inlen = (*mb_ptr2len)(in_str);
19927 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019928 cplen = inlen;
19929 idx = 0;
19930 for (p = fromstr; *p != NUL; p += fromlen)
19931 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019932 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019933 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019934 {
19935 for (p = tostr; *p != NUL; p += tolen)
19936 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019937 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019938 if (idx-- == 0)
19939 {
19940 cplen = tolen;
19941 cpstr = p;
19942 break;
19943 }
19944 }
19945 if (*p == NUL) /* tostr is shorter than fromstr */
19946 goto error;
19947 break;
19948 }
19949 ++idx;
19950 }
19951
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019952 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019953 {
19954 /* Check that fromstr and tostr have the same number of
19955 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019956 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019957 first = FALSE;
19958 for (p = tostr; *p != NUL; p += tolen)
19959 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019960 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019961 --idx;
19962 }
19963 if (idx != 0)
19964 goto error;
19965 }
19966
Bram Moolenaarcde88542015-08-11 19:14:00 +020019967 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019968 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019969 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019970
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019971 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019972 }
19973 else
19974#endif
19975 {
19976 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019977 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019978 if (p != NULL)
19979 ga_append(&ga, tostr[p - fromstr]);
19980 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019981 ga_append(&ga, *in_str);
19982 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019983 }
19984 }
19985
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019986 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020019987 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019988 ga_append(&ga, NUL);
19989
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019990 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019991}
19992
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019993#ifdef FEAT_FLOAT
19994/*
19995 * "trunc({float})" function
19996 */
19997 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019998f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019999{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020000 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020001
20002 rettv->v_type = VAR_FLOAT;
20003 if (get_float_arg(argvars, &f) == OK)
20004 /* trunc() is not in C90, use floor() or ceil() instead. */
20005 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20006 else
20007 rettv->vval.v_float = 0.0;
20008}
20009#endif
20010
Bram Moolenaar8299df92004-07-10 09:47:34 +000020011/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020012 * "type(expr)" function
20013 */
20014 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020015f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020016{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020017 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020018
20019 switch (argvars[0].v_type)
20020 {
20021 case VAR_NUMBER: n = 0; break;
20022 case VAR_STRING: n = 1; break;
20023 case VAR_FUNC: n = 2; break;
20024 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020025 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020026 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020027 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020028 if (argvars[0].vval.v_number == VVAL_FALSE
20029 || argvars[0].vval.v_number == VVAL_TRUE)
20030 n = 6;
20031 else
20032 n = 7;
20033 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020034 case VAR_JOB: n = 8; break;
20035 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020036 case VAR_UNKNOWN:
20037 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20038 n = -1;
20039 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020040 }
20041 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020042}
20043
20044/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020045 * "undofile(name)" function
20046 */
20047 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020048f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020049{
20050 rettv->v_type = VAR_STRING;
20051#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020052 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020053 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020054
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020055 if (*fname == NUL)
20056 {
20057 /* If there is no file name there will be no undo file. */
20058 rettv->vval.v_string = NULL;
20059 }
20060 else
20061 {
20062 char_u *ffname = FullName_save(fname, FALSE);
20063
20064 if (ffname != NULL)
20065 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20066 vim_free(ffname);
20067 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020068 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020069#else
20070 rettv->vval.v_string = NULL;
20071#endif
20072}
20073
20074/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020075 * "undotree()" function
20076 */
20077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020078f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020079{
20080 if (rettv_dict_alloc(rettv) == OK)
20081 {
20082 dict_T *dict = rettv->vval.v_dict;
20083 list_T *list;
20084
Bram Moolenaar730cde92010-06-27 05:18:54 +020020085 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020086 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020087 dict_add_nr_str(dict, "save_last",
20088 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020089 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20090 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020091 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020092
20093 list = list_alloc();
20094 if (list != NULL)
20095 {
20096 u_eval_tree(curbuf->b_u_oldhead, list);
20097 dict_add_list(dict, "entries", list);
20098 }
20099 }
20100}
20101
20102/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020103 * "values(dict)" function
20104 */
20105 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020106f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020107{
20108 dict_list(argvars, rettv, 1);
20109}
20110
20111/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020112 * "virtcol(string)" function
20113 */
20114 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020115f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020116{
20117 colnr_T vcol = 0;
20118 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020119 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020120
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020121 fp = var2fpos(&argvars[0], FALSE, &fnum);
20122 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20123 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020124 {
20125 getvvcol(curwin, fp, NULL, NULL, &vcol);
20126 ++vcol;
20127 }
20128
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020129 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020130}
20131
20132/*
20133 * "visualmode()" function
20134 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020135 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010020136f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020137{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020138 char_u str[2];
20139
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020140 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020141 str[0] = curbuf->b_visual_mode_eval;
20142 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020143 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020144
20145 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020146 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020147 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020148}
20149
20150/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020151 * "wildmenumode()" function
20152 */
20153 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020154f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020155{
20156#ifdef FEAT_WILDMENU
20157 if (wild_menu_showing)
20158 rettv->vval.v_number = 1;
20159#endif
20160}
20161
20162/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020163 * "winbufnr(nr)" function
20164 */
20165 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020166f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020167{
20168 win_T *wp;
20169
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020170 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020171 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020172 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020173 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020174 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020175}
20176
20177/*
20178 * "wincol()" function
20179 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020180 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020181f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020182{
20183 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020184 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020185}
20186
20187/*
20188 * "winheight(nr)" function
20189 */
20190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020191f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020192{
20193 win_T *wp;
20194
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020195 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020196 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020197 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020198 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020199 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020200}
20201
20202/*
20203 * "winline()" function
20204 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020206f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020207{
20208 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020209 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020210}
20211
20212/*
20213 * "winnr()" function
20214 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020215 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020216f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020217{
20218 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020219
Bram Moolenaar071d4272004-06-13 20:20:40 +000020220#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020221 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020222#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020223 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020224}
20225
20226/*
20227 * "winrestcmd()" function
20228 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020229 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020230f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020231{
20232#ifdef FEAT_WINDOWS
20233 win_T *wp;
20234 int winnr = 1;
20235 garray_T ga;
20236 char_u buf[50];
20237
20238 ga_init2(&ga, (int)sizeof(char), 70);
20239 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20240 {
20241 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20242 ga_concat(&ga, buf);
20243# ifdef FEAT_VERTSPLIT
20244 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20245 ga_concat(&ga, buf);
20246# endif
20247 ++winnr;
20248 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020249 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020250
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020251 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020252#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020253 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020254#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020255 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020256}
20257
20258/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020259 * "winrestview()" function
20260 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020261 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020262f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020263{
20264 dict_T *dict;
20265
20266 if (argvars[0].v_type != VAR_DICT
20267 || (dict = argvars[0].vval.v_dict) == NULL)
20268 EMSG(_(e_invarg));
20269 else
20270 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020271 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20272 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20273 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20274 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020275#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020276 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20277 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020278#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020279 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20280 {
20281 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20282 curwin->w_set_curswant = FALSE;
20283 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020284
Bram Moolenaar82c25852014-05-28 16:47:16 +020020285 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20286 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020287#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020288 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20289 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020290#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020291 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20292 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20293 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20294 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020295
20296 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020297 win_new_height(curwin, curwin->w_height);
20298# ifdef FEAT_VERTSPLIT
20299 win_new_width(curwin, W_WIDTH(curwin));
20300# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020301 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020302
Bram Moolenaarb851a962014-10-31 15:45:52 +010020303 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020304 curwin->w_topline = 1;
20305 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20306 curwin->w_topline = curbuf->b_ml.ml_line_count;
20307#ifdef FEAT_DIFF
20308 check_topfill(curwin, TRUE);
20309#endif
20310 }
20311}
20312
20313/*
20314 * "winsaveview()" function
20315 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020316 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020317f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020318{
20319 dict_T *dict;
20320
Bram Moolenaara800b422010-06-27 01:15:55 +020020321 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020322 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020323 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020324
20325 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20326 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20327#ifdef FEAT_VIRTUALEDIT
20328 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20329#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020330 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020331 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20332
20333 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20334#ifdef FEAT_DIFF
20335 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20336#endif
20337 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20338 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20339}
20340
20341/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020342 * "winwidth(nr)" function
20343 */
20344 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020345f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020346{
20347 win_T *wp;
20348
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020349 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020350 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020351 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020352 else
20353#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020354 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020355#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020356 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020357#endif
20358}
20359
Bram Moolenaar071d4272004-06-13 20:20:40 +000020360/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020361 * "wordcount()" function
20362 */
20363 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020364f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010020365{
20366 if (rettv_dict_alloc(rettv) == FAIL)
20367 return;
20368 cursor_pos_info(rettv->vval.v_dict);
20369}
20370
20371/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020372 * Write list of strings to file
20373 */
20374 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020375write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020376{
20377 listitem_T *li;
20378 int c;
20379 int ret = OK;
20380 char_u *s;
20381
20382 for (li = list->lv_first; li != NULL; li = li->li_next)
20383 {
20384 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20385 {
20386 if (*s == '\n')
20387 c = putc(NUL, fd);
20388 else
20389 c = putc(*s, fd);
20390 if (c == EOF)
20391 {
20392 ret = FAIL;
20393 break;
20394 }
20395 }
20396 if (!binary || li->li_next != NULL)
20397 if (putc('\n', fd) == EOF)
20398 {
20399 ret = FAIL;
20400 break;
20401 }
20402 if (ret == FAIL)
20403 {
20404 EMSG(_(e_write));
20405 break;
20406 }
20407 }
20408 return ret;
20409}
20410
20411/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020412 * "writefile()" function
20413 */
20414 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020415f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020416{
20417 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020418 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020419 char_u *fname;
20420 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020421 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020422
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020423 if (check_restricted() || check_secure())
20424 return;
20425
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020426 if (argvars[0].v_type != VAR_LIST)
20427 {
20428 EMSG2(_(e_listarg), "writefile()");
20429 return;
20430 }
20431 if (argvars[0].vval.v_list == NULL)
20432 return;
20433
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020434 if (argvars[2].v_type != VAR_UNKNOWN)
20435 {
20436 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20437 binary = TRUE;
20438 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20439 append = TRUE;
20440 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020441
20442 /* Always open the file in binary mode, library functions have a mind of
20443 * their own about CR-LF conversion. */
20444 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020445 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20446 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020447 {
20448 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20449 ret = -1;
20450 }
20451 else
20452 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020453 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20454 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020455 fclose(fd);
20456 }
20457
20458 rettv->vval.v_number = ret;
20459}
20460
20461/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020462 * "xor(expr, expr)" function
20463 */
20464 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020465f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020466{
20467 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20468 ^ get_tv_number_chk(&argvars[1], NULL);
20469}
20470
20471
20472/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020473 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020474 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020475 */
20476 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020477var2fpos(
20478 typval_T *varp,
20479 int dollar_lnum, /* TRUE when $ is last line */
20480 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020481{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020482 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020483 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020484 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020485
Bram Moolenaara5525202006-03-02 22:52:09 +000020486 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020487 if (varp->v_type == VAR_LIST)
20488 {
20489 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020490 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020491 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020492 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020493
20494 l = varp->vval.v_list;
20495 if (l == NULL)
20496 return NULL;
20497
20498 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020499 pos.lnum = list_find_nr(l, 0L, &error);
20500 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020501 return NULL; /* invalid line number */
20502
20503 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020504 pos.col = list_find_nr(l, 1L, &error);
20505 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020506 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020507 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020508
20509 /* We accept "$" for the column number: last column. */
20510 li = list_find(l, 1L);
20511 if (li != NULL && li->li_tv.v_type == VAR_STRING
20512 && li->li_tv.vval.v_string != NULL
20513 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20514 pos.col = len + 1;
20515
Bram Moolenaara5525202006-03-02 22:52:09 +000020516 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020517 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020518 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020519 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020520
Bram Moolenaara5525202006-03-02 22:52:09 +000020521#ifdef FEAT_VIRTUALEDIT
20522 /* Get the virtual offset. Defaults to zero. */
20523 pos.coladd = list_find_nr(l, 2L, &error);
20524 if (error)
20525 pos.coladd = 0;
20526#endif
20527
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020528 return &pos;
20529 }
20530
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020531 name = get_tv_string_chk(varp);
20532 if (name == NULL)
20533 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020534 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020535 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020536 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20537 {
20538 if (VIsual_active)
20539 return &VIsual;
20540 return &curwin->w_cursor;
20541 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020542 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020543 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020544 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020545 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20546 return NULL;
20547 return pp;
20548 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020549
20550#ifdef FEAT_VIRTUALEDIT
20551 pos.coladd = 0;
20552#endif
20553
Bram Moolenaar477933c2007-07-17 14:32:23 +000020554 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020555 {
20556 pos.col = 0;
20557 if (name[1] == '0') /* "w0": first visible line */
20558 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020559 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020560 pos.lnum = curwin->w_topline;
20561 return &pos;
20562 }
20563 else if (name[1] == '$') /* "w$": last visible line */
20564 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020565 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020566 pos.lnum = curwin->w_botline - 1;
20567 return &pos;
20568 }
20569 }
20570 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020571 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020572 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020573 {
20574 pos.lnum = curbuf->b_ml.ml_line_count;
20575 pos.col = 0;
20576 }
20577 else
20578 {
20579 pos.lnum = curwin->w_cursor.lnum;
20580 pos.col = (colnr_T)STRLEN(ml_get_curline());
20581 }
20582 return &pos;
20583 }
20584 return NULL;
20585}
20586
20587/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020588 * Convert list in "arg" into a position and optional file number.
20589 * When "fnump" is NULL there is no file number, only 3 items.
20590 * Note that the column is passed on as-is, the caller may want to decrement
20591 * it to use 1 for the first column.
20592 * Return FAIL when conversion is not possible, doesn't check the position for
20593 * validity.
20594 */
20595 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020596list2fpos(
20597 typval_T *arg,
20598 pos_T *posp,
20599 int *fnump,
20600 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020601{
20602 list_T *l = arg->vval.v_list;
20603 long i = 0;
20604 long n;
20605
Bram Moolenaar493c1782014-05-28 14:34:46 +020020606 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20607 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020608 if (arg->v_type != VAR_LIST
20609 || l == NULL
20610 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020611 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020612 return FAIL;
20613
20614 if (fnump != NULL)
20615 {
20616 n = list_find_nr(l, i++, NULL); /* fnum */
20617 if (n < 0)
20618 return FAIL;
20619 if (n == 0)
20620 n = curbuf->b_fnum; /* current buffer */
20621 *fnump = n;
20622 }
20623
20624 n = list_find_nr(l, i++, NULL); /* lnum */
20625 if (n < 0)
20626 return FAIL;
20627 posp->lnum = n;
20628
20629 n = list_find_nr(l, i++, NULL); /* col */
20630 if (n < 0)
20631 return FAIL;
20632 posp->col = n;
20633
20634#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020020635 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020636 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000020637 posp->coladd = 0;
20638 else
20639 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020640#endif
20641
Bram Moolenaar493c1782014-05-28 14:34:46 +020020642 if (curswantp != NULL)
20643 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
20644
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020645 return OK;
20646}
20647
20648/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020649 * Get the length of an environment variable name.
20650 * Advance "arg" to the first character after the name.
20651 * Return 0 for error.
20652 */
20653 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020654get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020655{
20656 char_u *p;
20657 int len;
20658
20659 for (p = *arg; vim_isIDc(*p); ++p)
20660 ;
20661 if (p == *arg) /* no name found */
20662 return 0;
20663
20664 len = (int)(p - *arg);
20665 *arg = p;
20666 return len;
20667}
20668
20669/*
20670 * Get the length of the name of a function or internal variable.
20671 * "arg" is advanced to the first non-white character after the name.
20672 * Return 0 if something is wrong.
20673 */
20674 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020675get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020676{
20677 char_u *p;
20678 int len;
20679
20680 /* Find the end of the name. */
20681 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020682 {
20683 if (*p == ':')
20684 {
20685 /* "s:" is start of "s:var", but "n:" is not and can be used in
20686 * slice "[n:]". Also "xx:" is not a namespace. */
20687 len = (int)(p - *arg);
20688 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
20689 || len > 1)
20690 break;
20691 }
20692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020693 if (p == *arg) /* no name found */
20694 return 0;
20695
20696 len = (int)(p - *arg);
20697 *arg = skipwhite(p);
20698
20699 return len;
20700}
20701
20702/*
Bram Moolenaara7043832005-01-21 11:56:39 +000020703 * Get the length of the name of a variable or function.
20704 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020705 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020706 * Return -1 if curly braces expansion failed.
20707 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020708 * If the name contains 'magic' {}'s, expand them and return the
20709 * expanded name in an allocated string via 'alias' - caller must free.
20710 */
20711 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020712get_name_len(
20713 char_u **arg,
20714 char_u **alias,
20715 int evaluate,
20716 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020717{
20718 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020719 char_u *p;
20720 char_u *expr_start;
20721 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020722
20723 *alias = NULL; /* default to no alias */
20724
20725 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20726 && (*arg)[2] == (int)KE_SNR)
20727 {
20728 /* hard coded <SNR>, already translated */
20729 *arg += 3;
20730 return get_id_len(arg) + 3;
20731 }
20732 len = eval_fname_script(*arg);
20733 if (len > 0)
20734 {
20735 /* literal "<SID>", "s:" or "<SNR>" */
20736 *arg += len;
20737 }
20738
Bram Moolenaar071d4272004-06-13 20:20:40 +000020739 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020740 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020741 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020742 p = find_name_end(*arg, &expr_start, &expr_end,
20743 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020744 if (expr_start != NULL)
20745 {
20746 char_u *temp_string;
20747
20748 if (!evaluate)
20749 {
20750 len += (int)(p - *arg);
20751 *arg = skipwhite(p);
20752 return len;
20753 }
20754
20755 /*
20756 * Include any <SID> etc in the expanded string:
20757 * Thus the -len here.
20758 */
20759 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20760 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020761 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020762 *alias = temp_string;
20763 *arg = skipwhite(p);
20764 return (int)STRLEN(temp_string);
20765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020766
20767 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020768 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020769 EMSG2(_(e_invexpr2), *arg);
20770
20771 return len;
20772}
20773
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020774/*
20775 * Find the end of a variable or function name, taking care of magic braces.
20776 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20777 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020778 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020779 * Return a pointer to just after the name. Equal to "arg" if there is no
20780 * valid name.
20781 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020782 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020783find_name_end(
20784 char_u *arg,
20785 char_u **expr_start,
20786 char_u **expr_end,
20787 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020788{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020789 int mb_nest = 0;
20790 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020791 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020792 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020793
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020794 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020795 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020796 *expr_start = NULL;
20797 *expr_end = NULL;
20798 }
20799
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020800 /* Quick check for valid starting character. */
20801 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20802 return arg;
20803
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020804 for (p = arg; *p != NUL
20805 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020806 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020807 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020808 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020809 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020810 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020811 if (*p == '\'')
20812 {
20813 /* skip over 'string' to avoid counting [ and ] inside it. */
20814 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20815 ;
20816 if (*p == NUL)
20817 break;
20818 }
20819 else if (*p == '"')
20820 {
20821 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20822 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20823 if (*p == '\\' && p[1] != NUL)
20824 ++p;
20825 if (*p == NUL)
20826 break;
20827 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020828 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
20829 {
20830 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020831 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020832 len = (int)(p - arg);
20833 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010020834 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010020835 break;
20836 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020837
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020838 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020839 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020840 if (*p == '[')
20841 ++br_nest;
20842 else if (*p == ']')
20843 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020844 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020845
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020846 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020847 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020848 if (*p == '{')
20849 {
20850 mb_nest++;
20851 if (expr_start != NULL && *expr_start == NULL)
20852 *expr_start = p;
20853 }
20854 else if (*p == '}')
20855 {
20856 mb_nest--;
20857 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20858 *expr_end = p;
20859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020861 }
20862
20863 return p;
20864}
20865
20866/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020867 * Expands out the 'magic' {}'s in a variable/function name.
20868 * Note that this can call itself recursively, to deal with
20869 * constructs like foo{bar}{baz}{bam}
20870 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20871 * "in_start" ^
20872 * "expr_start" ^
20873 * "expr_end" ^
20874 * "in_end" ^
20875 *
20876 * Returns a new allocated string, which the caller must free.
20877 * Returns NULL for failure.
20878 */
20879 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020880make_expanded_name(
20881 char_u *in_start,
20882 char_u *expr_start,
20883 char_u *expr_end,
20884 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020885{
20886 char_u c1;
20887 char_u *retval = NULL;
20888 char_u *temp_result;
20889 char_u *nextcmd = NULL;
20890
20891 if (expr_end == NULL || in_end == NULL)
20892 return NULL;
20893 *expr_start = NUL;
20894 *expr_end = NUL;
20895 c1 = *in_end;
20896 *in_end = NUL;
20897
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020898 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020899 if (temp_result != NULL && nextcmd == NULL)
20900 {
20901 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20902 + (in_end - expr_end) + 1));
20903 if (retval != NULL)
20904 {
20905 STRCPY(retval, in_start);
20906 STRCAT(retval, temp_result);
20907 STRCAT(retval, expr_end + 1);
20908 }
20909 }
20910 vim_free(temp_result);
20911
20912 *in_end = c1; /* put char back for error messages */
20913 *expr_start = '{';
20914 *expr_end = '}';
20915
20916 if (retval != NULL)
20917 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020918 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020919 if (expr_start != NULL)
20920 {
20921 /* Further expansion! */
20922 temp_result = make_expanded_name(retval, expr_start,
20923 expr_end, temp_result);
20924 vim_free(retval);
20925 retval = temp_result;
20926 }
20927 }
20928
20929 return retval;
20930}
20931
20932/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020933 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020934 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020935 */
20936 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020937eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020938{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020939 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20940}
20941
20942/*
20943 * Return TRUE if character "c" can be used as the first character in a
20944 * variable or function name (excluding '{' and '}').
20945 */
20946 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020947eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020948{
20949 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020950}
20951
20952/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020953 * Set number v: variable to "val".
20954 */
20955 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020956set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020957{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020958 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020959}
20960
20961/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020962 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020963 */
20964 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010020965get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020966{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020967 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020968}
20969
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020970/*
20971 * Get string v: variable value. Uses a static buffer, can only be used once.
20972 */
20973 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020974get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020975{
20976 return get_tv_string(&vimvars[idx].vv_tv);
20977}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020978
Bram Moolenaar071d4272004-06-13 20:20:40 +000020979/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020980 * Get List v: variable value. Caller must take care of reference count when
20981 * needed.
20982 */
20983 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020984get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000020985{
20986 return vimvars[idx].vv_list;
20987}
20988
20989/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020990 * Set v:char to character "c".
20991 */
20992 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020993set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020994{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020995 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020996
20997#ifdef FEAT_MBYTE
20998 if (has_mbyte)
20999 buf[(*mb_char2bytes)(c, buf)] = NUL;
21000 else
21001#endif
21002 {
21003 buf[0] = c;
21004 buf[1] = NUL;
21005 }
21006 set_vim_var_string(VV_CHAR, buf, -1);
21007}
21008
21009/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021010 * Set v:count to "count" and v:count1 to "count1".
21011 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021012 */
21013 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021014set_vcount(
21015 long count,
21016 long count1,
21017 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021018{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021019 if (set_prevcount)
21020 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021021 vimvars[VV_COUNT].vv_nr = count;
21022 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021023}
21024
21025/*
21026 * Set string v: variable to a copy of "val".
21027 */
21028 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021029set_vim_var_string(
21030 int idx,
21031 char_u *val,
21032 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021033{
Bram Moolenaara542c682016-01-31 16:28:04 +010021034 clear_tv(&vimvars[idx].vv_di.di_tv);
21035 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021036 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021037 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021038 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021039 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021040 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021041 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021042}
21043
21044/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021045 * Set List v: variable to "val".
21046 */
21047 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021048set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021049{
Bram Moolenaara542c682016-01-31 16:28:04 +010021050 clear_tv(&vimvars[idx].vv_di.di_tv);
21051 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021052 vimvars[idx].vv_list = val;
21053 if (val != NULL)
21054 ++val->lv_refcount;
21055}
21056
21057/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021058 * Set Dictionary v: variable to "val".
21059 */
21060 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021061set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021062{
21063 int todo;
21064 hashitem_T *hi;
21065
Bram Moolenaara542c682016-01-31 16:28:04 +010021066 clear_tv(&vimvars[idx].vv_di.di_tv);
21067 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021068 vimvars[idx].vv_dict = val;
21069 if (val != NULL)
21070 {
21071 ++val->dv_refcount;
21072
21073 /* Set readonly */
21074 todo = (int)val->dv_hashtab.ht_used;
21075 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21076 {
21077 if (HASHITEM_EMPTY(hi))
21078 continue;
21079 --todo;
21080 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21081 }
21082 }
21083}
21084
21085/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021086 * Set v:register if needed.
21087 */
21088 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021089set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021090{
21091 char_u regname;
21092
21093 if (c == 0 || c == ' ')
21094 regname = '"';
21095 else
21096 regname = c;
21097 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021098 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021099 set_vim_var_string(VV_REG, &regname, 1);
21100}
21101
21102/*
21103 * Get or set v:exception. If "oldval" == NULL, return the current value.
21104 * Otherwise, restore the value to "oldval" and return NULL.
21105 * Must always be called in pairs to save and restore v:exception! Does not
21106 * take care of memory allocations.
21107 */
21108 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021109v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021110{
21111 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021112 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021113
Bram Moolenaare9a41262005-01-15 22:18:47 +000021114 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021115 return NULL;
21116}
21117
21118/*
21119 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21120 * Otherwise, restore the value to "oldval" and return NULL.
21121 * Must always be called in pairs to save and restore v:throwpoint! Does not
21122 * take care of memory allocations.
21123 */
21124 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021125v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021126{
21127 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021128 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021129
Bram Moolenaare9a41262005-01-15 22:18:47 +000021130 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021131 return NULL;
21132}
21133
21134#if defined(FEAT_AUTOCMD) || defined(PROTO)
21135/*
21136 * Set v:cmdarg.
21137 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21138 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21139 * Must always be called in pairs!
21140 */
21141 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021142set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021143{
21144 char_u *oldval;
21145 char_u *newval;
21146 unsigned len;
21147
Bram Moolenaare9a41262005-01-15 22:18:47 +000021148 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021149 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021150 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021151 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021152 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021153 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021154 }
21155
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021156 if (eap->force_bin == FORCE_BIN)
21157 len = 6;
21158 else if (eap->force_bin == FORCE_NOBIN)
21159 len = 8;
21160 else
21161 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021162
21163 if (eap->read_edit)
21164 len += 7;
21165
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021166 if (eap->force_ff != 0)
21167 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21168# ifdef FEAT_MBYTE
21169 if (eap->force_enc != 0)
21170 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021171 if (eap->bad_char != 0)
21172 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021173# endif
21174
21175 newval = alloc(len + 1);
21176 if (newval == NULL)
21177 return NULL;
21178
21179 if (eap->force_bin == FORCE_BIN)
21180 sprintf((char *)newval, " ++bin");
21181 else if (eap->force_bin == FORCE_NOBIN)
21182 sprintf((char *)newval, " ++nobin");
21183 else
21184 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021185
21186 if (eap->read_edit)
21187 STRCAT(newval, " ++edit");
21188
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021189 if (eap->force_ff != 0)
21190 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21191 eap->cmd + eap->force_ff);
21192# ifdef FEAT_MBYTE
21193 if (eap->force_enc != 0)
21194 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21195 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021196 if (eap->bad_char == BAD_KEEP)
21197 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21198 else if (eap->bad_char == BAD_DROP)
21199 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21200 else if (eap->bad_char != 0)
21201 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021202# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021203 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021204 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021205}
21206#endif
21207
21208/*
21209 * Get the value of internal variable "name".
21210 * Return OK or FAIL.
21211 */
21212 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021213get_var_tv(
21214 char_u *name,
21215 int len, /* length of "name" */
21216 typval_T *rettv, /* NULL when only checking existence */
21217 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21218 int verbose, /* may give error message */
21219 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021220{
21221 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021222 typval_T *tv = NULL;
21223 typval_T atv;
21224 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021225 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021226
21227 /* truncate the name, so that we can use strcmp() */
21228 cc = name[len];
21229 name[len] = NUL;
21230
21231 /*
21232 * Check for "b:changedtick".
21233 */
21234 if (STRCMP(name, "b:changedtick") == 0)
21235 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021236 atv.v_type = VAR_NUMBER;
21237 atv.vval.v_number = curbuf->b_changedtick;
21238 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021239 }
21240
21241 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021242 * Check for user-defined variables.
21243 */
21244 else
21245 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021246 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021247 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021248 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021249 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021250 if (dip != NULL)
21251 *dip = v;
21252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021253 }
21254
Bram Moolenaare9a41262005-01-15 22:18:47 +000021255 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021256 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021257 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021258 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021259 ret = FAIL;
21260 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021261 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021262 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021263
21264 name[len] = cc;
21265
21266 return ret;
21267}
21268
21269/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021270 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21271 * Also handle function call with Funcref variable: func(expr)
21272 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21273 */
21274 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021275handle_subscript(
21276 char_u **arg,
21277 typval_T *rettv,
21278 int evaluate, /* do more than finding the end */
21279 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021280{
21281 int ret = OK;
21282 dict_T *selfdict = NULL;
21283 char_u *s;
21284 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021285 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021286
21287 while (ret == OK
21288 && (**arg == '['
21289 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021290 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021291 && !vim_iswhite(*(*arg - 1)))
21292 {
21293 if (**arg == '(')
21294 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021295 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021296 if (evaluate)
21297 {
21298 functv = *rettv;
21299 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021300
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021301 /* Invoke the function. Recursive! */
21302 s = functv.vval.v_string;
21303 }
21304 else
21305 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021306 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021307 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
21308 &len, evaluate, selfdict);
21309
21310 /* Clear the funcref afterwards, so that deleting it while
21311 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021312 if (evaluate)
21313 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021314
21315 /* Stop the expression evaluation when immediately aborting on
21316 * error, or when an interrupt occurred or an exception was thrown
21317 * but not caught. */
21318 if (aborting())
21319 {
21320 if (ret == OK)
21321 clear_tv(rettv);
21322 ret = FAIL;
21323 }
21324 dict_unref(selfdict);
21325 selfdict = NULL;
21326 }
21327 else /* **arg == '[' || **arg == '.' */
21328 {
21329 dict_unref(selfdict);
21330 if (rettv->v_type == VAR_DICT)
21331 {
21332 selfdict = rettv->vval.v_dict;
21333 if (selfdict != NULL)
21334 ++selfdict->dv_refcount;
21335 }
21336 else
21337 selfdict = NULL;
21338 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21339 {
21340 clear_tv(rettv);
21341 ret = FAIL;
21342 }
21343 }
21344 }
21345 dict_unref(selfdict);
21346 return ret;
21347}
21348
21349/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021350 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021351 * value).
21352 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010021353 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021354alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021355{
Bram Moolenaar33570922005-01-25 22:26:29 +000021356 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021357}
21358
21359/*
21360 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021361 * The string "s" must have been allocated, it is consumed.
21362 * Return NULL for out of memory, the variable otherwise.
21363 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021364 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021365alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021366{
Bram Moolenaar33570922005-01-25 22:26:29 +000021367 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021368
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021369 rettv = alloc_tv();
21370 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021371 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021372 rettv->v_type = VAR_STRING;
21373 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021374 }
21375 else
21376 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021377 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021378}
21379
21380/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021381 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021382 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021383 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021384free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021385{
21386 if (varp != NULL)
21387 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021388 switch (varp->v_type)
21389 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021390 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021391 func_unref(varp->vval.v_string);
21392 /*FALLTHROUGH*/
21393 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021394 vim_free(varp->vval.v_string);
21395 break;
21396 case VAR_LIST:
21397 list_unref(varp->vval.v_list);
21398 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021399 case VAR_DICT:
21400 dict_unref(varp->vval.v_dict);
21401 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021402 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021403#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021404 job_unref(varp->vval.v_job);
21405 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021406#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021407 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021408#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021409 channel_unref(varp->vval.v_channel);
21410 break;
21411#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021412 case VAR_NUMBER:
21413 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021414 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010021415 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021416 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021418 vim_free(varp);
21419 }
21420}
21421
21422/*
21423 * Free the memory for a variable value and set the value to NULL or 0.
21424 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021425 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021426clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021427{
21428 if (varp != NULL)
21429 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021430 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021431 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021432 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021433 func_unref(varp->vval.v_string);
21434 /*FALLTHROUGH*/
21435 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021436 vim_free(varp->vval.v_string);
21437 varp->vval.v_string = NULL;
21438 break;
21439 case VAR_LIST:
21440 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021441 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021442 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021443 case VAR_DICT:
21444 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021445 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021446 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021447 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021448 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021449 varp->vval.v_number = 0;
21450 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021451 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021452#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021453 varp->vval.v_float = 0.0;
21454 break;
21455#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021456 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021457#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021458 job_unref(varp->vval.v_job);
21459 varp->vval.v_job = NULL;
21460#endif
21461 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021462 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021463#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021464 channel_unref(varp->vval.v_channel);
21465 varp->vval.v_channel = NULL;
21466#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021467 case VAR_UNKNOWN:
21468 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021469 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021470 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021471 }
21472}
21473
21474/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021475 * Set the value of a variable to NULL without freeing items.
21476 */
21477 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021478init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021479{
21480 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021481 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021482}
21483
21484/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021485 * Get the number value of a variable.
21486 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021487 * For incompatible types, return 0.
21488 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21489 * caller of incompatible types: it sets *denote to TRUE if "denote"
21490 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021491 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010021492 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021493get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021494{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021495 int error = FALSE;
21496
21497 return get_tv_number_chk(varp, &error); /* return 0L on error */
21498}
21499
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021500 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021501get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021502{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021503 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021504
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021505 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021506 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021507 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021508 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021509 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021510#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000021511 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021512 break;
21513#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021514 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021515 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021516 break;
21517 case VAR_STRING:
21518 if (varp->vval.v_string != NULL)
21519 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021520 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021521 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021522 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021523 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021524 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021525 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021526 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021527 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010021528 case VAR_SPECIAL:
21529 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
21530 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021531 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021532#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021533 EMSG(_("E910: Using a Job as a Number"));
21534 break;
21535#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021536 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021537#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021538 EMSG(_("E913: Using a Channel as a Number"));
21539 break;
21540#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021541 case VAR_UNKNOWN:
21542 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021543 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021544 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021545 if (denote == NULL) /* useful for values that must be unsigned */
21546 n = -1;
21547 else
21548 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021549 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021550}
21551
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021552#ifdef FEAT_FLOAT
21553 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021554get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021555{
21556 switch (varp->v_type)
21557 {
21558 case VAR_NUMBER:
21559 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021560 case VAR_FLOAT:
21561 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021562 case VAR_FUNC:
21563 EMSG(_("E891: Using a Funcref as a Float"));
21564 break;
21565 case VAR_STRING:
21566 EMSG(_("E892: Using a String as a Float"));
21567 break;
21568 case VAR_LIST:
21569 EMSG(_("E893: Using a List as a Float"));
21570 break;
21571 case VAR_DICT:
21572 EMSG(_("E894: Using a Dictionary as a Float"));
21573 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021574 case VAR_SPECIAL:
21575 EMSG(_("E907: Using a special value as a Float"));
21576 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021577 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021578# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021579 EMSG(_("E911: Using a Job as a Float"));
21580 break;
21581# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021582 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021583# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021584 EMSG(_("E914: Using a Channel as a Float"));
21585 break;
21586# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021587 case VAR_UNKNOWN:
21588 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021589 break;
21590 }
21591 return 0;
21592}
21593#endif
21594
Bram Moolenaar071d4272004-06-13 20:20:40 +000021595/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021596 * Get the lnum from the first argument.
21597 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021598 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021599 */
21600 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021601get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021602{
Bram Moolenaar33570922005-01-25 22:26:29 +000021603 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021604 linenr_T lnum;
21605
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021606 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021607 if (lnum == 0) /* no valid number, try using line() */
21608 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021609 rettv.v_type = VAR_NUMBER;
21610 f_line(argvars, &rettv);
21611 lnum = rettv.vval.v_number;
21612 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021613 }
21614 return lnum;
21615}
21616
21617/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000021618 * Get the lnum from the first argument.
21619 * Also accepts "$", then "buf" is used.
21620 * Returns 0 on error.
21621 */
21622 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021623get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000021624{
21625 if (argvars[0].v_type == VAR_STRING
21626 && argvars[0].vval.v_string != NULL
21627 && argvars[0].vval.v_string[0] == '$'
21628 && buf != NULL)
21629 return buf->b_ml.ml_line_count;
21630 return get_tv_number_chk(&argvars[0], NULL);
21631}
21632
21633/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021634 * Get the string value of a variable.
21635 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000021636 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21637 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021638 * If the String variable has never been set, return an empty string.
21639 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021640 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
21641 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021642 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010021643 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021644get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021645{
21646 static char_u mybuf[NUMBUFLEN];
21647
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021648 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021649}
21650
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010021651 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021652get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021653{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021654 char_u *res = get_tv_string_buf_chk(varp, buf);
21655
21656 return res != NULL ? res : (char_u *)"";
21657}
21658
Bram Moolenaar7d647822014-04-05 21:28:56 +020021659/*
21660 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
21661 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021662 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021663get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021664{
21665 static char_u mybuf[NUMBUFLEN];
21666
21667 return get_tv_string_buf_chk(varp, mybuf);
21668}
21669
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021670 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021671get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021672{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021673 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021674 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021675 case VAR_NUMBER:
21676 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
21677 return buf;
21678 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021679 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021680 break;
21681 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021682 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000021683 break;
21684 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021685 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021686 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021687 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021688#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020021689 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021690 break;
21691#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021692 case VAR_STRING:
21693 if (varp->vval.v_string != NULL)
21694 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021695 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010021696 case VAR_SPECIAL:
21697 STRCPY(buf, get_var_special_name(varp->vval.v_number));
21698 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021699 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021700#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021701 {
21702 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010021703 char *status;
21704
21705 if (job == NULL)
21706 return (char_u *)"no process";
21707 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010021708 : job->jv_status == JOB_ENDED ? "dead"
21709 : "run";
21710# ifdef UNIX
21711 vim_snprintf((char *)buf, NUMBUFLEN,
21712 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021713# elif defined(WIN32)
21714 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010021715 "process %ld %s",
21716 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021717 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010021718# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010021719 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010021720 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
21721# endif
21722 return buf;
21723 }
21724#endif
21725 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021726 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021727#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021728 {
21729 channel_T *channel = varp->vval.v_channel;
21730 char *status = channel_status(channel);
21731
Bram Moolenaar5cefd402016-02-16 12:44:26 +010021732 if (channel == NULL)
21733 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
21734 else
21735 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010021736 "channel %d %s", channel->ch_id, status);
21737 return buf;
21738 }
21739#endif
21740 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021741 case VAR_UNKNOWN:
21742 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021743 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021744 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021745 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021746}
21747
21748/*
21749 * Find variable "name" in the list of variables.
21750 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021751 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000021752 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000021753 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021754 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021755 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021756find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021757{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021758 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021759 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021760
Bram Moolenaara7043832005-01-21 11:56:39 +000021761 ht = find_var_ht(name, &varname);
21762 if (htp != NULL)
21763 *htp = ht;
21764 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021765 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021766 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021767}
21768
21769/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020021770 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000021771 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021772 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021773 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021774find_var_in_ht(
21775 hashtab_T *ht,
21776 int htname,
21777 char_u *varname,
21778 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000021779{
Bram Moolenaar33570922005-01-25 22:26:29 +000021780 hashitem_T *hi;
21781
21782 if (*varname == NUL)
21783 {
21784 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020021785 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000021786 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021787 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021788 case 'g': return &globvars_var;
21789 case 'v': return &vimvars_var;
21790 case 'b': return &curbuf->b_bufvar;
21791 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021792#ifdef FEAT_WINDOWS
21793 case 't': return &curtab->tp_winvar;
21794#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021795 case 'l': return current_funccal == NULL
21796 ? NULL : &current_funccal->l_vars_var;
21797 case 'a': return current_funccal == NULL
21798 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021799 }
21800 return NULL;
21801 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021802
21803 hi = hash_find(ht, varname);
21804 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021805 {
21806 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021807 * worked find the variable again. Don't auto-load a script if it was
21808 * loaded already, otherwise it would be loaded every time when
21809 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021810 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021811 {
21812 /* Note: script_autoload() may make "hi" invalid. It must either
21813 * be obtained again or not used. */
21814 if (!script_autoload(varname, FALSE) || aborting())
21815 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021816 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010021817 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021818 if (HASHITEM_EMPTY(hi))
21819 return NULL;
21820 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021821 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021822}
21823
21824/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021825 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020021826 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000021827 * Set "varname" to the start of name without ':'.
21828 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021829 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021830find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021831{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021832 hashitem_T *hi;
21833
Bram Moolenaar73627d02015-08-11 15:46:09 +020021834 if (name[0] == NUL)
21835 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021836 if (name[1] != ':')
21837 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021838 /* The name must not start with a colon or #. */
21839 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021840 return NULL;
21841 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000021842
21843 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021844 hi = hash_find(&compat_hashtab, name);
21845 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021846 return &compat_hashtab;
21847
Bram Moolenaar071d4272004-06-13 20:20:40 +000021848 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021849 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021850 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021851 }
21852 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021853 if (*name == 'g') /* global variable */
21854 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021855 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21856 */
21857 if (vim_strchr(name + 2, ':') != NULL
21858 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021859 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021860 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021861 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021862 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021863 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021864#ifdef FEAT_WINDOWS
21865 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021866 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021867#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021868 if (*name == 'v') /* v: variable */
21869 return &vimvarht;
21870 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021871 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000021872 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021873 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021874 if (*name == 's' /* script variable */
21875 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21876 return &SCRIPT_VARS(current_SID);
21877 return NULL;
21878}
21879
21880/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021881 * Get function call environment based on bactrace debug level
21882 */
21883 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021884get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021885{
21886 int i;
21887 funccall_T *funccal;
21888 funccall_T *temp_funccal;
21889
21890 funccal = current_funccal;
21891 if (debug_backtrace_level > 0)
21892 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010021893 for (i = 0; i < debug_backtrace_level; i++)
21894 {
21895 temp_funccal = funccal->caller;
21896 if (temp_funccal)
21897 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021898 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010021899 /* backtrace level overflow. reset to max */
21900 debug_backtrace_level = i;
21901 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010021902 }
21903 return funccal;
21904}
21905
21906/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021907 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021908 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021909 * Returns NULL when it doesn't exist.
21910 */
21911 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021912get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021913{
Bram Moolenaar33570922005-01-25 22:26:29 +000021914 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021915
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021916 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021917 if (v == NULL)
21918 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021919 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021920}
21921
21922/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021923 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021924 * sourcing this script and when executing functions defined in the script.
21925 */
21926 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021927new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021928{
Bram Moolenaara7043832005-01-21 11:56:39 +000021929 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021930 hashtab_T *ht;
21931 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021932
Bram Moolenaar071d4272004-06-13 20:20:40 +000021933 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21934 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021935 /* Re-allocating ga_data means that an ht_array pointing to
21936 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021937 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021938 for (i = 1; i <= ga_scripts.ga_len; ++i)
21939 {
21940 ht = &SCRIPT_VARS(i);
21941 if (ht->ht_mask == HT_INIT_SIZE - 1)
21942 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021943 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021944 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021945 }
21946
Bram Moolenaar071d4272004-06-13 20:20:40 +000021947 while (ga_scripts.ga_len < id)
21948 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021949 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021950 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021951 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021952 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021953 }
21954 }
21955}
21956
21957/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021958 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21959 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021960 */
21961 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021962init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021963{
Bram Moolenaar33570922005-01-25 22:26:29 +000021964 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021965 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021966 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021967 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021968 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021969 dict_var->di_tv.vval.v_dict = dict;
21970 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021971 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021972 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21973 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021974}
21975
21976/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021977 * Unreference a dictionary initialized by init_var_dict().
21978 */
21979 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021980unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020021981{
21982 /* Now the dict needs to be freed if no one else is using it, go back to
21983 * normal reference counting. */
21984 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21985 dict_unref(dict);
21986}
21987
21988/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021989 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021990 * Frees all allocated variables and the value they contain.
21991 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021992 */
21993 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021994vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021995{
21996 vars_clear_ext(ht, TRUE);
21997}
21998
21999/*
22000 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22001 */
22002 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022003vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022004{
Bram Moolenaara7043832005-01-21 11:56:39 +000022005 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022006 hashitem_T *hi;
22007 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022008
Bram Moolenaar33570922005-01-25 22:26:29 +000022009 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022010 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022011 for (hi = ht->ht_array; todo > 0; ++hi)
22012 {
22013 if (!HASHITEM_EMPTY(hi))
22014 {
22015 --todo;
22016
Bram Moolenaar33570922005-01-25 22:26:29 +000022017 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022018 * ht_array might change then. hash_clear() takes care of it
22019 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022020 v = HI2DI(hi);
22021 if (free_val)
22022 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022023 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022024 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022025 }
22026 }
22027 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022028 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022029}
22030
Bram Moolenaara7043832005-01-21 11:56:39 +000022031/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022032 * Delete a variable from hashtab "ht" at item "hi".
22033 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022034 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022035 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022036delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022037{
Bram Moolenaar33570922005-01-25 22:26:29 +000022038 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022039
22040 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022041 clear_tv(&di->di_tv);
22042 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022043}
22044
22045/*
22046 * List the value of one internal variable.
22047 */
22048 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022049list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022050{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022051 char_u *tofree;
22052 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022053 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022054
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022055 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022056 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022057 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022058 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022059}
22060
Bram Moolenaar071d4272004-06-13 20:20:40 +000022061 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022062list_one_var_a(
22063 char_u *prefix,
22064 char_u *name,
22065 int type,
22066 char_u *string,
22067 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022068{
Bram Moolenaar31859182007-08-14 20:41:13 +000022069 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22070 msg_start();
22071 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022072 if (name != NULL) /* "a:" vars don't have a name stored */
22073 msg_puts(name);
22074 msg_putchar(' ');
22075 msg_advance(22);
22076 if (type == VAR_NUMBER)
22077 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022078 else if (type == VAR_FUNC)
22079 msg_putchar('*');
22080 else if (type == VAR_LIST)
22081 {
22082 msg_putchar('[');
22083 if (*string == '[')
22084 ++string;
22085 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022086 else if (type == VAR_DICT)
22087 {
22088 msg_putchar('{');
22089 if (*string == '{')
22090 ++string;
22091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022092 else
22093 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022094
Bram Moolenaar071d4272004-06-13 20:20:40 +000022095 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022096
22097 if (type == VAR_FUNC)
22098 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022099 if (*first)
22100 {
22101 msg_clr_eos();
22102 *first = FALSE;
22103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022104}
22105
22106/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022107 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022108 * If the variable already exists, the value is updated.
22109 * Otherwise the variable is created.
22110 */
22111 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022112set_var(
22113 char_u *name,
22114 typval_T *tv,
22115 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022116{
Bram Moolenaar33570922005-01-25 22:26:29 +000022117 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022118 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022119 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022120
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022121 ht = find_var_ht(name, &varname);
22122 if (ht == NULL || *varname == NUL)
22123 {
22124 EMSG2(_(e_illvar), name);
22125 return;
22126 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022127 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022128
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022129 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
22130 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022131
Bram Moolenaar33570922005-01-25 22:26:29 +000022132 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022133 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022134 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022135 if (var_check_ro(v->di_flags, name, FALSE)
22136 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022137 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022138
22139 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022140 * Handle setting internal v: variables separately where needed to
22141 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022142 */
22143 if (ht == &vimvarht)
22144 {
22145 if (v->di_tv.v_type == VAR_STRING)
22146 {
22147 vim_free(v->di_tv.vval.v_string);
22148 if (copy || tv->v_type != VAR_STRING)
22149 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22150 else
22151 {
22152 /* Take over the string to avoid an extra alloc/free. */
22153 v->di_tv.vval.v_string = tv->vval.v_string;
22154 tv->vval.v_string = NULL;
22155 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022156 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022157 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022158 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022159 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022160 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022161 if (STRCMP(varname, "searchforward") == 0)
22162 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022163#ifdef FEAT_SEARCH_EXTRA
22164 else if (STRCMP(varname, "hlsearch") == 0)
22165 {
22166 no_hlsearch = !v->di_tv.vval.v_number;
22167 redraw_all_later(SOME_VALID);
22168 }
22169#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022170 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022171 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022172 else if (v->di_tv.v_type != tv->v_type)
22173 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022174 }
22175
22176 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022177 }
22178 else /* add a new variable */
22179 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022180 /* Can't add "v:" variable. */
22181 if (ht == &vimvarht)
22182 {
22183 EMSG2(_(e_illvar), name);
22184 return;
22185 }
22186
Bram Moolenaar92124a32005-06-17 22:03:40 +000022187 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022188 if (!valid_varname(varname))
22189 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022190
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022191 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22192 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022193 if (v == NULL)
22194 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022195 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022196 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022197 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022198 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022199 return;
22200 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022201 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022202 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022203
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022204 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022205 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022206 else
22207 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022208 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022209 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022210 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022212}
22213
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022214/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022215 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022216 * Also give an error message.
22217 */
22218 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022219var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022220{
22221 if (flags & DI_FLAGS_RO)
22222 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022223 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022224 return TRUE;
22225 }
22226 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22227 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022228 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022229 return TRUE;
22230 }
22231 return FALSE;
22232}
22233
22234/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022235 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22236 * Also give an error message.
22237 */
22238 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022239var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022240{
22241 if (flags & DI_FLAGS_FIX)
22242 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022243 EMSG2(_("E795: Cannot delete variable %s"),
22244 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022245 return TRUE;
22246 }
22247 return FALSE;
22248}
22249
22250/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022251 * Check if a funcref is assigned to a valid variable name.
22252 * Return TRUE and give an error if not.
22253 */
22254 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022255var_check_func_name(
22256 char_u *name, /* points to start of variable name */
22257 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022258{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022259 /* Allow for w: b: s: and t:. */
22260 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022261 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22262 ? name[2] : name[0]))
22263 {
22264 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22265 name);
22266 return TRUE;
22267 }
22268 /* Don't allow hiding a function. When "v" is not NULL we might be
22269 * assigning another function to the same var, the type is checked
22270 * below. */
22271 if (new_var && function_exists(name))
22272 {
22273 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22274 name);
22275 return TRUE;
22276 }
22277 return FALSE;
22278}
22279
22280/*
22281 * Check if a variable name is valid.
22282 * Return FALSE and give an error if not.
22283 */
22284 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022285valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022286{
22287 char_u *p;
22288
22289 for (p = varname; *p != NUL; ++p)
22290 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22291 && *p != AUTOLOAD_CHAR)
22292 {
22293 EMSG2(_(e_illvar), varname);
22294 return FALSE;
22295 }
22296 return TRUE;
22297}
22298
22299/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022300 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022301 * Also give an error message, using "name" or _("name") when use_gettext is
22302 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022303 */
22304 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022305tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022306{
22307 if (lock & VAR_LOCKED)
22308 {
22309 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022310 name == NULL ? (char_u *)_("Unknown")
22311 : use_gettext ? (char_u *)_(name)
22312 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022313 return TRUE;
22314 }
22315 if (lock & VAR_FIXED)
22316 {
22317 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022318 name == NULL ? (char_u *)_("Unknown")
22319 : use_gettext ? (char_u *)_(name)
22320 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022321 return TRUE;
22322 }
22323 return FALSE;
22324}
22325
22326/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022327 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022328 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022329 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022330 * It is OK for "from" and "to" to point to the same item. This is used to
22331 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022332 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022333 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022334copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022335{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022336 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022337 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022338 switch (from->v_type)
22339 {
22340 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022341 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022342 to->vval.v_number = from->vval.v_number;
22343 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022344 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022345#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022346 to->vval.v_float = from->vval.v_float;
22347 break;
22348#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022349 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022350#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022351 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010022352 if (to->vval.v_job != NULL)
22353 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022354 break;
22355#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022356 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022357#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022358 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022359 if (to->vval.v_channel != NULL)
22360 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010022361 break;
22362#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022363 case VAR_STRING:
22364 case VAR_FUNC:
22365 if (from->vval.v_string == NULL)
22366 to->vval.v_string = NULL;
22367 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022368 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022369 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022370 if (from->v_type == VAR_FUNC)
22371 func_ref(to->vval.v_string);
22372 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022373 break;
22374 case VAR_LIST:
22375 if (from->vval.v_list == NULL)
22376 to->vval.v_list = NULL;
22377 else
22378 {
22379 to->vval.v_list = from->vval.v_list;
22380 ++to->vval.v_list->lv_refcount;
22381 }
22382 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022383 case VAR_DICT:
22384 if (from->vval.v_dict == NULL)
22385 to->vval.v_dict = NULL;
22386 else
22387 {
22388 to->vval.v_dict = from->vval.v_dict;
22389 ++to->vval.v_dict->dv_refcount;
22390 }
22391 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022392 case VAR_UNKNOWN:
22393 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022394 break;
22395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022396}
22397
22398/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022399 * Make a copy of an item.
22400 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022401 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22402 * reference to an already copied list/dict can be used.
22403 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022404 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022405 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022406item_copy(
22407 typval_T *from,
22408 typval_T *to,
22409 int deep,
22410 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022411{
22412 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022413 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022414
Bram Moolenaar33570922005-01-25 22:26:29 +000022415 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022416 {
22417 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022418 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022419 }
22420 ++recurse;
22421
22422 switch (from->v_type)
22423 {
22424 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022425 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022426 case VAR_STRING:
22427 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010022428 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022429 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010022430 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022431 copy_tv(from, to);
22432 break;
22433 case VAR_LIST:
22434 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022435 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022436 if (from->vval.v_list == NULL)
22437 to->vval.v_list = NULL;
22438 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22439 {
22440 /* use the copy made earlier */
22441 to->vval.v_list = from->vval.v_list->lv_copylist;
22442 ++to->vval.v_list->lv_refcount;
22443 }
22444 else
22445 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22446 if (to->vval.v_list == NULL)
22447 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022448 break;
22449 case VAR_DICT:
22450 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022451 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022452 if (from->vval.v_dict == NULL)
22453 to->vval.v_dict = NULL;
22454 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22455 {
22456 /* use the copy made earlier */
22457 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22458 ++to->vval.v_dict->dv_refcount;
22459 }
22460 else
22461 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22462 if (to->vval.v_dict == NULL)
22463 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022464 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022465 case VAR_UNKNOWN:
22466 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022467 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022468 }
22469 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022470 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022471}
22472
22473/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022474 * ":echo expr1 ..." print each argument separated with a space, add a
22475 * newline at the end.
22476 * ":echon expr1 ..." print each argument plain.
22477 */
22478 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022479ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022480{
22481 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022482 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022483 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022484 char_u *p;
22485 int needclr = TRUE;
22486 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022487 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022488
22489 if (eap->skip)
22490 ++emsg_skip;
22491 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22492 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022493 /* If eval1() causes an error message the text from the command may
22494 * still need to be cleared. E.g., "echo 22,44". */
22495 need_clr_eos = needclr;
22496
Bram Moolenaar071d4272004-06-13 20:20:40 +000022497 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022498 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022499 {
22500 /*
22501 * Report the invalid expression unless the expression evaluation
22502 * has been cancelled due to an aborting error, an interrupt, or an
22503 * exception.
22504 */
22505 if (!aborting())
22506 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022507 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022508 break;
22509 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022510 need_clr_eos = FALSE;
22511
Bram Moolenaar071d4272004-06-13 20:20:40 +000022512 if (!eap->skip)
22513 {
22514 if (atstart)
22515 {
22516 atstart = FALSE;
22517 /* Call msg_start() after eval1(), evaluating the expression
22518 * may cause a message to appear. */
22519 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022520 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022521 /* Mark the saved text as finishing the line, so that what
22522 * follows is displayed on a new line when scrolling back
22523 * at the more prompt. */
22524 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022525 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022527 }
22528 else if (eap->cmdidx == CMD_echo)
22529 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022530 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022531 if (p != NULL)
22532 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022533 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022534 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022535 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022536 if (*p != TAB && needclr)
22537 {
22538 /* remove any text still there from the command */
22539 msg_clr_eos();
22540 needclr = FALSE;
22541 }
22542 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022543 }
22544 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022545 {
22546#ifdef FEAT_MBYTE
22547 if (has_mbyte)
22548 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022549 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022550
22551 (void)msg_outtrans_len_attr(p, i, echo_attr);
22552 p += i - 1;
22553 }
22554 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022555#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022556 (void)msg_outtrans_len_attr(p, 1, echo_attr);
22557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022558 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022559 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022560 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022561 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022562 arg = skipwhite(arg);
22563 }
22564 eap->nextcmd = check_nextcmd(arg);
22565
22566 if (eap->skip)
22567 --emsg_skip;
22568 else
22569 {
22570 /* remove text that may still be there from the command */
22571 if (needclr)
22572 msg_clr_eos();
22573 if (eap->cmdidx == CMD_echo)
22574 msg_end();
22575 }
22576}
22577
22578/*
22579 * ":echohl {name}".
22580 */
22581 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022582ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022583{
22584 int id;
22585
22586 id = syn_name2id(eap->arg);
22587 if (id == 0)
22588 echo_attr = 0;
22589 else
22590 echo_attr = syn_id2attr(id);
22591}
22592
22593/*
22594 * ":execute expr1 ..." execute the result of an expression.
22595 * ":echomsg expr1 ..." Print a message
22596 * ":echoerr expr1 ..." Print an error
22597 * Each gets spaces around each argument and a newline at the end for
22598 * echo commands
22599 */
22600 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022601ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022602{
22603 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022604 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022605 int ret = OK;
22606 char_u *p;
22607 garray_T ga;
22608 int len;
22609 int save_did_emsg;
22610
22611 ga_init2(&ga, 1, 80);
22612
22613 if (eap->skip)
22614 ++emsg_skip;
22615 while (*arg != NUL && *arg != '|' && *arg != '\n')
22616 {
22617 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022618 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022619 {
22620 /*
22621 * Report the invalid expression unless the expression evaluation
22622 * has been cancelled due to an aborting error, an interrupt, or an
22623 * exception.
22624 */
22625 if (!aborting())
22626 EMSG2(_(e_invexpr2), p);
22627 ret = FAIL;
22628 break;
22629 }
22630
22631 if (!eap->skip)
22632 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022633 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022634 len = (int)STRLEN(p);
22635 if (ga_grow(&ga, len + 2) == FAIL)
22636 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022637 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022638 ret = FAIL;
22639 break;
22640 }
22641 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022642 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000022643 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022644 ga.ga_len += len;
22645 }
22646
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022647 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022648 arg = skipwhite(arg);
22649 }
22650
22651 if (ret != FAIL && ga.ga_data != NULL)
22652 {
22653 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000022654 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022655 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000022656 out_flush();
22657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022658 else if (eap->cmdidx == CMD_echoerr)
22659 {
22660 /* We don't want to abort following commands, restore did_emsg. */
22661 save_did_emsg = did_emsg;
22662 EMSG((char_u *)ga.ga_data);
22663 if (!force_abort)
22664 did_emsg = save_did_emsg;
22665 }
22666 else if (eap->cmdidx == CMD_execute)
22667 do_cmdline((char_u *)ga.ga_data,
22668 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
22669 }
22670
22671 ga_clear(&ga);
22672
22673 if (eap->skip)
22674 --emsg_skip;
22675
22676 eap->nextcmd = check_nextcmd(arg);
22677}
22678
22679/*
22680 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
22681 * "arg" points to the "&" or '+' when called, to "option" when returning.
22682 * Returns NULL when no option name found. Otherwise pointer to the char
22683 * after the option name.
22684 */
22685 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022686find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022687{
22688 char_u *p = *arg;
22689
22690 ++p;
22691 if (*p == 'g' && p[1] == ':')
22692 {
22693 *opt_flags = OPT_GLOBAL;
22694 p += 2;
22695 }
22696 else if (*p == 'l' && p[1] == ':')
22697 {
22698 *opt_flags = OPT_LOCAL;
22699 p += 2;
22700 }
22701 else
22702 *opt_flags = 0;
22703
22704 if (!ASCII_ISALPHA(*p))
22705 return NULL;
22706 *arg = p;
22707
22708 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
22709 p += 4; /* termcap option */
22710 else
22711 while (ASCII_ISALPHA(*p))
22712 ++p;
22713 return p;
22714}
22715
22716/*
22717 * ":function"
22718 */
22719 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022720ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022721{
22722 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022723 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022724 int j;
22725 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022726 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022727 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022728 char_u *name = NULL;
22729 char_u *p;
22730 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022731 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022732 garray_T newargs;
22733 garray_T newlines;
22734 int varargs = FALSE;
22735 int mustend = FALSE;
22736 int flags = 0;
22737 ufunc_T *fp;
22738 int indent;
22739 int nesting;
22740 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022741 dictitem_T *v;
22742 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022743 static int func_nr = 0; /* number for nameless function */
22744 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022745 hashtab_T *ht;
22746 int todo;
22747 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022748 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022749
22750 /*
22751 * ":function" without argument: list functions.
22752 */
22753 if (ends_excmd(*eap->arg))
22754 {
22755 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022756 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022757 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000022758 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022759 {
22760 if (!HASHITEM_EMPTY(hi))
22761 {
22762 --todo;
22763 fp = HI2UF(hi);
22764 if (!isdigit(*fp->uf_name))
22765 list_func_head(fp, FALSE);
22766 }
22767 }
22768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022769 eap->nextcmd = check_nextcmd(eap->arg);
22770 return;
22771 }
22772
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022773 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022774 * ":function /pat": list functions matching pattern.
22775 */
22776 if (*eap->arg == '/')
22777 {
22778 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
22779 if (!eap->skip)
22780 {
22781 regmatch_T regmatch;
22782
22783 c = *p;
22784 *p = NUL;
22785 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
22786 *p = c;
22787 if (regmatch.regprog != NULL)
22788 {
22789 regmatch.rm_ic = p_ic;
22790
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022791 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022792 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
22793 {
22794 if (!HASHITEM_EMPTY(hi))
22795 {
22796 --todo;
22797 fp = HI2UF(hi);
22798 if (!isdigit(*fp->uf_name)
22799 && vim_regexec(&regmatch, fp->uf_name, 0))
22800 list_func_head(fp, FALSE);
22801 }
22802 }
Bram Moolenaar473de612013-06-08 18:19:48 +020022803 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000022804 }
22805 }
22806 if (*p == '/')
22807 ++p;
22808 eap->nextcmd = check_nextcmd(p);
22809 return;
22810 }
22811
22812 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022813 * Get the function name. There are these situations:
22814 * func normal function name
22815 * "name" == func, "fudi.fd_dict" == NULL
22816 * dict.func new dictionary entry
22817 * "name" == NULL, "fudi.fd_dict" set,
22818 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
22819 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022820 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022821 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
22822 * dict.func existing dict entry that's not a Funcref
22823 * "name" == NULL, "fudi.fd_dict" set,
22824 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022825 * s:func script-local function name
22826 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022827 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022828 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022829 name = trans_function_name(&p, eap->skip, 0, &fudi);
22830 paren = (vim_strchr(p, '(') != NULL);
22831 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022832 {
22833 /*
22834 * Return on an invalid expression in braces, unless the expression
22835 * evaluation has been cancelled due to an aborting error, an
22836 * interrupt, or an exception.
22837 */
22838 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022839 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022840 if (!eap->skip && fudi.fd_newkey != NULL)
22841 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022842 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022843 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022845 else
22846 eap->skip = TRUE;
22847 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022848
Bram Moolenaar071d4272004-06-13 20:20:40 +000022849 /* An error in a function call during evaluation of an expression in magic
22850 * braces should not cause the function not to be defined. */
22851 saved_did_emsg = did_emsg;
22852 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022853
22854 /*
22855 * ":function func" with only function name: list function.
22856 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022857 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022858 {
22859 if (!ends_excmd(*skipwhite(p)))
22860 {
22861 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022862 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022863 }
22864 eap->nextcmd = check_nextcmd(p);
22865 if (eap->nextcmd != NULL)
22866 *p = NUL;
22867 if (!eap->skip && !got_int)
22868 {
22869 fp = find_func(name);
22870 if (fp != NULL)
22871 {
22872 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022873 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022874 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022875 if (FUNCLINE(fp, j) == NULL)
22876 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022877 msg_putchar('\n');
22878 msg_outnum((long)(j + 1));
22879 if (j < 9)
22880 msg_putchar(' ');
22881 if (j < 99)
22882 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022883 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022884 out_flush(); /* show a line at a time */
22885 ui_breakcheck();
22886 }
22887 if (!got_int)
22888 {
22889 msg_putchar('\n');
22890 msg_puts((char_u *)" endfunction");
22891 }
22892 }
22893 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022894 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022895 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022896 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022897 }
22898
22899 /*
22900 * ":function name(arg1, arg2)" Define function.
22901 */
22902 p = skipwhite(p);
22903 if (*p != '(')
22904 {
22905 if (!eap->skip)
22906 {
22907 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022908 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022909 }
22910 /* attempt to continue by skipping some text */
22911 if (vim_strchr(p, '(') != NULL)
22912 p = vim_strchr(p, '(');
22913 }
22914 p = skipwhite(p + 1);
22915
22916 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22917 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22918
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022919 if (!eap->skip)
22920 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022921 /* Check the name of the function. Unless it's a dictionary function
22922 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022923 if (name != NULL)
22924 arg = name;
22925 else
22926 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022927 if (arg != NULL && (fudi.fd_di == NULL
22928 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022929 {
22930 if (*arg == K_SPECIAL)
22931 j = 3;
22932 else
22933 j = 0;
22934 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22935 : eval_isnamec(arg[j])))
22936 ++j;
22937 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022938 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022939 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022940 /* Disallow using the g: dict. */
22941 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22942 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022943 }
22944
Bram Moolenaar071d4272004-06-13 20:20:40 +000022945 /*
22946 * Isolate the arguments: "arg1, arg2, ...)"
22947 */
22948 while (*p != ')')
22949 {
22950 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22951 {
22952 varargs = TRUE;
22953 p += 3;
22954 mustend = TRUE;
22955 }
22956 else
22957 {
22958 arg = p;
22959 while (ASCII_ISALNUM(*p) || *p == '_')
22960 ++p;
22961 if (arg == p || isdigit(*arg)
22962 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22963 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22964 {
22965 if (!eap->skip)
22966 EMSG2(_("E125: Illegal argument: %s"), arg);
22967 break;
22968 }
22969 if (ga_grow(&newargs, 1) == FAIL)
22970 goto erret;
22971 c = *p;
22972 *p = NUL;
22973 arg = vim_strsave(arg);
22974 if (arg == NULL)
22975 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022976
22977 /* Check for duplicate argument name. */
22978 for (i = 0; i < newargs.ga_len; ++i)
22979 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22980 {
22981 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022982 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022983 goto erret;
22984 }
22985
Bram Moolenaar071d4272004-06-13 20:20:40 +000022986 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22987 *p = c;
22988 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022989 if (*p == ',')
22990 ++p;
22991 else
22992 mustend = TRUE;
22993 }
22994 p = skipwhite(p);
22995 if (mustend && *p != ')')
22996 {
22997 if (!eap->skip)
22998 EMSG2(_(e_invarg2), eap->arg);
22999 break;
23000 }
23001 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023002 if (*p != ')')
23003 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023004 ++p; /* skip the ')' */
23005
Bram Moolenaare9a41262005-01-15 22:18:47 +000023006 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023007 for (;;)
23008 {
23009 p = skipwhite(p);
23010 if (STRNCMP(p, "range", 5) == 0)
23011 {
23012 flags |= FC_RANGE;
23013 p += 5;
23014 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023015 else if (STRNCMP(p, "dict", 4) == 0)
23016 {
23017 flags |= FC_DICT;
23018 p += 4;
23019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023020 else if (STRNCMP(p, "abort", 5) == 0)
23021 {
23022 flags |= FC_ABORT;
23023 p += 5;
23024 }
23025 else
23026 break;
23027 }
23028
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023029 /* When there is a line break use what follows for the function body.
23030 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23031 if (*p == '\n')
23032 line_arg = p + 1;
23033 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023034 EMSG(_(e_trailing));
23035
23036 /*
23037 * Read the body of the function, until ":endfunction" is found.
23038 */
23039 if (KeyTyped)
23040 {
23041 /* Check if the function already exists, don't let the user type the
23042 * whole function before telling him it doesn't work! For a script we
23043 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023044 if (!eap->skip && !eap->forceit)
23045 {
23046 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23047 EMSG(_(e_funcdict));
23048 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023049 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023051
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023052 if (!eap->skip && did_emsg)
23053 goto erret;
23054
Bram Moolenaar071d4272004-06-13 20:20:40 +000023055 msg_putchar('\n'); /* don't overwrite the function name */
23056 cmdline_row = msg_row;
23057 }
23058
23059 indent = 2;
23060 nesting = 0;
23061 for (;;)
23062 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023063 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023064 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023065 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023066 saved_wait_return = FALSE;
23067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023068 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023069 sourcing_lnum_off = sourcing_lnum;
23070
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023071 if (line_arg != NULL)
23072 {
23073 /* Use eap->arg, split up in parts by line breaks. */
23074 theline = line_arg;
23075 p = vim_strchr(theline, '\n');
23076 if (p == NULL)
23077 line_arg += STRLEN(line_arg);
23078 else
23079 {
23080 *p = NUL;
23081 line_arg = p + 1;
23082 }
23083 }
23084 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023085 theline = getcmdline(':', 0L, indent);
23086 else
23087 theline = eap->getline(':', eap->cookie, indent);
23088 if (KeyTyped)
23089 lines_left = Rows - 1;
23090 if (theline == NULL)
23091 {
23092 EMSG(_("E126: Missing :endfunction"));
23093 goto erret;
23094 }
23095
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023096 /* Detect line continuation: sourcing_lnum increased more than one. */
23097 if (sourcing_lnum > sourcing_lnum_off + 1)
23098 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23099 else
23100 sourcing_lnum_off = 0;
23101
Bram Moolenaar071d4272004-06-13 20:20:40 +000023102 if (skip_until != NULL)
23103 {
23104 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23105 * don't check for ":endfunc". */
23106 if (STRCMP(theline, skip_until) == 0)
23107 {
23108 vim_free(skip_until);
23109 skip_until = NULL;
23110 }
23111 }
23112 else
23113 {
23114 /* skip ':' and blanks*/
23115 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23116 ;
23117
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023118 /* Check for "endfunction". */
23119 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023120 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023121 if (line_arg == NULL)
23122 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023123 break;
23124 }
23125
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023126 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023127 * at "end". */
23128 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23129 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023130 else if (STRNCMP(p, "if", 2) == 0
23131 || STRNCMP(p, "wh", 2) == 0
23132 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023133 || STRNCMP(p, "try", 3) == 0)
23134 indent += 2;
23135
23136 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023137 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023138 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023139 if (*p == '!')
23140 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023141 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023142 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23143 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023144 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023145 ++nesting;
23146 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023147 }
23148 }
23149
23150 /* Check for ":append" or ":insert". */
23151 p = skip_range(p, NULL);
23152 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23153 || (p[0] == 'i'
23154 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23155 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23156 skip_until = vim_strsave((char_u *)".");
23157
23158 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23159 arg = skipwhite(skiptowhite(p));
23160 if (arg[0] == '<' && arg[1] =='<'
23161 && ((p[0] == 'p' && p[1] == 'y'
23162 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23163 || (p[0] == 'p' && p[1] == 'e'
23164 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23165 || (p[0] == 't' && p[1] == 'c'
23166 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023167 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23168 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023169 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23170 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023171 || (p[0] == 'm' && p[1] == 'z'
23172 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023173 ))
23174 {
23175 /* ":python <<" continues until a dot, like ":append" */
23176 p = skipwhite(arg + 2);
23177 if (*p == NUL)
23178 skip_until = vim_strsave((char_u *)".");
23179 else
23180 skip_until = vim_strsave(p);
23181 }
23182 }
23183
23184 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023185 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023186 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023187 if (line_arg == NULL)
23188 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023189 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023190 }
23191
23192 /* Copy the line to newly allocated memory. get_one_sourceline()
23193 * allocates 250 bytes per line, this saves 80% on average. The cost
23194 * is an extra alloc/free. */
23195 p = vim_strsave(theline);
23196 if (p != NULL)
23197 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023198 if (line_arg == NULL)
23199 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023200 theline = p;
23201 }
23202
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023203 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23204
23205 /* Add NULL lines for continuation lines, so that the line count is
23206 * equal to the index in the growarray. */
23207 while (sourcing_lnum_off-- > 0)
23208 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023209
23210 /* Check for end of eap->arg. */
23211 if (line_arg != NULL && *line_arg == NUL)
23212 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023213 }
23214
23215 /* Don't define the function when skipping commands or when an error was
23216 * detected. */
23217 if (eap->skip || did_emsg)
23218 goto erret;
23219
23220 /*
23221 * If there are no errors, add the function
23222 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023223 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023224 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023225 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023226 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023227 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023228 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023229 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023230 goto erret;
23231 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023232
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023233 fp = find_func(name);
23234 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023235 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023236 if (!eap->forceit)
23237 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023238 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023239 goto erret;
23240 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023241 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023242 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023243 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023244 name);
23245 goto erret;
23246 }
23247 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023248 ga_clear_strings(&(fp->uf_args));
23249 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023250 vim_free(name);
23251 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023253 }
23254 else
23255 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023256 char numbuf[20];
23257
23258 fp = NULL;
23259 if (fudi.fd_newkey == NULL && !eap->forceit)
23260 {
23261 EMSG(_(e_funcdict));
23262 goto erret;
23263 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023264 if (fudi.fd_di == NULL)
23265 {
23266 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023267 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023268 goto erret;
23269 }
23270 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023271 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023272 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023273
23274 /* Give the function a sequential number. Can only be used with a
23275 * Funcref! */
23276 vim_free(name);
23277 sprintf(numbuf, "%d", ++func_nr);
23278 name = vim_strsave((char_u *)numbuf);
23279 if (name == NULL)
23280 goto erret;
23281 }
23282
23283 if (fp == NULL)
23284 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023285 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023286 {
23287 int slen, plen;
23288 char_u *scriptname;
23289
23290 /* Check that the autoload name matches the script name. */
23291 j = FAIL;
23292 if (sourcing_name != NULL)
23293 {
23294 scriptname = autoload_name(name);
23295 if (scriptname != NULL)
23296 {
23297 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023298 plen = (int)STRLEN(p);
23299 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023300 if (slen > plen && fnamecmp(p,
23301 sourcing_name + slen - plen) == 0)
23302 j = OK;
23303 vim_free(scriptname);
23304 }
23305 }
23306 if (j == FAIL)
23307 {
23308 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23309 goto erret;
23310 }
23311 }
23312
23313 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023314 if (fp == NULL)
23315 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023316
23317 if (fudi.fd_dict != NULL)
23318 {
23319 if (fudi.fd_di == NULL)
23320 {
23321 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023322 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023323 if (fudi.fd_di == NULL)
23324 {
23325 vim_free(fp);
23326 goto erret;
23327 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023328 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23329 {
23330 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023331 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023332 goto erret;
23333 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023334 }
23335 else
23336 /* overwrite existing dict entry */
23337 clear_tv(&fudi.fd_di->di_tv);
23338 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023339 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023340 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023341 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023342
23343 /* behave like "dict" was used */
23344 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023345 }
23346
Bram Moolenaar071d4272004-06-13 20:20:40 +000023347 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023348 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023349 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23350 {
23351 vim_free(fp);
23352 goto erret;
23353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023354 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023355 fp->uf_args = newargs;
23356 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023357#ifdef FEAT_PROFILE
23358 fp->uf_tml_count = NULL;
23359 fp->uf_tml_total = NULL;
23360 fp->uf_tml_self = NULL;
23361 fp->uf_profiling = FALSE;
23362 if (prof_def_func())
23363 func_do_profile(fp);
23364#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023365 fp->uf_varargs = varargs;
23366 fp->uf_flags = flags;
23367 fp->uf_calls = 0;
23368 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023369 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023370
23371erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023372 ga_clear_strings(&newargs);
23373 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023374ret_free:
23375 vim_free(skip_until);
23376 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023377 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023378 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023379 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023380}
23381
23382/*
23383 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023384 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023385 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023386 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010023387 * TFN_INT: internal function name OK
23388 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023389 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023390 * Advances "pp" to just after the function name (if no error).
23391 */
23392 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023393trans_function_name(
23394 char_u **pp,
23395 int skip, /* only find the end, don't evaluate */
23396 int flags,
23397 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023398{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023399 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023400 char_u *start;
23401 char_u *end;
23402 int lead;
23403 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023404 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023405 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023406
23407 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023408 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023409 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023410
23411 /* Check for hard coded <SNR>: already translated function ID (from a user
23412 * command). */
23413 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23414 && (*pp)[2] == (int)KE_SNR)
23415 {
23416 *pp += 3;
23417 len = get_id_len(pp) + 3;
23418 return vim_strnsave(start, len);
23419 }
23420
23421 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23422 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023423 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023424 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023425 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023426
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023427 /* Note that TFN_ flags use the same values as GLV_ flags. */
23428 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023429 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023430 if (end == start)
23431 {
23432 if (!skip)
23433 EMSG(_("E129: Function name required"));
23434 goto theend;
23435 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023436 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023437 {
23438 /*
23439 * Report an invalid expression in braces, unless the expression
23440 * evaluation has been cancelled due to an aborting error, an
23441 * interrupt, or an exception.
23442 */
23443 if (!aborting())
23444 {
23445 if (end != NULL)
23446 EMSG2(_(e_invarg2), start);
23447 }
23448 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023449 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023450 goto theend;
23451 }
23452
23453 if (lv.ll_tv != NULL)
23454 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023455 if (fdp != NULL)
23456 {
23457 fdp->fd_dict = lv.ll_dict;
23458 fdp->fd_newkey = lv.ll_newkey;
23459 lv.ll_newkey = NULL;
23460 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023461 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023462 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23463 {
23464 name = vim_strsave(lv.ll_tv->vval.v_string);
23465 *pp = end;
23466 }
23467 else
23468 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023469 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23470 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023471 EMSG(_(e_funcref));
23472 else
23473 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023474 name = NULL;
23475 }
23476 goto theend;
23477 }
23478
23479 if (lv.ll_name == NULL)
23480 {
23481 /* Error found, but continue after the function name. */
23482 *pp = end;
23483 goto theend;
23484 }
23485
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023486 /* Check if the name is a Funcref. If so, use the value. */
23487 if (lv.ll_exp_name != NULL)
23488 {
23489 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023490 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023491 if (name == lv.ll_exp_name)
23492 name = NULL;
23493 }
23494 else
23495 {
23496 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010023497 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023498 if (name == *pp)
23499 name = NULL;
23500 }
23501 if (name != NULL)
23502 {
23503 name = vim_strsave(name);
23504 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023505 if (STRNCMP(name, "<SNR>", 5) == 0)
23506 {
23507 /* Change "<SNR>" to the byte sequence. */
23508 name[0] = K_SPECIAL;
23509 name[1] = KS_EXTRA;
23510 name[2] = (int)KE_SNR;
23511 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23512 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023513 goto theend;
23514 }
23515
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023516 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023517 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023518 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023519 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23520 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23521 {
23522 /* When there was "s:" already or the name expanded to get a
23523 * leading "s:" then remove it. */
23524 lv.ll_name += 2;
23525 len -= 2;
23526 lead = 2;
23527 }
23528 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023529 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023530 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023531 /* skip over "s:" and "g:" */
23532 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023533 lv.ll_name += 2;
23534 len = (int)(end - lv.ll_name);
23535 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023536
23537 /*
23538 * Copy the function name to allocated memory.
23539 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23540 * Accept <SNR>123_name() outside a script.
23541 */
23542 if (skip)
23543 lead = 0; /* do nothing */
23544 else if (lead > 0)
23545 {
23546 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000023547 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
23548 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023549 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000023550 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023551 if (current_SID <= 0)
23552 {
23553 EMSG(_(e_usingsid));
23554 goto theend;
23555 }
23556 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
23557 lead += (int)STRLEN(sid_buf);
23558 }
23559 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023560 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023561 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023562 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023563 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023564 goto theend;
23565 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023566 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023567 {
23568 char_u *cp = vim_strchr(lv.ll_name, ':');
23569
23570 if (cp != NULL && cp < end)
23571 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023572 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023573 goto theend;
23574 }
23575 }
23576
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023577 name = alloc((unsigned)(len + lead + 1));
23578 if (name != NULL)
23579 {
23580 if (lead > 0)
23581 {
23582 name[0] = K_SPECIAL;
23583 name[1] = KS_EXTRA;
23584 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000023585 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023586 STRCPY(name + 3, sid_buf);
23587 }
23588 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023589 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023590 }
23591 *pp = end;
23592
23593theend:
23594 clear_lval(&lv);
23595 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023596}
23597
23598/*
23599 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
23600 * Return 2 if "p" starts with "s:".
23601 * Return 0 otherwise.
23602 */
23603 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023604eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023605{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010023606 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
23607 * the standard library function. */
23608 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
23609 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023610 return 5;
23611 if (p[0] == 's' && p[1] == ':')
23612 return 2;
23613 return 0;
23614}
23615
23616/*
23617 * Return TRUE if "p" starts with "<SID>" or "s:".
23618 * Only works if eval_fname_script() returned non-zero for "p"!
23619 */
23620 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023621eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023622{
23623 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
23624}
23625
23626/*
23627 * List the head of the function: "name(arg1, arg2)".
23628 */
23629 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023630list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023631{
23632 int j;
23633
23634 msg_start();
23635 if (indent)
23636 MSG_PUTS(" ");
23637 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023638 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023639 {
23640 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023641 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023642 }
23643 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023644 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023645 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023646 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023647 {
23648 if (j)
23649 MSG_PUTS(", ");
23650 msg_puts(FUNCARG(fp, j));
23651 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023652 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023653 {
23654 if (j)
23655 MSG_PUTS(", ");
23656 MSG_PUTS("...");
23657 }
23658 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020023659 if (fp->uf_flags & FC_ABORT)
23660 MSG_PUTS(" abort");
23661 if (fp->uf_flags & FC_RANGE)
23662 MSG_PUTS(" range");
23663 if (fp->uf_flags & FC_DICT)
23664 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023665 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000023666 if (p_verbose > 0)
23667 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023668}
23669
23670/*
23671 * Find a function by name, return pointer to it in ufuncs.
23672 * Return NULL for unknown function.
23673 */
23674 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023675find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023676{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023677 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023678
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023679 hi = hash_find(&func_hashtab, name);
23680 if (!HASHITEM_EMPTY(hi))
23681 return HI2UF(hi);
23682 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023683}
23684
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023685#if defined(EXITFREE) || defined(PROTO)
23686 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023687free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023688{
23689 hashitem_T *hi;
23690
23691 /* Need to start all over every time, because func_free() may change the
23692 * hash table. */
23693 while (func_hashtab.ht_used > 0)
23694 for (hi = func_hashtab.ht_array; ; ++hi)
23695 if (!HASHITEM_EMPTY(hi))
23696 {
23697 func_free(HI2UF(hi));
23698 break;
23699 }
23700}
23701#endif
23702
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023703 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023704translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023705{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023706 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023707 return find_internal_func(name) >= 0;
23708 return find_func(name) != NULL;
23709}
23710
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023711/*
23712 * Return TRUE if a function "name" exists.
23713 */
23714 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023715function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023716{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000023717 char_u *nm = name;
23718 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023719 int n = FALSE;
23720
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023721 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
23722 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000023723 nm = skipwhite(nm);
23724
23725 /* Only accept "funcname", "funcname ", "funcname (..." and
23726 * "funcname(...", not "funcname!...". */
23727 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023728 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000023729 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023730 return n;
23731}
23732
Bram Moolenaara1544c02013-05-30 12:35:52 +020023733 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023734get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020023735{
23736 char_u *nm = name;
23737 char_u *p;
23738
23739 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
23740
23741 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023742 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020023743 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020023744
Bram Moolenaara1544c02013-05-30 12:35:52 +020023745 vim_free(p);
23746 return NULL;
23747}
23748
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023749/*
23750 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023751 * lower case letter and doesn't contain AUTOLOAD_CHAR.
23752 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023753 */
23754 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023755builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023756{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020023757 char_u *p;
23758
23759 if (!ASCII_ISLOWER(name[0]))
23760 return FALSE;
23761 p = vim_strchr(name, AUTOLOAD_CHAR);
23762 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023763}
23764
Bram Moolenaar05159a02005-02-26 23:04:13 +000023765#if defined(FEAT_PROFILE) || defined(PROTO)
23766/*
23767 * Start profiling function "fp".
23768 */
23769 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023770func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023771{
Bram Moolenaar904c6222010-07-24 16:57:39 +020023772 int len = fp->uf_lines.ga_len;
23773
23774 if (len == 0)
23775 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023776 fp->uf_tm_count = 0;
23777 profile_zero(&fp->uf_tm_self);
23778 profile_zero(&fp->uf_tm_total);
23779 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023780 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023781 if (fp->uf_tml_total == NULL)
23782 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023783 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023784 if (fp->uf_tml_self == NULL)
23785 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020023786 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023787 fp->uf_tml_idx = -1;
23788 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
23789 || fp->uf_tml_self == NULL)
23790 return; /* out of memory */
23791
23792 fp->uf_profiling = TRUE;
23793}
23794
23795/*
23796 * Dump the profiling results for all functions in file "fd".
23797 */
23798 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023799func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023800{
23801 hashitem_T *hi;
23802 int todo;
23803 ufunc_T *fp;
23804 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000023805 ufunc_T **sorttab;
23806 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023807
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023808 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023809 if (todo == 0)
23810 return; /* nothing to dump */
23811
Bram Moolenaare2e4b982015-06-09 20:30:51 +020023812 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000023813
Bram Moolenaar05159a02005-02-26 23:04:13 +000023814 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
23815 {
23816 if (!HASHITEM_EMPTY(hi))
23817 {
23818 --todo;
23819 fp = HI2UF(hi);
23820 if (fp->uf_profiling)
23821 {
Bram Moolenaar73830342005-02-28 22:48:19 +000023822 if (sorttab != NULL)
23823 sorttab[st_len++] = fp;
23824
Bram Moolenaar05159a02005-02-26 23:04:13 +000023825 if (fp->uf_name[0] == K_SPECIAL)
23826 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
23827 else
23828 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
23829 if (fp->uf_tm_count == 1)
23830 fprintf(fd, "Called 1 time\n");
23831 else
23832 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
23833 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
23834 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
23835 fprintf(fd, "\n");
23836 fprintf(fd, "count total (s) self (s)\n");
23837
23838 for (i = 0; i < fp->uf_lines.ga_len; ++i)
23839 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023840 if (FUNCLINE(fp, i) == NULL)
23841 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023842 prof_func_line(fd, fp->uf_tml_count[i],
23843 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023844 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23845 }
23846 fprintf(fd, "\n");
23847 }
23848 }
23849 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023850
23851 if (sorttab != NULL && st_len > 0)
23852 {
23853 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23854 prof_total_cmp);
23855 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23856 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23857 prof_self_cmp);
23858 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23859 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023860
23861 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023862}
Bram Moolenaar73830342005-02-28 22:48:19 +000023863
23864 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023865prof_sort_list(
23866 FILE *fd,
23867 ufunc_T **sorttab,
23868 int st_len,
23869 char *title,
23870 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023871{
23872 int i;
23873 ufunc_T *fp;
23874
23875 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23876 fprintf(fd, "count total (s) self (s) function\n");
23877 for (i = 0; i < 20 && i < st_len; ++i)
23878 {
23879 fp = sorttab[i];
23880 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23881 prefer_self);
23882 if (fp->uf_name[0] == K_SPECIAL)
23883 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23884 else
23885 fprintf(fd, " %s()\n", fp->uf_name);
23886 }
23887 fprintf(fd, "\n");
23888}
23889
23890/*
23891 * Print the count and times for one function or function line.
23892 */
23893 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023894prof_func_line(
23895 FILE *fd,
23896 int count,
23897 proftime_T *total,
23898 proftime_T *self,
23899 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000023900{
23901 if (count > 0)
23902 {
23903 fprintf(fd, "%5d ", count);
23904 if (prefer_self && profile_equal(total, self))
23905 fprintf(fd, " ");
23906 else
23907 fprintf(fd, "%s ", profile_msg(total));
23908 if (!prefer_self && profile_equal(total, self))
23909 fprintf(fd, " ");
23910 else
23911 fprintf(fd, "%s ", profile_msg(self));
23912 }
23913 else
23914 fprintf(fd, " ");
23915}
23916
23917/*
23918 * Compare function for total time sorting.
23919 */
23920 static int
23921#ifdef __BORLANDC__
23922_RTLENTRYF
23923#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010023924prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000023925{
23926 ufunc_T *p1, *p2;
23927
23928 p1 = *(ufunc_T **)s1;
23929 p2 = *(ufunc_T **)s2;
23930 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23931}
23932
23933/*
23934 * Compare function for self time sorting.
23935 */
23936 static int
23937#ifdef __BORLANDC__
23938_RTLENTRYF
23939#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010023940prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000023941{
23942 ufunc_T *p1, *p2;
23943
23944 p1 = *(ufunc_T **)s1;
23945 p2 = *(ufunc_T **)s2;
23946 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23947}
23948
Bram Moolenaar05159a02005-02-26 23:04:13 +000023949#endif
23950
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023951/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023952 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023953 * Return TRUE if a package was loaded.
23954 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023955 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023956script_autoload(
23957 char_u *name,
23958 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023959{
23960 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023961 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023962 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023963 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023964
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023965 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023966 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023967 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023968 return FALSE;
23969
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023970 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023971
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023972 /* Find the name in the list of previously loaded package names. Skip
23973 * "autoload/", it's always the same. */
23974 for (i = 0; i < ga_loaded.ga_len; ++i)
23975 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23976 break;
23977 if (!reload && i < ga_loaded.ga_len)
23978 ret = FALSE; /* was loaded already */
23979 else
23980 {
23981 /* Remember the name if it wasn't loaded already. */
23982 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23983 {
23984 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23985 tofree = NULL;
23986 }
23987
23988 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010023989 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023990 ret = TRUE;
23991 }
23992
23993 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023994 return ret;
23995}
23996
23997/*
23998 * Return the autoload script name for a function or variable name.
23999 * Returns NULL when out of memory.
24000 */
24001 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024002autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024003{
24004 char_u *p;
24005 char_u *scriptname;
24006
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024007 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024008 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24009 if (scriptname == NULL)
24010 return FALSE;
24011 STRCPY(scriptname, "autoload/");
24012 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024013 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024014 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024015 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024016 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024017 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024018}
24019
Bram Moolenaar071d4272004-06-13 20:20:40 +000024020#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24021
24022/*
24023 * Function given to ExpandGeneric() to obtain the list of user defined
24024 * function names.
24025 */
24026 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024027get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024028{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024029 static long_u done;
24030 static hashitem_T *hi;
24031 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024032
24033 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024034 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024035 done = 0;
24036 hi = func_hashtab.ht_array;
24037 }
24038 if (done < func_hashtab.ht_used)
24039 {
24040 if (done++ > 0)
24041 ++hi;
24042 while (HASHITEM_EMPTY(hi))
24043 ++hi;
24044 fp = HI2UF(hi);
24045
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024046 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024047 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024048
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024049 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24050 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024051
24052 cat_func_name(IObuff, fp);
24053 if (xp->xp_context != EXPAND_USER_FUNC)
24054 {
24055 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024056 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024057 STRCAT(IObuff, ")");
24058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024059 return IObuff;
24060 }
24061 return NULL;
24062}
24063
24064#endif /* FEAT_CMDL_COMPL */
24065
24066/*
24067 * Copy the function name of "fp" to buffer "buf".
24068 * "buf" must be able to hold the function name plus three bytes.
24069 * Takes care of script-local function names.
24070 */
24071 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024072cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024073{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024074 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024075 {
24076 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024077 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024078 }
24079 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024080 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024081}
24082
24083/*
24084 * ":delfunction {name}"
24085 */
24086 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024087ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024088{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024089 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024090 char_u *p;
24091 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024092 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024093
24094 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024095 name = trans_function_name(&p, eap->skip, 0, &fudi);
24096 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024097 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024098 {
24099 if (fudi.fd_dict != NULL && !eap->skip)
24100 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024101 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024103 if (!ends_excmd(*skipwhite(p)))
24104 {
24105 vim_free(name);
24106 EMSG(_(e_trailing));
24107 return;
24108 }
24109 eap->nextcmd = check_nextcmd(p);
24110 if (eap->nextcmd != NULL)
24111 *p = NUL;
24112
24113 if (!eap->skip)
24114 fp = find_func(name);
24115 vim_free(name);
24116
24117 if (!eap->skip)
24118 {
24119 if (fp == NULL)
24120 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024121 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024122 return;
24123 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024124 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024125 {
24126 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24127 return;
24128 }
24129
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024130 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024131 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024132 /* Delete the dict item that refers to the function, it will
24133 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024134 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024135 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024136 else
24137 func_free(fp);
24138 }
24139}
24140
24141/*
24142 * Free a function and remove it from the list of functions.
24143 */
24144 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024145func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024146{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024147 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024148
24149 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024150 ga_clear_strings(&(fp->uf_args));
24151 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024152#ifdef FEAT_PROFILE
24153 vim_free(fp->uf_tml_count);
24154 vim_free(fp->uf_tml_total);
24155 vim_free(fp->uf_tml_self);
24156#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024157
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024158 /* remove the function from the function hashtable */
24159 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24160 if (HASHITEM_EMPTY(hi))
24161 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024162 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024163 hash_remove(&func_hashtab, hi);
24164
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024165 vim_free(fp);
24166}
24167
24168/*
24169 * Unreference a Function: decrement the reference count and free it when it
24170 * becomes zero. Only for numbered functions.
24171 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024172 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024173func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024174{
24175 ufunc_T *fp;
24176
24177 if (name != NULL && isdigit(*name))
24178 {
24179 fp = find_func(name);
24180 if (fp == NULL)
24181 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024182 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024183 {
24184 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024185 * when "uf_calls" becomes zero. */
24186 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024187 func_free(fp);
24188 }
24189 }
24190}
24191
24192/*
24193 * Count a reference to a Function.
24194 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024195 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024196func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024197{
24198 ufunc_T *fp;
24199
24200 if (name != NULL && isdigit(*name))
24201 {
24202 fp = find_func(name);
24203 if (fp == NULL)
24204 EMSG2(_(e_intern2), "func_ref()");
24205 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024206 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024207 }
24208}
24209
24210/*
24211 * Call a user function.
24212 */
24213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024214call_user_func(
24215 ufunc_T *fp, /* pointer to function */
24216 int argcount, /* nr of args */
24217 typval_T *argvars, /* arguments */
24218 typval_T *rettv, /* return value */
24219 linenr_T firstline, /* first line of range */
24220 linenr_T lastline, /* last line of range */
24221 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024222{
Bram Moolenaar33570922005-01-25 22:26:29 +000024223 char_u *save_sourcing_name;
24224 linenr_T save_sourcing_lnum;
24225 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024226 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024227 int save_did_emsg;
24228 static int depth = 0;
24229 dictitem_T *v;
24230 int fixvar_idx = 0; /* index in fixvar[] */
24231 int i;
24232 int ai;
24233 char_u numbuf[NUMBUFLEN];
24234 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024235 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024236#ifdef FEAT_PROFILE
24237 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024238 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024239#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024240
24241 /* If depth of calling is getting too high, don't execute the function */
24242 if (depth >= p_mfd)
24243 {
24244 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024245 rettv->v_type = VAR_NUMBER;
24246 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024247 return;
24248 }
24249 ++depth;
24250
24251 line_breakcheck(); /* check for CTRL-C hit */
24252
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024253 fc = (funccall_T *)alloc(sizeof(funccall_T));
24254 fc->caller = current_funccal;
24255 current_funccal = fc;
24256 fc->func = fp;
24257 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024258 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024259 fc->linenr = 0;
24260 fc->returned = FALSE;
24261 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024262 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024263 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24264 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024265
Bram Moolenaar33570922005-01-25 22:26:29 +000024266 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024267 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024268 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24269 * each argument variable and saves a lot of time.
24270 */
24271 /*
24272 * Init l: variables.
24273 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024274 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024275 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024276 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024277 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24278 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024279 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024280 name = v->di_key;
24281 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024282 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024283 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024284 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024285 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024286 v->di_tv.vval.v_dict = selfdict;
24287 ++selfdict->dv_refcount;
24288 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024289
Bram Moolenaar33570922005-01-25 22:26:29 +000024290 /*
24291 * Init a: variables.
24292 * Set a:0 to "argcount".
24293 * Set a:000 to a list with room for the "..." arguments.
24294 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024295 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024296 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024297 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024298 /* Use "name" to avoid a warning from some compiler that checks the
24299 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024300 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024301 name = v->di_key;
24302 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024303 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024304 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024305 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024306 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024307 v->di_tv.vval.v_list = &fc->l_varlist;
24308 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24309 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24310 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024311
24312 /*
24313 * Set a:firstline to "firstline" and a:lastline to "lastline".
24314 * Set a:name to named arguments.
24315 * Set a:N to the "..." arguments.
24316 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024317 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024318 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024319 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024320 (varnumber_T)lastline);
24321 for (i = 0; i < argcount; ++i)
24322 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024323 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024324 if (ai < 0)
24325 /* named argument a:name */
24326 name = FUNCARG(fp, i);
24327 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024328 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024329 /* "..." argument a:1, a:2, etc. */
24330 sprintf((char *)numbuf, "%d", ai + 1);
24331 name = numbuf;
24332 }
24333 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24334 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024335 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024336 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24337 }
24338 else
24339 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024340 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24341 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024342 if (v == NULL)
24343 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024344 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024345 }
24346 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024347 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024348
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024349 /* Note: the values are copied directly to avoid alloc/free.
24350 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024351 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024352 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024353
24354 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24355 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024356 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24357 fc->l_listitems[ai].li_tv = argvars[i];
24358 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024359 }
24360 }
24361
Bram Moolenaar071d4272004-06-13 20:20:40 +000024362 /* Don't redraw while executing the function. */
24363 ++RedrawingDisabled;
24364 save_sourcing_name = sourcing_name;
24365 save_sourcing_lnum = sourcing_lnum;
24366 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024367 /* need space for function name + ("function " + 3) or "[number]" */
24368 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24369 + STRLEN(fp->uf_name) + 20;
24370 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024371 if (sourcing_name != NULL)
24372 {
24373 if (save_sourcing_name != NULL
24374 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024375 sprintf((char *)sourcing_name, "%s[%d]..",
24376 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024377 else
24378 STRCPY(sourcing_name, "function ");
24379 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24380
24381 if (p_verbose >= 12)
24382 {
24383 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024384 verbose_enter_scroll();
24385
Bram Moolenaar555b2802005-05-19 21:08:39 +000024386 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024387 if (p_verbose >= 14)
24388 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024389 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024390 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024391 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024392 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024393
24394 msg_puts((char_u *)"(");
24395 for (i = 0; i < argcount; ++i)
24396 {
24397 if (i > 0)
24398 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024399 if (argvars[i].v_type == VAR_NUMBER)
24400 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024401 else
24402 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024403 /* Do not want errors such as E724 here. */
24404 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024405 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024406 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024407 if (s != NULL)
24408 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024409 if (vim_strsize(s) > MSG_BUF_CLEN)
24410 {
24411 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24412 s = buf;
24413 }
24414 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024415 vim_free(tofree);
24416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024417 }
24418 }
24419 msg_puts((char_u *)")");
24420 }
24421 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024422
24423 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024424 --no_wait_return;
24425 }
24426 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024427#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024428 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024429 {
24430 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24431 func_do_profile(fp);
24432 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024433 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024434 {
24435 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024436 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024437 profile_zero(&fp->uf_tm_children);
24438 }
24439 script_prof_save(&wait_start);
24440 }
24441#endif
24442
Bram Moolenaar071d4272004-06-13 20:20:40 +000024443 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024444 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024445 save_did_emsg = did_emsg;
24446 did_emsg = FALSE;
24447
24448 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024449 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024450 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24451
24452 --RedrawingDisabled;
24453
24454 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024455 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024456 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024457 clear_tv(rettv);
24458 rettv->v_type = VAR_NUMBER;
24459 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024460 }
24461
Bram Moolenaar05159a02005-02-26 23:04:13 +000024462#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024463 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024464 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024465 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024466 profile_end(&call_start);
24467 profile_sub_wait(&wait_start, &call_start);
24468 profile_add(&fp->uf_tm_total, &call_start);
24469 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024470 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024471 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024472 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24473 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024474 }
24475 }
24476#endif
24477
Bram Moolenaar071d4272004-06-13 20:20:40 +000024478 /* when being verbose, mention the return value */
24479 if (p_verbose >= 12)
24480 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024481 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024482 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024483
Bram Moolenaar071d4272004-06-13 20:20:40 +000024484 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024485 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024486 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024487 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024488 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024489 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024490 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024491 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024492 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024493 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024494 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024495
Bram Moolenaar555b2802005-05-19 21:08:39 +000024496 /* The value may be very long. Skip the middle part, so that we
24497 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024498 * truncate it at the end. Don't want errors such as E724 here. */
24499 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024500 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024501 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024502 if (s != NULL)
24503 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024504 if (vim_strsize(s) > MSG_BUF_CLEN)
24505 {
24506 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24507 s = buf;
24508 }
24509 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024510 vim_free(tofree);
24511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024512 }
24513 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024514
24515 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024516 --no_wait_return;
24517 }
24518
24519 vim_free(sourcing_name);
24520 sourcing_name = save_sourcing_name;
24521 sourcing_lnum = save_sourcing_lnum;
24522 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024523#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024524 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024525 script_prof_restore(&wait_start);
24526#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024527
24528 if (p_verbose >= 12 && sourcing_name != NULL)
24529 {
24530 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024531 verbose_enter_scroll();
24532
Bram Moolenaar555b2802005-05-19 21:08:39 +000024533 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024534 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024535
24536 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024537 --no_wait_return;
24538 }
24539
24540 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024541 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024542 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024543
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024544 /* If the a:000 list and the l: and a: dicts are not referenced we can
24545 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024546 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
24547 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
24548 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
24549 {
24550 free_funccal(fc, FALSE);
24551 }
24552 else
24553 {
24554 hashitem_T *hi;
24555 listitem_T *li;
24556 int todo;
24557
24558 /* "fc" is still in use. This can happen when returning "a:000" or
24559 * assigning "l:" to a global variable.
24560 * Link "fc" in the list for garbage collection later. */
24561 fc->caller = previous_funccal;
24562 previous_funccal = fc;
24563
24564 /* Make a copy of the a: variables, since we didn't do that above. */
24565 todo = (int)fc->l_avars.dv_hashtab.ht_used;
24566 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
24567 {
24568 if (!HASHITEM_EMPTY(hi))
24569 {
24570 --todo;
24571 v = HI2DI(hi);
24572 copy_tv(&v->di_tv, &v->di_tv);
24573 }
24574 }
24575
24576 /* Make a copy of the a:000 items, since we didn't do that above. */
24577 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24578 copy_tv(&li->li_tv, &li->li_tv);
24579 }
24580}
24581
24582/*
24583 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024584 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024585 */
24586 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024587can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024588{
24589 return (fc->l_varlist.lv_copyID != copyID
24590 && fc->l_vars.dv_copyID != copyID
24591 && fc->l_avars.dv_copyID != copyID);
24592}
24593
24594/*
24595 * Free "fc" and what it contains.
24596 */
24597 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024598free_funccal(
24599 funccall_T *fc,
24600 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024601{
24602 listitem_T *li;
24603
24604 /* The a: variables typevals may not have been allocated, only free the
24605 * allocated variables. */
24606 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
24607
24608 /* free all l: variables */
24609 vars_clear(&fc->l_vars.dv_hashtab);
24610
24611 /* Free the a:000 variables if they were allocated. */
24612 if (free_val)
24613 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
24614 clear_tv(&li->li_tv);
24615
24616 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024617}
24618
24619/*
Bram Moolenaar33570922005-01-25 22:26:29 +000024620 * Add a number variable "name" to dict "dp" with value "nr".
24621 */
24622 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024623add_nr_var(
24624 dict_T *dp,
24625 dictitem_T *v,
24626 char *name,
24627 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000024628{
24629 STRCPY(v->di_key, name);
24630 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24631 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
24632 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024633 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024634 v->di_tv.vval.v_number = nr;
24635}
24636
24637/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000024638 * ":return [expr]"
24639 */
24640 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024641ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024642{
24643 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000024644 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024645 int returning = FALSE;
24646
24647 if (current_funccal == NULL)
24648 {
24649 EMSG(_("E133: :return not inside a function"));
24650 return;
24651 }
24652
24653 if (eap->skip)
24654 ++emsg_skip;
24655
24656 eap->nextcmd = NULL;
24657 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024658 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024659 {
24660 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024661 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024662 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024663 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024664 }
24665 /* It's safer to return also on error. */
24666 else if (!eap->skip)
24667 {
24668 /*
24669 * Return unless the expression evaluation has been cancelled due to an
24670 * aborting error, an interrupt, or an exception.
24671 */
24672 if (!aborting())
24673 returning = do_return(eap, FALSE, TRUE, NULL);
24674 }
24675
24676 /* When skipping or the return gets pending, advance to the next command
24677 * in this line (!returning). Otherwise, ignore the rest of the line.
24678 * Following lines will be ignored by get_func_line(). */
24679 if (returning)
24680 eap->nextcmd = NULL;
24681 else if (eap->nextcmd == NULL) /* no argument */
24682 eap->nextcmd = check_nextcmd(arg);
24683
24684 if (eap->skip)
24685 --emsg_skip;
24686}
24687
24688/*
24689 * Return from a function. Possibly makes the return pending. Also called
24690 * for a pending return at the ":endtry" or after returning from an extra
24691 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000024692 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024693 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024694 * FALSE when the return gets pending.
24695 */
24696 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024697do_return(
24698 exarg_T *eap,
24699 int reanimate,
24700 int is_cmd,
24701 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024702{
24703 int idx;
24704 struct condstack *cstack = eap->cstack;
24705
24706 if (reanimate)
24707 /* Undo the return. */
24708 current_funccal->returned = FALSE;
24709
24710 /*
24711 * Cleanup (and inactivate) conditionals, but stop when a try conditional
24712 * not in its finally clause (which then is to be executed next) is found.
24713 * In this case, make the ":return" pending for execution at the ":endtry".
24714 * Otherwise, return normally.
24715 */
24716 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
24717 if (idx >= 0)
24718 {
24719 cstack->cs_pending[idx] = CSTP_RETURN;
24720
24721 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024722 /* A pending return again gets pending. "rettv" points to an
24723 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000024724 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024725 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024726 else
24727 {
24728 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024729 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024730 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024731 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024732
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024733 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024734 {
24735 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024736 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024737 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024738 else
24739 EMSG(_(e_outofmem));
24740 }
24741 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024742 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024743
24744 if (reanimate)
24745 {
24746 /* The pending return value could be overwritten by a ":return"
24747 * without argument in a finally clause; reset the default
24748 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024749 current_funccal->rettv->v_type = VAR_NUMBER;
24750 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024751 }
24752 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024753 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024754 }
24755 else
24756 {
24757 current_funccal->returned = TRUE;
24758
24759 /* If the return is carried out now, store the return value. For
24760 * a return immediately after reanimation, the value is already
24761 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024762 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024763 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024764 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000024765 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024766 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024767 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024768 }
24769 }
24770
24771 return idx < 0;
24772}
24773
24774/*
24775 * Free the variable with a pending return value.
24776 */
24777 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024778discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024779{
Bram Moolenaar33570922005-01-25 22:26:29 +000024780 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024781}
24782
24783/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024784 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000024785 * is an allocated string. Used by report_pending() for verbose messages.
24786 */
24787 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024788get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024789{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024790 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024791 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024792 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024793
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024794 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024795 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024796 if (s == NULL)
24797 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024798
24799 STRCPY(IObuff, ":return ");
24800 STRNCPY(IObuff + 8, s, IOSIZE - 8);
24801 if (STRLEN(s) + 8 >= IOSIZE)
24802 STRCPY(IObuff + IOSIZE - 4, "...");
24803 vim_free(tofree);
24804 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024805}
24806
24807/*
24808 * Get next function line.
24809 * Called by do_cmdline() to get the next line.
24810 * Returns allocated string, or NULL for end of function.
24811 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024812 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024813get_func_line(
24814 int c UNUSED,
24815 void *cookie,
24816 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024817{
Bram Moolenaar33570922005-01-25 22:26:29 +000024818 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024819 ufunc_T *fp = fcp->func;
24820 char_u *retval;
24821 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024822
24823 /* If breakpoints have been added/deleted need to check for it. */
24824 if (fcp->dbg_tick != debug_tick)
24825 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024826 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024827 sourcing_lnum);
24828 fcp->dbg_tick = debug_tick;
24829 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024830#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024831 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024832 func_line_end(cookie);
24833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024834
Bram Moolenaar05159a02005-02-26 23:04:13 +000024835 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024836 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24837 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024838 retval = NULL;
24839 else
24840 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024841 /* Skip NULL lines (continuation lines). */
24842 while (fcp->linenr < gap->ga_len
24843 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24844 ++fcp->linenr;
24845 if (fcp->linenr >= gap->ga_len)
24846 retval = NULL;
24847 else
24848 {
24849 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24850 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024851#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024852 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024853 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024854#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024856 }
24857
24858 /* Did we encounter a breakpoint? */
24859 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24860 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024861 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024862 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024863 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024864 sourcing_lnum);
24865 fcp->dbg_tick = debug_tick;
24866 }
24867
24868 return retval;
24869}
24870
Bram Moolenaar05159a02005-02-26 23:04:13 +000024871#if defined(FEAT_PROFILE) || defined(PROTO)
24872/*
24873 * Called when starting to read a function line.
24874 * "sourcing_lnum" must be correct!
24875 * When skipping lines it may not actually be executed, but we won't find out
24876 * until later and we need to store the time now.
24877 */
24878 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024879func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024880{
24881 funccall_T *fcp = (funccall_T *)cookie;
24882 ufunc_T *fp = fcp->func;
24883
24884 if (fp->uf_profiling && sourcing_lnum >= 1
24885 && sourcing_lnum <= fp->uf_lines.ga_len)
24886 {
24887 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024888 /* Skip continuation lines. */
24889 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24890 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024891 fp->uf_tml_execed = FALSE;
24892 profile_start(&fp->uf_tml_start);
24893 profile_zero(&fp->uf_tml_children);
24894 profile_get_wait(&fp->uf_tml_wait);
24895 }
24896}
24897
24898/*
24899 * Called when actually executing a function line.
24900 */
24901 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024902func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024903{
24904 funccall_T *fcp = (funccall_T *)cookie;
24905 ufunc_T *fp = fcp->func;
24906
24907 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24908 fp->uf_tml_execed = TRUE;
24909}
24910
24911/*
24912 * Called when done with a function line.
24913 */
24914 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024915func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024916{
24917 funccall_T *fcp = (funccall_T *)cookie;
24918 ufunc_T *fp = fcp->func;
24919
24920 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24921 {
24922 if (fp->uf_tml_execed)
24923 {
24924 ++fp->uf_tml_count[fp->uf_tml_idx];
24925 profile_end(&fp->uf_tml_start);
24926 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024927 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024928 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24929 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024930 }
24931 fp->uf_tml_idx = -1;
24932 }
24933}
24934#endif
24935
Bram Moolenaar071d4272004-06-13 20:20:40 +000024936/*
24937 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024938 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024939 */
24940 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024941func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024942{
Bram Moolenaar33570922005-01-25 22:26:29 +000024943 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024944
24945 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24946 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024947 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024948 || fcp->returned);
24949}
24950
24951/*
24952 * return TRUE if cookie indicates a function which "abort"s on errors.
24953 */
24954 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024955func_has_abort(
24956 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024957{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024958 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024959}
24960
24961#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24962typedef enum
24963{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024964 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24965 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24966 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024967} var_flavour_T;
24968
Bram Moolenaar48e697e2016-01-23 22:17:30 +010024969static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024970
24971 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010024972var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024973{
24974 char_u *p = varname;
24975
24976 if (ASCII_ISUPPER(*p))
24977 {
24978 while (*(++p))
24979 if (ASCII_ISLOWER(*p))
24980 return VAR_FLAVOUR_SESSION;
24981 return VAR_FLAVOUR_VIMINFO;
24982 }
24983 else
24984 return VAR_FLAVOUR_DEFAULT;
24985}
24986#endif
24987
24988#if defined(FEAT_VIMINFO) || defined(PROTO)
24989/*
24990 * Restore global vars that start with a capital from the viminfo file
24991 */
24992 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024993read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024994{
24995 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024996 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024997 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024998 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024999
25000 if (!writing && (find_viminfo_parameter('!') != NULL))
25001 {
25002 tab = vim_strchr(virp->vir_line + 1, '\t');
25003 if (tab != NULL)
25004 {
25005 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025006 switch (*tab)
25007 {
25008 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025009#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025010 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025011#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025012 case 'D': type = VAR_DICT; break;
25013 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025014 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025016
25017 tab = vim_strchr(tab, '\t');
25018 if (tab != NULL)
25019 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025020 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025021 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025022 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025023 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025024#ifdef FEAT_FLOAT
25025 else if (type == VAR_FLOAT)
25026 (void)string2float(tab + 1, &tv.vval.v_float);
25027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025028 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025029 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025030 if (type == VAR_DICT || type == VAR_LIST)
25031 {
25032 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25033
25034 if (etv == NULL)
25035 /* Failed to parse back the dict or list, use it as a
25036 * string. */
25037 tv.v_type = VAR_STRING;
25038 else
25039 {
25040 vim_free(tv.vval.v_string);
25041 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025042 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025043 }
25044 }
25045
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025046 /* when in a function use global variables */
25047 save_funccal = current_funccal;
25048 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025049 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025050 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025051
25052 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025053 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025054 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25055 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025056 }
25057 }
25058 }
25059
25060 return viminfo_readline(virp);
25061}
25062
25063/*
25064 * Write global vars that start with a capital to the viminfo file
25065 */
25066 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025067write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025068{
Bram Moolenaar33570922005-01-25 22:26:29 +000025069 hashitem_T *hi;
25070 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025071 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025072 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025073 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025074 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025075 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025076
25077 if (find_viminfo_parameter('!') == NULL)
25078 return;
25079
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025080 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025081
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025082 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025083 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025084 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025085 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025086 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025087 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025088 this_var = HI2DI(hi);
25089 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025090 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025091 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025092 {
25093 case VAR_STRING: s = "STR"; break;
25094 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025095 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025096 case VAR_DICT: s = "DIC"; break;
25097 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025098 case VAR_SPECIAL: s = "XPL"; break;
25099
25100 case VAR_UNKNOWN:
25101 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025102 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025103 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025104 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025105 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025106 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025107 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025108 if (p != NULL)
25109 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025110 vim_free(tofree);
25111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025112 }
25113 }
25114}
25115#endif
25116
25117#if defined(FEAT_SESSION) || defined(PROTO)
25118 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025119store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025120{
Bram Moolenaar33570922005-01-25 22:26:29 +000025121 hashitem_T *hi;
25122 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025123 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025124 char_u *p, *t;
25125
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025126 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025127 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025128 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025129 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025130 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025131 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025132 this_var = HI2DI(hi);
25133 if ((this_var->di_tv.v_type == VAR_NUMBER
25134 || this_var->di_tv.v_type == VAR_STRING)
25135 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025136 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025137 /* Escape special characters with a backslash. Turn a LF and
25138 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025139 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025140 (char_u *)"\\\"\n\r");
25141 if (p == NULL) /* out of memory */
25142 break;
25143 for (t = p; *t != NUL; ++t)
25144 if (*t == '\n')
25145 *t = 'n';
25146 else if (*t == '\r')
25147 *t = 'r';
25148 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025149 this_var->di_key,
25150 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25151 : ' ',
25152 p,
25153 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25154 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025155 || put_eol(fd) == FAIL)
25156 {
25157 vim_free(p);
25158 return FAIL;
25159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025160 vim_free(p);
25161 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025162#ifdef FEAT_FLOAT
25163 else if (this_var->di_tv.v_type == VAR_FLOAT
25164 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25165 {
25166 float_T f = this_var->di_tv.vval.v_float;
25167 int sign = ' ';
25168
25169 if (f < 0)
25170 {
25171 f = -f;
25172 sign = '-';
25173 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025174 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025175 this_var->di_key, sign, f) < 0)
25176 || put_eol(fd) == FAIL)
25177 return FAIL;
25178 }
25179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025180 }
25181 }
25182 return OK;
25183}
25184#endif
25185
Bram Moolenaar661b1822005-07-28 22:36:45 +000025186/*
25187 * Display script name where an item was last set.
25188 * Should only be invoked when 'verbose' is non-zero.
25189 */
25190 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025191last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025192{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025193 char_u *p;
25194
Bram Moolenaar661b1822005-07-28 22:36:45 +000025195 if (scriptID != 0)
25196 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025197 p = home_replace_save(NULL, get_scriptname(scriptID));
25198 if (p != NULL)
25199 {
25200 verbose_enter();
25201 MSG_PUTS(_("\n\tLast set from "));
25202 MSG_PUTS(p);
25203 vim_free(p);
25204 verbose_leave();
25205 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025206 }
25207}
25208
Bram Moolenaard812df62008-11-09 12:46:09 +000025209/*
25210 * List v:oldfiles in a nice way.
25211 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025212 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025213ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025214{
25215 list_T *l = vimvars[VV_OLDFILES].vv_list;
25216 listitem_T *li;
25217 int nr = 0;
25218
25219 if (l == NULL)
25220 msg((char_u *)_("No old files"));
25221 else
25222 {
25223 msg_start();
25224 msg_scroll = TRUE;
25225 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25226 {
25227 msg_outnum((long)++nr);
25228 MSG_PUTS(": ");
25229 msg_outtrans(get_tv_string(&li->li_tv));
25230 msg_putchar('\n');
25231 out_flush(); /* output one line at a time */
25232 ui_breakcheck();
25233 }
25234 /* Assume "got_int" was set to truncate the listing. */
25235 got_int = FALSE;
25236
25237#ifdef FEAT_BROWSE_CMD
25238 if (cmdmod.browse)
25239 {
25240 quit_more = FALSE;
25241 nr = prompt_for_number(FALSE);
25242 msg_starthere();
25243 if (nr > 0)
25244 {
25245 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25246 (long)nr);
25247
25248 if (p != NULL)
25249 {
25250 p = expand_env_save(p);
25251 eap->arg = p;
25252 eap->cmdidx = CMD_edit;
25253 cmdmod.browse = FALSE;
25254 do_exedit(eap, NULL);
25255 vim_free(p);
25256 }
25257 }
25258 }
25259#endif
25260 }
25261}
25262
Bram Moolenaar53744302015-07-17 17:38:22 +020025263/* reset v:option_new, v:option_old and v:option_type */
25264 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025265reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025266{
25267 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25268 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25269 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25270}
25271
25272
Bram Moolenaar071d4272004-06-13 20:20:40 +000025273#endif /* FEAT_EVAL */
25274
Bram Moolenaar071d4272004-06-13 20:20:40 +000025275
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025276#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025277
25278#ifdef WIN3264
25279/*
25280 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25281 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025282static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25283static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25284static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025285
25286/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025287 * Get the short path (8.3) for the filename in "fnamep".
25288 * Only works for a valid file name.
25289 * When the path gets longer "fnamep" is changed and the allocated buffer
25290 * is put in "bufp".
25291 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25292 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025293 */
25294 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025295get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025296{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025297 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025298 char_u *newbuf;
25299
25300 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025301 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025302 if (l > len - 1)
25303 {
25304 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025305 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025306 newbuf = vim_strnsave(*fnamep, l);
25307 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025308 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025309
25310 vim_free(*bufp);
25311 *fnamep = *bufp = newbuf;
25312
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025313 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025314 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025315 }
25316
25317 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025318 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025319}
25320
25321/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025322 * Get the short path (8.3) for the filename in "fname". The converted
25323 * path is returned in "bufp".
25324 *
25325 * Some of the directories specified in "fname" may not exist. This function
25326 * will shorten the existing directories at the beginning of the path and then
25327 * append the remaining non-existing path.
25328 *
25329 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025330 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025331 * bufp - Pointer to an allocated buffer for the filename.
25332 * fnamelen - Length of the filename pointed to by fname
25333 *
25334 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025335 */
25336 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025337shortpath_for_invalid_fname(
25338 char_u **fname,
25339 char_u **bufp,
25340 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025341{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025342 char_u *short_fname, *save_fname, *pbuf_unused;
25343 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025344 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025345 int old_len, len;
25346 int new_len, sfx_len;
25347 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025348
25349 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025350 old_len = *fnamelen;
25351 save_fname = vim_strnsave(*fname, old_len);
25352 pbuf_unused = NULL;
25353 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025354
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025355 endp = save_fname + old_len - 1; /* Find the end of the copy */
25356 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025357
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025358 /*
25359 * Try shortening the supplied path till it succeeds by removing one
25360 * directory at a time from the tail of the path.
25361 */
25362 len = 0;
25363 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025364 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025365 /* go back one path-separator */
25366 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25367 --endp;
25368 if (endp <= save_fname)
25369 break; /* processed the complete path */
25370
25371 /*
25372 * Replace the path separator with a NUL and try to shorten the
25373 * resulting path.
25374 */
25375 ch = *endp;
25376 *endp = 0;
25377 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025378 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025379 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25380 {
25381 retval = FAIL;
25382 goto theend;
25383 }
25384 *endp = ch; /* preserve the string */
25385
25386 if (len > 0)
25387 break; /* successfully shortened the path */
25388
25389 /* failed to shorten the path. Skip the path separator */
25390 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025391 }
25392
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025393 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025394 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025395 /*
25396 * Succeeded in shortening the path. Now concatenate the shortened
25397 * path with the remaining path at the tail.
25398 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025399
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025400 /* Compute the length of the new path. */
25401 sfx_len = (int)(save_endp - endp) + 1;
25402 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025403
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025404 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025405 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025406 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025407 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025408 /* There is not enough space in the currently allocated string,
25409 * copy it to a buffer big enough. */
25410 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025411 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025412 {
25413 retval = FAIL;
25414 goto theend;
25415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025416 }
25417 else
25418 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025419 /* Transfer short_fname to the main buffer (it's big enough),
25420 * unless get_short_pathname() did its work in-place. */
25421 *fname = *bufp = save_fname;
25422 if (short_fname != save_fname)
25423 vim_strncpy(save_fname, short_fname, len);
25424 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025425 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025426
25427 /* concat the not-shortened part of the path */
25428 vim_strncpy(*fname + len, endp, sfx_len);
25429 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025430 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025431
25432theend:
25433 vim_free(pbuf_unused);
25434 vim_free(save_fname);
25435
25436 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025437}
25438
25439/*
25440 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025441 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025442 */
25443 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025444shortpath_for_partial(
25445 char_u **fnamep,
25446 char_u **bufp,
25447 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025448{
25449 int sepcount, len, tflen;
25450 char_u *p;
25451 char_u *pbuf, *tfname;
25452 int hasTilde;
25453
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025454 /* Count up the path separators from the RHS.. so we know which part
25455 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025456 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025457 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025458 if (vim_ispathsep(*p))
25459 ++sepcount;
25460
25461 /* Need full path first (use expand_env() to remove a "~/") */
25462 hasTilde = (**fnamep == '~');
25463 if (hasTilde)
25464 pbuf = tfname = expand_env_save(*fnamep);
25465 else
25466 pbuf = tfname = FullName_save(*fnamep, FALSE);
25467
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025468 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025469
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025470 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25471 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025472
25473 if (len == 0)
25474 {
25475 /* Don't have a valid filename, so shorten the rest of the
25476 * path if we can. This CAN give us invalid 8.3 filenames, but
25477 * there's not a lot of point in guessing what it might be.
25478 */
25479 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025480 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25481 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025482 }
25483
25484 /* Count the paths backward to find the beginning of the desired string. */
25485 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025486 {
25487#ifdef FEAT_MBYTE
25488 if (has_mbyte)
25489 p -= mb_head_off(tfname, p);
25490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025491 if (vim_ispathsep(*p))
25492 {
25493 if (sepcount == 0 || (hasTilde && sepcount == 1))
25494 break;
25495 else
25496 sepcount --;
25497 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025499 if (hasTilde)
25500 {
25501 --p;
25502 if (p >= tfname)
25503 *p = '~';
25504 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025505 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025506 }
25507 else
25508 ++p;
25509
25510 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25511 vim_free(*bufp);
25512 *fnamelen = (int)STRLEN(p);
25513 *bufp = pbuf;
25514 *fnamep = p;
25515
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025516 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025517}
25518#endif /* WIN3264 */
25519
25520/*
25521 * Adjust a filename, according to a string of modifiers.
25522 * *fnamep must be NUL terminated when called. When returning, the length is
25523 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025524 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025525 * When there is an error, *fnamep is set to NULL.
25526 */
25527 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025528modify_fname(
25529 char_u *src, /* string with modifiers */
25530 int *usedlen, /* characters after src that are used */
25531 char_u **fnamep, /* file name so far */
25532 char_u **bufp, /* buffer for allocated file name or NULL */
25533 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025534{
25535 int valid = 0;
25536 char_u *tail;
25537 char_u *s, *p, *pbuf;
25538 char_u dirname[MAXPATHL];
25539 int c;
25540 int has_fullname = 0;
25541#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025542 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025543 int has_shortname = 0;
25544#endif
25545
25546repeat:
25547 /* ":p" - full path/file_name */
25548 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
25549 {
25550 has_fullname = 1;
25551
25552 valid |= VALID_PATH;
25553 *usedlen += 2;
25554
25555 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
25556 if ((*fnamep)[0] == '~'
25557#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
25558 && ((*fnamep)[1] == '/'
25559# ifdef BACKSLASH_IN_FILENAME
25560 || (*fnamep)[1] == '\\'
25561# endif
25562 || (*fnamep)[1] == NUL)
25563
25564#endif
25565 )
25566 {
25567 *fnamep = expand_env_save(*fnamep);
25568 vim_free(*bufp); /* free any allocated file name */
25569 *bufp = *fnamep;
25570 if (*fnamep == NULL)
25571 return -1;
25572 }
25573
25574 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025575 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025576 {
25577 if (vim_ispathsep(*p)
25578 && p[1] == '.'
25579 && (p[2] == NUL
25580 || vim_ispathsep(p[2])
25581 || (p[2] == '.'
25582 && (p[3] == NUL || vim_ispathsep(p[3])))))
25583 break;
25584 }
25585
25586 /* FullName_save() is slow, don't use it when not needed. */
25587 if (*p != NUL || !vim_isAbsName(*fnamep))
25588 {
25589 *fnamep = FullName_save(*fnamep, *p != NUL);
25590 vim_free(*bufp); /* free any allocated file name */
25591 *bufp = *fnamep;
25592 if (*fnamep == NULL)
25593 return -1;
25594 }
25595
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025596#ifdef WIN3264
25597# if _WIN32_WINNT >= 0x0500
25598 if (vim_strchr(*fnamep, '~') != NULL)
25599 {
25600 /* Expand 8.3 filename to full path. Needed to make sure the same
25601 * file does not have two different names.
25602 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
25603 p = alloc(_MAX_PATH + 1);
25604 if (p != NULL)
25605 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025606 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020025607 {
25608 vim_free(*bufp);
25609 *bufp = *fnamep = p;
25610 }
25611 else
25612 vim_free(p);
25613 }
25614 }
25615# endif
25616#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025617 /* Append a path separator to a directory. */
25618 if (mch_isdir(*fnamep))
25619 {
25620 /* Make room for one or two extra characters. */
25621 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
25622 vim_free(*bufp); /* free any allocated file name */
25623 *bufp = *fnamep;
25624 if (*fnamep == NULL)
25625 return -1;
25626 add_pathsep(*fnamep);
25627 }
25628 }
25629
25630 /* ":." - path relative to the current directory */
25631 /* ":~" - path relative to the home directory */
25632 /* ":8" - shortname path - postponed till after */
25633 while (src[*usedlen] == ':'
25634 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
25635 {
25636 *usedlen += 2;
25637 if (c == '8')
25638 {
25639#ifdef WIN3264
25640 has_shortname = 1; /* Postpone this. */
25641#endif
25642 continue;
25643 }
25644 pbuf = NULL;
25645 /* Need full path first (use expand_env() to remove a "~/") */
25646 if (!has_fullname)
25647 {
25648 if (c == '.' && **fnamep == '~')
25649 p = pbuf = expand_env_save(*fnamep);
25650 else
25651 p = pbuf = FullName_save(*fnamep, FALSE);
25652 }
25653 else
25654 p = *fnamep;
25655
25656 has_fullname = 0;
25657
25658 if (p != NULL)
25659 {
25660 if (c == '.')
25661 {
25662 mch_dirname(dirname, MAXPATHL);
25663 s = shorten_fname(p, dirname);
25664 if (s != NULL)
25665 {
25666 *fnamep = s;
25667 if (pbuf != NULL)
25668 {
25669 vim_free(*bufp); /* free any allocated file name */
25670 *bufp = pbuf;
25671 pbuf = NULL;
25672 }
25673 }
25674 }
25675 else
25676 {
25677 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
25678 /* Only replace it when it starts with '~' */
25679 if (*dirname == '~')
25680 {
25681 s = vim_strsave(dirname);
25682 if (s != NULL)
25683 {
25684 *fnamep = s;
25685 vim_free(*bufp);
25686 *bufp = s;
25687 }
25688 }
25689 }
25690 vim_free(pbuf);
25691 }
25692 }
25693
25694 tail = gettail(*fnamep);
25695 *fnamelen = (int)STRLEN(*fnamep);
25696
25697 /* ":h" - head, remove "/file_name", can be repeated */
25698 /* Don't remove the first "/" or "c:\" */
25699 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
25700 {
25701 valid |= VALID_HEAD;
25702 *usedlen += 2;
25703 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025704 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025705 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025706 *fnamelen = (int)(tail - *fnamep);
25707#ifdef VMS
25708 if (*fnamelen > 0)
25709 *fnamelen += 1; /* the path separator is part of the path */
25710#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000025711 if (*fnamelen == 0)
25712 {
25713 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
25714 p = vim_strsave((char_u *)".");
25715 if (p == NULL)
25716 return -1;
25717 vim_free(*bufp);
25718 *bufp = *fnamep = tail = p;
25719 *fnamelen = 1;
25720 }
25721 else
25722 {
25723 while (tail > s && !after_pathsep(s, tail))
25724 mb_ptr_back(*fnamep, tail);
25725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025726 }
25727
25728 /* ":8" - shortname */
25729 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
25730 {
25731 *usedlen += 2;
25732#ifdef WIN3264
25733 has_shortname = 1;
25734#endif
25735 }
25736
25737#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025738 /*
25739 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025740 */
25741 if (has_shortname)
25742 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025743 /* Copy the string if it is shortened by :h and when it wasn't copied
25744 * yet, because we are going to change it in place. Avoids changing
25745 * the buffer name for "%:8". */
25746 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025747 {
25748 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020025749 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025750 return -1;
25751 vim_free(*bufp);
25752 *bufp = *fnamep = p;
25753 }
25754
25755 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020025756 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025757 if (!has_fullname && !vim_isAbsName(*fnamep))
25758 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025759 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025760 return -1;
25761 }
25762 else
25763 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025764 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025765
Bram Moolenaardc935552011-08-17 15:23:23 +020025766 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025767 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025768 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025769 return -1;
25770
25771 if (l == 0)
25772 {
Bram Moolenaardc935552011-08-17 15:23:23 +020025773 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025774 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025775 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025776 return -1;
25777 }
25778 *fnamelen = l;
25779 }
25780 }
25781#endif /* WIN3264 */
25782
25783 /* ":t" - tail, just the basename */
25784 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
25785 {
25786 *usedlen += 2;
25787 *fnamelen -= (int)(tail - *fnamep);
25788 *fnamep = tail;
25789 }
25790
25791 /* ":e" - extension, can be repeated */
25792 /* ":r" - root, without extension, can be repeated */
25793 while (src[*usedlen] == ':'
25794 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
25795 {
25796 /* find a '.' in the tail:
25797 * - for second :e: before the current fname
25798 * - otherwise: The last '.'
25799 */
25800 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
25801 s = *fnamep - 2;
25802 else
25803 s = *fnamep + *fnamelen - 1;
25804 for ( ; s > tail; --s)
25805 if (s[0] == '.')
25806 break;
25807 if (src[*usedlen + 1] == 'e') /* :e */
25808 {
25809 if (s > tail)
25810 {
25811 *fnamelen += (int)(*fnamep - (s + 1));
25812 *fnamep = s + 1;
25813#ifdef VMS
25814 /* cut version from the extension */
25815 s = *fnamep + *fnamelen - 1;
25816 for ( ; s > *fnamep; --s)
25817 if (s[0] == ';')
25818 break;
25819 if (s > *fnamep)
25820 *fnamelen = s - *fnamep;
25821#endif
25822 }
25823 else if (*fnamep <= tail)
25824 *fnamelen = 0;
25825 }
25826 else /* :r */
25827 {
25828 if (s > tail) /* remove one extension */
25829 *fnamelen = (int)(s - *fnamep);
25830 }
25831 *usedlen += 2;
25832 }
25833
25834 /* ":s?pat?foo?" - substitute */
25835 /* ":gs?pat?foo?" - global substitute */
25836 if (src[*usedlen] == ':'
25837 && (src[*usedlen + 1] == 's'
25838 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25839 {
25840 char_u *str;
25841 char_u *pat;
25842 char_u *sub;
25843 int sep;
25844 char_u *flags;
25845 int didit = FALSE;
25846
25847 flags = (char_u *)"";
25848 s = src + *usedlen + 2;
25849 if (src[*usedlen + 1] == 'g')
25850 {
25851 flags = (char_u *)"g";
25852 ++s;
25853 }
25854
25855 sep = *s++;
25856 if (sep)
25857 {
25858 /* find end of pattern */
25859 p = vim_strchr(s, sep);
25860 if (p != NULL)
25861 {
25862 pat = vim_strnsave(s, (int)(p - s));
25863 if (pat != NULL)
25864 {
25865 s = p + 1;
25866 /* find end of substitution */
25867 p = vim_strchr(s, sep);
25868 if (p != NULL)
25869 {
25870 sub = vim_strnsave(s, (int)(p - s));
25871 str = vim_strnsave(*fnamep, *fnamelen);
25872 if (sub != NULL && str != NULL)
25873 {
25874 *usedlen = (int)(p + 1 - src);
25875 s = do_string_sub(str, pat, sub, flags);
25876 if (s != NULL)
25877 {
25878 *fnamep = s;
25879 *fnamelen = (int)STRLEN(s);
25880 vim_free(*bufp);
25881 *bufp = s;
25882 didit = TRUE;
25883 }
25884 }
25885 vim_free(sub);
25886 vim_free(str);
25887 }
25888 vim_free(pat);
25889 }
25890 }
25891 /* after using ":s", repeat all the modifiers */
25892 if (didit)
25893 goto repeat;
25894 }
25895 }
25896
Bram Moolenaar26df0922014-02-23 23:39:13 +010025897 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25898 {
25899 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25900 if (p == NULL)
25901 return -1;
25902 vim_free(*bufp);
25903 *bufp = *fnamep = p;
25904 *fnamelen = (int)STRLEN(p);
25905 *usedlen += 2;
25906 }
25907
Bram Moolenaar071d4272004-06-13 20:20:40 +000025908 return valid;
25909}
25910
25911/*
25912 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25913 * "flags" can be "g" to do a global substitute.
25914 * Returns an allocated string, NULL for error.
25915 */
25916 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025917do_string_sub(
25918 char_u *str,
25919 char_u *pat,
25920 char_u *sub,
25921 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025922{
25923 int sublen;
25924 regmatch_T regmatch;
25925 int i;
25926 int do_all;
25927 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025928 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025929 garray_T ga;
25930 char_u *ret;
25931 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025932 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025933
25934 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25935 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025936 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025937
25938 ga_init2(&ga, 1, 200);
25939
25940 do_all = (flags[0] == 'g');
25941
25942 regmatch.rm_ic = p_ic;
25943 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25944 if (regmatch.regprog != NULL)
25945 {
25946 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025947 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025948 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25949 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025950 /* Skip empty match except for first match. */
25951 if (regmatch.startp[0] == regmatch.endp[0])
25952 {
25953 if (zero_width == regmatch.startp[0])
25954 {
25955 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025956 i = MB_PTR2LEN(tail);
25957 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25958 (size_t)i);
25959 ga.ga_len += i;
25960 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025961 continue;
25962 }
25963 zero_width = regmatch.startp[0];
25964 }
25965
Bram Moolenaar071d4272004-06-13 20:20:40 +000025966 /*
25967 * Get some space for a temporary buffer to do the substitution
25968 * into. It will contain:
25969 * - The text up to where the match is.
25970 * - The substituted text.
25971 * - The text after the match.
25972 */
25973 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025974 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025975 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25976 {
25977 ga_clear(&ga);
25978 break;
25979 }
25980
25981 /* copy the text up to where the match is */
25982 i = (int)(regmatch.startp[0] - tail);
25983 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25984 /* add the substituted text */
25985 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25986 + ga.ga_len + i, TRUE, TRUE, FALSE);
25987 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025988 tail = regmatch.endp[0];
25989 if (*tail == NUL)
25990 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025991 if (!do_all)
25992 break;
25993 }
25994
25995 if (ga.ga_data != NULL)
25996 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25997
Bram Moolenaar473de612013-06-08 18:19:48 +020025998 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025999 }
26000
26001 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26002 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026003 if (p_cpo == empty_option)
26004 p_cpo = save_cpo;
26005 else
26006 /* Darn, evaluating {sub} expression changed the value. */
26007 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026008
26009 return ret;
26010}
26011
26012#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */