blob: 070485f19e4e6e02034c9b20661c8e7c50a47675 [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 Moolenaar5f436fc2016-03-22 22:34:03 +0100212/* List heads for garbage collection. Although there can be a reference loop
213 * from partial to dict to partial, we don't need to keep track of the partial,
214 * since it will get freed when the dict is unused and gets freed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000215static dict_T *first_dict = NULL; /* list of all dicts */
216static list_T *first_list = NULL; /* list of all lists */
217
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000218/* From user function to hashitem and back. */
219static ufunc_T dumuf;
220#define UF2HIKEY(fp) ((fp)->uf_name)
221#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
222#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
223
224#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
225#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226
Bram Moolenaar33570922005-01-25 22:26:29 +0000227#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
228#define VAR_SHORT_LEN 20 /* short variable name length */
229#define FIXVAR_CNT 12 /* number of fixed variables */
230
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000232typedef struct funccall_S funccall_T;
233
234struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235{
236 ufunc_T *func; /* function being called */
237 int linenr; /* next line to be executed */
238 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000239 struct /* fixed variables for arguments */
240 {
241 dictitem_T var; /* variable (without room for name) */
242 char_u room[VAR_SHORT_LEN]; /* room for the name */
243 } fixvar[FIXVAR_CNT];
244 dict_T l_vars; /* l: local function variables */
245 dictitem_T l_vars_var; /* variable for l: scope */
246 dict_T l_avars; /* a: argument variables */
247 dictitem_T l_avars_var; /* variable for a: scope */
248 list_T l_varlist; /* list for a:000 */
249 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
250 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 linenr_T breakpoint; /* next line with breakpoint or zero */
252 int dbg_tick; /* debug_tick when breakpoint was set */
253 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000254#ifdef FEAT_PROFILE
255 proftime_T prof_child; /* time spent in a child */
256#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000257 funccall_T *caller; /* calling function or NULL */
258};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259
260/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000261 * Info used by a ":for" loop.
262 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000263typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000264{
265 int fi_semicolon; /* TRUE if ending in '; var]' */
266 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000267 listwatch_T fi_lw; /* keep an eye on the item used. */
268 list_T *fi_list; /* list being used */
269} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000270
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000271/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000272 * Struct used by trans_function_name()
273 */
274typedef struct
275{
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000277 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000278 dictitem_T *fd_di; /* Dictionary item used */
279} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000280
Bram Moolenaara7043832005-01-21 11:56:39 +0000281
282/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000283 * Array to hold the value of v: variables.
284 * The value is in a dictitem, so that it can also be used in the v: scope.
285 * The reason to use this table anyway is for very quick access to the
286 * variables with the VV_ defines.
287 */
288#include "version.h"
289
290/* values for vv_flags: */
291#define VV_COMPAT 1 /* compatible, also used without "v:" */
292#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000293#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100295#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
Bram Moolenaar33570922005-01-25 22:26:29 +0000296
297static struct vimvar
298{
299 char *vv_name; /* name of variable, without v: */
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100300 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000301 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
302} vimvars[VV_LEN] =
303{
304 /*
305 * The order here must match the VV_ defines in vim.h!
306 * Initializing a union does not work, leave tv.vval empty to get zero's.
307 */
308 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
309 {VV_NAME("count1", VAR_NUMBER), VV_RO},
310 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
311 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
312 {VV_NAME("warningmsg", VAR_STRING), 0},
313 {VV_NAME("statusmsg", VAR_STRING), 0},
314 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
315 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
316 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
317 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
318 {VV_NAME("termresponse", VAR_STRING), VV_RO},
319 {VV_NAME("fname", VAR_STRING), VV_RO},
320 {VV_NAME("lang", VAR_STRING), VV_RO},
321 {VV_NAME("lc_time", VAR_STRING), VV_RO},
322 {VV_NAME("ctype", VAR_STRING), VV_RO},
323 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
324 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
325 {VV_NAME("fname_in", VAR_STRING), VV_RO},
326 {VV_NAME("fname_out", VAR_STRING), VV_RO},
327 {VV_NAME("fname_new", VAR_STRING), VV_RO},
328 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
329 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
330 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
331 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
332 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
333 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
334 {VV_NAME("progname", VAR_STRING), VV_RO},
335 {VV_NAME("servername", VAR_STRING), VV_RO},
336 {VV_NAME("dying", VAR_NUMBER), VV_RO},
337 {VV_NAME("exception", VAR_STRING), VV_RO},
338 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
339 {VV_NAME("register", VAR_STRING), VV_RO},
340 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
341 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000342 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
343 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000344 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000345 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
346 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000347 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000352 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000353 {VV_NAME("swapname", VAR_STRING), VV_RO},
354 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000355 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200356 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000357 {VV_NAME("mouse_win", VAR_NUMBER), 0},
358 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
359 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000360 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000361 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100362 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000363 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200364 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200365 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200366 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200367 {VV_NAME("option_new", VAR_STRING), VV_RO},
368 {VV_NAME("option_old", VAR_STRING), VV_RO},
369 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100370 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100371 {VV_NAME("false", VAR_SPECIAL), VV_RO},
372 {VV_NAME("true", VAR_SPECIAL), VV_RO},
373 {VV_NAME("null", VAR_SPECIAL), VV_RO},
374 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100375 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000376};
377
378/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000379#define vv_type vv_di.di_tv.v_type
380#define vv_nr vv_di.di_tv.vval.v_number
381#define vv_float vv_di.di_tv.vval.v_float
382#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000383#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200384#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000385#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000386
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200387static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000388#define vimvarht vimvardict.dv_hashtab
389
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100390static void prepare_vimvar(int idx, typval_T *save_tv);
391static void restore_vimvar(int idx, typval_T *save_tv);
392static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
393static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
394static char_u *skip_var_one(char_u *arg);
395static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
396static void list_glob_vars(int *first);
397static void list_buf_vars(int *first);
398static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000399#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100400static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000401#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100402static void list_vim_vars(int *first);
403static void list_script_vars(int *first);
404static void list_func_vars(int *first);
405static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
406static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
407static int check_changedtick(char_u *arg);
408static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
409static void clear_lval(lval_T *lp);
410static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
411static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
412static void list_fix_watch(list_T *l, listitem_T *item);
413static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
414static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
415static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
416static void item_lock(typval_T *tv, int deep, int lock);
417static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000418
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100419static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
420static int eval1(char_u **arg, typval_T *rettv, int evaluate);
421static int eval2(char_u **arg, typval_T *rettv, int evaluate);
422static int eval3(char_u **arg, typval_T *rettv, int evaluate);
423static int eval4(char_u **arg, typval_T *rettv, int evaluate);
424static int eval5(char_u **arg, typval_T *rettv, int evaluate);
425static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
426static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000427
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100428static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
429static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
430static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
431static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
432static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
433static long list_len(list_T *l);
434static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
435static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
436static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
437static long list_find_nr(list_T *l, long idx, int *errorp);
438static long list_idx_of_item(list_T *l, listitem_T *item);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100439static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
440static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
441static list_T *list_copy(list_T *orig, int deep, int copyID);
442static char_u *list2string(typval_T *tv, int copyID);
443static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap);
444static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo, int copyID);
445static int free_unref_items(int copyID);
446static dictitem_T *dictitem_copy(dictitem_T *org);
447static void dictitem_remove(dict_T *dict, dictitem_T *item);
448static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
449static long dict_len(dict_T *d);
450static char_u *dict2string(typval_T *tv, int copyID);
451static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
452static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
453static char_u *tv2string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
454static char_u *string_quote(char_u *str, int function);
455static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
456static int find_internal_func(char_u *name);
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100457static char_u *deref_func_name(char_u *name, int *lenp, partial_T **partial, int no_autoload);
458static int get_func_tv(char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, partial_T *partial, dict_T *selfdict);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100459static void emsg_funcname(char *ermsg, char_u *name);
460static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000461
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000462#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100463static void f_abs(typval_T *argvars, typval_T *rettv);
464static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000465#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100466static void f_add(typval_T *argvars, typval_T *rettv);
467static void f_alloc_fail(typval_T *argvars, typval_T *rettv);
468static void f_and(typval_T *argvars, typval_T *rettv);
469static void f_append(typval_T *argvars, typval_T *rettv);
470static void f_argc(typval_T *argvars, typval_T *rettv);
471static void f_argidx(typval_T *argvars, typval_T *rettv);
472static void f_arglistid(typval_T *argvars, typval_T *rettv);
473static void f_argv(typval_T *argvars, typval_T *rettv);
474static void f_assert_equal(typval_T *argvars, typval_T *rettv);
475static void f_assert_exception(typval_T *argvars, typval_T *rettv);
476static void f_assert_fails(typval_T *argvars, typval_T *rettv);
477static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaarea6553b2016-03-27 15:13:38 +0200478static void f_assert_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +0200479static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
480static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100481static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000482#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100483static void f_asin(typval_T *argvars, typval_T *rettv);
484static void f_atan(typval_T *argvars, typval_T *rettv);
485static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000486#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100487static void f_browse(typval_T *argvars, typval_T *rettv);
488static void f_browsedir(typval_T *argvars, typval_T *rettv);
489static void f_bufexists(typval_T *argvars, typval_T *rettv);
490static void f_buflisted(typval_T *argvars, typval_T *rettv);
491static void f_bufloaded(typval_T *argvars, typval_T *rettv);
492static void f_bufname(typval_T *argvars, typval_T *rettv);
493static void f_bufnr(typval_T *argvars, typval_T *rettv);
494static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
495static void f_byte2line(typval_T *argvars, typval_T *rettv);
496static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
497static void f_byteidx(typval_T *argvars, typval_T *rettv);
498static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
499static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000500#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100501static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000502#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100503#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100504static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100505static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
506static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100507static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100508static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100509static void f_ch_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100510static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100511static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
512static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100513static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100514static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100515static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
516static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100517static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100518static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100519#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100520static void f_changenr(typval_T *argvars, typval_T *rettv);
521static void f_char2nr(typval_T *argvars, typval_T *rettv);
522static void f_cindent(typval_T *argvars, typval_T *rettv);
523static void f_clearmatches(typval_T *argvars, typval_T *rettv);
524static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000525#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100526static void f_complete(typval_T *argvars, typval_T *rettv);
527static void f_complete_add(typval_T *argvars, typval_T *rettv);
528static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000529#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100530static void f_confirm(typval_T *argvars, typval_T *rettv);
531static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000532#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100533static void f_cos(typval_T *argvars, typval_T *rettv);
534static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000535#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100536static void f_count(typval_T *argvars, typval_T *rettv);
537static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
538static void f_cursor(typval_T *argsvars, typval_T *rettv);
539static void f_deepcopy(typval_T *argvars, typval_T *rettv);
540static void f_delete(typval_T *argvars, typval_T *rettv);
541static void f_did_filetype(typval_T *argvars, typval_T *rettv);
542static void f_diff_filler(typval_T *argvars, typval_T *rettv);
543static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +0100544static void f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100545static void f_empty(typval_T *argvars, typval_T *rettv);
546static void f_escape(typval_T *argvars, typval_T *rettv);
547static void f_eval(typval_T *argvars, typval_T *rettv);
548static void f_eventhandler(typval_T *argvars, typval_T *rettv);
549static void f_executable(typval_T *argvars, typval_T *rettv);
550static void f_exepath(typval_T *argvars, typval_T *rettv);
551static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200552#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100553static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200554#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100555static void f_expand(typval_T *argvars, typval_T *rettv);
556static void f_extend(typval_T *argvars, typval_T *rettv);
557static void f_feedkeys(typval_T *argvars, typval_T *rettv);
558static void f_filereadable(typval_T *argvars, typval_T *rettv);
559static void f_filewritable(typval_T *argvars, typval_T *rettv);
560static void f_filter(typval_T *argvars, typval_T *rettv);
561static void f_finddir(typval_T *argvars, typval_T *rettv);
562static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000563#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100564static void f_float2nr(typval_T *argvars, typval_T *rettv);
565static void f_floor(typval_T *argvars, typval_T *rettv);
566static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000567#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100568static void f_fnameescape(typval_T *argvars, typval_T *rettv);
569static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
570static void f_foldclosed(typval_T *argvars, typval_T *rettv);
571static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
572static void f_foldlevel(typval_T *argvars, typval_T *rettv);
573static void f_foldtext(typval_T *argvars, typval_T *rettv);
574static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
575static void f_foreground(typval_T *argvars, typval_T *rettv);
576static void f_function(typval_T *argvars, typval_T *rettv);
577static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
578static void f_get(typval_T *argvars, typval_T *rettv);
579static void f_getbufline(typval_T *argvars, typval_T *rettv);
580static void f_getbufvar(typval_T *argvars, typval_T *rettv);
581static void f_getchar(typval_T *argvars, typval_T *rettv);
582static void f_getcharmod(typval_T *argvars, typval_T *rettv);
583static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
584static void f_getcmdline(typval_T *argvars, typval_T *rettv);
585static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
586static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
587static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
588static void f_getcwd(typval_T *argvars, typval_T *rettv);
589static void f_getfontname(typval_T *argvars, typval_T *rettv);
590static void f_getfperm(typval_T *argvars, typval_T *rettv);
591static void f_getfsize(typval_T *argvars, typval_T *rettv);
592static void f_getftime(typval_T *argvars, typval_T *rettv);
593static void f_getftype(typval_T *argvars, typval_T *rettv);
594static void f_getline(typval_T *argvars, typval_T *rettv);
595static void f_getmatches(typval_T *argvars, typval_T *rettv);
596static void f_getpid(typval_T *argvars, typval_T *rettv);
597static void f_getcurpos(typval_T *argvars, typval_T *rettv);
598static void f_getpos(typval_T *argvars, typval_T *rettv);
599static void f_getqflist(typval_T *argvars, typval_T *rettv);
600static void f_getreg(typval_T *argvars, typval_T *rettv);
601static void f_getregtype(typval_T *argvars, typval_T *rettv);
602static void f_gettabvar(typval_T *argvars, typval_T *rettv);
603static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
604static void f_getwinposx(typval_T *argvars, typval_T *rettv);
605static void f_getwinposy(typval_T *argvars, typval_T *rettv);
606static void f_getwinvar(typval_T *argvars, typval_T *rettv);
607static void f_glob(typval_T *argvars, typval_T *rettv);
608static void f_globpath(typval_T *argvars, typval_T *rettv);
609static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
610static void f_has(typval_T *argvars, typval_T *rettv);
611static void f_has_key(typval_T *argvars, typval_T *rettv);
612static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
613static void f_hasmapto(typval_T *argvars, typval_T *rettv);
614static void f_histadd(typval_T *argvars, typval_T *rettv);
615static void f_histdel(typval_T *argvars, typval_T *rettv);
616static void f_histget(typval_T *argvars, typval_T *rettv);
617static void f_histnr(typval_T *argvars, typval_T *rettv);
618static void f_hlID(typval_T *argvars, typval_T *rettv);
619static void f_hlexists(typval_T *argvars, typval_T *rettv);
620static void f_hostname(typval_T *argvars, typval_T *rettv);
621static void f_iconv(typval_T *argvars, typval_T *rettv);
622static void f_indent(typval_T *argvars, typval_T *rettv);
623static void f_index(typval_T *argvars, typval_T *rettv);
624static void f_input(typval_T *argvars, typval_T *rettv);
625static void f_inputdialog(typval_T *argvars, typval_T *rettv);
626static void f_inputlist(typval_T *argvars, typval_T *rettv);
627static void f_inputrestore(typval_T *argvars, typval_T *rettv);
628static void f_inputsave(typval_T *argvars, typval_T *rettv);
629static void f_inputsecret(typval_T *argvars, typval_T *rettv);
630static void f_insert(typval_T *argvars, typval_T *rettv);
631static void f_invert(typval_T *argvars, typval_T *rettv);
632static void f_isdirectory(typval_T *argvars, typval_T *rettv);
633static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100634#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
635static void f_isnan(typval_T *argvars, typval_T *rettv);
636#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100637static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100638#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100639static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8950a562016-03-12 15:22:55 +0100640static void f_job_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar65edff82016-02-21 16:40:11 +0100641static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100642static void f_job_start(typval_T *argvars, typval_T *rettv);
643static void f_job_stop(typval_T *argvars, typval_T *rettv);
644static void f_job_status(typval_T *argvars, typval_T *rettv);
645#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100646static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100647static void f_js_decode(typval_T *argvars, typval_T *rettv);
648static void f_js_encode(typval_T *argvars, typval_T *rettv);
649static void f_json_decode(typval_T *argvars, typval_T *rettv);
650static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100651static void f_keys(typval_T *argvars, typval_T *rettv);
652static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
653static void f_len(typval_T *argvars, typval_T *rettv);
654static void f_libcall(typval_T *argvars, typval_T *rettv);
655static void f_libcallnr(typval_T *argvars, typval_T *rettv);
656static void f_line(typval_T *argvars, typval_T *rettv);
657static void f_line2byte(typval_T *argvars, typval_T *rettv);
658static void f_lispindent(typval_T *argvars, typval_T *rettv);
659static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000660#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100661static void f_log(typval_T *argvars, typval_T *rettv);
662static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000663#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200664#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100665static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200666#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100667static void f_map(typval_T *argvars, typval_T *rettv);
668static void f_maparg(typval_T *argvars, typval_T *rettv);
669static void f_mapcheck(typval_T *argvars, typval_T *rettv);
670static void f_match(typval_T *argvars, typval_T *rettv);
671static void f_matchadd(typval_T *argvars, typval_T *rettv);
672static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
673static void f_matcharg(typval_T *argvars, typval_T *rettv);
674static void f_matchdelete(typval_T *argvars, typval_T *rettv);
675static void f_matchend(typval_T *argvars, typval_T *rettv);
676static void f_matchlist(typval_T *argvars, typval_T *rettv);
677static void f_matchstr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +0200678static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100679static void f_max(typval_T *argvars, typval_T *rettv);
680static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000681#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100682static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000683#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100684static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100685#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100686static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100687#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100688static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
689static void f_nr2char(typval_T *argvars, typval_T *rettv);
690static void f_or(typval_T *argvars, typval_T *rettv);
691static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100692#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100693static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100694#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000695#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100696static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000697#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100698static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
699static void f_printf(typval_T *argvars, typval_T *rettv);
700static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200701#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100702static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200703#endif
704#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100705static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200706#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100707static void f_range(typval_T *argvars, typval_T *rettv);
708static void f_readfile(typval_T *argvars, typval_T *rettv);
709static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100710#ifdef FEAT_FLOAT
711static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
712#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100713static void f_reltimestr(typval_T *argvars, typval_T *rettv);
714static void f_remote_expr(typval_T *argvars, typval_T *rettv);
715static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
716static void f_remote_peek(typval_T *argvars, typval_T *rettv);
717static void f_remote_read(typval_T *argvars, typval_T *rettv);
718static void f_remote_send(typval_T *argvars, typval_T *rettv);
719static void f_remove(typval_T *argvars, typval_T *rettv);
720static void f_rename(typval_T *argvars, typval_T *rettv);
721static void f_repeat(typval_T *argvars, typval_T *rettv);
722static void f_resolve(typval_T *argvars, typval_T *rettv);
723static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000724#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100725static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000726#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100727static void f_screenattr(typval_T *argvars, typval_T *rettv);
728static void f_screenchar(typval_T *argvars, typval_T *rettv);
729static void f_screencol(typval_T *argvars, typval_T *rettv);
730static void f_screenrow(typval_T *argvars, typval_T *rettv);
731static void f_search(typval_T *argvars, typval_T *rettv);
732static void f_searchdecl(typval_T *argvars, typval_T *rettv);
733static void f_searchpair(typval_T *argvars, typval_T *rettv);
734static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
735static void f_searchpos(typval_T *argvars, typval_T *rettv);
736static void f_server2client(typval_T *argvars, typval_T *rettv);
737static void f_serverlist(typval_T *argvars, typval_T *rettv);
738static void f_setbufvar(typval_T *argvars, typval_T *rettv);
739static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
740static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100741static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100742static void f_setline(typval_T *argvars, typval_T *rettv);
743static void f_setloclist(typval_T *argvars, typval_T *rettv);
744static void f_setmatches(typval_T *argvars, typval_T *rettv);
745static void f_setpos(typval_T *argvars, typval_T *rettv);
746static void f_setqflist(typval_T *argvars, typval_T *rettv);
747static void f_setreg(typval_T *argvars, typval_T *rettv);
748static void f_settabvar(typval_T *argvars, typval_T *rettv);
749static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
750static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100751#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100752static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100753#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100754static void f_shellescape(typval_T *argvars, typval_T *rettv);
755static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
756static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000757#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100758static void f_sin(typval_T *argvars, typval_T *rettv);
759static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000760#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100761static void f_sort(typval_T *argvars, typval_T *rettv);
762static void f_soundfold(typval_T *argvars, typval_T *rettv);
763static void f_spellbadword(typval_T *argvars, typval_T *rettv);
764static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
765static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000766#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100767static void f_sqrt(typval_T *argvars, typval_T *rettv);
768static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000769#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100770static void f_str2nr(typval_T *argvars, typval_T *rettv);
771static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000772#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100773static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000774#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100775static void f_stridx(typval_T *argvars, typval_T *rettv);
776static void f_string(typval_T *argvars, typval_T *rettv);
777static void f_strlen(typval_T *argvars, typval_T *rettv);
778static void f_strpart(typval_T *argvars, typval_T *rettv);
779static void f_strridx(typval_T *argvars, typval_T *rettv);
780static void f_strtrans(typval_T *argvars, typval_T *rettv);
781static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
782static void f_strwidth(typval_T *argvars, typval_T *rettv);
783static void f_submatch(typval_T *argvars, typval_T *rettv);
784static void f_substitute(typval_T *argvars, typval_T *rettv);
785static void f_synID(typval_T *argvars, typval_T *rettv);
786static void f_synIDattr(typval_T *argvars, typval_T *rettv);
787static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
788static void f_synstack(typval_T *argvars, typval_T *rettv);
789static void f_synconcealed(typval_T *argvars, typval_T *rettv);
790static void f_system(typval_T *argvars, typval_T *rettv);
791static void f_systemlist(typval_T *argvars, typval_T *rettv);
792static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
793static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
794static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
795static void f_taglist(typval_T *argvars, typval_T *rettv);
796static void f_tagfiles(typval_T *argvars, typval_T *rettv);
797static void f_tempname(typval_T *argvars, typval_T *rettv);
798static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200799#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100800static void f_tan(typval_T *argvars, typval_T *rettv);
801static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200802#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +0100803#ifdef FEAT_TIMERS
804static void f_timer_start(typval_T *argvars, typval_T *rettv);
805static void f_timer_stop(typval_T *argvars, typval_T *rettv);
806#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100807static void f_tolower(typval_T *argvars, typval_T *rettv);
808static void f_toupper(typval_T *argvars, typval_T *rettv);
809static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000810#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100811static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000812#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100813static void f_type(typval_T *argvars, typval_T *rettv);
814static void f_undofile(typval_T *argvars, typval_T *rettv);
815static void f_undotree(typval_T *argvars, typval_T *rettv);
816static void f_uniq(typval_T *argvars, typval_T *rettv);
817static void f_values(typval_T *argvars, typval_T *rettv);
818static void f_virtcol(typval_T *argvars, typval_T *rettv);
819static void f_visualmode(typval_T *argvars, typval_T *rettv);
820static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +0100821static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar86edef62016-03-13 18:07:30 +0100822static void f_win_getid(typval_T *argvars, typval_T *rettv);
823static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
824static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
825static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100826static void f_winbufnr(typval_T *argvars, typval_T *rettv);
827static void f_wincol(typval_T *argvars, typval_T *rettv);
828static void f_winheight(typval_T *argvars, typval_T *rettv);
829static void f_winline(typval_T *argvars, typval_T *rettv);
830static void f_winnr(typval_T *argvars, typval_T *rettv);
831static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
832static void f_winrestview(typval_T *argvars, typval_T *rettv);
833static void f_winsaveview(typval_T *argvars, typval_T *rettv);
834static void f_winwidth(typval_T *argvars, typval_T *rettv);
835static void f_writefile(typval_T *argvars, typval_T *rettv);
836static void f_wordcount(typval_T *argvars, typval_T *rettv);
837static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000838
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100839static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
840static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
841static int get_env_len(char_u **arg);
842static int get_id_len(char_u **arg);
843static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
844static 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 +0000845#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
846#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
847 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100848static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
849static int eval_isnamec(int c);
850static int eval_isnamec1(int c);
851static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
852static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100853static typval_T *alloc_string_tv(char_u *string);
854static void init_tv(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100855#ifdef FEAT_FLOAT
856static float_T get_tv_float(typval_T *varp);
857#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100858static linenr_T get_tv_lnum(typval_T *argvars);
859static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100860static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
861static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
862static hashtab_T *find_var_ht(char_u *name, char_u **varname);
863static funccall_T *get_funccal(void);
864static void vars_clear_ext(hashtab_T *ht, int free_val);
865static void delete_var(hashtab_T *ht, hashitem_T *hi);
866static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
867static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
868static void set_var(char_u *name, typval_T *varp, int copy);
869static int var_check_ro(int flags, char_u *name, int use_gettext);
870static int var_check_fixed(int flags, char_u *name, int use_gettext);
871static int var_check_func_name(char_u *name, int new_var);
872static int valid_varname(char_u *varname);
873static int tv_check_lock(int lock, char_u *name, int use_gettext);
874static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
875static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar65639032016-03-16 21:40:30 +0100876static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd, partial_T **partial);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100877static int eval_fname_script(char_u *p);
878static int eval_fname_sid(char_u *p);
879static void list_func_head(ufunc_T *fp, int indent);
880static ufunc_T *find_func(char_u *name);
881static int function_exists(char_u *name);
882static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000883#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100884static void func_do_profile(ufunc_T *fp);
885static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
886static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000887static int
888# ifdef __BORLANDC__
889 _RTLENTRYF
890# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100891 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000892static int
893# ifdef __BORLANDC__
894 _RTLENTRYF
895# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100896 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000897#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100898static int script_autoload(char_u *name, int reload);
899static char_u *autoload_name(char_u *name);
900static void cat_func_name(char_u *buf, ufunc_T *fp);
901static void func_free(ufunc_T *fp);
902static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
903static int can_free_funccal(funccall_T *fc, int copyID) ;
904static void free_funccal(funccall_T *fc, int free_val);
905static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
906static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
907static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
908static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
909static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
910static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
911static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
912static int write_list(FILE *fd, list_T *list, int binary);
913static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000914
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200915
916#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100917static int compare_func_name(const void *s1, const void *s2);
918static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200919#endif
920
Bram Moolenaar33570922005-01-25 22:26:29 +0000921/*
922 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000923 */
924 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100925eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000926{
Bram Moolenaar33570922005-01-25 22:26:29 +0000927 int i;
928 struct vimvar *p;
929
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200930 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
931 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200932 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000933 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000934 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000935
936 for (i = 0; i < VV_LEN; ++i)
937 {
938 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200939 if (STRLEN(p->vv_name) > 16)
940 {
941 EMSG("INTERNAL: name too long, increase size of dictitem16_T");
942 getout(1);
943 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000944 STRCPY(p->vv_di.di_key, p->vv_name);
945 if (p->vv_flags & VV_RO)
946 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
947 else if (p->vv_flags & VV_RO_SBX)
948 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
949 else
950 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000951
952 /* add to v: scope dict, unless the value is not always available */
953 if (p->vv_type != VAR_UNKNOWN)
954 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000955 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000956 /* add to compat scope dict */
957 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000958 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100959 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
960
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000961 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100962 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200963 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100964 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100965
966 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
967 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
968 set_vim_var_nr(VV_NONE, VVAL_NONE);
969 set_vim_var_nr(VV_NULL, VVAL_NULL);
970
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200971 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200972
973#ifdef EBCDIC
974 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100975 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200976 */
977 sortFunctions();
978#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000979}
980
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000981#if defined(EXITFREE) || defined(PROTO)
982 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100983eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000984{
985 int i;
986 struct vimvar *p;
987
988 for (i = 0; i < VV_LEN; ++i)
989 {
990 p = &vimvars[i];
991 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000992 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000993 vim_free(p->vv_str);
994 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000995 }
996 else if (p->vv_di.di_tv.v_type == VAR_LIST)
997 {
998 list_unref(p->vv_list);
999 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +00001000 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001001 }
1002 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +00001003 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001004 hash_clear(&compat_hashtab);
1005
Bram Moolenaard9fba312005-06-26 22:34:35 +00001006 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001007# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02001008 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001009# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001010
1011 /* global variables */
1012 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +00001013
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001014 /* autoloaded script names */
1015 ga_clear_strings(&ga_loaded);
1016
Bram Moolenaarcca74132013-09-25 21:00:28 +02001017 /* Script-local variables. First clear all the variables and in a second
1018 * loop free the scriptvar_T, because a variable in one script might hold
1019 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001020 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001021 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001022 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001023 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001024 ga_clear(&ga_scripts);
1025
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001026 /* unreferenced lists and dicts */
1027 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001028
1029 /* functions */
1030 free_all_functions();
1031 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001032}
1033#endif
1034
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001035/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 * Return the name of the executed function.
1037 */
1038 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001039func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001041 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042}
1043
1044/*
1045 * Return the address holding the next breakpoint line for a funccall cookie.
1046 */
1047 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001048func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049{
Bram Moolenaar33570922005-01-25 22:26:29 +00001050 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051}
1052
1053/*
1054 * Return the address holding the debug tick for a funccall cookie.
1055 */
1056 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001057func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058{
Bram Moolenaar33570922005-01-25 22:26:29 +00001059 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060}
1061
1062/*
1063 * Return the nesting level for a funccall cookie.
1064 */
1065 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001066func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067{
Bram Moolenaar33570922005-01-25 22:26:29 +00001068 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069}
1070
1071/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001072funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001074/* pointer to list of previously used funccal, still around because some
1075 * item in it is still being used. */
1076funccall_T *previous_funccal = NULL;
1077
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078/*
1079 * Return TRUE when a function was ended by a ":return" command.
1080 */
1081 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001082current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083{
1084 return current_funccal->returned;
1085}
1086
1087
1088/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 * Set an internal variable to a string value. Creates the variable if it does
1090 * not already exist.
1091 */
1092 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001093set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001095 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001096 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097
1098 val = vim_strsave(value);
1099 if (val != NULL)
1100 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001101 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001102 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001104 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001105 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 }
1107 }
1108}
1109
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001111static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001112static char_u *redir_endp = NULL;
1113static char_u *redir_varname = NULL;
1114
1115/*
1116 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001117 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001118 * Returns OK if successfully completed the setup. FAIL otherwise.
1119 */
1120 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001121var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001122{
1123 int save_emsg;
1124 int err;
1125 typval_T tv;
1126
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001127 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001128 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129 {
1130 EMSG(_(e_invarg));
1131 return FAIL;
1132 }
1133
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001134 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001135 redir_varname = vim_strsave(name);
1136 if (redir_varname == NULL)
1137 return FAIL;
1138
1139 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1140 if (redir_lval == NULL)
1141 {
1142 var_redir_stop();
1143 return FAIL;
1144 }
1145
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001146 /* The output is stored in growarray "redir_ga" until redirection ends. */
1147 ga_init2(&redir_ga, (int)sizeof(char), 500);
1148
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001149 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001150 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001151 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001152 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1153 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001154 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001155 if (redir_endp != NULL && *redir_endp != NUL)
1156 /* Trailing characters are present after the variable name */
1157 EMSG(_(e_trailing));
1158 else
1159 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001160 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001161 var_redir_stop();
1162 return FAIL;
1163 }
1164
1165 /* check if we can write to the variable: set it to or append an empty
1166 * string */
1167 save_emsg = did_emsg;
1168 did_emsg = FALSE;
1169 tv.v_type = VAR_STRING;
1170 tv.vval.v_string = (char_u *)"";
1171 if (append)
1172 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1173 else
1174 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001175 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001176 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001177 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001178 if (err)
1179 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001180 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001181 var_redir_stop();
1182 return FAIL;
1183 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001184
1185 return OK;
1186}
1187
1188/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001189 * Append "value[value_len]" to the variable set by var_redir_start().
1190 * The actual appending is postponed until redirection ends, because the value
1191 * appended may in fact be the string we write to, changing it may cause freed
1192 * memory to be used:
1193 * :redir => foo
1194 * :let foo
1195 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001196 */
1197 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001198var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001199{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001200 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001201
1202 if (redir_lval == NULL)
1203 return;
1204
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001205 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001206 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001207 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001208 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001209
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001210 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001211 {
1212 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001213 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001214 }
1215 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001216 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001217}
1218
1219/*
1220 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001221 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001222 */
1223 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001224var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001225{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001226 typval_T tv;
1227
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001228 if (redir_lval != NULL)
1229 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001230 /* If there was no error: assign the text to the variable. */
1231 if (redir_endp != NULL)
1232 {
1233 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1234 tv.v_type = VAR_STRING;
1235 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001236 /* Call get_lval() again, if it's inside a Dict or List it may
1237 * have changed. */
1238 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001239 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001240 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1241 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1242 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001243 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001244
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001245 /* free the collected output */
1246 vim_free(redir_ga.ga_data);
1247 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001248
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001249 vim_free(redir_lval);
1250 redir_lval = NULL;
1251 }
1252 vim_free(redir_varname);
1253 redir_varname = NULL;
1254}
1255
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256# if defined(FEAT_MBYTE) || defined(PROTO)
1257 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001258eval_charconvert(
1259 char_u *enc_from,
1260 char_u *enc_to,
1261 char_u *fname_from,
1262 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263{
1264 int err = FALSE;
1265
1266 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1267 set_vim_var_string(VV_CC_TO, enc_to, -1);
1268 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1269 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1270 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1271 err = TRUE;
1272 set_vim_var_string(VV_CC_FROM, NULL, -1);
1273 set_vim_var_string(VV_CC_TO, NULL, -1);
1274 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1275 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1276
1277 if (err)
1278 return FAIL;
1279 return OK;
1280}
1281# endif
1282
1283# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1284 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001285eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286{
1287 int err = FALSE;
1288
1289 set_vim_var_string(VV_FNAME_IN, fname, -1);
1290 set_vim_var_string(VV_CMDARG, args, -1);
1291 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1292 err = TRUE;
1293 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1294 set_vim_var_string(VV_CMDARG, NULL, -1);
1295
1296 if (err)
1297 {
1298 mch_remove(fname);
1299 return FAIL;
1300 }
1301 return OK;
1302}
1303# endif
1304
1305# if defined(FEAT_DIFF) || defined(PROTO)
1306 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001307eval_diff(
1308 char_u *origfile,
1309 char_u *newfile,
1310 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311{
1312 int err = FALSE;
1313
1314 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1315 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1316 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1317 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1318 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1319 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1320 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1321}
1322
1323 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001324eval_patch(
1325 char_u *origfile,
1326 char_u *difffile,
1327 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328{
1329 int err;
1330
1331 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1332 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1333 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1334 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1335 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1336 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1337 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1338}
1339# endif
1340
1341/*
1342 * Top level evaluation function, returning a boolean.
1343 * Sets "error" to TRUE if there was an error.
1344 * Return TRUE or FALSE.
1345 */
1346 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001347eval_to_bool(
1348 char_u *arg,
1349 int *error,
1350 char_u **nextcmd,
1351 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352{
Bram Moolenaar33570922005-01-25 22:26:29 +00001353 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 int retval = FALSE;
1355
1356 if (skip)
1357 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001358 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 else
1361 {
1362 *error = FALSE;
1363 if (!skip)
1364 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001365 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001366 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 }
1368 }
1369 if (skip)
1370 --emsg_skip;
1371
1372 return retval;
1373}
1374
1375/*
1376 * Top level evaluation function, returning a string. If "skip" is TRUE,
1377 * only parsing to "nextcmd" is done, without reporting errors. Return
1378 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1379 */
1380 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001381eval_to_string_skip(
1382 char_u *arg,
1383 char_u **nextcmd,
1384 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385{
Bram Moolenaar33570922005-01-25 22:26:29 +00001386 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 char_u *retval;
1388
1389 if (skip)
1390 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001391 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 retval = NULL;
1393 else
1394 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001395 retval = vim_strsave(get_tv_string(&tv));
1396 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 }
1398 if (skip)
1399 --emsg_skip;
1400
1401 return retval;
1402}
1403
1404/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001405 * Skip over an expression at "*pp".
1406 * Return FAIL for an error, OK otherwise.
1407 */
1408 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001409skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001410{
Bram Moolenaar33570922005-01-25 22:26:29 +00001411 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001412
1413 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001414 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001415}
1416
1417/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001419 * When "convert" is TRUE convert a List into a sequence of lines and convert
1420 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 * Return pointer to allocated memory, or NULL for failure.
1422 */
1423 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001424eval_to_string(
1425 char_u *arg,
1426 char_u **nextcmd,
1427 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428{
Bram Moolenaar33570922005-01-25 22:26:29 +00001429 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001431 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001432#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001433 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001434#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001436 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 retval = NULL;
1438 else
1439 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001440 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001441 {
1442 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001443 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001444 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001445 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001446 if (tv.vval.v_list->lv_len > 0)
1447 ga_append(&ga, NL);
1448 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001449 ga_append(&ga, NUL);
1450 retval = (char_u *)ga.ga_data;
1451 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001452#ifdef FEAT_FLOAT
1453 else if (convert && tv.v_type == VAR_FLOAT)
1454 {
1455 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1456 retval = vim_strsave(numbuf);
1457 }
1458#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001459 else
1460 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001461 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 }
1463
1464 return retval;
1465}
1466
1467/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001468 * Call eval_to_string() without using current local variables and using
1469 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 */
1471 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001472eval_to_string_safe(
1473 char_u *arg,
1474 char_u **nextcmd,
1475 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476{
1477 char_u *retval;
1478 void *save_funccalp;
1479
1480 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001481 if (use_sandbox)
1482 ++sandbox;
1483 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001484 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001485 if (use_sandbox)
1486 --sandbox;
1487 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 restore_funccal(save_funccalp);
1489 return retval;
1490}
1491
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492/*
1493 * Top level evaluation function, returning a number.
1494 * Evaluates "expr" silently.
1495 * Returns -1 for an error.
1496 */
1497 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001498eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499{
Bram Moolenaar33570922005-01-25 22:26:29 +00001500 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001502 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503
1504 ++emsg_off;
1505
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001506 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 retval = -1;
1508 else
1509 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001510 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001511 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 }
1513 --emsg_off;
1514
1515 return retval;
1516}
1517
Bram Moolenaara40058a2005-07-11 22:42:07 +00001518/*
1519 * Prepare v: variable "idx" to be used.
1520 * Save the current typeval in "save_tv".
1521 * When not used yet add the variable to the v: hashtable.
1522 */
1523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001524prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001525{
1526 *save_tv = vimvars[idx].vv_tv;
1527 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1528 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1529}
1530
1531/*
1532 * Restore v: variable "idx" to typeval "save_tv".
1533 * When no longer defined, remove the variable from the v: hashtable.
1534 */
1535 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001536restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001537{
1538 hashitem_T *hi;
1539
Bram Moolenaara40058a2005-07-11 22:42:07 +00001540 vimvars[idx].vv_tv = *save_tv;
1541 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1542 {
1543 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1544 if (HASHITEM_EMPTY(hi))
1545 EMSG2(_(e_intern2), "restore_vimvar()");
1546 else
1547 hash_remove(&vimvarht, hi);
1548 }
1549}
1550
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001551#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001552/*
1553 * Evaluate an expression to a list with suggestions.
1554 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001555 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001556 */
1557 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001558eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001559{
1560 typval_T save_val;
1561 typval_T rettv;
1562 list_T *list = NULL;
1563 char_u *p = skipwhite(expr);
1564
1565 /* Set "v:val" to the bad word. */
1566 prepare_vimvar(VV_VAL, &save_val);
1567 vimvars[VV_VAL].vv_type = VAR_STRING;
1568 vimvars[VV_VAL].vv_str = badword;
1569 if (p_verbose == 0)
1570 ++emsg_off;
1571
1572 if (eval1(&p, &rettv, TRUE) == OK)
1573 {
1574 if (rettv.v_type != VAR_LIST)
1575 clear_tv(&rettv);
1576 else
1577 list = rettv.vval.v_list;
1578 }
1579
1580 if (p_verbose == 0)
1581 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001582 restore_vimvar(VV_VAL, &save_val);
1583
1584 return list;
1585}
1586
1587/*
1588 * "list" is supposed to contain two items: a word and a number. Return the
1589 * word in "pp" and the number as the return value.
1590 * Return -1 if anything isn't right.
1591 * Used to get the good word and score from the eval_spell_expr() result.
1592 */
1593 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001594get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001595{
1596 listitem_T *li;
1597
1598 li = list->lv_first;
1599 if (li == NULL)
1600 return -1;
1601 *pp = get_tv_string(&li->li_tv);
1602
1603 li = li->li_next;
1604 if (li == NULL)
1605 return -1;
1606 return get_tv_number(&li->li_tv);
1607}
1608#endif
1609
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001610/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001611 * Top level evaluation function.
1612 * Returns an allocated typval_T with the result.
1613 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001614 */
1615 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001616eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001617{
1618 typval_T *tv;
1619
1620 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001621 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001622 {
1623 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001624 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001625 }
1626
1627 return tv;
1628}
1629
1630
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001632 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001633 * Uses argv[argc] for the function arguments. Only Number and String
1634 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001635 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001637 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001638call_vim_function(
1639 char_u *func,
1640 int argc,
1641 char_u **argv,
1642 int safe, /* use the sandbox */
1643 int str_arg_only, /* all arguments are strings */
1644 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645{
Bram Moolenaar33570922005-01-25 22:26:29 +00001646 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 long n;
1648 int len;
1649 int i;
1650 int doesrange;
1651 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001652 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001654 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001656 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657
1658 for (i = 0; i < argc; i++)
1659 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001660 /* Pass a NULL or empty argument as an empty string */
1661 if (argv[i] == NULL || *argv[i] == NUL)
1662 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001663 argvars[i].v_type = VAR_STRING;
1664 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001665 continue;
1666 }
1667
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001668 if (str_arg_only)
1669 len = 0;
1670 else
1671 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001672 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 if (len != 0 && len == (int)STRLEN(argv[i]))
1674 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001675 argvars[i].v_type = VAR_NUMBER;
1676 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 }
1678 else
1679 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001680 argvars[i].v_type = VAR_STRING;
1681 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 }
1683 }
1684
1685 if (safe)
1686 {
1687 save_funccalp = save_funccal();
1688 ++sandbox;
1689 }
1690
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001691 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1692 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001694 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 if (safe)
1696 {
1697 --sandbox;
1698 restore_funccal(save_funccalp);
1699 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001700 vim_free(argvars);
1701
1702 if (ret == FAIL)
1703 clear_tv(rettv);
1704
1705 return ret;
1706}
1707
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001708/*
1709 * Call vimL function "func" and return the result as a number.
1710 * Returns -1 when calling the function fails.
1711 * Uses argv[argc] for the function arguments.
1712 */
1713 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001714call_func_retnr(
1715 char_u *func,
1716 int argc,
1717 char_u **argv,
1718 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001719{
1720 typval_T rettv;
1721 long retval;
1722
1723 /* All arguments are passed as strings, no conversion to number. */
1724 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1725 return -1;
1726
1727 retval = get_tv_number_chk(&rettv, NULL);
1728 clear_tv(&rettv);
1729 return retval;
1730}
1731
1732#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1733 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1734
Bram Moolenaar4f688582007-07-24 12:34:30 +00001735# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001736/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001737 * Call vimL function "func" and return the result as a string.
1738 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001739 * Uses argv[argc] for the function arguments.
1740 */
1741 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001742call_func_retstr(
1743 char_u *func,
1744 int argc,
1745 char_u **argv,
1746 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001747{
1748 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001749 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001750
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001751 /* All arguments are passed as strings, no conversion to number. */
1752 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001753 return NULL;
1754
1755 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001756 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 return retval;
1758}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001759# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001760
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001761/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001762 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001763 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001764 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001765 */
1766 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001767call_func_retlist(
1768 char_u *func,
1769 int argc,
1770 char_u **argv,
1771 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001772{
1773 typval_T rettv;
1774
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001775 /* All arguments are passed as strings, no conversion to number. */
1776 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001777 return NULL;
1778
1779 if (rettv.v_type != VAR_LIST)
1780 {
1781 clear_tv(&rettv);
1782 return NULL;
1783 }
1784
1785 return rettv.vval.v_list;
1786}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787#endif
1788
1789/*
1790 * Save the current function call pointer, and set it to NULL.
1791 * Used when executing autocommands and for ":source".
1792 */
1793 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001794save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001796 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 current_funccal = NULL;
1799 return (void *)fc;
1800}
1801
1802 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001803restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001805 funccall_T *fc = (funccall_T *)vfc;
1806
1807 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808}
1809
Bram Moolenaar05159a02005-02-26 23:04:13 +00001810#if defined(FEAT_PROFILE) || defined(PROTO)
1811/*
1812 * Prepare profiling for entering a child or something else that is not
1813 * counted for the script/function itself.
1814 * Should always be called in pair with prof_child_exit().
1815 */
1816 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001817prof_child_enter(
1818 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001819{
1820 funccall_T *fc = current_funccal;
1821
1822 if (fc != NULL && fc->func->uf_profiling)
1823 profile_start(&fc->prof_child);
1824 script_prof_save(tm);
1825}
1826
1827/*
1828 * Take care of time spent in a child.
1829 * Should always be called after prof_child_enter().
1830 */
1831 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001832prof_child_exit(
1833 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001834{
1835 funccall_T *fc = current_funccal;
1836
1837 if (fc != NULL && fc->func->uf_profiling)
1838 {
1839 profile_end(&fc->prof_child);
1840 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1841 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1842 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1843 }
1844 script_prof_restore(tm);
1845}
1846#endif
1847
1848
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849#ifdef FEAT_FOLDING
1850/*
1851 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1852 * it in "*cp". Doesn't give error messages.
1853 */
1854 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001855eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856{
Bram Moolenaar33570922005-01-25 22:26:29 +00001857 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 int retval;
1859 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001860 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1861 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862
1863 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001864 if (use_sandbox)
1865 ++sandbox;
1866 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001868 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 retval = 0;
1870 else
1871 {
1872 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001873 if (tv.v_type == VAR_NUMBER)
1874 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001875 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 retval = 0;
1877 else
1878 {
1879 /* If the result is a string, check if there is a non-digit before
1880 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001881 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 if (!VIM_ISDIGIT(*s) && *s != '-')
1883 *cp = *s++;
1884 retval = atol((char *)s);
1885 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001886 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 }
1888 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001889 if (use_sandbox)
1890 --sandbox;
1891 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892
1893 return retval;
1894}
1895#endif
1896
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001898 * ":let" list all variable values
1899 * ":let var1 var2" list variable values
1900 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001901 * ":let var += expr" assignment command.
1902 * ":let var -= expr" assignment command.
1903 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001904 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 */
1906 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001907ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908{
1909 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001910 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001911 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001913 int var_count = 0;
1914 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001915 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001916 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001917 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918
Bram Moolenaardb552d602006-03-23 22:59:57 +00001919 argend = skip_var_list(arg, &var_count, &semicolon);
1920 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001921 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001922 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1923 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001924 expr = skipwhite(argend);
1925 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1926 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001928 /*
1929 * ":let" without "=": list variables
1930 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001931 if (*arg == '[')
1932 EMSG(_(e_invarg));
1933 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001934 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001935 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001936 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001937 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001938 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001939 list_glob_vars(&first);
1940 list_buf_vars(&first);
1941 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001942#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001943 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001944#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001945 list_script_vars(&first);
1946 list_func_vars(&first);
1947 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 eap->nextcmd = check_nextcmd(arg);
1950 }
1951 else
1952 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001953 op[0] = '=';
1954 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001955 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001956 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001957 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1958 op[0] = *expr; /* +=, -= or .= */
1959 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001960 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001961 else
1962 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001963
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 if (eap->skip)
1965 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001966 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 if (eap->skip)
1968 {
1969 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001970 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 --emsg_skip;
1972 }
1973 else if (i != FAIL)
1974 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001975 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001976 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001977 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 }
1979 }
1980}
1981
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001982/*
1983 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1984 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001985 * When "nextchars" is not NULL it points to a string with characters that
1986 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1987 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001988 * Returns OK or FAIL;
1989 */
1990 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001991ex_let_vars(
1992 char_u *arg_start,
1993 typval_T *tv,
1994 int copy, /* copy values from "tv", don't move */
1995 int semicolon, /* from skip_var_list() */
1996 int var_count, /* from skip_var_list() */
1997 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001998{
1999 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00002000 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002001 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00002002 listitem_T *item;
2003 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002004
2005 if (*arg != '[')
2006 {
2007 /*
2008 * ":let var = expr" or ":for var in list"
2009 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002010 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002011 return FAIL;
2012 return OK;
2013 }
2014
2015 /*
2016 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2017 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002018 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002019 {
2020 EMSG(_(e_listreq));
2021 return FAIL;
2022 }
2023
2024 i = list_len(l);
2025 if (semicolon == 0 && var_count < i)
2026 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002027 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002028 return FAIL;
2029 }
2030 if (var_count - semicolon > i)
2031 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002032 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002033 return FAIL;
2034 }
2035
2036 item = l->lv_first;
2037 while (*arg != ']')
2038 {
2039 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002040 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002041 item = item->li_next;
2042 if (arg == NULL)
2043 return FAIL;
2044
2045 arg = skipwhite(arg);
2046 if (*arg == ';')
2047 {
2048 /* Put the rest of the list (may be empty) in the var after ';'.
2049 * Create a new list for this. */
2050 l = list_alloc();
2051 if (l == NULL)
2052 return FAIL;
2053 while (item != NULL)
2054 {
2055 list_append_tv(l, &item->li_tv);
2056 item = item->li_next;
2057 }
2058
2059 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002060 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002061 ltv.vval.v_list = l;
2062 l->lv_refcount = 1;
2063
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002064 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2065 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002066 clear_tv(&ltv);
2067 if (arg == NULL)
2068 return FAIL;
2069 break;
2070 }
2071 else if (*arg != ',' && *arg != ']')
2072 {
2073 EMSG2(_(e_intern2), "ex_let_vars()");
2074 return FAIL;
2075 }
2076 }
2077
2078 return OK;
2079}
2080
2081/*
2082 * Skip over assignable variable "var" or list of variables "[var, var]".
2083 * Used for ":let varvar = expr" and ":for varvar in expr".
2084 * For "[var, var]" increment "*var_count" for each variable.
2085 * for "[var, var; var]" set "semicolon".
2086 * Return NULL for an error.
2087 */
2088 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002089skip_var_list(
2090 char_u *arg,
2091 int *var_count,
2092 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002093{
2094 char_u *p, *s;
2095
2096 if (*arg == '[')
2097 {
2098 /* "[var, var]": find the matching ']'. */
2099 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002100 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002101 {
2102 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2103 s = skip_var_one(p);
2104 if (s == p)
2105 {
2106 EMSG2(_(e_invarg2), p);
2107 return NULL;
2108 }
2109 ++*var_count;
2110
2111 p = skipwhite(s);
2112 if (*p == ']')
2113 break;
2114 else if (*p == ';')
2115 {
2116 if (*semicolon == 1)
2117 {
2118 EMSG(_("Double ; in list of variables"));
2119 return NULL;
2120 }
2121 *semicolon = 1;
2122 }
2123 else if (*p != ',')
2124 {
2125 EMSG2(_(e_invarg2), p);
2126 return NULL;
2127 }
2128 }
2129 return p + 1;
2130 }
2131 else
2132 return skip_var_one(arg);
2133}
2134
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002135/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002136 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002137 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002138 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002139 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002140skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002141{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002142 if (*arg == '@' && arg[1] != NUL)
2143 return arg + 2;
2144 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2145 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002146}
2147
Bram Moolenaara7043832005-01-21 11:56:39 +00002148/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002149 * List variables for hashtab "ht" with prefix "prefix".
2150 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002151 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002152 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002153list_hashtable_vars(
2154 hashtab_T *ht,
2155 char_u *prefix,
2156 int empty,
2157 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002158{
Bram Moolenaar33570922005-01-25 22:26:29 +00002159 hashitem_T *hi;
2160 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002161 int todo;
2162
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002163 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002164 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2165 {
2166 if (!HASHITEM_EMPTY(hi))
2167 {
2168 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002169 di = HI2DI(hi);
2170 if (empty || di->di_tv.v_type != VAR_STRING
2171 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002172 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002173 }
2174 }
2175}
2176
2177/*
2178 * List global variables.
2179 */
2180 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002181list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002182{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002183 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002184}
2185
2186/*
2187 * List buffer variables.
2188 */
2189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002190list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002191{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002192 char_u numbuf[NUMBUFLEN];
2193
Bram Moolenaar429fa852013-04-15 12:27:36 +02002194 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002195 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002196
2197 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002198 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2199 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002200}
2201
2202/*
2203 * List window variables.
2204 */
2205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002206list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002207{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002208 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002209 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002210}
2211
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002212#ifdef FEAT_WINDOWS
2213/*
2214 * List tab page variables.
2215 */
2216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002217list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002218{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002219 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002220 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002221}
2222#endif
2223
Bram Moolenaara7043832005-01-21 11:56:39 +00002224/*
2225 * List Vim variables.
2226 */
2227 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002228list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002229{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002230 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002231}
2232
2233/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002234 * List script-local variables, if there is a script.
2235 */
2236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002237list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002238{
2239 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002240 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2241 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002242}
2243
2244/*
2245 * List function variables, if there is a function.
2246 */
2247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002248list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002249{
2250 if (current_funccal != NULL)
2251 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002252 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002253}
2254
2255/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002256 * List variables in "arg".
2257 */
2258 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002259list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002260{
2261 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002262 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002263 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002264 char_u *name_start;
2265 char_u *arg_subsc;
2266 char_u *tofree;
2267 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002268
2269 while (!ends_excmd(*arg) && !got_int)
2270 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002271 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002272 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002273 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002274 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2275 {
2276 emsg_severe = TRUE;
2277 EMSG(_(e_trailing));
2278 break;
2279 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002280 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002281 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002282 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002283 /* get_name_len() takes care of expanding curly braces */
2284 name_start = name = arg;
2285 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2286 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002287 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002288 /* This is mainly to keep test 49 working: when expanding
2289 * curly braces fails overrule the exception error message. */
2290 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002291 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002292 emsg_severe = TRUE;
2293 EMSG2(_(e_invarg2), arg);
2294 break;
2295 }
2296 error = TRUE;
2297 }
2298 else
2299 {
2300 if (tofree != NULL)
2301 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002302 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002303 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002304 else
2305 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002306 /* handle d.key, l[idx], f(expr) */
2307 arg_subsc = arg;
2308 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002309 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002310 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002311 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002312 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002313 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002314 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002315 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002316 case 'g': list_glob_vars(first); break;
2317 case 'b': list_buf_vars(first); break;
2318 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002319#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002320 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002321#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002322 case 'v': list_vim_vars(first); break;
2323 case 's': list_script_vars(first); break;
2324 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002325 default:
2326 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002327 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002328 }
2329 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002330 {
2331 char_u numbuf[NUMBUFLEN];
2332 char_u *tf;
2333 int c;
2334 char_u *s;
2335
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002336 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002337 c = *arg;
2338 *arg = NUL;
2339 list_one_var_a((char_u *)"",
2340 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002341 tv.v_type,
2342 s == NULL ? (char_u *)"" : s,
2343 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002344 *arg = c;
2345 vim_free(tf);
2346 }
2347 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002348 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002349 }
2350 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002351
2352 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002353 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002354
2355 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002356 }
2357
2358 return arg;
2359}
2360
2361/*
2362 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2363 * Returns a pointer to the char just after the var name.
2364 * Returns NULL if there is an error.
2365 */
2366 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002367ex_let_one(
2368 char_u *arg, /* points to variable name */
2369 typval_T *tv, /* value to assign to variable */
2370 int copy, /* copy value from "tv" */
2371 char_u *endchars, /* valid chars after variable name or NULL */
2372 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002373{
2374 int c1;
2375 char_u *name;
2376 char_u *p;
2377 char_u *arg_end = NULL;
2378 int len;
2379 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002380 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002381
2382 /*
2383 * ":let $VAR = expr": Set environment variable.
2384 */
2385 if (*arg == '$')
2386 {
2387 /* Find the end of the name. */
2388 ++arg;
2389 name = arg;
2390 len = get_env_len(&arg);
2391 if (len == 0)
2392 EMSG2(_(e_invarg2), name - 1);
2393 else
2394 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002395 if (op != NULL && (*op == '+' || *op == '-'))
2396 EMSG2(_(e_letwrong), op);
2397 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002398 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002399 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002400 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002401 {
2402 c1 = name[len];
2403 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002404 p = get_tv_string_chk(tv);
2405 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002406 {
2407 int mustfree = FALSE;
2408 char_u *s = vim_getenv(name, &mustfree);
2409
2410 if (s != NULL)
2411 {
2412 p = tofree = concat_str(s, p);
2413 if (mustfree)
2414 vim_free(s);
2415 }
2416 }
2417 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002418 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002419 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002420 if (STRICMP(name, "HOME") == 0)
2421 init_homedir();
2422 else if (didset_vim && STRICMP(name, "VIM") == 0)
2423 didset_vim = FALSE;
2424 else if (didset_vimruntime
2425 && STRICMP(name, "VIMRUNTIME") == 0)
2426 didset_vimruntime = FALSE;
2427 arg_end = arg;
2428 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002429 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002430 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002431 }
2432 }
2433 }
2434
2435 /*
2436 * ":let &option = expr": Set option value.
2437 * ":let &l:option = expr": Set local option value.
2438 * ":let &g:option = expr": Set global option value.
2439 */
2440 else if (*arg == '&')
2441 {
2442 /* Find the end of the name. */
2443 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002444 if (p == NULL || (endchars != NULL
2445 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002446 EMSG(_(e_letunexp));
2447 else
2448 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002449 long n;
2450 int opt_type;
2451 long numval;
2452 char_u *stringval = NULL;
2453 char_u *s;
2454
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002455 c1 = *p;
2456 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002457
2458 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002459 s = get_tv_string_chk(tv); /* != NULL if number or string */
2460 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002461 {
2462 opt_type = get_option_value(arg, &numval,
2463 &stringval, opt_flags);
2464 if ((opt_type == 1 && *op == '.')
2465 || (opt_type == 0 && *op != '.'))
2466 EMSG2(_(e_letwrong), op);
2467 else
2468 {
2469 if (opt_type == 1) /* number */
2470 {
2471 if (*op == '+')
2472 n = numval + n;
2473 else
2474 n = numval - n;
2475 }
2476 else if (opt_type == 0 && stringval != NULL) /* string */
2477 {
2478 s = concat_str(stringval, s);
2479 vim_free(stringval);
2480 stringval = s;
2481 }
2482 }
2483 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002484 if (s != NULL)
2485 {
2486 set_option_value(arg, n, s, opt_flags);
2487 arg_end = p;
2488 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002489 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002490 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002491 }
2492 }
2493
2494 /*
2495 * ":let @r = expr": Set register contents.
2496 */
2497 else if (*arg == '@')
2498 {
2499 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002500 if (op != NULL && (*op == '+' || *op == '-'))
2501 EMSG2(_(e_letwrong), op);
2502 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002503 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002504 EMSG(_(e_letunexp));
2505 else
2506 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002507 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002508 char_u *s;
2509
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002510 p = get_tv_string_chk(tv);
2511 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002512 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002513 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002514 if (s != NULL)
2515 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002516 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002517 vim_free(s);
2518 }
2519 }
2520 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002521 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002522 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002523 arg_end = arg + 1;
2524 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002525 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002526 }
2527 }
2528
2529 /*
2530 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002532 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002533 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002534 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002535 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002536
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002537 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002538 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002539 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002540 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2541 EMSG(_(e_letunexp));
2542 else
2543 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002544 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545 arg_end = p;
2546 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002547 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002548 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002549 }
2550
2551 else
2552 EMSG2(_(e_invarg2), arg);
2553
2554 return arg_end;
2555}
2556
2557/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002558 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2559 */
2560 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002561check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002562{
2563 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2564 {
2565 EMSG2(_(e_readonlyvar), arg);
2566 return TRUE;
2567 }
2568 return FALSE;
2569}
2570
2571/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572 * Get an lval: variable, Dict item or List item that can be assigned a value
2573 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2574 * "name.key", "name.key[expr]" etc.
2575 * Indexing only works if "name" is an existing List or Dictionary.
2576 * "name" points to the start of the name.
2577 * If "rettv" is not NULL it points to the value to be assigned.
2578 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2579 * wrong; must end in space or cmd separator.
2580 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002581 * flags:
2582 * GLV_QUIET: do not give error messages
2583 * GLV_NO_AUTOLOAD: do not use script autoloading
2584 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002586 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002587 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002588 */
2589 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002590get_lval(
2591 char_u *name,
2592 typval_T *rettv,
2593 lval_T *lp,
2594 int unlet,
2595 int skip,
2596 int flags, /* GLV_ values */
2597 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002598{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002599 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 char_u *expr_start, *expr_end;
2601 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002602 dictitem_T *v;
2603 typval_T var1;
2604 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002606 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002607 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002608 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002609 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002610 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002611
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002612 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002613 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002614
2615 if (skip)
2616 {
2617 /* When skipping just find the end of the name. */
2618 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002619 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002620 }
2621
2622 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002623 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 if (expr_start != NULL)
2625 {
2626 /* Don't expand the name when we already know there is an error. */
2627 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2628 && *p != '[' && *p != '.')
2629 {
2630 EMSG(_(e_trailing));
2631 return NULL;
2632 }
2633
2634 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2635 if (lp->ll_exp_name == NULL)
2636 {
2637 /* Report an invalid expression in braces, unless the
2638 * expression evaluation has been cancelled due to an
2639 * aborting error, an interrupt, or an exception. */
2640 if (!aborting() && !quiet)
2641 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002642 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002643 EMSG2(_(e_invarg2), name);
2644 return NULL;
2645 }
2646 }
2647 lp->ll_name = lp->ll_exp_name;
2648 }
2649 else
2650 lp->ll_name = name;
2651
2652 /* Without [idx] or .key we are done. */
2653 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2654 return p;
2655
2656 cc = *p;
2657 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002658 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 if (v == NULL && !quiet)
2660 EMSG2(_(e_undefvar), lp->ll_name);
2661 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002662 if (v == NULL)
2663 return NULL;
2664
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002665 /*
2666 * Loop until no more [idx] or .key is following.
2667 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002668 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002670 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002671 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2672 && !(lp->ll_tv->v_type == VAR_DICT
2673 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002674 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002675 if (!quiet)
2676 EMSG(_("E689: Can only index a List or Dictionary"));
2677 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002678 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002680 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002681 if (!quiet)
2682 EMSG(_("E708: [:] must come last"));
2683 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002684 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002685
Bram Moolenaar8c711452005-01-14 21:53:12 +00002686 len = -1;
2687 if (*p == '.')
2688 {
2689 key = p + 1;
2690 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2691 ;
2692 if (len == 0)
2693 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694 if (!quiet)
2695 EMSG(_(e_emptykey));
2696 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 }
2698 p = key + len;
2699 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002700 else
2701 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002702 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002703 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002704 if (*p == ':')
2705 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002706 else
2707 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 empty1 = FALSE;
2709 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002711 if (get_tv_string_chk(&var1) == NULL)
2712 {
2713 /* not a number or string */
2714 clear_tv(&var1);
2715 return NULL;
2716 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 }
2718
2719 /* Optionally get the second index [ :expr]. */
2720 if (*p == ':')
2721 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002725 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002726 if (!empty1)
2727 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002728 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002729 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002730 if (rettv != NULL && (rettv->v_type != VAR_LIST
2731 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002732 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002733 if (!quiet)
2734 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002735 if (!empty1)
2736 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002737 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002738 }
2739 p = skipwhite(p + 1);
2740 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002741 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002742 else
2743 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002744 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2746 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002747 if (!empty1)
2748 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002750 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002751 if (get_tv_string_chk(&var2) == NULL)
2752 {
2753 /* not a number or string */
2754 if (!empty1)
2755 clear_tv(&var1);
2756 clear_tv(&var2);
2757 return NULL;
2758 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002759 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002760 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002761 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002762 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002764
Bram Moolenaar8c711452005-01-14 21:53:12 +00002765 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002766 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002767 if (!quiet)
2768 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002769 if (!empty1)
2770 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002772 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002774 }
2775
2776 /* Skip to past ']'. */
2777 ++p;
2778 }
2779
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002780 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002781 {
2782 if (len == -1)
2783 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002784 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002785 key = get_tv_string_chk(&var1); /* is number or string */
2786 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002787 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002788 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002789 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002790 }
2791 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002792 lp->ll_list = NULL;
2793 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002794 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002795
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002796 /* When assigning to a scope dictionary check that a function and
2797 * variable name is valid (only variable name unless it is l: or
2798 * g: dictionary). Disallow overwriting a builtin function. */
2799 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002800 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002801 int prevval;
2802 int wrong;
2803
2804 if (len != -1)
2805 {
2806 prevval = key[len];
2807 key[len] = NUL;
2808 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002809 else
2810 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002811 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2812 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002813 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002814 || !valid_varname(key);
2815 if (len != -1)
2816 key[len] = prevval;
2817 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002818 return NULL;
2819 }
2820
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002821 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002822 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002823 /* Can't add "v:" variable. */
2824 if (lp->ll_dict == &vimvardict)
2825 {
2826 EMSG2(_(e_illvar), name);
2827 return NULL;
2828 }
2829
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002830 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002831 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002832 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002833 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002834 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002835 if (len == -1)
2836 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002837 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002838 }
2839 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002841 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002842 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002843 if (len == -1)
2844 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002845 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002846 p = NULL;
2847 break;
2848 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002849 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002850 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002851 return NULL;
2852
Bram Moolenaar8c711452005-01-14 21:53:12 +00002853 if (len == -1)
2854 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002856 }
2857 else
2858 {
2859 /*
2860 * Get the number and item for the only or first index of the List.
2861 */
2862 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002863 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002864 else
2865 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002866 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002867 clear_tv(&var1);
2868 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002869 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 lp->ll_list = lp->ll_tv->vval.v_list;
2871 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2872 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002873 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002874 if (lp->ll_n1 < 0)
2875 {
2876 lp->ll_n1 = 0;
2877 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2878 }
2879 }
2880 if (lp->ll_li == NULL)
2881 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002883 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002884 if (!quiet)
2885 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002887 }
2888
2889 /*
2890 * May need to find the item or absolute index for the second
2891 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 * When no index given: "lp->ll_empty2" is TRUE.
2893 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002894 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002896 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002897 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002898 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002899 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002900 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002901 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002902 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002903 {
2904 if (!quiet)
2905 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002907 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002909 }
2910
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2912 if (lp->ll_n1 < 0)
2913 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2914 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002915 {
2916 if (!quiet)
2917 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002919 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002920 }
2921
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002922 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002923 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002924 }
2925
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002926 return p;
2927}
2928
2929/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002930 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002931 */
2932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002933clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002934{
2935 vim_free(lp->ll_exp_name);
2936 vim_free(lp->ll_newkey);
2937}
2938
2939/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002940 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002941 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002942 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002943 */
2944 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002945set_var_lval(
2946 lval_T *lp,
2947 char_u *endp,
2948 typval_T *rettv,
2949 int copy,
2950 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002951{
2952 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002953 listitem_T *ri;
2954 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002955
2956 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002957 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002958 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002959 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002960 cc = *endp;
2961 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002962 if (op != NULL && *op != '=')
2963 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002964 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002965
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002966 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002967 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002968 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002969 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002970 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002971 if ((di == NULL
2972 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2973 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2974 FALSE)))
2975 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002976 set_var(lp->ll_name, &tv, FALSE);
2977 clear_tv(&tv);
2978 }
2979 }
2980 else
2981 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002982 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002983 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002984 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002985 else if (tv_check_lock(lp->ll_newkey == NULL
2986 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002987 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002988 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002989 else if (lp->ll_range)
2990 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002991 listitem_T *ll_li = lp->ll_li;
2992 int ll_n1 = lp->ll_n1;
2993
2994 /*
2995 * Check whether any of the list items is locked
2996 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002997 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002998 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002999 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003000 return;
3001 ri = ri->li_next;
3002 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
3003 break;
3004 ll_li = ll_li->li_next;
3005 ++ll_n1;
3006 }
3007
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003008 /*
3009 * Assign the List values to the list items.
3010 */
3011 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003012 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003013 if (op != NULL && *op != '=')
3014 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3015 else
3016 {
3017 clear_tv(&lp->ll_li->li_tv);
3018 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3019 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003020 ri = ri->li_next;
3021 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3022 break;
3023 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003024 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003025 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003026 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003027 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003028 ri = NULL;
3029 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003030 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003031 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003032 lp->ll_li = lp->ll_li->li_next;
3033 ++lp->ll_n1;
3034 }
3035 if (ri != NULL)
3036 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003037 else if (lp->ll_empty2
3038 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003039 : lp->ll_n1 != lp->ll_n2)
3040 EMSG(_("E711: List value has not enough items"));
3041 }
3042 else
3043 {
3044 /*
3045 * Assign to a List or Dictionary item.
3046 */
3047 if (lp->ll_newkey != NULL)
3048 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003049 if (op != NULL && *op != '=')
3050 {
3051 EMSG2(_(e_letwrong), op);
3052 return;
3053 }
3054
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003055 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003056 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003057 if (di == NULL)
3058 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003059 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3060 {
3061 vim_free(di);
3062 return;
3063 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003064 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003065 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003066 else if (op != NULL && *op != '=')
3067 {
3068 tv_op(lp->ll_tv, rettv, op);
3069 return;
3070 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003071 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003072 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003073
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003074 /*
3075 * Assign the value to the variable or list item.
3076 */
3077 if (copy)
3078 copy_tv(rettv, lp->ll_tv);
3079 else
3080 {
3081 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003082 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003083 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003084 }
3085 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003086}
3087
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003088/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003089 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3090 * Returns OK or FAIL.
3091 */
3092 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003093tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003094{
3095 long n;
3096 char_u numbuf[NUMBUFLEN];
3097 char_u *s;
3098
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003099 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3100 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3101 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003102 {
3103 switch (tv1->v_type)
3104 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003105 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003106 case VAR_DICT:
3107 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003108 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003109 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003110 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003111 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003112 break;
3113
3114 case VAR_LIST:
3115 if (*op != '+' || tv2->v_type != VAR_LIST)
3116 break;
3117 /* List += List */
3118 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3119 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3120 return OK;
3121
3122 case VAR_NUMBER:
3123 case VAR_STRING:
3124 if (tv2->v_type == VAR_LIST)
3125 break;
3126 if (*op == '+' || *op == '-')
3127 {
3128 /* nr += nr or nr -= nr*/
3129 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003130#ifdef FEAT_FLOAT
3131 if (tv2->v_type == VAR_FLOAT)
3132 {
3133 float_T f = n;
3134
3135 if (*op == '+')
3136 f += tv2->vval.v_float;
3137 else
3138 f -= tv2->vval.v_float;
3139 clear_tv(tv1);
3140 tv1->v_type = VAR_FLOAT;
3141 tv1->vval.v_float = f;
3142 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003143 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003144#endif
3145 {
3146 if (*op == '+')
3147 n += get_tv_number(tv2);
3148 else
3149 n -= get_tv_number(tv2);
3150 clear_tv(tv1);
3151 tv1->v_type = VAR_NUMBER;
3152 tv1->vval.v_number = n;
3153 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003154 }
3155 else
3156 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003157 if (tv2->v_type == VAR_FLOAT)
3158 break;
3159
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003160 /* str .= str */
3161 s = get_tv_string(tv1);
3162 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3163 clear_tv(tv1);
3164 tv1->v_type = VAR_STRING;
3165 tv1->vval.v_string = s;
3166 }
3167 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003168
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003169 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003170#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003171 {
3172 float_T f;
3173
3174 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3175 && tv2->v_type != VAR_NUMBER
3176 && tv2->v_type != VAR_STRING))
3177 break;
3178 if (tv2->v_type == VAR_FLOAT)
3179 f = tv2->vval.v_float;
3180 else
3181 f = get_tv_number(tv2);
3182 if (*op == '+')
3183 tv1->vval.v_float += f;
3184 else
3185 tv1->vval.v_float -= f;
3186 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003187#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003188 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003189 }
3190 }
3191
3192 EMSG2(_(e_letwrong), op);
3193 return FAIL;
3194}
3195
3196/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003197 * Add a watcher to a list.
3198 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003199 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003200list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003201{
3202 lw->lw_next = l->lv_watch;
3203 l->lv_watch = lw;
3204}
3205
3206/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003207 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003208 * No warning when it isn't found...
3209 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003210 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003211list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003212{
Bram Moolenaar33570922005-01-25 22:26:29 +00003213 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003214
3215 lwp = &l->lv_watch;
3216 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3217 {
3218 if (lw == lwrem)
3219 {
3220 *lwp = lw->lw_next;
3221 break;
3222 }
3223 lwp = &lw->lw_next;
3224 }
3225}
3226
3227/*
3228 * Just before removing an item from a list: advance watchers to the next
3229 * item.
3230 */
3231 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003232list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003233{
Bram Moolenaar33570922005-01-25 22:26:29 +00003234 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003235
3236 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3237 if (lw->lw_item == item)
3238 lw->lw_item = item->li_next;
3239}
3240
3241/*
3242 * Evaluate the expression used in a ":for var in expr" command.
3243 * "arg" points to "var".
3244 * Set "*errp" to TRUE for an error, FALSE otherwise;
3245 * Return a pointer that holds the info. Null when there is an error.
3246 */
3247 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003248eval_for_line(
3249 char_u *arg,
3250 int *errp,
3251 char_u **nextcmdp,
3252 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003253{
Bram Moolenaar33570922005-01-25 22:26:29 +00003254 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003255 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003256 typval_T tv;
3257 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003258
3259 *errp = TRUE; /* default: there is an error */
3260
Bram Moolenaar33570922005-01-25 22:26:29 +00003261 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003262 if (fi == NULL)
3263 return NULL;
3264
3265 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3266 if (expr == NULL)
3267 return fi;
3268
3269 expr = skipwhite(expr);
3270 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3271 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003272 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003273 return fi;
3274 }
3275
3276 if (skip)
3277 ++emsg_skip;
3278 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3279 {
3280 *errp = FALSE;
3281 if (!skip)
3282 {
3283 l = tv.vval.v_list;
3284 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003285 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003286 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003287 clear_tv(&tv);
3288 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003289 else
3290 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003291 /* No need to increment the refcount, it's already set for the
3292 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003293 fi->fi_list = l;
3294 list_add_watch(l, &fi->fi_lw);
3295 fi->fi_lw.lw_item = l->lv_first;
3296 }
3297 }
3298 }
3299 if (skip)
3300 --emsg_skip;
3301
3302 return fi;
3303}
3304
3305/*
3306 * Use the first item in a ":for" list. Advance to the next.
3307 * Assign the values to the variable (list). "arg" points to the first one.
3308 * Return TRUE when a valid item was found, FALSE when at end of list or
3309 * something wrong.
3310 */
3311 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003312next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003313{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003314 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003315 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003316 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003317
3318 item = fi->fi_lw.lw_item;
3319 if (item == NULL)
3320 result = FALSE;
3321 else
3322 {
3323 fi->fi_lw.lw_item = item->li_next;
3324 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3325 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3326 }
3327 return result;
3328}
3329
3330/*
3331 * Free the structure used to store info used by ":for".
3332 */
3333 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003334free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003335{
Bram Moolenaar33570922005-01-25 22:26:29 +00003336 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003337
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003338 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003339 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003340 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003341 list_unref(fi->fi_list);
3342 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003343 vim_free(fi);
3344}
3345
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3347
3348 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003349set_context_for_expression(
3350 expand_T *xp,
3351 char_u *arg,
3352 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353{
3354 int got_eq = FALSE;
3355 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003356 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003358 if (cmdidx == CMD_let)
3359 {
3360 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003361 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003362 {
3363 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003364 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003365 {
3366 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003367 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003368 if (vim_iswhite(*p))
3369 break;
3370 }
3371 return;
3372 }
3373 }
3374 else
3375 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3376 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 while ((xp->xp_pattern = vim_strpbrk(arg,
3378 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3379 {
3380 c = *xp->xp_pattern;
3381 if (c == '&')
3382 {
3383 c = xp->xp_pattern[1];
3384 if (c == '&')
3385 {
3386 ++xp->xp_pattern;
3387 xp->xp_context = cmdidx != CMD_let || got_eq
3388 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3389 }
3390 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003391 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003393 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3394 xp->xp_pattern += 2;
3395
3396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 }
3398 else if (c == '$')
3399 {
3400 /* environment variable */
3401 xp->xp_context = EXPAND_ENV_VARS;
3402 }
3403 else if (c == '=')
3404 {
3405 got_eq = TRUE;
3406 xp->xp_context = EXPAND_EXPRESSION;
3407 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003408 else if (c == '#'
3409 && xp->xp_context == EXPAND_EXPRESSION)
3410 {
3411 /* Autoload function/variable contains '#'. */
3412 break;
3413 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003414 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 && xp->xp_context == EXPAND_FUNCTIONS
3416 && vim_strchr(xp->xp_pattern, '(') == NULL)
3417 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003418 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 break;
3420 }
3421 else if (cmdidx != CMD_let || got_eq)
3422 {
3423 if (c == '"') /* string */
3424 {
3425 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3426 if (c == '\\' && xp->xp_pattern[1] != NUL)
3427 ++xp->xp_pattern;
3428 xp->xp_context = EXPAND_NOTHING;
3429 }
3430 else if (c == '\'') /* literal string */
3431 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003432 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3434 /* skip */ ;
3435 xp->xp_context = EXPAND_NOTHING;
3436 }
3437 else if (c == '|')
3438 {
3439 if (xp->xp_pattern[1] == '|')
3440 {
3441 ++xp->xp_pattern;
3442 xp->xp_context = EXPAND_EXPRESSION;
3443 }
3444 else
3445 xp->xp_context = EXPAND_COMMANDS;
3446 }
3447 else
3448 xp->xp_context = EXPAND_EXPRESSION;
3449 }
3450 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003451 /* Doesn't look like something valid, expand as an expression
3452 * anyway. */
3453 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 arg = xp->xp_pattern;
3455 if (*arg != NUL)
3456 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3457 /* skip */ ;
3458 }
3459 xp->xp_pattern = arg;
3460}
3461
3462#endif /* FEAT_CMDL_COMPL */
3463
3464/*
3465 * ":1,25call func(arg1, arg2)" function call.
3466 */
3467 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003468ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469{
3470 char_u *arg = eap->arg;
3471 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003473 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003475 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 linenr_T lnum;
3477 int doesrange;
3478 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003479 funcdict_T fudi;
Bram Moolenaar9e63f612016-03-17 23:13:28 +01003480 partial_T *partial = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003482 if (eap->skip)
3483 {
3484 /* trans_function_name() doesn't work well when skipping, use eval0()
3485 * instead to skip to any following command, e.g. for:
3486 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003487 ++emsg_skip;
3488 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3489 clear_tv(&rettv);
3490 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003491 return;
3492 }
3493
Bram Moolenaar65639032016-03-16 21:40:30 +01003494 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003495 if (fudi.fd_newkey != NULL)
3496 {
3497 /* Still need to give an error message for missing key. */
3498 EMSG2(_(e_dictkey), fudi.fd_newkey);
3499 vim_free(fudi.fd_newkey);
3500 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003501 if (tofree == NULL)
3502 return;
3503
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003504 /* Increase refcount on dictionary, it could get deleted when evaluating
3505 * the arguments. */
3506 if (fudi.fd_dict != NULL)
3507 ++fudi.fd_dict->dv_refcount;
3508
Bram Moolenaar65639032016-03-16 21:40:30 +01003509 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3510 * contents. For VAR_PARTIAL get its partial, unless we already have one
3511 * from trans_function_name(). */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003512 len = (int)STRLEN(tofree);
Bram Moolenaar65639032016-03-16 21:40:30 +01003513 name = deref_func_name(tofree, &len,
3514 partial != NULL ? NULL : &partial, FALSE);
3515
Bram Moolenaar532c7802005-01-27 14:44:31 +00003516 /* Skip white space to allow ":call func ()". Not good, but required for
3517 * backward compatibility. */
3518 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003519 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520
3521 if (*startarg != '(')
3522 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003523 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 goto end;
3525 }
3526
3527 /*
3528 * When skipping, evaluate the function once, to find the end of the
3529 * arguments.
3530 * When the function takes a range, this is discovered after the first
3531 * call, and the loop is broken.
3532 */
3533 if (eap->skip)
3534 {
3535 ++emsg_skip;
3536 lnum = eap->line2; /* do it once, also with an invalid range */
3537 }
3538 else
3539 lnum = eap->line1;
3540 for ( ; lnum <= eap->line2; ++lnum)
3541 {
3542 if (!eap->skip && eap->addr_count > 0)
3543 {
3544 curwin->w_cursor.lnum = lnum;
3545 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003546#ifdef FEAT_VIRTUALEDIT
3547 curwin->w_cursor.coladd = 0;
3548#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 }
3550 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003551 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003552 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003553 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 {
3555 failed = TRUE;
3556 break;
3557 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003558
3559 /* Handle a function returning a Funcref, Dictionary or List. */
3560 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3561 {
3562 failed = TRUE;
3563 break;
3564 }
3565
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003566 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 if (doesrange || eap->skip)
3568 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003569
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003571 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003572 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003573 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 if (aborting())
3575 break;
3576 }
3577 if (eap->skip)
3578 --emsg_skip;
3579
3580 if (!failed)
3581 {
3582 /* Check for trailing illegal characters and a following command. */
3583 if (!ends_excmd(*arg))
3584 {
3585 emsg_severe = TRUE;
3586 EMSG(_(e_trailing));
3587 }
3588 else
3589 eap->nextcmd = check_nextcmd(arg);
3590 }
3591
3592end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003593 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003594 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595}
3596
3597/*
3598 * ":unlet[!] var1 ... " command.
3599 */
3600 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003601ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003603 ex_unletlock(eap, eap->arg, 0);
3604}
3605
3606/*
3607 * ":lockvar" and ":unlockvar" commands
3608 */
3609 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003610ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003611{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003613 int deep = 2;
3614
3615 if (eap->forceit)
3616 deep = -1;
3617 else if (vim_isdigit(*arg))
3618 {
3619 deep = getdigits(&arg);
3620 arg = skipwhite(arg);
3621 }
3622
3623 ex_unletlock(eap, arg, deep);
3624}
3625
3626/*
3627 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3628 */
3629 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003630ex_unletlock(
3631 exarg_T *eap,
3632 char_u *argstart,
3633 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003634{
3635 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003638 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639
3640 do
3641 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003642 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003643 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003644 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003645 if (lv.ll_name == NULL)
3646 error = TRUE; /* error but continue parsing */
3647 if (name_end == NULL || (!vim_iswhite(*name_end)
3648 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003650 if (name_end != NULL)
3651 {
3652 emsg_severe = TRUE;
3653 EMSG(_(e_trailing));
3654 }
3655 if (!(eap->skip || error))
3656 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 break;
3658 }
3659
3660 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003661 {
3662 if (eap->cmdidx == CMD_unlet)
3663 {
3664 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3665 error = TRUE;
3666 }
3667 else
3668 {
3669 if (do_lock_var(&lv, name_end, deep,
3670 eap->cmdidx == CMD_lockvar) == FAIL)
3671 error = TRUE;
3672 }
3673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003675 if (!eap->skip)
3676 clear_lval(&lv);
3677
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 arg = skipwhite(name_end);
3679 } while (!ends_excmd(*arg));
3680
3681 eap->nextcmd = check_nextcmd(arg);
3682}
3683
Bram Moolenaar8c711452005-01-14 21:53:12 +00003684 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003685do_unlet_var(
3686 lval_T *lp,
3687 char_u *name_end,
3688 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003689{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003690 int ret = OK;
3691 int cc;
3692
3693 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003694 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003695 cc = *name_end;
3696 *name_end = NUL;
3697
3698 /* Normal name or expanded name. */
3699 if (check_changedtick(lp->ll_name))
3700 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003701 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003702 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003703 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003704 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003705 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003706 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003707 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003708 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003709 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003710 else if (lp->ll_range)
3711 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003712 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003713 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003714 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003715
3716 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3717 {
3718 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003719 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003720 return FAIL;
3721 ll_li = li;
3722 ++ll_n1;
3723 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003724
3725 /* Delete a range of List items. */
3726 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3727 {
3728 li = lp->ll_li->li_next;
3729 listitem_remove(lp->ll_list, lp->ll_li);
3730 lp->ll_li = li;
3731 ++lp->ll_n1;
3732 }
3733 }
3734 else
3735 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003736 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003737 /* unlet a List item. */
3738 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003739 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003740 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003741 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003742 }
3743
3744 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003745}
3746
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747/*
3748 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003749 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 */
3751 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003752do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753{
Bram Moolenaar33570922005-01-25 22:26:29 +00003754 hashtab_T *ht;
3755 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003756 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003757 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003758 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759
Bram Moolenaar33570922005-01-25 22:26:29 +00003760 ht = find_var_ht(name, &varname);
3761 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003763 if (ht == &globvarht)
3764 d = &globvardict;
3765 else if (current_funccal != NULL
3766 && ht == &current_funccal->l_vars.dv_hashtab)
3767 d = &current_funccal->l_vars;
3768 else if (ht == &compat_hashtab)
3769 d = &vimvardict;
3770 else
3771 {
3772 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3773 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3774 }
3775 if (d == NULL)
3776 {
3777 EMSG2(_(e_intern2), "do_unlet()");
3778 return FAIL;
3779 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003780 hi = hash_find(ht, varname);
3781 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003782 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003783 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003784 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003785 || var_check_ro(di->di_flags, name, FALSE)
3786 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003787 return FAIL;
3788
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003789 delete_var(ht, hi);
3790 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003793 if (forceit)
3794 return OK;
3795 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 return FAIL;
3797}
3798
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003799/*
3800 * Lock or unlock variable indicated by "lp".
3801 * "deep" is the levels to go (-1 for unlimited);
3802 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3803 */
3804 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003805do_lock_var(
3806 lval_T *lp,
3807 char_u *name_end,
3808 int deep,
3809 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003810{
3811 int ret = OK;
3812 int cc;
3813 dictitem_T *di;
3814
3815 if (deep == 0) /* nothing to do */
3816 return OK;
3817
3818 if (lp->ll_tv == NULL)
3819 {
3820 cc = *name_end;
3821 *name_end = NUL;
3822
3823 /* Normal name or expanded name. */
3824 if (check_changedtick(lp->ll_name))
3825 ret = FAIL;
3826 else
3827 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003828 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003829 if (di == NULL)
3830 ret = FAIL;
3831 else
3832 {
3833 if (lock)
3834 di->di_flags |= DI_FLAGS_LOCK;
3835 else
3836 di->di_flags &= ~DI_FLAGS_LOCK;
3837 item_lock(&di->di_tv, deep, lock);
3838 }
3839 }
3840 *name_end = cc;
3841 }
3842 else if (lp->ll_range)
3843 {
3844 listitem_T *li = lp->ll_li;
3845
3846 /* (un)lock a range of List items. */
3847 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3848 {
3849 item_lock(&li->li_tv, deep, lock);
3850 li = li->li_next;
3851 ++lp->ll_n1;
3852 }
3853 }
3854 else if (lp->ll_list != NULL)
3855 /* (un)lock a List item. */
3856 item_lock(&lp->ll_li->li_tv, deep, lock);
3857 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003858 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003859 item_lock(&lp->ll_di->di_tv, deep, lock);
3860
3861 return ret;
3862}
3863
3864/*
3865 * Lock or unlock an item. "deep" is nr of levels to go.
3866 */
3867 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003868item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003869{
3870 static int recurse = 0;
3871 list_T *l;
3872 listitem_T *li;
3873 dict_T *d;
3874 hashitem_T *hi;
3875 int todo;
3876
3877 if (recurse >= DICT_MAXNEST)
3878 {
3879 EMSG(_("E743: variable nested too deep for (un)lock"));
3880 return;
3881 }
3882 if (deep == 0)
3883 return;
3884 ++recurse;
3885
3886 /* lock/unlock the item itself */
3887 if (lock)
3888 tv->v_lock |= VAR_LOCKED;
3889 else
3890 tv->v_lock &= ~VAR_LOCKED;
3891
3892 switch (tv->v_type)
3893 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003894 case VAR_UNKNOWN:
3895 case VAR_NUMBER:
3896 case VAR_STRING:
3897 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003898 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003899 case VAR_FLOAT:
3900 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003901 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003902 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003903 break;
3904
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003905 case VAR_LIST:
3906 if ((l = tv->vval.v_list) != NULL)
3907 {
3908 if (lock)
3909 l->lv_lock |= VAR_LOCKED;
3910 else
3911 l->lv_lock &= ~VAR_LOCKED;
3912 if (deep < 0 || deep > 1)
3913 /* recursive: lock/unlock the items the List contains */
3914 for (li = l->lv_first; li != NULL; li = li->li_next)
3915 item_lock(&li->li_tv, deep - 1, lock);
3916 }
3917 break;
3918 case VAR_DICT:
3919 if ((d = tv->vval.v_dict) != NULL)
3920 {
3921 if (lock)
3922 d->dv_lock |= VAR_LOCKED;
3923 else
3924 d->dv_lock &= ~VAR_LOCKED;
3925 if (deep < 0 || deep > 1)
3926 {
3927 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003928 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003929 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3930 {
3931 if (!HASHITEM_EMPTY(hi))
3932 {
3933 --todo;
3934 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3935 }
3936 }
3937 }
3938 }
3939 }
3940 --recurse;
3941}
3942
Bram Moolenaara40058a2005-07-11 22:42:07 +00003943/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003944 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3945 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003946 */
3947 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003948tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003949{
3950 return (tv->v_lock & VAR_LOCKED)
3951 || (tv->v_type == VAR_LIST
3952 && tv->vval.v_list != NULL
3953 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3954 || (tv->v_type == VAR_DICT
3955 && tv->vval.v_dict != NULL
3956 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3957}
3958
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3960/*
3961 * Delete all "menutrans_" variables.
3962 */
3963 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003964del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965{
Bram Moolenaar33570922005-01-25 22:26:29 +00003966 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003967 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968
Bram Moolenaar33570922005-01-25 22:26:29 +00003969 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003970 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003971 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003972 {
3973 if (!HASHITEM_EMPTY(hi))
3974 {
3975 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003976 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3977 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003978 }
3979 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003980 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981}
3982#endif
3983
3984#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3985
3986/*
3987 * Local string buffer for the next two functions to store a variable name
3988 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3989 * get_user_var_name().
3990 */
3991
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003992static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993
3994static char_u *varnamebuf = NULL;
3995static int varnamebuflen = 0;
3996
3997/*
3998 * Function to concatenate a prefix and a variable name.
3999 */
4000 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004001cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002{
4003 int len;
4004
4005 len = (int)STRLEN(name) + 3;
4006 if (len > varnamebuflen)
4007 {
4008 vim_free(varnamebuf);
4009 len += 10; /* some additional space */
4010 varnamebuf = alloc(len);
4011 if (varnamebuf == NULL)
4012 {
4013 varnamebuflen = 0;
4014 return NULL;
4015 }
4016 varnamebuflen = len;
4017 }
4018 *varnamebuf = prefix;
4019 varnamebuf[1] = ':';
4020 STRCPY(varnamebuf + 2, name);
4021 return varnamebuf;
4022}
4023
4024/*
4025 * Function given to ExpandGeneric() to obtain the list of user defined
4026 * (global/buffer/window/built-in) variable names.
4027 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004029get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004031 static long_u gdone;
4032 static long_u bdone;
4033 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004034#ifdef FEAT_WINDOWS
4035 static long_u tdone;
4036#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004037 static int vidx;
4038 static hashitem_T *hi;
4039 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040
4041 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004042 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004043 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004044#ifdef FEAT_WINDOWS
4045 tdone = 0;
4046#endif
4047 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004048
4049 /* Global variables */
4050 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004052 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004053 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004054 else
4055 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004056 while (HASHITEM_EMPTY(hi))
4057 ++hi;
4058 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4059 return cat_prefix_varname('g', hi->hi_key);
4060 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004062
4063 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004064 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004065 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004067 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004068 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004069 else
4070 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004071 while (HASHITEM_EMPTY(hi))
4072 ++hi;
4073 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004075 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004077 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 return (char_u *)"b:changedtick";
4079 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004080
4081 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004082 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004083 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004085 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004086 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004087 else
4088 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004089 while (HASHITEM_EMPTY(hi))
4090 ++hi;
4091 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004093
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004094#ifdef FEAT_WINDOWS
4095 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004096 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004097 if (tdone < ht->ht_used)
4098 {
4099 if (tdone++ == 0)
4100 hi = ht->ht_array;
4101 else
4102 ++hi;
4103 while (HASHITEM_EMPTY(hi))
4104 ++hi;
4105 return cat_prefix_varname('t', hi->hi_key);
4106 }
4107#endif
4108
Bram Moolenaar33570922005-01-25 22:26:29 +00004109 /* v: variables */
4110 if (vidx < VV_LEN)
4111 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112
4113 vim_free(varnamebuf);
4114 varnamebuf = NULL;
4115 varnamebuflen = 0;
4116 return NULL;
4117}
4118
4119#endif /* FEAT_CMDL_COMPL */
4120
4121/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004122 * Return TRUE if "pat" matches "text".
4123 * Does not use 'cpo' and always uses 'magic'.
4124 */
4125 static int
4126pattern_match(char_u *pat, char_u *text, int ic)
4127{
4128 int matches = FALSE;
4129 char_u *save_cpo;
4130 regmatch_T regmatch;
4131
4132 /* avoid 'l' flag in 'cpoptions' */
4133 save_cpo = p_cpo;
4134 p_cpo = (char_u *)"";
4135 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
4136 if (regmatch.regprog != NULL)
4137 {
4138 regmatch.rm_ic = ic;
4139 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
4140 vim_regfree(regmatch.regprog);
4141 }
4142 p_cpo = save_cpo;
4143 return matches;
4144}
4145
4146/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 * types for expressions.
4148 */
4149typedef enum
4150{
4151 TYPE_UNKNOWN = 0
4152 , TYPE_EQUAL /* == */
4153 , TYPE_NEQUAL /* != */
4154 , TYPE_GREATER /* > */
4155 , TYPE_GEQUAL /* >= */
4156 , TYPE_SMALLER /* < */
4157 , TYPE_SEQUAL /* <= */
4158 , TYPE_MATCH /* =~ */
4159 , TYPE_NOMATCH /* !~ */
4160} exptype_T;
4161
4162/*
4163 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004164 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4166 */
4167
4168/*
4169 * Handle zero level expression.
4170 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004171 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004172 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 * Return OK or FAIL.
4174 */
4175 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004176eval0(
4177 char_u *arg,
4178 typval_T *rettv,
4179 char_u **nextcmd,
4180 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181{
4182 int ret;
4183 char_u *p;
4184
4185 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004186 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 if (ret == FAIL || !ends_excmd(*p))
4188 {
4189 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004190 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 /*
4192 * Report the invalid expression unless the expression evaluation has
4193 * been cancelled due to an aborting error, an interrupt, or an
4194 * exception.
4195 */
4196 if (!aborting())
4197 EMSG2(_(e_invexpr2), arg);
4198 ret = FAIL;
4199 }
4200 if (nextcmd != NULL)
4201 *nextcmd = check_nextcmd(p);
4202
4203 return ret;
4204}
4205
4206/*
4207 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004208 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 *
4210 * "arg" must point to the first non-white of the expression.
4211 * "arg" is advanced to the next non-white after the recognized expression.
4212 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004213 * Note: "rettv.v_lock" is not set.
4214 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 * Return OK or FAIL.
4216 */
4217 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004218eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219{
4220 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004221 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222
4223 /*
4224 * Get the first variable.
4225 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004226 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 return FAIL;
4228
4229 if ((*arg)[0] == '?')
4230 {
4231 result = FALSE;
4232 if (evaluate)
4233 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004234 int error = FALSE;
4235
4236 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004238 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004239 if (error)
4240 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 }
4242
4243 /*
4244 * Get the second variable.
4245 */
4246 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004247 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 return FAIL;
4249
4250 /*
4251 * Check for the ":".
4252 */
4253 if ((*arg)[0] != ':')
4254 {
4255 EMSG(_("E109: Missing ':' after '?'"));
4256 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004257 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 return FAIL;
4259 }
4260
4261 /*
4262 * Get the third variable.
4263 */
4264 *arg = skipwhite(*arg + 1);
4265 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4266 {
4267 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004268 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 return FAIL;
4270 }
4271 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004272 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 }
4274
4275 return OK;
4276}
4277
4278/*
4279 * Handle first level expression:
4280 * expr2 || expr2 || expr2 logical OR
4281 *
4282 * "arg" must point to the first non-white of the expression.
4283 * "arg" is advanced to the next non-white after the recognized expression.
4284 *
4285 * Return OK or FAIL.
4286 */
4287 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004288eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289{
Bram Moolenaar33570922005-01-25 22:26:29 +00004290 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 long result;
4292 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004293 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294
4295 /*
4296 * Get the first variable.
4297 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004298 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 return FAIL;
4300
4301 /*
4302 * Repeat until there is no following "||".
4303 */
4304 first = TRUE;
4305 result = FALSE;
4306 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4307 {
4308 if (evaluate && first)
4309 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004310 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004312 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004313 if (error)
4314 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 first = FALSE;
4316 }
4317
4318 /*
4319 * Get the second variable.
4320 */
4321 *arg = skipwhite(*arg + 2);
4322 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4323 return FAIL;
4324
4325 /*
4326 * Compute the result.
4327 */
4328 if (evaluate && !result)
4329 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004330 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004332 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004333 if (error)
4334 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 }
4336 if (evaluate)
4337 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004338 rettv->v_type = VAR_NUMBER;
4339 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 }
4341 }
4342
4343 return OK;
4344}
4345
4346/*
4347 * Handle second level expression:
4348 * expr3 && expr3 && expr3 logical AND
4349 *
4350 * "arg" must point to the first non-white of the expression.
4351 * "arg" is advanced to the next non-white after the recognized expression.
4352 *
4353 * Return OK or FAIL.
4354 */
4355 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004356eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357{
Bram Moolenaar33570922005-01-25 22:26:29 +00004358 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 long result;
4360 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004361 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362
4363 /*
4364 * Get the first variable.
4365 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004366 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 return FAIL;
4368
4369 /*
4370 * Repeat until there is no following "&&".
4371 */
4372 first = TRUE;
4373 result = TRUE;
4374 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4375 {
4376 if (evaluate && first)
4377 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004378 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004380 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004381 if (error)
4382 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 first = FALSE;
4384 }
4385
4386 /*
4387 * Get the second variable.
4388 */
4389 *arg = skipwhite(*arg + 2);
4390 if (eval4(arg, &var2, evaluate && result) == FAIL)
4391 return FAIL;
4392
4393 /*
4394 * Compute the result.
4395 */
4396 if (evaluate && result)
4397 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004398 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004400 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004401 if (error)
4402 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 }
4404 if (evaluate)
4405 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004406 rettv->v_type = VAR_NUMBER;
4407 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 }
4409 }
4410
4411 return OK;
4412}
4413
4414/*
4415 * Handle third level expression:
4416 * var1 == var2
4417 * var1 =~ var2
4418 * var1 != var2
4419 * var1 !~ var2
4420 * var1 > var2
4421 * var1 >= var2
4422 * var1 < var2
4423 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004424 * var1 is var2
4425 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 *
4427 * "arg" must point to the first non-white of the expression.
4428 * "arg" is advanced to the next non-white after the recognized expression.
4429 *
4430 * Return OK or FAIL.
4431 */
4432 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004433eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434{
Bram Moolenaar33570922005-01-25 22:26:29 +00004435 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 char_u *p;
4437 int i;
4438 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004439 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 int len = 2;
4441 long n1, n2;
4442 char_u *s1, *s2;
4443 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445
4446 /*
4447 * Get the first variable.
4448 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004449 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 return FAIL;
4451
4452 p = *arg;
4453 switch (p[0])
4454 {
4455 case '=': if (p[1] == '=')
4456 type = TYPE_EQUAL;
4457 else if (p[1] == '~')
4458 type = TYPE_MATCH;
4459 break;
4460 case '!': if (p[1] == '=')
4461 type = TYPE_NEQUAL;
4462 else if (p[1] == '~')
4463 type = TYPE_NOMATCH;
4464 break;
4465 case '>': if (p[1] != '=')
4466 {
4467 type = TYPE_GREATER;
4468 len = 1;
4469 }
4470 else
4471 type = TYPE_GEQUAL;
4472 break;
4473 case '<': if (p[1] != '=')
4474 {
4475 type = TYPE_SMALLER;
4476 len = 1;
4477 }
4478 else
4479 type = TYPE_SEQUAL;
4480 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004481 case 'i': if (p[1] == 's')
4482 {
4483 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4484 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004485 i = p[len];
4486 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004487 {
4488 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4489 type_is = TRUE;
4490 }
4491 }
4492 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 }
4494
4495 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004496 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 */
4498 if (type != TYPE_UNKNOWN)
4499 {
4500 /* extra question mark appended: ignore case */
4501 if (p[len] == '?')
4502 {
4503 ic = TRUE;
4504 ++len;
4505 }
4506 /* extra '#' appended: match case */
4507 else if (p[len] == '#')
4508 {
4509 ic = FALSE;
4510 ++len;
4511 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004512 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 else
4514 ic = p_ic;
4515
4516 /*
4517 * Get the second variable.
4518 */
4519 *arg = skipwhite(p + len);
4520 if (eval5(arg, &var2, evaluate) == FAIL)
4521 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004522 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 return FAIL;
4524 }
4525
4526 if (evaluate)
4527 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004528 if (type_is && rettv->v_type != var2.v_type)
4529 {
4530 /* For "is" a different type always means FALSE, for "notis"
4531 * it means TRUE. */
4532 n1 = (type == TYPE_NEQUAL);
4533 }
4534 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4535 {
4536 if (type_is)
4537 {
4538 n1 = (rettv->v_type == var2.v_type
4539 && rettv->vval.v_list == var2.vval.v_list);
4540 if (type == TYPE_NEQUAL)
4541 n1 = !n1;
4542 }
4543 else if (rettv->v_type != var2.v_type
4544 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4545 {
4546 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004547 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004548 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004549 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004550 clear_tv(rettv);
4551 clear_tv(&var2);
4552 return FAIL;
4553 }
4554 else
4555 {
4556 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004557 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4558 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004559 if (type == TYPE_NEQUAL)
4560 n1 = !n1;
4561 }
4562 }
4563
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004564 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4565 {
4566 if (type_is)
4567 {
4568 n1 = (rettv->v_type == var2.v_type
4569 && rettv->vval.v_dict == var2.vval.v_dict);
4570 if (type == TYPE_NEQUAL)
4571 n1 = !n1;
4572 }
4573 else if (rettv->v_type != var2.v_type
4574 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4575 {
4576 if (rettv->v_type != var2.v_type)
4577 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4578 else
4579 EMSG(_("E736: Invalid operation for Dictionary"));
4580 clear_tv(rettv);
4581 clear_tv(&var2);
4582 return FAIL;
4583 }
4584 else
4585 {
4586 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004587 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4588 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004589 if (type == TYPE_NEQUAL)
4590 n1 = !n1;
4591 }
4592 }
4593
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004594 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4595 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004596 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004597 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004598 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004599 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004600 clear_tv(rettv);
4601 clear_tv(&var2);
4602 return FAIL;
4603 }
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004604 n1 = tv_equal(rettv, &var2, FALSE, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004605 if (type == TYPE_NEQUAL)
4606 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004607 }
4608
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004609#ifdef FEAT_FLOAT
4610 /*
4611 * If one of the two variables is a float, compare as a float.
4612 * When using "=~" or "!~", always compare as string.
4613 */
4614 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4615 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4616 {
4617 float_T f1, f2;
4618
4619 if (rettv->v_type == VAR_FLOAT)
4620 f1 = rettv->vval.v_float;
4621 else
4622 f1 = get_tv_number(rettv);
4623 if (var2.v_type == VAR_FLOAT)
4624 f2 = var2.vval.v_float;
4625 else
4626 f2 = get_tv_number(&var2);
4627 n1 = FALSE;
4628 switch (type)
4629 {
4630 case TYPE_EQUAL: n1 = (f1 == f2); break;
4631 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4632 case TYPE_GREATER: n1 = (f1 > f2); break;
4633 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4634 case TYPE_SMALLER: n1 = (f1 < f2); break;
4635 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4636 case TYPE_UNKNOWN:
4637 case TYPE_MATCH:
4638 case TYPE_NOMATCH: break; /* avoid gcc warning */
4639 }
4640 }
4641#endif
4642
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 /*
4644 * If one of the two variables is a number, compare as a number.
4645 * When using "=~" or "!~", always compare as string.
4646 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004647 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4649 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004650 n1 = get_tv_number(rettv);
4651 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 switch (type)
4653 {
4654 case TYPE_EQUAL: n1 = (n1 == n2); break;
4655 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4656 case TYPE_GREATER: n1 = (n1 > n2); break;
4657 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4658 case TYPE_SMALLER: n1 = (n1 < n2); break;
4659 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4660 case TYPE_UNKNOWN:
4661 case TYPE_MATCH:
4662 case TYPE_NOMATCH: break; /* avoid gcc warning */
4663 }
4664 }
4665 else
4666 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004667 s1 = get_tv_string_buf(rettv, buf1);
4668 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4670 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4671 else
4672 i = 0;
4673 n1 = FALSE;
4674 switch (type)
4675 {
4676 case TYPE_EQUAL: n1 = (i == 0); break;
4677 case TYPE_NEQUAL: n1 = (i != 0); break;
4678 case TYPE_GREATER: n1 = (i > 0); break;
4679 case TYPE_GEQUAL: n1 = (i >= 0); break;
4680 case TYPE_SMALLER: n1 = (i < 0); break;
4681 case TYPE_SEQUAL: n1 = (i <= 0); break;
4682
4683 case TYPE_MATCH:
4684 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004685 n1 = pattern_match(s2, s1, ic);
4686 if (type == TYPE_NOMATCH)
4687 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 break;
4689
4690 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4691 }
4692 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004693 clear_tv(rettv);
4694 clear_tv(&var2);
4695 rettv->v_type = VAR_NUMBER;
4696 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 }
4698 }
4699
4700 return OK;
4701}
4702
4703/*
4704 * Handle fourth level expression:
4705 * + number addition
4706 * - number subtraction
4707 * . string concatenation
4708 *
4709 * "arg" must point to the first non-white of the expression.
4710 * "arg" is advanced to the next non-white after the recognized expression.
4711 *
4712 * Return OK or FAIL.
4713 */
4714 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004715eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716{
Bram Moolenaar33570922005-01-25 22:26:29 +00004717 typval_T var2;
4718 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 int op;
4720 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004721#ifdef FEAT_FLOAT
4722 float_T f1 = 0, f2 = 0;
4723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 char_u *s1, *s2;
4725 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4726 char_u *p;
4727
4728 /*
4729 * Get the first variable.
4730 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004731 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 return FAIL;
4733
4734 /*
4735 * Repeat computing, until no '+', '-' or '.' is following.
4736 */
4737 for (;;)
4738 {
4739 op = **arg;
4740 if (op != '+' && op != '-' && op != '.')
4741 break;
4742
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004743 if ((op != '+' || rettv->v_type != VAR_LIST)
4744#ifdef FEAT_FLOAT
4745 && (op == '.' || rettv->v_type != VAR_FLOAT)
4746#endif
4747 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004748 {
4749 /* For "list + ...", an illegal use of the first operand as
4750 * a number cannot be determined before evaluating the 2nd
4751 * operand: if this is also a list, all is ok.
4752 * For "something . ...", "something - ..." or "non-list + ...",
4753 * we know that the first operand needs to be a string or number
4754 * without evaluating the 2nd operand. So check before to avoid
4755 * side effects after an error. */
4756 if (evaluate && get_tv_string_chk(rettv) == NULL)
4757 {
4758 clear_tv(rettv);
4759 return FAIL;
4760 }
4761 }
4762
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 /*
4764 * Get the second variable.
4765 */
4766 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004767 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004769 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 return FAIL;
4771 }
4772
4773 if (evaluate)
4774 {
4775 /*
4776 * Compute the result.
4777 */
4778 if (op == '.')
4779 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004780 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4781 s2 = get_tv_string_buf_chk(&var2, buf2);
4782 if (s2 == NULL) /* type error ? */
4783 {
4784 clear_tv(rettv);
4785 clear_tv(&var2);
4786 return FAIL;
4787 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004788 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004789 clear_tv(rettv);
4790 rettv->v_type = VAR_STRING;
4791 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004793 else if (op == '+' && rettv->v_type == VAR_LIST
4794 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004795 {
4796 /* concatenate Lists */
4797 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4798 &var3) == FAIL)
4799 {
4800 clear_tv(rettv);
4801 clear_tv(&var2);
4802 return FAIL;
4803 }
4804 clear_tv(rettv);
4805 *rettv = var3;
4806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 else
4808 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004809 int error = FALSE;
4810
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004811#ifdef FEAT_FLOAT
4812 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004813 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004814 f1 = rettv->vval.v_float;
4815 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004816 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004817 else
4818#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004819 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004820 n1 = get_tv_number_chk(rettv, &error);
4821 if (error)
4822 {
4823 /* This can only happen for "list + non-list". For
4824 * "non-list + ..." or "something - ...", we returned
4825 * before evaluating the 2nd operand. */
4826 clear_tv(rettv);
4827 return FAIL;
4828 }
4829#ifdef FEAT_FLOAT
4830 if (var2.v_type == VAR_FLOAT)
4831 f1 = n1;
4832#endif
4833 }
4834#ifdef FEAT_FLOAT
4835 if (var2.v_type == VAR_FLOAT)
4836 {
4837 f2 = var2.vval.v_float;
4838 n2 = 0;
4839 }
4840 else
4841#endif
4842 {
4843 n2 = get_tv_number_chk(&var2, &error);
4844 if (error)
4845 {
4846 clear_tv(rettv);
4847 clear_tv(&var2);
4848 return FAIL;
4849 }
4850#ifdef FEAT_FLOAT
4851 if (rettv->v_type == VAR_FLOAT)
4852 f2 = n2;
4853#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004854 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004855 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004856
4857#ifdef FEAT_FLOAT
4858 /* If there is a float on either side the result is a float. */
4859 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4860 {
4861 if (op == '+')
4862 f1 = f1 + f2;
4863 else
4864 f1 = f1 - f2;
4865 rettv->v_type = VAR_FLOAT;
4866 rettv->vval.v_float = f1;
4867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004869#endif
4870 {
4871 if (op == '+')
4872 n1 = n1 + n2;
4873 else
4874 n1 = n1 - n2;
4875 rettv->v_type = VAR_NUMBER;
4876 rettv->vval.v_number = n1;
4877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004879 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 }
4881 }
4882 return OK;
4883}
4884
4885/*
4886 * Handle fifth level expression:
4887 * * number multiplication
4888 * / number division
4889 * % number modulo
4890 *
4891 * "arg" must point to the first non-white of the expression.
4892 * "arg" is advanced to the next non-white after the recognized expression.
4893 *
4894 * Return OK or FAIL.
4895 */
4896 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004897eval6(
4898 char_u **arg,
4899 typval_T *rettv,
4900 int evaluate,
4901 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902{
Bram Moolenaar33570922005-01-25 22:26:29 +00004903 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 int op;
4905 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004906#ifdef FEAT_FLOAT
4907 int use_float = FALSE;
4908 float_T f1 = 0, f2;
4909#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004910 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911
4912 /*
4913 * Get the first variable.
4914 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004915 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 return FAIL;
4917
4918 /*
4919 * Repeat computing, until no '*', '/' or '%' is following.
4920 */
4921 for (;;)
4922 {
4923 op = **arg;
4924 if (op != '*' && op != '/' && op != '%')
4925 break;
4926
4927 if (evaluate)
4928 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004929#ifdef FEAT_FLOAT
4930 if (rettv->v_type == VAR_FLOAT)
4931 {
4932 f1 = rettv->vval.v_float;
4933 use_float = TRUE;
4934 n1 = 0;
4935 }
4936 else
4937#endif
4938 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004939 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004940 if (error)
4941 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 }
4943 else
4944 n1 = 0;
4945
4946 /*
4947 * Get the second variable.
4948 */
4949 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004950 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 return FAIL;
4952
4953 if (evaluate)
4954 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004955#ifdef FEAT_FLOAT
4956 if (var2.v_type == VAR_FLOAT)
4957 {
4958 if (!use_float)
4959 {
4960 f1 = n1;
4961 use_float = TRUE;
4962 }
4963 f2 = var2.vval.v_float;
4964 n2 = 0;
4965 }
4966 else
4967#endif
4968 {
4969 n2 = get_tv_number_chk(&var2, &error);
4970 clear_tv(&var2);
4971 if (error)
4972 return FAIL;
4973#ifdef FEAT_FLOAT
4974 if (use_float)
4975 f2 = n2;
4976#endif
4977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978
4979 /*
4980 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004981 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004983#ifdef FEAT_FLOAT
4984 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004986 if (op == '*')
4987 f1 = f1 * f2;
4988 else if (op == '/')
4989 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004990# ifdef VMS
4991 /* VMS crashes on divide by zero, work around it */
4992 if (f2 == 0.0)
4993 {
4994 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004995 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004996 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004997 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004998 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004999 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005000 }
5001 else
5002 f1 = f1 / f2;
5003# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005004 /* We rely on the floating point library to handle divide
5005 * by zero to result in "inf" and not a crash. */
5006 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005007# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005010 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005011 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005012 return FAIL;
5013 }
5014 rettv->v_type = VAR_FLOAT;
5015 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 }
5017 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005020 if (op == '*')
5021 n1 = n1 * n2;
5022 else if (op == '/')
5023 {
5024 if (n2 == 0) /* give an error message? */
5025 {
5026 if (n1 == 0)
5027 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5028 else if (n1 < 0)
5029 n1 = -0x7fffffffL;
5030 else
5031 n1 = 0x7fffffffL;
5032 }
5033 else
5034 n1 = n1 / n2;
5035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005037 {
5038 if (n2 == 0) /* give an error message? */
5039 n1 = 0;
5040 else
5041 n1 = n1 % n2;
5042 }
5043 rettv->v_type = VAR_NUMBER;
5044 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 }
5047 }
5048
5049 return OK;
5050}
5051
5052/*
5053 * Handle sixth level expression:
5054 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005055 * "string" string constant
5056 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 * &option-name option value
5058 * @r register contents
5059 * identifier variable value
5060 * function() function call
5061 * $VAR environment variable
5062 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005063 * [expr, expr] List
5064 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 *
5066 * Also handle:
5067 * ! in front logical NOT
5068 * - in front unary minus
5069 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005070 * trailing [] subscript in String or List
5071 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 *
5073 * "arg" must point to the first non-white of the expression.
5074 * "arg" is advanced to the next non-white after the recognized expression.
5075 *
5076 * Return OK or FAIL.
5077 */
5078 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005079eval7(
5080 char_u **arg,
5081 typval_T *rettv,
5082 int evaluate,
5083 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 long n;
5086 int len;
5087 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 char_u *start_leader, *end_leader;
5089 int ret = OK;
5090 char_u *alias;
5091
5092 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005093 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005094 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005096 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097
5098 /*
5099 * Skip '!' and '-' characters. They are handled later.
5100 */
5101 start_leader = *arg;
5102 while (**arg == '!' || **arg == '-' || **arg == '+')
5103 *arg = skipwhite(*arg + 1);
5104 end_leader = *arg;
5105
5106 switch (**arg)
5107 {
5108 /*
5109 * Number constant.
5110 */
5111 case '0':
5112 case '1':
5113 case '2':
5114 case '3':
5115 case '4':
5116 case '5':
5117 case '6':
5118 case '7':
5119 case '8':
5120 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005121 {
5122#ifdef FEAT_FLOAT
5123 char_u *p = skipdigits(*arg + 1);
5124 int get_float = FALSE;
5125
5126 /* We accept a float when the format matches
5127 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005128 * strict to avoid backwards compatibility problems.
5129 * Don't look for a float after the "." operator, so that
5130 * ":let vers = 1.2.3" doesn't fail. */
5131 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005133 get_float = TRUE;
5134 p = skipdigits(p + 2);
5135 if (*p == 'e' || *p == 'E')
5136 {
5137 ++p;
5138 if (*p == '-' || *p == '+')
5139 ++p;
5140 if (!vim_isdigit(*p))
5141 get_float = FALSE;
5142 else
5143 p = skipdigits(p + 1);
5144 }
5145 if (ASCII_ISALPHA(*p) || *p == '.')
5146 get_float = FALSE;
5147 }
5148 if (get_float)
5149 {
5150 float_T f;
5151
5152 *arg += string2float(*arg, &f);
5153 if (evaluate)
5154 {
5155 rettv->v_type = VAR_FLOAT;
5156 rettv->vval.v_float = f;
5157 }
5158 }
5159 else
5160#endif
5161 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005162 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005163 *arg += len;
5164 if (evaluate)
5165 {
5166 rettv->v_type = VAR_NUMBER;
5167 rettv->vval.v_number = n;
5168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 }
5170 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172
5173 /*
5174 * String constant: "string".
5175 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005176 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 break;
5178
5179 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005180 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005182 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005183 break;
5184
5185 /*
5186 * List: [expr, expr]
5187 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005188 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 break;
5190
5191 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005192 * Dictionary: {key: val, key: val}
5193 */
5194 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5195 break;
5196
5197 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005198 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005200 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 break;
5202
5203 /*
5204 * Environment variable: $VAR.
5205 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005206 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 break;
5208
5209 /*
5210 * Register contents: @r.
5211 */
5212 case '@': ++*arg;
5213 if (evaluate)
5214 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005215 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005216 rettv->vval.v_string = get_reg_contents(**arg,
5217 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 }
5219 if (**arg != NUL)
5220 ++*arg;
5221 break;
5222
5223 /*
5224 * nested expression: (expression).
5225 */
5226 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005227 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 if (**arg == ')')
5229 ++*arg;
5230 else if (ret == OK)
5231 {
5232 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005233 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 ret = FAIL;
5235 }
5236 break;
5237
Bram Moolenaar8c711452005-01-14 21:53:12 +00005238 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 break;
5240 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005241
5242 if (ret == NOTDONE)
5243 {
5244 /*
5245 * Must be a variable or function name.
5246 * Can also be a curly-braces kind of name: {expr}.
5247 */
5248 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005249 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005250 if (alias != NULL)
5251 s = alias;
5252
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005253 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005254 ret = FAIL;
5255 else
5256 {
5257 if (**arg == '(') /* recursive! */
5258 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005259 partial_T *partial;
5260
Bram Moolenaar8c711452005-01-14 21:53:12 +00005261 /* If "s" is the name of a variable of type VAR_FUNC
5262 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005263 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005264
5265 /* Invoke the function. */
5266 ret = get_func_tv(s, len, rettv, arg,
5267 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005268 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005269
5270 /* If evaluate is FALSE rettv->v_type was not set in
5271 * get_func_tv, but it's needed in handle_subscript() to parse
5272 * what follows. So set it here. */
5273 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5274 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005275 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005276 rettv->v_type = VAR_FUNC;
5277 }
5278
Bram Moolenaar8c711452005-01-14 21:53:12 +00005279 /* Stop the expression evaluation when immediately
5280 * aborting on error, or when an interrupt occurred or
5281 * an exception was thrown but not caught. */
5282 if (aborting())
5283 {
5284 if (ret == OK)
5285 clear_tv(rettv);
5286 ret = FAIL;
5287 }
5288 }
5289 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005290 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005291 else
5292 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005293 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005294 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005295 }
5296
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 *arg = skipwhite(*arg);
5298
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005299 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5300 * expr(expr). */
5301 if (ret == OK)
5302 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303
5304 /*
5305 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5306 */
5307 if (ret == OK && evaluate && end_leader > start_leader)
5308 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005309 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005310 int val = 0;
5311#ifdef FEAT_FLOAT
5312 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005313
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005314 if (rettv->v_type == VAR_FLOAT)
5315 f = rettv->vval.v_float;
5316 else
5317#endif
5318 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005319 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005321 clear_tv(rettv);
5322 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005324 else
5325 {
5326 while (end_leader > start_leader)
5327 {
5328 --end_leader;
5329 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005330 {
5331#ifdef FEAT_FLOAT
5332 if (rettv->v_type == VAR_FLOAT)
5333 f = !f;
5334 else
5335#endif
5336 val = !val;
5337 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005338 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005339 {
5340#ifdef FEAT_FLOAT
5341 if (rettv->v_type == VAR_FLOAT)
5342 f = -f;
5343 else
5344#endif
5345 val = -val;
5346 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005347 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005348#ifdef FEAT_FLOAT
5349 if (rettv->v_type == VAR_FLOAT)
5350 {
5351 clear_tv(rettv);
5352 rettv->vval.v_float = f;
5353 }
5354 else
5355#endif
5356 {
5357 clear_tv(rettv);
5358 rettv->v_type = VAR_NUMBER;
5359 rettv->vval.v_number = val;
5360 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 }
5363
5364 return ret;
5365}
5366
5367/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005368 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5369 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005370 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5371 */
5372 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005373eval_index(
5374 char_u **arg,
5375 typval_T *rettv,
5376 int evaluate,
5377 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005378{
5379 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005380 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005381 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005382 long len = -1;
5383 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005384 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005385 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005386
Bram Moolenaara03f2332016-02-06 18:09:59 +01005387 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005388 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005389 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005390 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005391 if (verbose)
5392 EMSG(_("E695: Cannot index a Funcref"));
5393 return FAIL;
5394 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005395#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005396 if (verbose)
5397 EMSG(_(e_float_as_string));
5398 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005399#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005400 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005401 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005402 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005403 if (verbose)
5404 EMSG(_("E909: Cannot index a special variable"));
5405 return FAIL;
5406 case VAR_UNKNOWN:
5407 if (evaluate)
5408 return FAIL;
5409 /* FALLTHROUGH */
5410
5411 case VAR_STRING:
5412 case VAR_NUMBER:
5413 case VAR_LIST:
5414 case VAR_DICT:
5415 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005416 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005417
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005418 init_tv(&var1);
5419 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005420 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005421 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005422 /*
5423 * dict.name
5424 */
5425 key = *arg + 1;
5426 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5427 ;
5428 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005429 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005430 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005431 }
5432 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005433 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005434 /*
5435 * something[idx]
5436 *
5437 * Get the (first) variable from inside the [].
5438 */
5439 *arg = skipwhite(*arg + 1);
5440 if (**arg == ':')
5441 empty1 = TRUE;
5442 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5443 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005444 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5445 {
5446 /* not a number or string */
5447 clear_tv(&var1);
5448 return FAIL;
5449 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005450
5451 /*
5452 * Get the second variable from inside the [:].
5453 */
5454 if (**arg == ':')
5455 {
5456 range = TRUE;
5457 *arg = skipwhite(*arg + 1);
5458 if (**arg == ']')
5459 empty2 = TRUE;
5460 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5461 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005462 if (!empty1)
5463 clear_tv(&var1);
5464 return FAIL;
5465 }
5466 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5467 {
5468 /* not a number or string */
5469 if (!empty1)
5470 clear_tv(&var1);
5471 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005472 return FAIL;
5473 }
5474 }
5475
5476 /* Check for the ']'. */
5477 if (**arg != ']')
5478 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005479 if (verbose)
5480 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005481 clear_tv(&var1);
5482 if (range)
5483 clear_tv(&var2);
5484 return FAIL;
5485 }
5486 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005487 }
5488
5489 if (evaluate)
5490 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005491 n1 = 0;
5492 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005493 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005494 n1 = get_tv_number(&var1);
5495 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005496 }
5497 if (range)
5498 {
5499 if (empty2)
5500 n2 = -1;
5501 else
5502 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005503 n2 = get_tv_number(&var2);
5504 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005505 }
5506 }
5507
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005508 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005509 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005510 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005511 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005512 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005513 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005514 case VAR_SPECIAL:
5515 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005516 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005517 break; /* not evaluating, skipping over subscript */
5518
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005519 case VAR_NUMBER:
5520 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005521 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005522 len = (long)STRLEN(s);
5523 if (range)
5524 {
5525 /* The resulting variable is a substring. If the indexes
5526 * are out of range the result is empty. */
5527 if (n1 < 0)
5528 {
5529 n1 = len + n1;
5530 if (n1 < 0)
5531 n1 = 0;
5532 }
5533 if (n2 < 0)
5534 n2 = len + n2;
5535 else if (n2 >= len)
5536 n2 = len;
5537 if (n1 >= len || n2 < 0 || n1 > n2)
5538 s = NULL;
5539 else
5540 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5541 }
5542 else
5543 {
5544 /* The resulting variable is a string of a single
5545 * character. If the index is too big or negative the
5546 * result is empty. */
5547 if (n1 >= len || n1 < 0)
5548 s = NULL;
5549 else
5550 s = vim_strnsave(s + n1, 1);
5551 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005552 clear_tv(rettv);
5553 rettv->v_type = VAR_STRING;
5554 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005555 break;
5556
5557 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005558 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005559 if (n1 < 0)
5560 n1 = len + n1;
5561 if (!empty1 && (n1 < 0 || n1 >= len))
5562 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005563 /* For a range we allow invalid values and return an empty
5564 * list. A list index out of range is an error. */
5565 if (!range)
5566 {
5567 if (verbose)
5568 EMSGN(_(e_listidx), n1);
5569 return FAIL;
5570 }
5571 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005572 }
5573 if (range)
5574 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005575 list_T *l;
5576 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005577
5578 if (n2 < 0)
5579 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005580 else if (n2 >= len)
5581 n2 = len - 1;
5582 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005583 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005584 l = list_alloc();
5585 if (l == NULL)
5586 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005587 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005588 n1 <= n2; ++n1)
5589 {
5590 if (list_append_tv(l, &item->li_tv) == FAIL)
5591 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005592 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005593 return FAIL;
5594 }
5595 item = item->li_next;
5596 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005597 clear_tv(rettv);
5598 rettv->v_type = VAR_LIST;
5599 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005600 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005601 }
5602 else
5603 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005604 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005605 clear_tv(rettv);
5606 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005607 }
5608 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005609
5610 case VAR_DICT:
5611 if (range)
5612 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005613 if (verbose)
5614 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005615 if (len == -1)
5616 clear_tv(&var1);
5617 return FAIL;
5618 }
5619 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005620 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005621
5622 if (len == -1)
5623 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005624 key = get_tv_string_chk(&var1);
5625 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005626 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005627 clear_tv(&var1);
5628 return FAIL;
5629 }
5630 }
5631
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005632 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005633
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005634 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005635 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005636 if (len == -1)
5637 clear_tv(&var1);
5638 if (item == NULL)
5639 return FAIL;
5640
5641 copy_tv(&item->di_tv, &var1);
5642 clear_tv(rettv);
5643 *rettv = var1;
5644 }
5645 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005646 }
5647 }
5648
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005649 return OK;
5650}
5651
5652/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 * Get an option value.
5654 * "arg" points to the '&' or '+' before the option name.
5655 * "arg" is advanced to character after the option name.
5656 * Return OK or FAIL.
5657 */
5658 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005659get_option_tv(
5660 char_u **arg,
5661 typval_T *rettv, /* when NULL, only check if option exists */
5662 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663{
5664 char_u *option_end;
5665 long numval;
5666 char_u *stringval;
5667 int opt_type;
5668 int c;
5669 int working = (**arg == '+'); /* has("+option") */
5670 int ret = OK;
5671 int opt_flags;
5672
5673 /*
5674 * Isolate the option name and find its value.
5675 */
5676 option_end = find_option_end(arg, &opt_flags);
5677 if (option_end == NULL)
5678 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005679 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 EMSG2(_("E112: Option name missing: %s"), *arg);
5681 return FAIL;
5682 }
5683
5684 if (!evaluate)
5685 {
5686 *arg = option_end;
5687 return OK;
5688 }
5689
5690 c = *option_end;
5691 *option_end = NUL;
5692 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005693 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694
5695 if (opt_type == -3) /* invalid name */
5696 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005697 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 EMSG2(_("E113: Unknown option: %s"), *arg);
5699 ret = FAIL;
5700 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005701 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 {
5703 if (opt_type == -2) /* hidden string option */
5704 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005705 rettv->v_type = VAR_STRING;
5706 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 }
5708 else if (opt_type == -1) /* hidden number option */
5709 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005710 rettv->v_type = VAR_NUMBER;
5711 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 }
5713 else if (opt_type == 1) /* number option */
5714 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005715 rettv->v_type = VAR_NUMBER;
5716 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 }
5718 else /* string option */
5719 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005720 rettv->v_type = VAR_STRING;
5721 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 }
5723 }
5724 else if (working && (opt_type == -2 || opt_type == -1))
5725 ret = FAIL;
5726
5727 *option_end = c; /* put back for error messages */
5728 *arg = option_end;
5729
5730 return ret;
5731}
5732
5733/*
5734 * Allocate a variable for a string constant.
5735 * Return OK or FAIL.
5736 */
5737 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005738get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739{
5740 char_u *p;
5741 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 int extra = 0;
5743
5744 /*
5745 * Find the end of the string, skipping backslashed characters.
5746 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005747 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 {
5749 if (*p == '\\' && p[1] != NUL)
5750 {
5751 ++p;
5752 /* A "\<x>" form occupies at least 4 characters, and produces up
5753 * to 6 characters: reserve space for 2 extra */
5754 if (*p == '<')
5755 extra += 2;
5756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 }
5758
5759 if (*p != '"')
5760 {
5761 EMSG2(_("E114: Missing quote: %s"), *arg);
5762 return FAIL;
5763 }
5764
5765 /* If only parsing, set *arg and return here */
5766 if (!evaluate)
5767 {
5768 *arg = p + 1;
5769 return OK;
5770 }
5771
5772 /*
5773 * Copy the string into allocated memory, handling backslashed
5774 * characters.
5775 */
5776 name = alloc((unsigned)(p - *arg + extra));
5777 if (name == NULL)
5778 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005779 rettv->v_type = VAR_STRING;
5780 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781
Bram Moolenaar8c711452005-01-14 21:53:12 +00005782 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 {
5784 if (*p == '\\')
5785 {
5786 switch (*++p)
5787 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005788 case 'b': *name++ = BS; ++p; break;
5789 case 'e': *name++ = ESC; ++p; break;
5790 case 'f': *name++ = FF; ++p; break;
5791 case 'n': *name++ = NL; ++p; break;
5792 case 'r': *name++ = CAR; ++p; break;
5793 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794
5795 case 'X': /* hex: "\x1", "\x12" */
5796 case 'x':
5797 case 'u': /* Unicode: "\u0023" */
5798 case 'U':
5799 if (vim_isxdigit(p[1]))
5800 {
5801 int n, nr;
5802 int c = toupper(*p);
5803
5804 if (c == 'X')
5805 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005806 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005808 else
5809 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 nr = 0;
5811 while (--n >= 0 && vim_isxdigit(p[1]))
5812 {
5813 ++p;
5814 nr = (nr << 4) + hex2nr(*p);
5815 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005816 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817#ifdef FEAT_MBYTE
5818 /* For "\u" store the number according to
5819 * 'encoding'. */
5820 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005821 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 else
5823#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005824 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 break;
5827
5828 /* octal: "\1", "\12", "\123" */
5829 case '0':
5830 case '1':
5831 case '2':
5832 case '3':
5833 case '4':
5834 case '5':
5835 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005836 case '7': *name = *p++ - '0';
5837 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005839 *name = (*name << 3) + *p++ - '0';
5840 if (*p >= '0' && *p <= '7')
5841 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005843 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 break;
5845
5846 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005847 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 if (extra != 0)
5849 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005850 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 break;
5852 }
5853 /* FALLTHROUGH */
5854
Bram Moolenaar8c711452005-01-14 21:53:12 +00005855 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 break;
5857 }
5858 }
5859 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005860 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005863 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 *arg = p + 1;
5865
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 return OK;
5867}
5868
5869/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005870 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 * Return OK or FAIL.
5872 */
5873 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005874get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875{
5876 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005877 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005878 int reduce = 0;
5879
5880 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005881 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005882 */
5883 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5884 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005885 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005886 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005887 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005888 break;
5889 ++reduce;
5890 ++p;
5891 }
5892 }
5893
Bram Moolenaar8c711452005-01-14 21:53:12 +00005894 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005895 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005896 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005897 return FAIL;
5898 }
5899
Bram Moolenaar8c711452005-01-14 21:53:12 +00005900 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005901 if (!evaluate)
5902 {
5903 *arg = p + 1;
5904 return OK;
5905 }
5906
5907 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005908 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005909 */
5910 str = alloc((unsigned)((p - *arg) - reduce));
5911 if (str == NULL)
5912 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005913 rettv->v_type = VAR_STRING;
5914 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005915
Bram Moolenaar8c711452005-01-14 21:53:12 +00005916 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005917 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005918 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005919 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005920 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005921 break;
5922 ++p;
5923 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005924 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005925 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005926 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005927 *arg = p + 1;
5928
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005929 return OK;
5930}
5931
5932/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005933 * Allocate a variable for a List and fill it from "*arg".
5934 * Return OK or FAIL.
5935 */
5936 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005937get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005938{
Bram Moolenaar33570922005-01-25 22:26:29 +00005939 list_T *l = NULL;
5940 typval_T tv;
5941 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005942
5943 if (evaluate)
5944 {
5945 l = list_alloc();
5946 if (l == NULL)
5947 return FAIL;
5948 }
5949
5950 *arg = skipwhite(*arg + 1);
5951 while (**arg != ']' && **arg != NUL)
5952 {
5953 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5954 goto failret;
5955 if (evaluate)
5956 {
5957 item = listitem_alloc();
5958 if (item != NULL)
5959 {
5960 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005961 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005962 list_append(l, item);
5963 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005964 else
5965 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005966 }
5967
5968 if (**arg == ']')
5969 break;
5970 if (**arg != ',')
5971 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005972 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005973 goto failret;
5974 }
5975 *arg = skipwhite(*arg + 1);
5976 }
5977
5978 if (**arg != ']')
5979 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005980 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005981failret:
5982 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005983 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005984 return FAIL;
5985 }
5986
5987 *arg = skipwhite(*arg + 1);
5988 if (evaluate)
5989 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005990 rettv->v_type = VAR_LIST;
5991 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005992 ++l->lv_refcount;
5993 }
5994
5995 return OK;
5996}
5997
5998/*
5999 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006000 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006001 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006002 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006003list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006004{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006005 list_T *l;
6006
6007 l = (list_T *)alloc_clear(sizeof(list_T));
6008 if (l != NULL)
6009 {
6010 /* Prepend the list to the list of lists for garbage collection. */
6011 if (first_list != NULL)
6012 first_list->lv_used_prev = l;
6013 l->lv_used_prev = NULL;
6014 l->lv_used_next = first_list;
6015 first_list = l;
6016 }
6017 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006018}
6019
6020/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006021 * Allocate an empty list for a return value.
6022 * Returns OK or FAIL.
6023 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006024 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006025rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006026{
6027 list_T *l = list_alloc();
6028
6029 if (l == NULL)
6030 return FAIL;
6031
6032 rettv->vval.v_list = l;
6033 rettv->v_type = VAR_LIST;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02006034 rettv->v_lock = 0;
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006035 ++l->lv_refcount;
6036 return OK;
6037}
6038
6039/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006040 * Unreference a list: decrement the reference count and free it when it
6041 * becomes zero.
6042 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006043 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006044list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006045{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006046 if (l != NULL && --l->lv_refcount <= 0)
6047 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006048}
6049
6050/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006051 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006052 * Ignores the reference count.
6053 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006054 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006055list_free(
6056 list_T *l,
6057 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006058{
Bram Moolenaar33570922005-01-25 22:26:29 +00006059 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006060
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006061 /* Remove the list from the list of lists for garbage collection. */
6062 if (l->lv_used_prev == NULL)
6063 first_list = l->lv_used_next;
6064 else
6065 l->lv_used_prev->lv_used_next = l->lv_used_next;
6066 if (l->lv_used_next != NULL)
6067 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6068
Bram Moolenaard9fba312005-06-26 22:34:35 +00006069 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006070 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006071 /* Remove the item before deleting it. */
6072 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006073 if (recurse || (item->li_tv.v_type != VAR_LIST
6074 && item->li_tv.v_type != VAR_DICT))
6075 clear_tv(&item->li_tv);
6076 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006077 }
6078 vim_free(l);
6079}
6080
6081/*
6082 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006083 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006084 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006085 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006086listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006087{
Bram Moolenaar33570922005-01-25 22:26:29 +00006088 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006089}
6090
6091/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006092 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006093 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006094 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006095listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006096{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006097 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006098 vim_free(item);
6099}
6100
6101/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006102 * Remove a list item from a List and free it. Also clears the value.
6103 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006104 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006105listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006106{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006107 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006108 listitem_free(item);
6109}
6110
6111/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006112 * Get the number of items in a list.
6113 */
6114 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006115list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006116{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006117 if (l == NULL)
6118 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006119 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006120}
6121
6122/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006123 * Return TRUE when two lists have exactly the same values.
6124 */
6125 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006126list_equal(
6127 list_T *l1,
6128 list_T *l2,
6129 int ic, /* ignore case for strings */
6130 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006131{
Bram Moolenaar33570922005-01-25 22:26:29 +00006132 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006133
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006134 if (l1 == NULL || l2 == NULL)
6135 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006136 if (l1 == l2)
6137 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006138 if (list_len(l1) != list_len(l2))
6139 return FALSE;
6140
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006141 for (item1 = l1->lv_first, item2 = l2->lv_first;
6142 item1 != NULL && item2 != NULL;
6143 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006144 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006145 return FALSE;
6146 return item1 == NULL && item2 == NULL;
6147}
6148
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006149/*
6150 * Return the dictitem that an entry in a hashtable points to.
6151 */
6152 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006153dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006154{
6155 return HI2DI(hi);
6156}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006157
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006158/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006159 * Return TRUE when two dictionaries have exactly the same key/values.
6160 */
6161 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006162dict_equal(
6163 dict_T *d1,
6164 dict_T *d2,
6165 int ic, /* ignore case for strings */
6166 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006167{
Bram Moolenaar33570922005-01-25 22:26:29 +00006168 hashitem_T *hi;
6169 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006170 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006171
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006172 if (d1 == NULL || d2 == NULL)
6173 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006174 if (d1 == d2)
6175 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006176 if (dict_len(d1) != dict_len(d2))
6177 return FALSE;
6178
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006179 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006180 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006181 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006182 if (!HASHITEM_EMPTY(hi))
6183 {
6184 item2 = dict_find(d2, hi->hi_key, -1);
6185 if (item2 == NULL)
6186 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006187 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006188 return FALSE;
6189 --todo;
6190 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006191 }
6192 return TRUE;
6193}
6194
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006195static int tv_equal_recurse_limit;
6196
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006197/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006198 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006199 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006200 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006201 */
6202 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006203tv_equal(
6204 typval_T *tv1,
6205 typval_T *tv2,
6206 int ic, /* ignore case */
6207 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006208{
6209 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006210 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006211 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006212 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006213
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006214 /* For VAR_FUNC and VAR_PARTIAL only compare the function name. */
6215 if ((tv1->v_type == VAR_FUNC
6216 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
6217 && (tv2->v_type == VAR_FUNC
6218 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
6219 {
6220 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
6221 : tv1->vval.v_partial->pt_name;
6222 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
6223 : tv2->vval.v_partial->pt_name;
6224 return (s1 != NULL && s2 != NULL && STRCMP(s1, s2) == 0);
6225 }
6226
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006227 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006228 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006229
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006230 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006231 * recursiveness to a limit. We guess they are equal then.
6232 * A fixed limit has the problem of still taking an awful long time.
6233 * Reduce the limit every time running into it. That should work fine for
6234 * deeply linked structures that are not recursively linked and catch
6235 * recursiveness quickly. */
6236 if (!recursive)
6237 tv_equal_recurse_limit = 1000;
6238 if (recursive_cnt >= tv_equal_recurse_limit)
6239 {
6240 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006241 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006242 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006243
6244 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006245 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006246 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006247 ++recursive_cnt;
6248 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6249 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006250 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006251
6252 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006253 ++recursive_cnt;
6254 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6255 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006256 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006257
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006258 case VAR_NUMBER:
6259 return tv1->vval.v_number == tv2->vval.v_number;
6260
6261 case VAR_STRING:
6262 s1 = get_tv_string_buf(tv1, buf1);
6263 s2 = get_tv_string_buf(tv2, buf2);
6264 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006265
6266 case VAR_SPECIAL:
6267 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006268
6269 case VAR_FLOAT:
6270#ifdef FEAT_FLOAT
6271 return tv1->vval.v_float == tv2->vval.v_float;
6272#endif
6273 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006274#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006275 return tv1->vval.v_job == tv2->vval.v_job;
6276#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006277 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006278#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006279 return tv1->vval.v_channel == tv2->vval.v_channel;
6280#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006281 case VAR_FUNC:
6282 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006283 case VAR_UNKNOWN:
6284 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006285 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006286
Bram Moolenaara03f2332016-02-06 18:09:59 +01006287 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6288 * does not equal anything, not even itself. */
6289 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006290}
6291
6292/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006293 * Locate item with index "n" in list "l" and return it.
6294 * A negative index is counted from the end; -1 is the last item.
6295 * Returns NULL when "n" is out of range.
6296 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006297 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006298list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006299{
Bram Moolenaar33570922005-01-25 22:26:29 +00006300 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006301 long idx;
6302
6303 if (l == NULL)
6304 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006305
6306 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006307 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006308 n = l->lv_len + n;
6309
6310 /* Check for index out of range. */
6311 if (n < 0 || n >= l->lv_len)
6312 return NULL;
6313
6314 /* When there is a cached index may start search from there. */
6315 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006316 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006317 if (n < l->lv_idx / 2)
6318 {
6319 /* closest to the start of the list */
6320 item = l->lv_first;
6321 idx = 0;
6322 }
6323 else if (n > (l->lv_idx + l->lv_len) / 2)
6324 {
6325 /* closest to the end of the list */
6326 item = l->lv_last;
6327 idx = l->lv_len - 1;
6328 }
6329 else
6330 {
6331 /* closest to the cached index */
6332 item = l->lv_idx_item;
6333 idx = l->lv_idx;
6334 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006335 }
6336 else
6337 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006338 if (n < l->lv_len / 2)
6339 {
6340 /* closest to the start of the list */
6341 item = l->lv_first;
6342 idx = 0;
6343 }
6344 else
6345 {
6346 /* closest to the end of the list */
6347 item = l->lv_last;
6348 idx = l->lv_len - 1;
6349 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006350 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006351
6352 while (n > idx)
6353 {
6354 /* search forward */
6355 item = item->li_next;
6356 ++idx;
6357 }
6358 while (n < idx)
6359 {
6360 /* search backward */
6361 item = item->li_prev;
6362 --idx;
6363 }
6364
6365 /* cache the used index */
6366 l->lv_idx = idx;
6367 l->lv_idx_item = item;
6368
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006369 return item;
6370}
6371
6372/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006373 * Get list item "l[idx]" as a number.
6374 */
6375 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006376list_find_nr(
6377 list_T *l,
6378 long idx,
6379 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006380{
6381 listitem_T *li;
6382
6383 li = list_find(l, idx);
6384 if (li == NULL)
6385 {
6386 if (errorp != NULL)
6387 *errorp = TRUE;
6388 return -1L;
6389 }
6390 return get_tv_number_chk(&li->li_tv, errorp);
6391}
6392
6393/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006394 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6395 */
6396 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006397list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006398{
6399 listitem_T *li;
6400
6401 li = list_find(l, idx - 1);
6402 if (li == NULL)
6403 {
6404 EMSGN(_(e_listidx), idx);
6405 return NULL;
6406 }
6407 return get_tv_string(&li->li_tv);
6408}
6409
6410/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006411 * Locate "item" list "l" and return its index.
6412 * Returns -1 when "item" is not in the list.
6413 */
6414 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006415list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006416{
6417 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006418 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006419
6420 if (l == NULL)
6421 return -1;
6422 idx = 0;
6423 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6424 ++idx;
6425 if (li == NULL)
6426 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006427 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006428}
6429
6430/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006431 * Append item "item" to the end of list "l".
6432 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006433 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006434list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006435{
6436 if (l->lv_last == NULL)
6437 {
6438 /* empty list */
6439 l->lv_first = item;
6440 l->lv_last = item;
6441 item->li_prev = NULL;
6442 }
6443 else
6444 {
6445 l->lv_last->li_next = item;
6446 item->li_prev = l->lv_last;
6447 l->lv_last = item;
6448 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006449 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006450 item->li_next = NULL;
6451}
6452
6453/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006454 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006455 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006456 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006457 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006458list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006459{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006460 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006461
Bram Moolenaar05159a02005-02-26 23:04:13 +00006462 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006463 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006464 copy_tv(tv, &li->li_tv);
6465 list_append(l, li);
6466 return OK;
6467}
6468
6469/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006470 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006471 * Return FAIL when out of memory.
6472 */
6473 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006474list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006475{
6476 listitem_T *li = listitem_alloc();
6477
6478 if (li == NULL)
6479 return FAIL;
6480 li->li_tv.v_type = VAR_DICT;
6481 li->li_tv.v_lock = 0;
6482 li->li_tv.vval.v_dict = dict;
6483 list_append(list, li);
6484 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006485 return OK;
6486}
6487
6488/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006489 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006490 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006491 * Returns FAIL when out of memory.
6492 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006493 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006494list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006495{
6496 listitem_T *li = listitem_alloc();
6497
6498 if (li == NULL)
6499 return FAIL;
6500 list_append(l, li);
6501 li->li_tv.v_type = VAR_STRING;
6502 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006503 if (str == NULL)
6504 li->li_tv.vval.v_string = NULL;
6505 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006506 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006507 return FAIL;
6508 return OK;
6509}
6510
6511/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006512 * Append "n" to list "l".
6513 * Returns FAIL when out of memory.
6514 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006515 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006516list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006517{
6518 listitem_T *li;
6519
6520 li = listitem_alloc();
6521 if (li == NULL)
6522 return FAIL;
6523 li->li_tv.v_type = VAR_NUMBER;
6524 li->li_tv.v_lock = 0;
6525 li->li_tv.vval.v_number = n;
6526 list_append(l, li);
6527 return OK;
6528}
6529
6530/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006531 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006532 * If "item" is NULL append at the end.
6533 * Return FAIL when out of memory.
6534 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006535 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006536list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006537{
Bram Moolenaar33570922005-01-25 22:26:29 +00006538 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006539
6540 if (ni == NULL)
6541 return FAIL;
6542 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006543 list_insert(l, ni, item);
6544 return OK;
6545}
6546
6547 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006548list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006549{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006550 if (item == NULL)
6551 /* Append new item at end of list. */
6552 list_append(l, ni);
6553 else
6554 {
6555 /* Insert new item before existing item. */
6556 ni->li_prev = item->li_prev;
6557 ni->li_next = item;
6558 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006559 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006560 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006561 ++l->lv_idx;
6562 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006563 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006564 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006565 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006566 l->lv_idx_item = NULL;
6567 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006568 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006569 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006570 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006571}
6572
6573/*
6574 * Extend "l1" with "l2".
6575 * If "bef" is NULL append at the end, otherwise insert before this item.
6576 * Returns FAIL when out of memory.
6577 */
6578 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006579list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006580{
Bram Moolenaar33570922005-01-25 22:26:29 +00006581 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006582 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006583
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006584 /* We also quit the loop when we have inserted the original item count of
6585 * the list, avoid a hang when we extend a list with itself. */
6586 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006587 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6588 return FAIL;
6589 return OK;
6590}
6591
6592/*
6593 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6594 * Return FAIL when out of memory.
6595 */
6596 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006597list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006598{
Bram Moolenaar33570922005-01-25 22:26:29 +00006599 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006600
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006601 if (l1 == NULL || l2 == NULL)
6602 return FAIL;
6603
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006604 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006605 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006606 if (l == NULL)
6607 return FAIL;
6608 tv->v_type = VAR_LIST;
6609 tv->vval.v_list = l;
6610
6611 /* append all items from the second list */
6612 return list_extend(l, l2, NULL);
6613}
6614
6615/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006616 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006617 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006618 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006619 * Returns NULL when out of memory.
6620 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006621 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006622list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006623{
Bram Moolenaar33570922005-01-25 22:26:29 +00006624 list_T *copy;
6625 listitem_T *item;
6626 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006627
6628 if (orig == NULL)
6629 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006630
6631 copy = list_alloc();
6632 if (copy != NULL)
6633 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006634 if (copyID != 0)
6635 {
6636 /* Do this before adding the items, because one of the items may
6637 * refer back to this list. */
6638 orig->lv_copyID = copyID;
6639 orig->lv_copylist = copy;
6640 }
6641 for (item = orig->lv_first; item != NULL && !got_int;
6642 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006643 {
6644 ni = listitem_alloc();
6645 if (ni == NULL)
6646 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006647 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006648 {
6649 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6650 {
6651 vim_free(ni);
6652 break;
6653 }
6654 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006655 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006656 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006657 list_append(copy, ni);
6658 }
6659 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006660 if (item != NULL)
6661 {
6662 list_unref(copy);
6663 copy = NULL;
6664 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006665 }
6666
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006667 return copy;
6668}
6669
6670/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006671 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006672 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006673 * This used to be called list_remove, but that conflicts with a Sun header
6674 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006675 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006676 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006677vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006678{
Bram Moolenaar33570922005-01-25 22:26:29 +00006679 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006680
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006681 /* notify watchers */
6682 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006683 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006684 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006685 list_fix_watch(l, ip);
6686 if (ip == item2)
6687 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006688 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006689
6690 if (item2->li_next == NULL)
6691 l->lv_last = item->li_prev;
6692 else
6693 item2->li_next->li_prev = item->li_prev;
6694 if (item->li_prev == NULL)
6695 l->lv_first = item2->li_next;
6696 else
6697 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006698 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006699}
6700
6701/*
6702 * Return an allocated string with the string representation of a list.
6703 * May return NULL.
6704 */
6705 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006706list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006707{
6708 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006709
6710 if (tv->vval.v_list == NULL)
6711 return NULL;
6712 ga_init2(&ga, (int)sizeof(char), 80);
6713 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006714 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006715 {
6716 vim_free(ga.ga_data);
6717 return NULL;
6718 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006719 ga_append(&ga, ']');
6720 ga_append(&ga, NUL);
6721 return (char_u *)ga.ga_data;
6722}
6723
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006724typedef struct join_S {
6725 char_u *s;
6726 char_u *tofree;
6727} join_T;
6728
6729 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006730list_join_inner(
6731 garray_T *gap, /* to store the result in */
6732 list_T *l,
6733 char_u *sep,
6734 int echo_style,
6735 int copyID,
6736 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006737{
6738 int i;
6739 join_T *p;
6740 int len;
6741 int sumlen = 0;
6742 int first = TRUE;
6743 char_u *tofree;
6744 char_u numbuf[NUMBUFLEN];
6745 listitem_T *item;
6746 char_u *s;
6747
6748 /* Stringify each item in the list. */
6749 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6750 {
6751 if (echo_style)
6752 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6753 else
6754 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6755 if (s == NULL)
6756 return FAIL;
6757
6758 len = (int)STRLEN(s);
6759 sumlen += len;
6760
Bram Moolenaarcde88542015-08-11 19:14:00 +02006761 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006762 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6763 if (tofree != NULL || s != numbuf)
6764 {
6765 p->s = s;
6766 p->tofree = tofree;
6767 }
6768 else
6769 {
6770 p->s = vim_strnsave(s, len);
6771 p->tofree = p->s;
6772 }
6773
6774 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006775 if (did_echo_string_emsg) /* recursion error, bail out */
6776 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006777 }
6778
6779 /* Allocate result buffer with its total size, avoid re-allocation and
6780 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6781 if (join_gap->ga_len >= 2)
6782 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6783 if (ga_grow(gap, sumlen + 2) == FAIL)
6784 return FAIL;
6785
6786 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6787 {
6788 if (first)
6789 first = FALSE;
6790 else
6791 ga_concat(gap, sep);
6792 p = ((join_T *)join_gap->ga_data) + i;
6793
6794 if (p->s != NULL)
6795 ga_concat(gap, p->s);
6796 line_breakcheck();
6797 }
6798
6799 return OK;
6800}
6801
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006802/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006803 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006804 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006805 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006806 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006807 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006808list_join(
6809 garray_T *gap,
6810 list_T *l,
6811 char_u *sep,
6812 int echo_style,
6813 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006814{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006815 garray_T join_ga;
6816 int retval;
6817 join_T *p;
6818 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006819
Bram Moolenaard39a7512015-04-16 22:51:22 +02006820 if (l->lv_len < 1)
6821 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006822 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6823 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6824
6825 /* Dispose each item in join_ga. */
6826 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006827 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006828 p = (join_T *)join_ga.ga_data;
6829 for (i = 0; i < join_ga.ga_len; ++i)
6830 {
6831 vim_free(p->tofree);
6832 ++p;
6833 }
6834 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006835 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006836
6837 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006838}
6839
6840/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006841 * Return the next (unique) copy ID.
6842 * Used for serializing nested structures.
6843 */
6844 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006845get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006846{
6847 current_copyID += COPYID_INC;
6848 return current_copyID;
6849}
6850
6851/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006852 * Garbage collection for lists and dictionaries.
6853 *
6854 * We use reference counts to be able to free most items right away when they
6855 * are no longer used. But for composite items it's possible that it becomes
6856 * unused while the reference count is > 0: When there is a recursive
6857 * reference. Example:
6858 * :let l = [1, 2, 3]
6859 * :let d = {9: l}
6860 * :let l[1] = d
6861 *
6862 * Since this is quite unusual we handle this with garbage collection: every
6863 * once in a while find out which lists and dicts are not referenced from any
6864 * variable.
6865 *
6866 * Here is a good reference text about garbage collection (refers to Python
6867 * but it applies to all reference-counting mechanisms):
6868 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006869 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006870
6871/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006872 * Do garbage collection for lists and dicts.
6873 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006874 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006875 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006876garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006877{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006878 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006879 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006880 buf_T *buf;
6881 win_T *wp;
6882 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006883 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006884 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006885 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006886#ifdef FEAT_WINDOWS
6887 tabpage_T *tp;
6888#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006889
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006890 /* Only do this once. */
6891 want_garbage_collect = FALSE;
6892 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006893 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006894
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006895 /* We advance by two because we add one for items referenced through
6896 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006897 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006898
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006899 /*
6900 * 1. Go through all accessible variables and mark all lists and dicts
6901 * with copyID.
6902 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006903
6904 /* Don't free variables in the previous_funccal list unless they are only
6905 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006906 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006907 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6908 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006909 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6910 NULL);
6911 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6912 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006913 }
6914
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006915 /* script-local variables */
6916 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006917 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006918
6919 /* buffer-local variables */
6920 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006921 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6922 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006923
6924 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006925 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006926 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6927 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006928#ifdef FEAT_AUTOCMD
6929 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006930 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6931 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006932#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006933
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006934#ifdef FEAT_WINDOWS
6935 /* tabpage-local variables */
6936 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006937 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6938 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006939#endif
6940
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006941 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006942 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006943
6944 /* function-local variables */
6945 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6946 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006947 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6948 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006949 }
6950
Bram Moolenaard812df62008-11-09 12:46:09 +00006951 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006952 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006953
Bram Moolenaar1dced572012-04-05 16:54:08 +02006954#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006955 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006956#endif
6957
Bram Moolenaardb913952012-06-29 12:54:53 +02006958#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006959 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006960#endif
6961
6962#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006963 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006964#endif
6965
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006966#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006967 abort = abort || set_ref_in_channel(copyID);
6968#endif
6969
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006970 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006971 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006972 /*
6973 * 2. Free lists and dictionaries that are not referenced.
6974 */
6975 did_free = free_unref_items(copyID);
6976
6977 /*
6978 * 3. Check if any funccal can be freed now.
6979 */
6980 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006981 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006982 if (can_free_funccal(*pfc, copyID))
6983 {
6984 fc = *pfc;
6985 *pfc = fc->caller;
6986 free_funccal(fc, TRUE);
6987 did_free = TRUE;
6988 did_free_funccal = TRUE;
6989 }
6990 else
6991 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006992 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006993 if (did_free_funccal)
6994 /* When a funccal was freed some more items might be garbage
6995 * collected, so run again. */
6996 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006997 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006998 else if (p_verbose > 0)
6999 {
7000 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
7001 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007002
7003 return did_free;
7004}
7005
7006/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01007007 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007008 */
7009 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007010free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007011{
Bram Moolenaare71eea82015-02-03 17:10:06 +01007012 dict_T *dd, *dd_next;
7013 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007014 int did_free = FALSE;
7015
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007016 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007017 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007018 */
7019 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007020 {
7021 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007022 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007023 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007024 /* Free the Dictionary and ordinary items it contains, but don't
7025 * recurse into Lists and Dictionaries, they will be in the list
7026 * of dicts or list of lists. */
7027 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007028 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007029 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007030 dd = dd_next;
7031 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007032
7033 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007034 * Go through the list of lists and free items without the copyID.
7035 * But don't free a list that has a watcher (used in a for loop), these
7036 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007037 */
7038 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007039 {
7040 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007041 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7042 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007043 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007044 /* Free the List and ordinary items it contains, but don't recurse
7045 * into Lists and Dictionaries, they will be in the list of dicts
7046 * or list of lists. */
7047 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007048 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007049 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007050 ll = ll_next;
7051 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007052
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007053 return did_free;
7054}
7055
7056/*
7057 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007058 * "list_stack" is used to add lists to be marked. Can be NULL.
7059 *
7060 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007061 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007062 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007063set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007064{
7065 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007066 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007067 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007068 hashtab_T *cur_ht;
7069 ht_stack_T *ht_stack = NULL;
7070 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007071
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007072 cur_ht = ht;
7073 for (;;)
7074 {
7075 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007076 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007077 /* Mark each item in the hashtab. If the item contains a hashtab
7078 * it is added to ht_stack, if it contains a list it is added to
7079 * list_stack. */
7080 todo = (int)cur_ht->ht_used;
7081 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7082 if (!HASHITEM_EMPTY(hi))
7083 {
7084 --todo;
7085 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7086 &ht_stack, list_stack);
7087 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007088 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007089
7090 if (ht_stack == NULL)
7091 break;
7092
7093 /* take an item from the stack */
7094 cur_ht = ht_stack->ht;
7095 tempitem = ht_stack;
7096 ht_stack = ht_stack->prev;
7097 free(tempitem);
7098 }
7099
7100 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007101}
7102
7103/*
7104 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007105 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7106 *
7107 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007108 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007109 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007110set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007111{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007112 listitem_T *li;
7113 int abort = FALSE;
7114 list_T *cur_l;
7115 list_stack_T *list_stack = NULL;
7116 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007117
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007118 cur_l = l;
7119 for (;;)
7120 {
7121 if (!abort)
7122 /* Mark each item in the list. If the item contains a hashtab
7123 * it is added to ht_stack, if it contains a list it is added to
7124 * list_stack. */
7125 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7126 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7127 ht_stack, &list_stack);
7128 if (list_stack == NULL)
7129 break;
7130
7131 /* take an item from the stack */
7132 cur_l = list_stack->list;
7133 tempitem = list_stack;
7134 list_stack = list_stack->prev;
7135 free(tempitem);
7136 }
7137
7138 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007139}
7140
7141/*
7142 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007143 * "list_stack" is used to add lists to be marked. Can be NULL.
7144 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7145 *
7146 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007147 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007148 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007149set_ref_in_item(
7150 typval_T *tv,
7151 int copyID,
7152 ht_stack_T **ht_stack,
7153 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007154{
7155 dict_T *dd;
7156 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007157 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007158
Bram Moolenaar5f436fc2016-03-22 22:34:03 +01007159 if (tv->v_type == VAR_DICT || tv->v_type == VAR_PARTIAL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007160 {
Bram Moolenaar5f436fc2016-03-22 22:34:03 +01007161 if (tv->v_type == VAR_DICT)
7162 dd = tv->vval.v_dict;
7163 else if (tv->vval.v_partial != NULL)
7164 dd = tv->vval.v_partial->pt_dict;
7165 else
7166 dd = NULL;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007167 if (dd != NULL && dd->dv_copyID != copyID)
7168 {
7169 /* Didn't see this dict yet. */
7170 dd->dv_copyID = copyID;
7171 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007172 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007173 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7174 }
7175 else
7176 {
7177 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7178 if (newitem == NULL)
7179 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007180 else
7181 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007182 newitem->ht = &dd->dv_hashtab;
7183 newitem->prev = *ht_stack;
7184 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007185 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007186 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007187 }
7188 }
7189 else if (tv->v_type == VAR_LIST)
7190 {
7191 ll = tv->vval.v_list;
7192 if (ll != NULL && ll->lv_copyID != copyID)
7193 {
7194 /* Didn't see this list yet. */
7195 ll->lv_copyID = copyID;
7196 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007197 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007198 abort = set_ref_in_list(ll, copyID, ht_stack);
7199 }
7200 else
7201 {
7202 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007203 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007204 if (newitem == NULL)
7205 abort = TRUE;
7206 else
7207 {
7208 newitem->list = ll;
7209 newitem->prev = *list_stack;
7210 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007211 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007212 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007213 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007214 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007215 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007216}
7217
Bram Moolenaar5f436fc2016-03-22 22:34:03 +01007218 static void
7219partial_free(partial_T *pt, int free_dict)
7220{
7221 int i;
7222
7223 for (i = 0; i < pt->pt_argc; ++i)
7224 clear_tv(&pt->pt_argv[i]);
7225 vim_free(pt->pt_argv);
7226 if (free_dict)
7227 dict_unref(pt->pt_dict);
7228 func_unref(pt->pt_name);
7229 vim_free(pt->pt_name);
7230 vim_free(pt);
7231}
7232
7233/*
7234 * Unreference a closure: decrement the reference count and free it when it
7235 * becomes zero.
7236 */
7237 void
7238partial_unref(partial_T *pt)
7239{
7240 if (pt != NULL && --pt->pt_refcount <= 0)
7241 partial_free(pt, TRUE);
7242}
7243
Bram Moolenaard9fba312005-06-26 22:34:35 +00007244/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007245 * Allocate an empty header for a dictionary.
7246 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007247 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007248dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007249{
Bram Moolenaar33570922005-01-25 22:26:29 +00007250 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007251
Bram Moolenaar33570922005-01-25 22:26:29 +00007252 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007253 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007254 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007255 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007256 if (first_dict != NULL)
7257 first_dict->dv_used_prev = d;
7258 d->dv_used_next = first_dict;
7259 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007260 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007261
Bram Moolenaar33570922005-01-25 22:26:29 +00007262 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007263 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007264 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007265 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007266 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007267 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007268 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007269}
7270
7271/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007272 * Allocate an empty dict for a return value.
7273 * Returns OK or FAIL.
7274 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007275 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007276rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007277{
7278 dict_T *d = dict_alloc();
7279
7280 if (d == NULL)
7281 return FAIL;
7282
7283 rettv->vval.v_dict = d;
7284 rettv->v_type = VAR_DICT;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02007285 rettv->v_lock = 0;
Bram Moolenaara800b422010-06-27 01:15:55 +02007286 ++d->dv_refcount;
7287 return OK;
7288}
7289
7290
7291/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007292 * Unreference a Dictionary: decrement the reference count and free it when it
7293 * becomes zero.
7294 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007295 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007296dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007297{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007298 if (d != NULL && --d->dv_refcount <= 0)
7299 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007300}
7301
7302/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007303 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007304 * Ignores the reference count.
7305 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007306 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007307dict_free(
7308 dict_T *d,
7309 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007310{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007311 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007312 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007313 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007314
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007315 /* Remove the dict from the list of dicts for garbage collection. */
7316 if (d->dv_used_prev == NULL)
7317 first_dict = d->dv_used_next;
7318 else
7319 d->dv_used_prev->dv_used_next = d->dv_used_next;
7320 if (d->dv_used_next != NULL)
7321 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7322
7323 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007324 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007325 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007326 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007327 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007328 if (!HASHITEM_EMPTY(hi))
7329 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007330 /* Remove the item before deleting it, just in case there is
7331 * something recursive causing trouble. */
7332 di = HI2DI(hi);
7333 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007334 if (recurse || (di->di_tv.v_type != VAR_LIST
7335 && di->di_tv.v_type != VAR_DICT))
Bram Moolenaar5f436fc2016-03-22 22:34:03 +01007336 {
7337 if (!recurse && di->di_tv.v_type == VAR_PARTIAL)
7338 {
7339 partial_T *pt = di->di_tv.vval.v_partial;
7340
7341 /* We unref the partial but not the dict it refers to. */
7342 if (pt != NULL && --pt->pt_refcount == 0)
7343 partial_free(pt, FALSE);
7344 }
7345 else
7346 clear_tv(&di->di_tv);
7347 }
Bram Moolenaar685295c2006-10-15 20:37:38 +00007348 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007349 --todo;
7350 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007351 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007352 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007353 vim_free(d);
7354}
7355
7356/*
7357 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007358 * The "key" is copied to the new item.
7359 * Note that the value of the item "di_tv" still needs to be initialized!
7360 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007361 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007362 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007363dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007364{
Bram Moolenaar33570922005-01-25 22:26:29 +00007365 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007366
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007367 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007368 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007369 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007370 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007371 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007372 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007373 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007374}
7375
7376/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007377 * Make a copy of a Dictionary item.
7378 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007379 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007380dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007381{
Bram Moolenaar33570922005-01-25 22:26:29 +00007382 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007383
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007384 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7385 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007386 if (di != NULL)
7387 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007388 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007389 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007390 copy_tv(&org->di_tv, &di->di_tv);
7391 }
7392 return di;
7393}
7394
7395/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007396 * Remove item "item" from Dictionary "dict" and free it.
7397 */
7398 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007399dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007400{
Bram Moolenaar33570922005-01-25 22:26:29 +00007401 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007402
Bram Moolenaar33570922005-01-25 22:26:29 +00007403 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007404 if (HASHITEM_EMPTY(hi))
7405 EMSG2(_(e_intern2), "dictitem_remove()");
7406 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007407 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007408 dictitem_free(item);
7409}
7410
7411/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007412 * Free a dict item. Also clears the value.
7413 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007414 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007415dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007416{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007417 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007418 if (item->di_flags & DI_FLAGS_ALLOC)
7419 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007420}
7421
7422/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007423 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7424 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007425 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007426 * Returns NULL when out of memory.
7427 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007428 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007429dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007430{
Bram Moolenaar33570922005-01-25 22:26:29 +00007431 dict_T *copy;
7432 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007433 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007434 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007435
7436 if (orig == NULL)
7437 return NULL;
7438
7439 copy = dict_alloc();
7440 if (copy != NULL)
7441 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007442 if (copyID != 0)
7443 {
7444 orig->dv_copyID = copyID;
7445 orig->dv_copydict = copy;
7446 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007447 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007448 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007449 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007450 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007451 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007452 --todo;
7453
7454 di = dictitem_alloc(hi->hi_key);
7455 if (di == NULL)
7456 break;
7457 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007458 {
7459 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7460 copyID) == FAIL)
7461 {
7462 vim_free(di);
7463 break;
7464 }
7465 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007466 else
7467 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7468 if (dict_add(copy, di) == FAIL)
7469 {
7470 dictitem_free(di);
7471 break;
7472 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007473 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007474 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007475
Bram Moolenaare9a41262005-01-15 22:18:47 +00007476 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007477 if (todo > 0)
7478 {
7479 dict_unref(copy);
7480 copy = NULL;
7481 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007482 }
7483
7484 return copy;
7485}
7486
7487/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007488 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007489 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007490 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007491 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007492dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007493{
Bram Moolenaar33570922005-01-25 22:26:29 +00007494 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007495}
7496
Bram Moolenaar8c711452005-01-14 21:53:12 +00007497/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007498 * Add a number or string entry to dictionary "d".
7499 * When "str" is NULL use number "nr", otherwise use "str".
7500 * Returns FAIL when out of memory and when key already exists.
7501 */
7502 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007503dict_add_nr_str(
7504 dict_T *d,
7505 char *key,
7506 long nr,
7507 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007508{
7509 dictitem_T *item;
7510
7511 item = dictitem_alloc((char_u *)key);
7512 if (item == NULL)
7513 return FAIL;
7514 item->di_tv.v_lock = 0;
7515 if (str == NULL)
7516 {
7517 item->di_tv.v_type = VAR_NUMBER;
7518 item->di_tv.vval.v_number = nr;
7519 }
7520 else
7521 {
7522 item->di_tv.v_type = VAR_STRING;
7523 item->di_tv.vval.v_string = vim_strsave(str);
7524 }
7525 if (dict_add(d, item) == FAIL)
7526 {
7527 dictitem_free(item);
7528 return FAIL;
7529 }
7530 return OK;
7531}
7532
7533/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007534 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007535 * Returns FAIL when out of memory and when key already exists.
7536 */
7537 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007538dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007539{
7540 dictitem_T *item;
7541
7542 item = dictitem_alloc((char_u *)key);
7543 if (item == NULL)
7544 return FAIL;
7545 item->di_tv.v_lock = 0;
7546 item->di_tv.v_type = VAR_LIST;
7547 item->di_tv.vval.v_list = list;
7548 if (dict_add(d, item) == FAIL)
7549 {
7550 dictitem_free(item);
7551 return FAIL;
7552 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007553 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007554 return OK;
7555}
7556
7557/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007558 * Get the number of items in a Dictionary.
7559 */
7560 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007561dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007562{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007563 if (d == NULL)
7564 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007565 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007566}
7567
7568/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007569 * Find item "key[len]" in Dictionary "d".
7570 * If "len" is negative use strlen(key).
7571 * Returns NULL when not found.
7572 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007573 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007574dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007575{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007576#define AKEYLEN 200
7577 char_u buf[AKEYLEN];
7578 char_u *akey;
7579 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007580 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007581
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007582 if (len < 0)
7583 akey = key;
7584 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007585 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007586 tofree = akey = vim_strnsave(key, len);
7587 if (akey == NULL)
7588 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007589 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007590 else
7591 {
7592 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007593 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007594 akey = buf;
7595 }
7596
Bram Moolenaar33570922005-01-25 22:26:29 +00007597 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007598 vim_free(tofree);
7599 if (HASHITEM_EMPTY(hi))
7600 return NULL;
7601 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007602}
7603
7604/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007605 * Get a string item from a dictionary.
7606 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007607 * Returns NULL if the entry doesn't exist or out of memory.
7608 */
7609 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007610get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007611{
7612 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007613 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007614
7615 di = dict_find(d, key, -1);
7616 if (di == NULL)
7617 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007618 s = get_tv_string(&di->di_tv);
7619 if (save && s != NULL)
7620 s = vim_strsave(s);
7621 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007622}
7623
7624/*
7625 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007626 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007627 */
7628 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007629get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007630{
7631 dictitem_T *di;
7632
7633 di = dict_find(d, key, -1);
7634 if (di == NULL)
7635 return 0;
7636 return get_tv_number(&di->di_tv);
7637}
7638
7639/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007640 * Return an allocated string with the string representation of a Dictionary.
7641 * May return NULL.
7642 */
7643 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007644dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007645{
7646 garray_T ga;
7647 int first = TRUE;
7648 char_u *tofree;
7649 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007650 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007651 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007652 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007653 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007654
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007655 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007656 return NULL;
7657 ga_init2(&ga, (int)sizeof(char), 80);
7658 ga_append(&ga, '{');
7659
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007660 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007661 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007662 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007663 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007664 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007665 --todo;
7666
7667 if (first)
7668 first = FALSE;
7669 else
7670 ga_concat(&ga, (char_u *)", ");
7671
7672 tofree = string_quote(hi->hi_key, FALSE);
7673 if (tofree != NULL)
7674 {
7675 ga_concat(&ga, tofree);
7676 vim_free(tofree);
7677 }
7678 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007679 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007680 if (s != NULL)
7681 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007682 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007683 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007684 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007685 line_breakcheck();
7686
Bram Moolenaar8c711452005-01-14 21:53:12 +00007687 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007688 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007689 if (todo > 0)
7690 {
7691 vim_free(ga.ga_data);
7692 return NULL;
7693 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007694
7695 ga_append(&ga, '}');
7696 ga_append(&ga, NUL);
7697 return (char_u *)ga.ga_data;
7698}
7699
7700/*
7701 * Allocate a variable for a Dictionary and fill it from "*arg".
7702 * Return OK or FAIL. Returns NOTDONE for {expr}.
7703 */
7704 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007705get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007706{
Bram Moolenaar33570922005-01-25 22:26:29 +00007707 dict_T *d = NULL;
7708 typval_T tvkey;
7709 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007710 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007711 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007712 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007713 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007714
7715 /*
7716 * First check if it's not a curly-braces thing: {expr}.
7717 * Must do this without evaluating, otherwise a function may be called
7718 * twice. Unfortunately this means we need to call eval1() twice for the
7719 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007720 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007721 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007722 if (*start != '}')
7723 {
7724 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7725 return FAIL;
7726 if (*start == '}')
7727 return NOTDONE;
7728 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007729
7730 if (evaluate)
7731 {
7732 d = dict_alloc();
7733 if (d == NULL)
7734 return FAIL;
7735 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007736 tvkey.v_type = VAR_UNKNOWN;
7737 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007738
7739 *arg = skipwhite(*arg + 1);
7740 while (**arg != '}' && **arg != NUL)
7741 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007742 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007743 goto failret;
7744 if (**arg != ':')
7745 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007746 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007747 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007748 goto failret;
7749 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007750 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007751 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007752 key = get_tv_string_buf_chk(&tvkey, buf);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02007753 if (key == NULL)
Bram Moolenaar037cc642007-09-13 18:40:54 +00007754 {
7755 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
Bram Moolenaar037cc642007-09-13 18:40:54 +00007756 clear_tv(&tvkey);
7757 goto failret;
7758 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007759 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007760
7761 *arg = skipwhite(*arg + 1);
7762 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7763 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007764 if (evaluate)
7765 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007766 goto failret;
7767 }
7768 if (evaluate)
7769 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007770 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007771 if (item != NULL)
7772 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007773 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007774 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007775 clear_tv(&tv);
7776 goto failret;
7777 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007778 item = dictitem_alloc(key);
7779 clear_tv(&tvkey);
7780 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007781 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007782 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007783 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007784 if (dict_add(d, item) == FAIL)
7785 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007786 }
7787 }
7788
7789 if (**arg == '}')
7790 break;
7791 if (**arg != ',')
7792 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007793 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007794 goto failret;
7795 }
7796 *arg = skipwhite(*arg + 1);
7797 }
7798
7799 if (**arg != '}')
7800 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007801 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007802failret:
7803 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007804 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007805 return FAIL;
7806 }
7807
7808 *arg = skipwhite(*arg + 1);
7809 if (evaluate)
7810 {
7811 rettv->v_type = VAR_DICT;
7812 rettv->vval.v_dict = d;
7813 ++d->dv_refcount;
7814 }
7815
7816 return OK;
7817}
7818
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007819#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01007820#endif
7821
Bram Moolenaar17a13432016-01-24 14:22:10 +01007822 static char *
7823get_var_special_name(int nr)
7824{
7825 switch (nr)
7826 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007827 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01007828 case VVAL_TRUE: return "v:true";
7829 case VVAL_NONE: return "v:none";
7830 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007831 }
7832 EMSG2(_(e_intern2), "get_var_special_name()");
7833 return "42";
7834}
7835
Bram Moolenaar8c711452005-01-14 21:53:12 +00007836/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007837 * Return a string with the string representation of a variable.
7838 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007839 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007840 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007841 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007842 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007843 */
7844 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007845echo_string(
7846 typval_T *tv,
7847 char_u **tofree,
7848 char_u *numbuf,
7849 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007850{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007851 static int recurse = 0;
7852 char_u *r = NULL;
7853
Bram Moolenaar33570922005-01-25 22:26:29 +00007854 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007855 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007856 if (!did_echo_string_emsg)
7857 {
7858 /* Only give this message once for a recursive call to avoid
7859 * flooding the user with errors. And stop iterating over lists
7860 * and dicts. */
7861 did_echo_string_emsg = TRUE;
7862 EMSG(_("E724: variable nested too deep for displaying"));
7863 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007864 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007865 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007866 }
7867 ++recurse;
7868
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007869 switch (tv->v_type)
7870 {
7871 case VAR_FUNC:
7872 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007873 r = tv->vval.v_string;
7874 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007875
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007876 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01007877 {
7878 partial_T *pt = tv->vval.v_partial;
7879 char_u *fname = string_quote(pt == NULL ? NULL
7880 : pt->pt_name, FALSE);
7881 garray_T ga;
7882 int i;
7883 char_u *tf;
7884
7885 ga_init2(&ga, 1, 100);
7886 ga_concat(&ga, (char_u *)"function(");
7887 if (fname != NULL)
7888 {
7889 ga_concat(&ga, fname);
7890 vim_free(fname);
7891 }
7892 if (pt != NULL && pt->pt_argc > 0)
7893 {
7894 ga_concat(&ga, (char_u *)", [");
7895 for (i = 0; i < pt->pt_argc; ++i)
7896 {
7897 if (i > 0)
7898 ga_concat(&ga, (char_u *)", ");
7899 ga_concat(&ga,
7900 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
7901 vim_free(tf);
7902 }
7903 ga_concat(&ga, (char_u *)"]");
7904 }
7905 if (pt != NULL && pt->pt_dict != NULL)
7906 {
7907 typval_T dtv;
7908
7909 ga_concat(&ga, (char_u *)", ");
7910 dtv.v_type = VAR_DICT;
7911 dtv.vval.v_dict = pt->pt_dict;
7912 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
7913 vim_free(tf);
7914 }
7915 ga_concat(&ga, (char_u *)")");
7916
7917 *tofree = ga.ga_data;
7918 r = *tofree;
7919 break;
7920 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007921
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007922 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007923 if (tv->vval.v_list == NULL)
7924 {
7925 *tofree = NULL;
7926 r = NULL;
7927 }
7928 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7929 {
7930 *tofree = NULL;
7931 r = (char_u *)"[...]";
7932 }
7933 else
7934 {
7935 tv->vval.v_list->lv_copyID = copyID;
7936 *tofree = list2string(tv, copyID);
7937 r = *tofree;
7938 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007939 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007940
Bram Moolenaar8c711452005-01-14 21:53:12 +00007941 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007942 if (tv->vval.v_dict == NULL)
7943 {
7944 *tofree = NULL;
7945 r = NULL;
7946 }
7947 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7948 {
7949 *tofree = NULL;
7950 r = (char_u *)"{...}";
7951 }
7952 else
7953 {
7954 tv->vval.v_dict->dv_copyID = copyID;
7955 *tofree = dict2string(tv, copyID);
7956 r = *tofree;
7957 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007958 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007959
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007960 case VAR_STRING:
7961 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007962 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007963 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007964 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007965 *tofree = NULL;
7966 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007967 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007968
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007969 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007970#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007971 *tofree = NULL;
7972 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7973 r = numbuf;
7974 break;
7975#endif
7976
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007977 case VAR_SPECIAL:
7978 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007979 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007980 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007981 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007982
Bram Moolenaar8502c702014-06-17 12:51:16 +02007983 if (--recurse == 0)
7984 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007985 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007986}
7987
7988/*
7989 * Return a string with the string representation of a variable.
7990 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7991 * "numbuf" is used for a number.
7992 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007993 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007994 */
7995 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007996tv2string(
7997 typval_T *tv,
7998 char_u **tofree,
7999 char_u *numbuf,
8000 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008001{
8002 switch (tv->v_type)
8003 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008004 case VAR_FUNC:
8005 *tofree = string_quote(tv->vval.v_string, TRUE);
8006 return *tofree;
8007 case VAR_STRING:
8008 *tofree = string_quote(tv->vval.v_string, FALSE);
8009 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008010 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01008011#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008012 *tofree = NULL;
8013 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
8014 return numbuf;
8015#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00008016 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008017 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00008018 case VAR_DICT:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008019 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008020 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008021 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008022 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008023 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008024 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008025 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008026 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008027}
8028
8029/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008030 * Return string "str" in ' quotes, doubling ' characters.
8031 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008032 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008033 */
8034 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008035string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008036{
Bram Moolenaar33570922005-01-25 22:26:29 +00008037 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008038 char_u *p, *r, *s;
8039
Bram Moolenaar33570922005-01-25 22:26:29 +00008040 len = (function ? 13 : 3);
8041 if (str != NULL)
8042 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008043 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008044 for (p = str; *p != NUL; mb_ptr_adv(p))
8045 if (*p == '\'')
8046 ++len;
8047 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008048 s = r = alloc(len);
8049 if (r != NULL)
8050 {
8051 if (function)
8052 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008053 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008054 r += 10;
8055 }
8056 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008057 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008058 if (str != NULL)
8059 for (p = str; *p != NUL; )
8060 {
8061 if (*p == '\'')
8062 *r++ = '\'';
8063 MB_COPY_CHAR(p, r);
8064 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008065 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008066 if (function)
8067 *r++ = ')';
8068 *r++ = NUL;
8069 }
8070 return s;
8071}
8072
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008073#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008074/*
8075 * Convert the string "text" to a floating point number.
8076 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8077 * this always uses a decimal point.
8078 * Returns the length of the text that was consumed.
8079 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008080 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008081string2float(
8082 char_u *text,
8083 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008084{
8085 char *s = (char *)text;
8086 float_T f;
8087
8088 f = strtod(s, &s);
8089 *value = f;
8090 return (int)((char_u *)s - text);
8091}
8092#endif
8093
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008094/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 * Get the value of an environment variable.
8096 * "arg" is pointing to the '$'. It is advanced to after the name.
8097 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008098 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 */
8100 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008101get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102{
8103 char_u *string = NULL;
8104 int len;
8105 int cc;
8106 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008107 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108
8109 ++*arg;
8110 name = *arg;
8111 len = get_env_len(arg);
8112 if (evaluate)
8113 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008114 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008115 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008116
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008117 cc = name[len];
8118 name[len] = NUL;
8119 /* first try vim_getenv(), fast for normal environment vars */
8120 string = vim_getenv(name, &mustfree);
8121 if (string != NULL && *string != NUL)
8122 {
8123 if (!mustfree)
8124 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008126 else
8127 {
8128 if (mustfree)
8129 vim_free(string);
8130
8131 /* next try expanding things like $VIM and ${HOME} */
8132 string = expand_env_save(name - 1);
8133 if (string != NULL && *string == '$')
8134 {
8135 vim_free(string);
8136 string = NULL;
8137 }
8138 }
8139 name[len] = cc;
8140
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008141 rettv->v_type = VAR_STRING;
8142 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 }
8144
8145 return OK;
8146}
8147
8148/*
8149 * Array with names and number of arguments of all internal functions
8150 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8151 */
8152static struct fst
8153{
8154 char *f_name; /* function name */
8155 char f_min_argc; /* minimal number of arguments */
8156 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008157 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008158 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159} functions[] =
8160{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008161#ifdef FEAT_FLOAT
8162 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008163 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008164#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008165 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008166 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008167 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 {"append", 2, 2, f_append},
8169 {"argc", 0, 0, f_argc},
8170 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008171 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008172 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008173#ifdef FEAT_FLOAT
8174 {"asin", 1, 1, f_asin}, /* WJMc */
8175#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008176 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008177 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008178 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008179 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaarea6553b2016-03-27 15:13:38 +02008180 {"assert_match", 2, 3, f_assert_match},
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008181 {"assert_notequal", 2, 3, f_assert_notequal},
8182 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008183 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008184#ifdef FEAT_FLOAT
8185 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008186 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008189 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 {"bufexists", 1, 1, f_bufexists},
8191 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8192 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8193 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8194 {"buflisted", 1, 1, f_buflisted},
8195 {"bufloaded", 1, 1, f_bufloaded},
8196 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008197 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 {"bufwinnr", 1, 1, f_bufwinnr},
8199 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008200 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008201 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008202 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008203#ifdef FEAT_FLOAT
8204 {"ceil", 1, 1, f_ceil},
8205#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008206#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008207 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008208 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8209 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008210 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008211 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar03602ec2016-03-20 20:57:45 +01008212 {"ch_info", 1, 1, f_ch_info},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008213 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008214 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008215 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008216 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008217 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008218 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8219 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008220 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008221 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008222#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008223 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008224 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008226 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008228#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008229 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008230 {"complete_add", 1, 1, f_complete_add},
8231 {"complete_check", 0, 0, f_complete_check},
8232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008234 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008235#ifdef FEAT_FLOAT
8236 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008237 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008238#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008239 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008241 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008242 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008243 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008245 {"diff_filler", 1, 1, f_diff_filler},
8246 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008247 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008248 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008250 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 {"eventhandler", 0, 0, f_eventhandler},
8252 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008253 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008255#ifdef FEAT_FLOAT
8256 {"exp", 1, 1, f_exp},
8257#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008258 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008259 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008260 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8262 {"filereadable", 1, 1, f_filereadable},
8263 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008264 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008265 {"finddir", 1, 3, f_finddir},
8266 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008267#ifdef FEAT_FLOAT
8268 {"float2nr", 1, 1, f_float2nr},
8269 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008270 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008271#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008272 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 {"fnamemodify", 2, 2, f_fnamemodify},
8274 {"foldclosed", 1, 1, f_foldclosed},
8275 {"foldclosedend", 1, 1, f_foldclosedend},
8276 {"foldlevel", 1, 1, f_foldlevel},
8277 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008278 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008280 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008281 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008282 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008283 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008284 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 {"getchar", 0, 1, f_getchar},
8286 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008287 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 {"getcmdline", 0, 0, f_getcmdline},
8289 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008290 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008291 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008292 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008293 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008294 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008295 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 {"getfsize", 1, 1, f_getfsize},
8297 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008298 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008299 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008300 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008301 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008302 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008303 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008304 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008305 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008307 {"gettabvar", 2, 3, f_gettabvar},
8308 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 {"getwinposx", 0, 0, f_getwinposx},
8310 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008311 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008312 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008313 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008314 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008316 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008317 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008318 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8320 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8321 {"histadd", 2, 2, f_histadd},
8322 {"histdel", 1, 2, f_histdel},
8323 {"histget", 1, 2, f_histget},
8324 {"histnr", 1, 1, f_histnr},
8325 {"hlID", 1, 1, f_hlID},
8326 {"hlexists", 1, 1, f_hlexists},
8327 {"hostname", 0, 0, f_hostname},
8328 {"iconv", 3, 3, f_iconv},
8329 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008330 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008331 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008333 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 {"inputrestore", 0, 0, f_inputrestore},
8335 {"inputsave", 0, 0, f_inputsave},
8336 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008337 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008338 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008340 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008341#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8342 {"isnan", 1, 1, f_isnan},
8343#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008344 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008345#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008346 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008347 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008348 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008349 {"job_start", 1, 2, f_job_start},
8350 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008351 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008352#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008353 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008354 {"js_decode", 1, 1, f_js_decode},
8355 {"js_encode", 1, 1, f_js_encode},
8356 {"json_decode", 1, 1, f_json_decode},
8357 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008358 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008360 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 {"libcall", 3, 3, f_libcall},
8362 {"libcallnr", 3, 3, f_libcallnr},
8363 {"line", 1, 1, f_line},
8364 {"line2byte", 1, 1, f_line2byte},
8365 {"lispindent", 1, 1, f_lispindent},
8366 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008367#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008368 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008369 {"log10", 1, 1, f_log10},
8370#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008371#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008372 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008373#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008374 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008375 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008376 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008377 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008378 {"matchadd", 2, 5, f_matchadd},
8379 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008380 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008381 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008382 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008383 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008384 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar7fed5c12016-03-29 23:10:31 +02008385 {"matchstrpos", 2, 4, f_matchstrpos},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008386 {"max", 1, 1, f_max},
8387 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008388#ifdef vim_mkdir
8389 {"mkdir", 1, 3, f_mkdir},
8390#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008391 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008392#ifdef FEAT_MZSCHEME
8393 {"mzeval", 1, 1, f_mzeval},
8394#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008396 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008397 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008398 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008399#ifdef FEAT_PERL
8400 {"perleval", 1, 1, f_perleval},
8401#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008402#ifdef FEAT_FLOAT
8403 {"pow", 2, 2, f_pow},
8404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008406 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008407 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008408#ifdef FEAT_PYTHON3
8409 {"py3eval", 1, 1, f_py3eval},
8410#endif
8411#ifdef FEAT_PYTHON
8412 {"pyeval", 1, 1, f_pyeval},
8413#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008414 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008415 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008416 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008417#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008418 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008419#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008420 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 {"remote_expr", 2, 3, f_remote_expr},
8422 {"remote_foreground", 1, 1, f_remote_foreground},
8423 {"remote_peek", 1, 2, f_remote_peek},
8424 {"remote_read", 1, 1, f_remote_read},
8425 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008426 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008428 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008430 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008431#ifdef FEAT_FLOAT
8432 {"round", 1, 1, f_round},
8433#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008434 {"screenattr", 2, 2, f_screenattr},
8435 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008436 {"screencol", 0, 0, f_screencol},
8437 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008438 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008439 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008440 {"searchpair", 3, 7, f_searchpair},
8441 {"searchpairpos", 3, 7, f_searchpairpos},
8442 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 {"server2client", 2, 2, f_server2client},
8444 {"serverlist", 0, 0, f_serverlist},
8445 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008446 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008448 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008450 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008451 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008452 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008453 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008455 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008456 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008458#ifdef FEAT_CRYPT
8459 {"sha256", 1, 1, f_sha256},
8460#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008461 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008462 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008464#ifdef FEAT_FLOAT
8465 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008466 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008467#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008468 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008469 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008470 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008471 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008472 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008473#ifdef FEAT_FLOAT
8474 {"sqrt", 1, 1, f_sqrt},
8475 {"str2float", 1, 1, f_str2float},
8476#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008477 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008478 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008479 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480#ifdef HAVE_STRFTIME
8481 {"strftime", 1, 2, f_strftime},
8482#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008483 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008484 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 {"strlen", 1, 1, f_strlen},
8486 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008487 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008489 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008490 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 {"substitute", 4, 4, f_substitute},
8492 {"synID", 3, 3, f_synID},
8493 {"synIDattr", 2, 3, f_synIDattr},
8494 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008495 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008496 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008497 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008498 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008499 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008500 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008501 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008502 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008503 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008504#ifdef FEAT_FLOAT
8505 {"tan", 1, 1, f_tan},
8506 {"tanh", 1, 1, f_tanh},
8507#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008509 {"test", 1, 1, f_test},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008510#ifdef FEAT_TIMERS
8511 {"timer_start", 2, 3, f_timer_start},
8512 {"timer_stop", 1, 1, f_timer_stop},
8513#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 {"tolower", 1, 1, f_tolower},
8515 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008516 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008517#ifdef FEAT_FLOAT
8518 {"trunc", 1, 1, f_trunc},
8519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008521 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008522 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008523 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008524 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 {"virtcol", 1, 1, f_virtcol},
8526 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008527 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008528 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008529 {"win_getid", 0, 2, f_win_getid},
8530 {"win_gotoid", 1, 1, f_win_gotoid},
8531 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8532 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 {"winbufnr", 1, 1, f_winbufnr},
8534 {"wincol", 0, 0, f_wincol},
8535 {"winheight", 1, 1, f_winheight},
8536 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008537 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008539 {"winrestview", 1, 1, f_winrestview},
8540 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008542 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008543 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008544 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545};
8546
8547#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8548
8549/*
8550 * Function given to ExpandGeneric() to obtain the list of internal
8551 * or user defined function names.
8552 */
8553 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008554get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555{
8556 static int intidx = -1;
8557 char_u *name;
8558
8559 if (idx == 0)
8560 intidx = -1;
8561 if (intidx < 0)
8562 {
8563 name = get_user_func_name(xp, idx);
8564 if (name != NULL)
8565 return name;
8566 }
8567 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8568 {
8569 STRCPY(IObuff, functions[intidx].f_name);
8570 STRCAT(IObuff, "(");
8571 if (functions[intidx].f_max_argc == 0)
8572 STRCAT(IObuff, ")");
8573 return IObuff;
8574 }
8575
8576 return NULL;
8577}
8578
8579/*
8580 * Function given to ExpandGeneric() to obtain the list of internal or
8581 * user defined variable or function names.
8582 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008584get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585{
8586 static int intidx = -1;
8587 char_u *name;
8588
8589 if (idx == 0)
8590 intidx = -1;
8591 if (intidx < 0)
8592 {
8593 name = get_function_name(xp, idx);
8594 if (name != NULL)
8595 return name;
8596 }
8597 return get_user_var_name(xp, ++intidx);
8598}
8599
8600#endif /* FEAT_CMDL_COMPL */
8601
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008602#if defined(EBCDIC) || defined(PROTO)
8603/*
8604 * Compare struct fst by function name.
8605 */
8606 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008607compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008608{
8609 struct fst *p1 = (struct fst *)s1;
8610 struct fst *p2 = (struct fst *)s2;
8611
8612 return STRCMP(p1->f_name, p2->f_name);
8613}
8614
8615/*
8616 * Sort the function table by function name.
8617 * The sorting of the table above is ASCII dependant.
8618 * On machines using EBCDIC we have to sort it.
8619 */
8620 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008621sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008622{
8623 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8624
8625 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8626}
8627#endif
8628
8629
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630/*
8631 * Find internal function in table above.
8632 * Return index, or -1 if not found
8633 */
8634 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008635find_internal_func(
8636 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637{
8638 int first = 0;
8639 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8640 int cmp;
8641 int x;
8642
8643 /*
8644 * Find the function name in the table. Binary search.
8645 */
8646 while (first <= last)
8647 {
8648 x = first + ((unsigned)(last - first) >> 1);
8649 cmp = STRCMP(name, functions[x].f_name);
8650 if (cmp < 0)
8651 last = x - 1;
8652 else if (cmp > 0)
8653 first = x + 1;
8654 else
8655 return x;
8656 }
8657 return -1;
8658}
8659
8660/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008661 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8662 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01008663 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
8664 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008665 */
8666 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01008667deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008668{
Bram Moolenaar33570922005-01-25 22:26:29 +00008669 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008670 int cc;
8671
Bram Moolenaar65639032016-03-16 21:40:30 +01008672 if (partialp != NULL)
8673 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008674
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008675 cc = name[*lenp];
8676 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008677 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008678 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008679 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008680 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008681 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008682 {
8683 *lenp = 0;
8684 return (char_u *)""; /* just in case */
8685 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008686 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008687 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008688 }
8689
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008690 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
8691 {
Bram Moolenaar65639032016-03-16 21:40:30 +01008692 partial_T *pt = v->di_tv.vval.v_partial;
8693
8694 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008695 {
8696 *lenp = 0;
8697 return (char_u *)""; /* just in case */
8698 }
Bram Moolenaar65639032016-03-16 21:40:30 +01008699 if (partialp != NULL)
8700 *partialp = pt;
8701 *lenp = (int)STRLEN(pt->pt_name);
8702 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008703 }
8704
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008705 return name;
8706}
8707
8708/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 * Allocate a variable for the result of a function.
8710 * Return OK or FAIL.
8711 */
8712 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008713get_func_tv(
8714 char_u *name, /* name of the function */
8715 int len, /* length of "name" */
8716 typval_T *rettv,
8717 char_u **arg, /* argument, pointing to the '(' */
8718 linenr_T firstline, /* first line of range */
8719 linenr_T lastline, /* last line of range */
8720 int *doesrange, /* return: function handled range */
8721 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008722 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008723 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724{
8725 char_u *argp;
8726 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008727 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 int argcount = 0; /* number of arguments found */
8729
8730 /*
8731 * Get the arguments.
8732 */
8733 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008734 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 {
8736 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8737 if (*argp == ')' || *argp == ',' || *argp == NUL)
8738 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8740 {
8741 ret = FAIL;
8742 break;
8743 }
8744 ++argcount;
8745 if (*argp != ',')
8746 break;
8747 }
8748 if (*argp == ')')
8749 ++argp;
8750 else
8751 ret = FAIL;
8752
8753 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008754 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008755 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008757 {
8758 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008759 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008760 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008761 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763
8764 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008765 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766
8767 *arg = skipwhite(argp);
8768 return ret;
8769}
8770
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008771#define ERROR_UNKNOWN 0
8772#define ERROR_TOOMANY 1
8773#define ERROR_TOOFEW 2
8774#define ERROR_SCRIPT 3
8775#define ERROR_DICT 4
8776#define ERROR_NONE 5
8777#define ERROR_OTHER 6
8778#define FLEN_FIXED 40
8779
8780/*
8781 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8782 * Change <SNR>123_name() to K_SNR 123_name().
8783 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
8784 * (slow).
8785 */
8786 static char_u *
8787fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
8788{
8789 int llen;
8790 char_u *fname;
8791 int i;
8792
8793 llen = eval_fname_script(name);
8794 if (llen > 0)
8795 {
8796 fname_buf[0] = K_SPECIAL;
8797 fname_buf[1] = KS_EXTRA;
8798 fname_buf[2] = (int)KE_SNR;
8799 i = 3;
8800 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8801 {
8802 if (current_SID <= 0)
8803 *error = ERROR_SCRIPT;
8804 else
8805 {
8806 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8807 i = (int)STRLEN(fname_buf);
8808 }
8809 }
8810 if (i + STRLEN(name + llen) < FLEN_FIXED)
8811 {
8812 STRCPY(fname_buf + i, name + llen);
8813 fname = fname_buf;
8814 }
8815 else
8816 {
8817 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8818 if (fname == NULL)
8819 *error = ERROR_OTHER;
8820 else
8821 {
8822 *tofree = fname;
8823 mch_memmove(fname, fname_buf, (size_t)i);
8824 STRCPY(fname + i, name + llen);
8825 }
8826 }
8827 }
8828 else
8829 fname = name;
8830 return fname;
8831}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832
8833/*
8834 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008835 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008836 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008838 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008839call_func(
8840 char_u *funcname, /* name of the function */
8841 int len, /* length of "name" */
8842 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008843 int argcount_in, /* number of "argvars" */
8844 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008845 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008846 linenr_T firstline, /* first line of range */
8847 linenr_T lastline, /* last line of range */
8848 int *doesrange, /* return: function handled range */
8849 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008850 partial_T *partial, /* optional, can be NULL */
8851 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852{
8853 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 int error = ERROR_NONE;
8855 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008858 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008860 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008861 int argcount = argcount_in;
8862 typval_T *argvars = argvars_in;
8863 dict_T *selfdict = selfdict_in;
8864 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
8865 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008866
8867 /* Make a copy of the name, if it comes from a funcref variable it could
8868 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008869 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008870 if (name == NULL)
8871 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008873 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874
8875 *doesrange = FALSE;
8876
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008877 if (partial != NULL)
8878 {
8879 if (partial->pt_dict != NULL)
8880 {
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008881 /* When the function has a partial with a dict and there is a dict
8882 * argument, use the dict argument. That is backwards compatible.
8883 */
8884 if (selfdict_in == NULL)
8885 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008886 }
8887 if (error == ERROR_NONE && partial->pt_argc > 0)
8888 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008889 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
8890 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
8891 for (i = 0; i < argcount_in; ++i)
8892 argv[i + argv_clear] = argvars_in[i];
8893 argvars = argv;
8894 argcount = partial->pt_argc + argcount_in;
8895 }
8896 }
8897
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898
8899 /* execute the function if no errors detected and executing */
8900 if (evaluate && error == ERROR_NONE)
8901 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008902 char_u *rfname = fname;
8903
8904 /* Ignore "g:" before a function name. */
8905 if (fname[0] == 'g' && fname[1] == ':')
8906 rfname = fname + 2;
8907
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008908 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8909 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910 error = ERROR_UNKNOWN;
8911
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008912 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 {
8914 /*
8915 * User defined function.
8916 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008917 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008918
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008920 /* Trigger FuncUndefined event, may load the function. */
8921 if (fp == NULL
8922 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008923 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008924 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008926 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008927 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 }
8929#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008930 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008931 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008932 {
8933 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008934 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008935 }
8936
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 if (fp != NULL)
8938 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008939 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008941 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008943 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008945 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008946 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 else
8948 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008949 int did_save_redo = FALSE;
8950
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951 /*
8952 * Call the user function.
8953 * Save and restore search patterns, script variables and
8954 * redo buffer.
8955 */
8956 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008957#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008958 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008959#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008960 {
8961 saveRedobuff();
8962 did_save_redo = TRUE;
8963 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008964 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008965 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008966 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008967 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8968 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8969 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008970 /* Function was unreferenced while being used, free it
8971 * now. */
8972 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008973 if (did_save_redo)
8974 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975 restore_search_patterns();
8976 error = ERROR_NONE;
8977 }
8978 }
8979 }
8980 else
8981 {
8982 /*
8983 * Find the function name in the table, call its implementation.
8984 */
8985 i = find_internal_func(fname);
8986 if (i >= 0)
8987 {
8988 if (argcount < functions[i].f_min_argc)
8989 error = ERROR_TOOFEW;
8990 else if (argcount > functions[i].f_max_argc)
8991 error = ERROR_TOOMANY;
8992 else
8993 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008994 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008995 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996 error = ERROR_NONE;
8997 }
8998 }
8999 }
9000 /*
9001 * The function call (or "FuncUndefined" autocommand sequence) might
9002 * have been aborted by an error, an interrupt, or an explicitly thrown
9003 * exception that has not been caught so far. This situation can be
9004 * tested for by calling aborting(). For an error in an internal
9005 * function or for the "E132" error in call_user_func(), however, the
9006 * throw point at which the "force_abort" flag (temporarily reset by
9007 * emsg()) is normally updated has not been reached yet. We need to
9008 * update that flag first to make aborting() reliable.
9009 */
9010 update_force_abort();
9011 }
9012 if (error == ERROR_NONE)
9013 ret = OK;
9014
9015 /*
9016 * Report an error unless the argument evaluation or function call has been
9017 * cancelled due to an aborting error, an interrupt, or an exception.
9018 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00009019 if (!aborting())
9020 {
9021 switch (error)
9022 {
9023 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009024 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009025 break;
9026 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009027 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009028 break;
9029 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009030 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009031 name);
9032 break;
9033 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009034 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009035 name);
9036 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009037 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009038 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00009039 name);
9040 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009041 }
9042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009044 while (argv_clear > 0)
9045 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009046 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009047 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048
9049 return ret;
9050}
9051
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009052/*
9053 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009054 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009055 */
9056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009057emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009058{
9059 char_u *p;
9060
9061 if (*name == K_SPECIAL)
9062 p = concat_str((char_u *)"<SNR>", name + 3);
9063 else
9064 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009065 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009066 if (p != name)
9067 vim_free(p);
9068}
9069
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009070/*
9071 * Return TRUE for a non-zero Number and a non-empty String.
9072 */
9073 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009074non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009075{
9076 return ((argvars[0].v_type == VAR_NUMBER
9077 && argvars[0].vval.v_number != 0)
9078 || (argvars[0].v_type == VAR_STRING
9079 && argvars[0].vval.v_string != NULL
9080 && *argvars[0].vval.v_string != NUL));
9081}
9082
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083/*********************************************
9084 * Implementation of the built-in functions
9085 */
9086
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009087#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009088static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009089
9090/*
9091 * Get the float value of "argvars[0]" into "f".
9092 * Returns FAIL when the argument is not a Number or Float.
9093 */
9094 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009095get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009096{
9097 if (argvars[0].v_type == VAR_FLOAT)
9098 {
9099 *f = argvars[0].vval.v_float;
9100 return OK;
9101 }
9102 if (argvars[0].v_type == VAR_NUMBER)
9103 {
9104 *f = (float_T)argvars[0].vval.v_number;
9105 return OK;
9106 }
9107 EMSG(_("E808: Number or Float required"));
9108 return FAIL;
9109}
9110
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009111/*
9112 * "abs(expr)" function
9113 */
9114 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009115f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009116{
9117 if (argvars[0].v_type == VAR_FLOAT)
9118 {
9119 rettv->v_type = VAR_FLOAT;
9120 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9121 }
9122 else
9123 {
9124 varnumber_T n;
9125 int error = FALSE;
9126
9127 n = get_tv_number_chk(&argvars[0], &error);
9128 if (error)
9129 rettv->vval.v_number = -1;
9130 else if (n > 0)
9131 rettv->vval.v_number = n;
9132 else
9133 rettv->vval.v_number = -n;
9134 }
9135}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009136
9137/*
9138 * "acos()" function
9139 */
9140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009141f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009142{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009143 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009144
9145 rettv->v_type = VAR_FLOAT;
9146 if (get_float_arg(argvars, &f) == OK)
9147 rettv->vval.v_float = acos(f);
9148 else
9149 rettv->vval.v_float = 0.0;
9150}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009151#endif
9152
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009154 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155 */
9156 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009157f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158{
Bram Moolenaar33570922005-01-25 22:26:29 +00009159 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009161 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009162 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009164 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009165 && !tv_check_lock(l->lv_lock,
9166 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009167 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009168 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009169 }
9170 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009171 EMSG(_(e_listreq));
9172}
9173
9174/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009175 * "alloc_fail(id, countdown, repeat)" function
9176 */
9177 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009178f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009179{
9180 if (argvars[0].v_type != VAR_NUMBER
9181 || argvars[0].vval.v_number <= 0
9182 || argvars[1].v_type != VAR_NUMBER
9183 || argvars[1].vval.v_number < 0
9184 || argvars[2].v_type != VAR_NUMBER)
9185 EMSG(_(e_invarg));
9186 else
9187 {
9188 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009189 if (alloc_fail_id >= aid_last)
9190 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009191 alloc_fail_countdown = argvars[1].vval.v_number;
9192 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009193 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009194 }
9195}
9196
9197/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009198 * "and(expr, expr)" function
9199 */
9200 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009201f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009202{
9203 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9204 & get_tv_number_chk(&argvars[1], NULL);
9205}
9206
9207/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009208 * "append(lnum, string/list)" function
9209 */
9210 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009211f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009212{
9213 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009214 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009215 list_T *l = NULL;
9216 listitem_T *li = NULL;
9217 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009218 long added = 0;
9219
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009220 /* When coming here from Insert mode, sync undo, so that this can be
9221 * undone separately from what was previously inserted. */
9222 if (u_sync_once == 2)
9223 {
9224 u_sync_once = 1; /* notify that u_sync() was called */
9225 u_sync(TRUE);
9226 }
9227
Bram Moolenaar0d660222005-01-07 21:51:51 +00009228 lnum = get_tv_lnum(argvars);
9229 if (lnum >= 0
9230 && lnum <= curbuf->b_ml.ml_line_count
9231 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009232 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009233 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009234 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009235 l = argvars[1].vval.v_list;
9236 if (l == NULL)
9237 return;
9238 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009239 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009240 for (;;)
9241 {
9242 if (l == NULL)
9243 tv = &argvars[1]; /* append a string */
9244 else if (li == NULL)
9245 break; /* end of list */
9246 else
9247 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009248 line = get_tv_string_chk(tv);
9249 if (line == NULL) /* type error */
9250 {
9251 rettv->vval.v_number = 1; /* Failed */
9252 break;
9253 }
9254 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009255 ++added;
9256 if (l == NULL)
9257 break;
9258 li = li->li_next;
9259 }
9260
9261 appended_lines_mark(lnum, added);
9262 if (curwin->w_cursor.lnum > lnum)
9263 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009265 else
9266 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267}
9268
9269/*
9270 * "argc()" function
9271 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009273f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009275 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276}
9277
9278/*
9279 * "argidx()" function
9280 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009282f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009284 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285}
9286
9287/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009288 * "arglistid()" function
9289 */
9290 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009291f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009292{
9293 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009294
9295 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009296 wp = find_tabwin(&argvars[0], &argvars[1]);
9297 if (wp != NULL)
9298 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009299}
9300
9301/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302 * "argv(nr)" function
9303 */
9304 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009305f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306{
9307 int idx;
9308
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009309 if (argvars[0].v_type != VAR_UNKNOWN)
9310 {
9311 idx = get_tv_number_chk(&argvars[0], NULL);
9312 if (idx >= 0 && idx < ARGCOUNT)
9313 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9314 else
9315 rettv->vval.v_string = NULL;
9316 rettv->v_type = VAR_STRING;
9317 }
9318 else if (rettv_list_alloc(rettv) == OK)
9319 for (idx = 0; idx < ARGCOUNT; ++idx)
9320 list_append_string(rettv->vval.v_list,
9321 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322}
9323
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009324typedef enum
9325{
9326 ASSERT_EQUAL,
9327 ASSERT_NOTEQUAL,
9328 ASSERT_MATCH,
9329 ASSERT_NOTMATCH,
9330 ASSERT_OTHER,
9331} assert_type_T;
9332
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009333static void prepare_assert_error(garray_T*gap);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009334static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv, assert_type_T is_match);
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009335static void assert_error(garray_T *gap);
9336static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009337
9338/*
9339 * Prepare "gap" for an assert error and add the sourcing position.
9340 */
9341 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009342prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009343{
9344 char buf[NUMBUFLEN];
9345
9346 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009347 if (sourcing_name != NULL)
9348 {
9349 ga_concat(gap, sourcing_name);
9350 if (sourcing_lnum > 0)
9351 ga_concat(gap, (char_u *)" ");
9352 }
9353 if (sourcing_lnum > 0)
9354 {
9355 sprintf(buf, "line %ld", (long)sourcing_lnum);
9356 ga_concat(gap, (char_u *)buf);
9357 }
9358 if (sourcing_name != NULL || sourcing_lnum > 0)
9359 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009360}
9361
9362/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009363 * Append "str" to "gap", escaping unprintable characters.
9364 * Changes NL to \n, CR to \r, etc.
9365 */
9366 static void
9367ga_concat_esc(garray_T *gap, char_u *str)
9368{
9369 char_u *p;
9370 char_u buf[NUMBUFLEN];
9371
Bram Moolenaarf1551962016-03-15 12:55:58 +01009372 if (str == NULL)
9373 {
9374 ga_concat(gap, (char_u *)"NULL");
9375 return;
9376 }
9377
Bram Moolenaar23689172016-02-15 22:37:37 +01009378 for (p = str; *p != NUL; ++p)
9379 switch (*p)
9380 {
9381 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9382 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9383 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9384 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9385 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9386 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9387 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9388 default:
9389 if (*p < ' ')
9390 {
9391 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9392 ga_concat(gap, buf);
9393 }
9394 else
9395 ga_append(gap, *p);
9396 break;
9397 }
9398}
9399
9400/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009401 * Fill "gap" with information about an assert error.
9402 */
9403 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009404fill_assert_error(
9405 garray_T *gap,
9406 typval_T *opt_msg_tv,
9407 char_u *exp_str,
9408 typval_T *exp_tv,
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009409 typval_T *got_tv,
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009410 assert_type_T atype)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009411{
9412 char_u numbuf[NUMBUFLEN];
9413 char_u *tofree;
9414
9415 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9416 {
9417 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9418 vim_free(tofree);
9419 }
9420 else
9421 {
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009422 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009423 ga_concat(gap, (char_u *)"Pattern ");
9424 else
9425 ga_concat(gap, (char_u *)"Expected ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009426 if (exp_str == NULL)
9427 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009428 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009429 vim_free(tofree);
9430 }
9431 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009432 ga_concat_esc(gap, exp_str);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009433 if (atype == ASSERT_MATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009434 ga_concat(gap, (char_u *)" does not match ");
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009435 else if (atype == ASSERT_NOTMATCH)
9436 ga_concat(gap, (char_u *)" does match ");
9437 else if (atype == ASSERT_NOTEQUAL)
9438 ga_concat(gap, (char_u *)" differs from ");
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009439 else
9440 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009441 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009442 vim_free(tofree);
9443 }
9444}
Bram Moolenaar43345542015-11-29 17:35:35 +01009445
9446/*
9447 * Add an assert error to v:errors.
9448 */
9449 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009450assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009451{
9452 struct vimvar *vp = &vimvars[VV_ERRORS];
9453
9454 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9455 /* Make sure v:errors is a list. */
9456 set_vim_var_list(VV_ERRORS, list_alloc());
9457 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9458}
9459
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009460 static void
9461assert_equal_common(typval_T *argvars, assert_type_T atype)
9462{
9463 garray_T ga;
9464
9465 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9466 != (atype == ASSERT_EQUAL))
9467 {
9468 prepare_assert_error(&ga);
9469 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9470 atype);
9471 assert_error(&ga);
9472 ga_clear(&ga);
9473 }
9474}
9475
Bram Moolenaar43345542015-11-29 17:35:35 +01009476/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009477 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009478 */
9479 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009480f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009481{
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009482 assert_equal_common(argvars, ASSERT_EQUAL);
9483}
Bram Moolenaar43345542015-11-29 17:35:35 +01009484
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009485/*
9486 * "assert_notequal(expected, actual[, msg])" function
9487 */
9488 static void
9489f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
9490{
9491 assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar43345542015-11-29 17:35:35 +01009492}
9493
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009494/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009495 * "assert_exception(string[, msg])" function
9496 */
9497 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009498f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009499{
9500 garray_T ga;
9501 char *error;
9502
9503 error = (char *)get_tv_string_chk(&argvars[0]);
9504 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9505 {
9506 prepare_assert_error(&ga);
9507 ga_concat(&ga, (char_u *)"v:exception is not set");
9508 assert_error(&ga);
9509 ga_clear(&ga);
9510 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009511 else if (error != NULL
9512 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009513 {
9514 prepare_assert_error(&ga);
9515 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009516 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009517 assert_error(&ga);
9518 ga_clear(&ga);
9519 }
9520}
9521
9522/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009523 * "assert_fails(cmd [, error])" function
9524 */
9525 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009526f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009527{
9528 char_u *cmd = get_tv_string_chk(&argvars[0]);
9529 garray_T ga;
9530
9531 called_emsg = FALSE;
9532 suppress_errthrow = TRUE;
9533 emsg_silent = TRUE;
9534 do_cmdline_cmd(cmd);
9535 if (!called_emsg)
9536 {
9537 prepare_assert_error(&ga);
9538 ga_concat(&ga, (char_u *)"command did not fail: ");
9539 ga_concat(&ga, cmd);
9540 assert_error(&ga);
9541 ga_clear(&ga);
9542 }
9543 else if (argvars[1].v_type != VAR_UNKNOWN)
9544 {
9545 char_u buf[NUMBUFLEN];
9546 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9547
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009548 if (error == NULL
9549 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009550 {
9551 prepare_assert_error(&ga);
9552 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009553 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
Bram Moolenaara260b872016-01-15 20:48:22 +01009554 assert_error(&ga);
9555 ga_clear(&ga);
9556 }
9557 }
9558
9559 called_emsg = FALSE;
9560 suppress_errthrow = FALSE;
9561 emsg_silent = FALSE;
9562 emsg_on_display = FALSE;
9563 set_vim_var_string(VV_ERRMSG, NULL, 0);
9564}
9565
9566/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009567 * Common for assert_true() and assert_false().
9568 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009569 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009570assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009571{
9572 int error = FALSE;
9573 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009574
Bram Moolenaar37127922016-02-06 20:29:28 +01009575 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009576 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009577 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009578 if (argvars[0].v_type != VAR_NUMBER
9579 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9580 || error)
9581 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009582 prepare_assert_error(&ga);
9583 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009584 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009585 NULL, &argvars[0], ASSERT_OTHER);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009586 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009587 ga_clear(&ga);
9588 }
9589}
9590
9591/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009592 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009593 */
9594 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009595f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009596{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009597 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009598}
9599
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009600 static void
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009601assert_match_common(typval_T *argvars, assert_type_T atype)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009602{
9603 garray_T ga;
9604 char_u buf1[NUMBUFLEN];
9605 char_u buf2[NUMBUFLEN];
9606 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9607 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9608
Bram Moolenaar72188e92016-03-28 22:48:29 +02009609 if (pat == NULL || text == NULL)
9610 EMSG(_(e_invarg));
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009611 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009612 {
9613 prepare_assert_error(&ga);
9614 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009615 atype);
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009616 assert_error(&ga);
9617 ga_clear(&ga);
9618 }
9619}
9620
9621/*
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009622 * "assert_match(pattern, actual[, msg])" function
9623 */
9624 static void
9625f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
9626{
9627 assert_match_common(argvars, ASSERT_MATCH);
9628}
9629
9630/*
9631 * "assert_notmatch(pattern, actual[, msg])" function
9632 */
9633 static void
9634f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
9635{
9636 assert_match_common(argvars, ASSERT_NOTMATCH);
9637}
9638
9639/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009640 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009641 */
9642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009643f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009644{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009645 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009646}
9647
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009648#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009649/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009650 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009651 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009652 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009653f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009654{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009655 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009656
9657 rettv->v_type = VAR_FLOAT;
9658 if (get_float_arg(argvars, &f) == OK)
9659 rettv->vval.v_float = asin(f);
9660 else
9661 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009662}
9663
9664/*
9665 * "atan()" function
9666 */
9667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009668f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009669{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009670 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009671
9672 rettv->v_type = VAR_FLOAT;
9673 if (get_float_arg(argvars, &f) == OK)
9674 rettv->vval.v_float = atan(f);
9675 else
9676 rettv->vval.v_float = 0.0;
9677}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009678
9679/*
9680 * "atan2()" function
9681 */
9682 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009683f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009684{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009685 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009686
9687 rettv->v_type = VAR_FLOAT;
9688 if (get_float_arg(argvars, &fx) == OK
9689 && get_float_arg(&argvars[1], &fy) == OK)
9690 rettv->vval.v_float = atan2(fx, fy);
9691 else
9692 rettv->vval.v_float = 0.0;
9693}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009694#endif
9695
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696/*
9697 * "browse(save, title, initdir, default)" function
9698 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009700f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701{
9702#ifdef FEAT_BROWSE
9703 int save;
9704 char_u *title;
9705 char_u *initdir;
9706 char_u *defname;
9707 char_u buf[NUMBUFLEN];
9708 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009709 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009711 save = get_tv_number_chk(&argvars[0], &error);
9712 title = get_tv_string_chk(&argvars[1]);
9713 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9714 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009716 if (error || title == NULL || initdir == NULL || defname == NULL)
9717 rettv->vval.v_string = NULL;
9718 else
9719 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009720 do_browse(save ? BROWSE_SAVE : 0,
9721 title, defname, NULL, initdir, NULL, curbuf);
9722#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009723 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009724#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009725 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009726}
9727
9728/*
9729 * "browsedir(title, initdir)" function
9730 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009731 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009732f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009733{
9734#ifdef FEAT_BROWSE
9735 char_u *title;
9736 char_u *initdir;
9737 char_u buf[NUMBUFLEN];
9738
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009739 title = get_tv_string_chk(&argvars[0]);
9740 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009741
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009742 if (title == NULL || initdir == NULL)
9743 rettv->vval.v_string = NULL;
9744 else
9745 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009746 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009748 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009750 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751}
9752
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009753static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009754
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755/*
9756 * Find a buffer by number or exact name.
9757 */
9758 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009759find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760{
9761 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009763 if (avar->v_type == VAR_NUMBER)
9764 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009765 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009767 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009768 if (buf == NULL)
9769 {
9770 /* No full path name match, try a match with a URL or a "nofile"
9771 * buffer, these don't use the full path. */
9772 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9773 if (buf->b_fname != NULL
9774 && (path_with_url(buf->b_fname)
9775#ifdef FEAT_QUICKFIX
9776 || bt_nofile(buf)
9777#endif
9778 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009779 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009780 break;
9781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782 }
9783 return buf;
9784}
9785
9786/*
9787 * "bufexists(expr)" function
9788 */
9789 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009790f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009792 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793}
9794
9795/*
9796 * "buflisted(expr)" function
9797 */
9798 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009799f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800{
9801 buf_T *buf;
9802
9803 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009804 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805}
9806
9807/*
9808 * "bufloaded(expr)" function
9809 */
9810 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009811f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812{
9813 buf_T *buf;
9814
9815 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009816 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817}
9818
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01009819 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +01009820buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009822 int save_magic;
9823 char_u *save_cpo;
9824 buf_T *buf;
9825
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9827 save_magic = p_magic;
9828 p_magic = TRUE;
9829 save_cpo = p_cpo;
9830 p_cpo = (char_u *)"";
9831
9832 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009833 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009834
9835 p_magic = save_magic;
9836 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +01009837 return buf;
9838}
9839
9840/*
9841 * Get buffer by number or pattern.
9842 */
9843 static buf_T *
9844get_buf_tv(typval_T *tv, int curtab_only)
9845{
9846 char_u *name = tv->vval.v_string;
9847 buf_T *buf;
9848
9849 if (tv->v_type == VAR_NUMBER)
9850 return buflist_findnr((int)tv->vval.v_number);
9851 if (tv->v_type != VAR_STRING)
9852 return NULL;
9853 if (name == NULL || *name == NUL)
9854 return curbuf;
9855 if (name[0] == '$' && name[1] == NUL)
9856 return lastbuf;
9857
9858 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009859
9860 /* If not found, try expanding the name, like done for bufexists(). */
9861 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009862 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863
9864 return buf;
9865}
9866
9867/*
9868 * "bufname(expr)" function
9869 */
9870 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009871f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872{
9873 buf_T *buf;
9874
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009875 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009877 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009878 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009880 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009882 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883 --emsg_off;
9884}
9885
9886/*
9887 * "bufnr(expr)" function
9888 */
9889 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009890f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891{
9892 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009893 int error = FALSE;
9894 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009896 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009898 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009899 --emsg_off;
9900
9901 /* If the buffer isn't found and the second argument is not zero create a
9902 * new buffer. */
9903 if (buf == NULL
9904 && argvars[1].v_type != VAR_UNKNOWN
9905 && get_tv_number_chk(&argvars[1], &error) != 0
9906 && !error
9907 && (name = get_tv_string_chk(&argvars[0])) != NULL
9908 && !error)
9909 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9910
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009912 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009913 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009914 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915}
9916
9917/*
9918 * "bufwinnr(nr)" function
9919 */
9920 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009921f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922{
9923#ifdef FEAT_WINDOWS
9924 win_T *wp;
9925 int winnr = 0;
9926#endif
9927 buf_T *buf;
9928
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009929 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009930 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009931 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932#ifdef FEAT_WINDOWS
9933 for (wp = firstwin; wp; wp = wp->w_next)
9934 {
9935 ++winnr;
9936 if (wp->w_buffer == buf)
9937 break;
9938 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009939 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009941 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942#endif
9943 --emsg_off;
9944}
9945
9946/*
9947 * "byte2line(byte)" function
9948 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009950f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951{
9952#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009953 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954#else
9955 long boff = 0;
9956
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009957 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009959 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009961 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962 (linenr_T)0, &boff);
9963#endif
9964}
9965
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009966 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009967byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009968{
9969#ifdef FEAT_MBYTE
9970 char_u *t;
9971#endif
9972 char_u *str;
9973 long idx;
9974
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009975 str = get_tv_string_chk(&argvars[0]);
9976 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009977 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009978 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009979 return;
9980
9981#ifdef FEAT_MBYTE
9982 t = str;
9983 for ( ; idx > 0; idx--)
9984 {
9985 if (*t == NUL) /* EOL reached */
9986 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009987 if (enc_utf8 && comp)
9988 t += utf_ptr2len(t);
9989 else
9990 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009991 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009992 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009993#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009994 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009995 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009996#endif
9997}
9998
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009999/*
10000 * "byteidx()" function
10001 */
10002 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010003f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010004{
10005 byteidx(argvars, rettv, FALSE);
10006}
10007
10008/*
10009 * "byteidxcomp()" function
10010 */
10011 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010012f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010013{
10014 byteidx(argvars, rettv, TRUE);
10015}
10016
Bram Moolenaardb913952012-06-29 12:54:53 +020010017 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010018func_call(
10019 char_u *name,
10020 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010021 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010022 dict_T *selfdict,
10023 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020010024{
10025 listitem_T *item;
10026 typval_T argv[MAX_FUNC_ARGS + 1];
10027 int argc = 0;
10028 int dummy;
10029 int r = 0;
10030
10031 for (item = args->vval.v_list->lv_first; item != NULL;
10032 item = item->li_next)
10033 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010034 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +020010035 {
10036 EMSG(_("E699: Too many arguments"));
10037 break;
10038 }
10039 /* Make a copy of each argument. This is needed to be able to set
10040 * v_lock to VAR_FIXED in the copy without changing the original list.
10041 */
10042 copy_tv(&item->li_tv, &argv[argc++]);
10043 }
10044
10045 if (item == NULL)
10046 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
10047 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010048 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +020010049
10050 /* Free the arguments. */
10051 while (argc > 0)
10052 clear_tv(&argv[--argc]);
10053
10054 return r;
10055}
10056
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010057/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010058 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010059 */
10060 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010061f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010062{
10063 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010064 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000010065 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010066
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010067 if (argvars[1].v_type != VAR_LIST)
10068 {
10069 EMSG(_(e_listreq));
10070 return;
10071 }
10072 if (argvars[1].vval.v_list == NULL)
10073 return;
10074
10075 if (argvars[0].v_type == VAR_FUNC)
10076 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010077 else if (argvars[0].v_type == VAR_PARTIAL)
10078 {
10079 partial = argvars[0].vval.v_partial;
10080 func = partial->pt_name;
10081 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010082 else
10083 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010084 if (*func == NUL)
10085 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010086
Bram Moolenaare9a41262005-01-15 22:18:47 +000010087 if (argvars[2].v_type != VAR_UNKNOWN)
10088 {
10089 if (argvars[2].v_type != VAR_DICT)
10090 {
10091 EMSG(_(e_dictreq));
10092 return;
10093 }
10094 selfdict = argvars[2].vval.v_dict;
10095 }
10096
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010097 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010098}
10099
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010100#ifdef FEAT_FLOAT
10101/*
10102 * "ceil({float})" function
10103 */
10104 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010105f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010106{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010107 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010108
10109 rettv->v_type = VAR_FLOAT;
10110 if (get_float_arg(argvars, &f) == OK)
10111 rettv->vval.v_float = ceil(f);
10112 else
10113 rettv->vval.v_float = 0.0;
10114}
10115#endif
10116
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010117#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010118/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010119 * "ch_close()" function
10120 */
10121 static void
10122f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10123{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010124 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010125
10126 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010127 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010128 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010129 channel_clear(channel);
10130 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010131}
10132
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010133/*
10134 * "ch_getbufnr()" function
10135 */
10136 static void
10137f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10138{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010139 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010140
10141 rettv->vval.v_number = -1;
10142 if (channel != NULL)
10143 {
10144 char_u *what = get_tv_string(&argvars[1]);
10145 int part;
10146
10147 if (STRCMP(what, "err") == 0)
10148 part = PART_ERR;
10149 else if (STRCMP(what, "out") == 0)
10150 part = PART_OUT;
10151 else if (STRCMP(what, "in") == 0)
10152 part = PART_IN;
10153 else
10154 part = PART_SOCK;
10155 if (channel->ch_part[part].ch_buffer != NULL)
10156 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10157 }
10158}
10159
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010160/*
10161 * "ch_getjob()" function
10162 */
10163 static void
10164f_ch_getjob(typval_T *argvars, typval_T *rettv)
10165{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010166 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010167
10168 if (channel != NULL)
10169 {
10170 rettv->v_type = VAR_JOB;
10171 rettv->vval.v_job = channel->ch_job;
10172 if (channel->ch_job != NULL)
10173 ++channel->ch_job->jv_refcount;
10174 }
10175}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010176
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010177/*
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010178 * "ch_info()" function
10179 */
10180 static void
10181f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
10182{
10183 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
10184
10185 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
10186 channel_info(channel, rettv->vval.v_dict);
10187}
10188
10189/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010190 * "ch_log()" function
10191 */
10192 static void
10193f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10194{
10195 char_u *msg = get_tv_string(&argvars[0]);
10196 channel_T *channel = NULL;
10197
10198 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010199 channel = get_channel_arg(&argvars[1], TRUE);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010200
10201 ch_log(channel, (char *)msg);
10202}
10203
10204/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010205 * "ch_logfile()" function
10206 */
10207 static void
10208f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10209{
10210 char_u *fname;
10211 char_u *opt = (char_u *)"";
10212 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010213
10214 fname = get_tv_string(&argvars[0]);
10215 if (argvars[1].v_type == VAR_STRING)
10216 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010217 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010218}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010219
10220/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010221 * "ch_open()" function
10222 */
10223 static void
10224f_ch_open(typval_T *argvars, typval_T *rettv)
10225{
Bram Moolenaar77073442016-02-13 23:23:53 +010010226 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010227 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010228}
10229
10230/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010231 * "ch_read()" function
10232 */
10233 static void
10234f_ch_read(typval_T *argvars, typval_T *rettv)
10235{
10236 common_channel_read(argvars, rettv, FALSE);
10237}
10238
10239/*
10240 * "ch_readraw()" function
10241 */
10242 static void
10243f_ch_readraw(typval_T *argvars, typval_T *rettv)
10244{
10245 common_channel_read(argvars, rettv, TRUE);
10246}
10247
10248/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010249 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010250 */
10251 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010252f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10253{
10254 ch_expr_common(argvars, rettv, TRUE);
10255}
10256
10257/*
10258 * "ch_sendexpr()" function
10259 */
10260 static void
10261f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10262{
10263 ch_expr_common(argvars, rettv, FALSE);
10264}
10265
10266/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010267 * "ch_evalraw()" function
10268 */
10269 static void
10270f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10271{
10272 ch_raw_common(argvars, rettv, TRUE);
10273}
10274
10275/*
10276 * "ch_sendraw()" function
10277 */
10278 static void
10279f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10280{
10281 ch_raw_common(argvars, rettv, FALSE);
10282}
10283
10284/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010285 * "ch_setoptions()" function
10286 */
10287 static void
10288f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10289{
10290 channel_T *channel;
10291 jobopt_T opt;
10292
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010293 channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010294 if (channel == NULL)
10295 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010296 clear_job_options(&opt);
10297 if (get_job_options(&argvars[1], &opt,
10298 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == FAIL)
Bram Moolenaar132006c2016-02-19 22:38:15 +010010299 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010300 channel_set_options(channel, &opt);
10301}
10302
10303/*
10304 * "ch_status()" function
10305 */
10306 static void
10307f_ch_status(typval_T *argvars, typval_T *rettv)
10308{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010309 channel_T *channel;
10310
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010311 /* return an empty string by default */
10312 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010313 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010314
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010315 channel = get_channel_arg(&argvars[0], FALSE);
10316 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010317}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010318#endif
10319
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010320/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010321 * "changenr()" function
10322 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010323 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010324f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010325{
10326 rettv->vval.v_number = curbuf->b_u_seq_cur;
10327}
10328
10329/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330 * "char2nr(string)" function
10331 */
10332 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010333f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010334{
10335#ifdef FEAT_MBYTE
10336 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010337 {
10338 int utf8 = 0;
10339
10340 if (argvars[1].v_type != VAR_UNKNOWN)
10341 utf8 = get_tv_number_chk(&argvars[1], NULL);
10342
10343 if (utf8)
10344 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10345 else
10346 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348 else
10349#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010350 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010351}
10352
10353/*
10354 * "cindent(lnum)" function
10355 */
10356 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010357f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358{
10359#ifdef FEAT_CINDENT
10360 pos_T pos;
10361 linenr_T lnum;
10362
10363 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010364 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010365 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10366 {
10367 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010368 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369 curwin->w_cursor = pos;
10370 }
10371 else
10372#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010373 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374}
10375
10376/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010377 * "clearmatches()" function
10378 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010379 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010380f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010381{
10382#ifdef FEAT_SEARCH_EXTRA
10383 clear_matches(curwin);
10384#endif
10385}
10386
10387/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010388 * "col(string)" function
10389 */
10390 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010391f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010392{
10393 colnr_T col = 0;
10394 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010395 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010396
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010397 fp = var2fpos(&argvars[0], FALSE, &fnum);
10398 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399 {
10400 if (fp->col == MAXCOL)
10401 {
10402 /* '> can be MAXCOL, get the length of the line then */
10403 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010404 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010405 else
10406 col = MAXCOL;
10407 }
10408 else
10409 {
10410 col = fp->col + 1;
10411#ifdef FEAT_VIRTUALEDIT
10412 /* col(".") when the cursor is on the NUL at the end of the line
10413 * because of "coladd" can be seen as an extra column. */
10414 if (virtual_active() && fp == &curwin->w_cursor)
10415 {
10416 char_u *p = ml_get_cursor();
10417
10418 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10419 curwin->w_virtcol - curwin->w_cursor.coladd))
10420 {
10421# ifdef FEAT_MBYTE
10422 int l;
10423
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010424 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010425 col += l;
10426# else
10427 if (*p != NUL && p[1] == NUL)
10428 ++col;
10429# endif
10430 }
10431 }
10432#endif
10433 }
10434 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010435 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010436}
10437
Bram Moolenaar572cb562005-08-05 21:35:02 +000010438#if defined(FEAT_INS_EXPAND)
10439/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010440 * "complete()" function
10441 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010442 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010443f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010444{
10445 int startcol;
10446
10447 if ((State & INSERT) == 0)
10448 {
10449 EMSG(_("E785: complete() can only be used in Insert mode"));
10450 return;
10451 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010452
10453 /* Check for undo allowed here, because if something was already inserted
10454 * the line was already saved for undo and this check isn't done. */
10455 if (!undo_allowed())
10456 return;
10457
Bram Moolenaarade00832006-03-10 21:46:58 +000010458 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10459 {
10460 EMSG(_(e_invarg));
10461 return;
10462 }
10463
10464 startcol = get_tv_number_chk(&argvars[0], NULL);
10465 if (startcol <= 0)
10466 return;
10467
10468 set_completion(startcol - 1, argvars[1].vval.v_list);
10469}
10470
10471/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010472 * "complete_add()" function
10473 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010474 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010475f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010476{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010477 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010478}
10479
10480/*
10481 * "complete_check()" function
10482 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010483 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010484f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010485{
10486 int saved = RedrawingDisabled;
10487
10488 RedrawingDisabled = 0;
10489 ins_compl_check_keys(0);
10490 rettv->vval.v_number = compl_interrupted;
10491 RedrawingDisabled = saved;
10492}
10493#endif
10494
Bram Moolenaar071d4272004-06-13 20:20:40 +000010495/*
10496 * "confirm(message, buttons[, default [, type]])" function
10497 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010498 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010499f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500{
10501#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10502 char_u *message;
10503 char_u *buttons = NULL;
10504 char_u buf[NUMBUFLEN];
10505 char_u buf2[NUMBUFLEN];
10506 int def = 1;
10507 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010508 char_u *typestr;
10509 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010510
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010511 message = get_tv_string_chk(&argvars[0]);
10512 if (message == NULL)
10513 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010514 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010516 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10517 if (buttons == NULL)
10518 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010519 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010520 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010521 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010522 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010524 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10525 if (typestr == NULL)
10526 error = TRUE;
10527 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010528 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010529 switch (TOUPPER_ASC(*typestr))
10530 {
10531 case 'E': type = VIM_ERROR; break;
10532 case 'Q': type = VIM_QUESTION; break;
10533 case 'I': type = VIM_INFO; break;
10534 case 'W': type = VIM_WARNING; break;
10535 case 'G': type = VIM_GENERIC; break;
10536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010537 }
10538 }
10539 }
10540 }
10541
10542 if (buttons == NULL || *buttons == NUL)
10543 buttons = (char_u *)_("&Ok");
10544
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010545 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010546 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010547 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548#endif
10549}
10550
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010551/*
10552 * "copy()" function
10553 */
10554 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010555f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010556{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010557 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010558}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010560#ifdef FEAT_FLOAT
10561/*
10562 * "cos()" function
10563 */
10564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010565f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010566{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010567 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010568
10569 rettv->v_type = VAR_FLOAT;
10570 if (get_float_arg(argvars, &f) == OK)
10571 rettv->vval.v_float = cos(f);
10572 else
10573 rettv->vval.v_float = 0.0;
10574}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010575
10576/*
10577 * "cosh()" function
10578 */
10579 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010580f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010581{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010582 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010583
10584 rettv->v_type = VAR_FLOAT;
10585 if (get_float_arg(argvars, &f) == OK)
10586 rettv->vval.v_float = cosh(f);
10587 else
10588 rettv->vval.v_float = 0.0;
10589}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010590#endif
10591
Bram Moolenaar071d4272004-06-13 20:20:40 +000010592/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010593 * "count()" function
10594 */
10595 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010596f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010597{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010598 long n = 0;
10599 int ic = FALSE;
10600
Bram Moolenaare9a41262005-01-15 22:18:47 +000010601 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010602 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010603 listitem_T *li;
10604 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010605 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010606
Bram Moolenaare9a41262005-01-15 22:18:47 +000010607 if ((l = argvars[0].vval.v_list) != NULL)
10608 {
10609 li = l->lv_first;
10610 if (argvars[2].v_type != VAR_UNKNOWN)
10611 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010612 int error = FALSE;
10613
10614 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010615 if (argvars[3].v_type != VAR_UNKNOWN)
10616 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010617 idx = get_tv_number_chk(&argvars[3], &error);
10618 if (!error)
10619 {
10620 li = list_find(l, idx);
10621 if (li == NULL)
10622 EMSGN(_(e_listidx), idx);
10623 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010624 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010625 if (error)
10626 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010627 }
10628
10629 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010630 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010631 ++n;
10632 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010633 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010634 else if (argvars[0].v_type == VAR_DICT)
10635 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010636 int todo;
10637 dict_T *d;
10638 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010639
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010640 if ((d = argvars[0].vval.v_dict) != NULL)
10641 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010642 int error = FALSE;
10643
Bram Moolenaare9a41262005-01-15 22:18:47 +000010644 if (argvars[2].v_type != VAR_UNKNOWN)
10645 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010646 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010647 if (argvars[3].v_type != VAR_UNKNOWN)
10648 EMSG(_(e_invarg));
10649 }
10650
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010651 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010652 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010653 {
10654 if (!HASHITEM_EMPTY(hi))
10655 {
10656 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010657 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010658 ++n;
10659 }
10660 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010661 }
10662 }
10663 else
10664 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010665 rettv->vval.v_number = n;
10666}
10667
10668/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10670 *
10671 * Checks the existence of a cscope connection.
10672 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010673 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010674f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675{
10676#ifdef FEAT_CSCOPE
10677 int num = 0;
10678 char_u *dbpath = NULL;
10679 char_u *prepend = NULL;
10680 char_u buf[NUMBUFLEN];
10681
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010682 if (argvars[0].v_type != VAR_UNKNOWN
10683 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010685 num = (int)get_tv_number(&argvars[0]);
10686 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010687 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010688 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010689 }
10690
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010691 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010692#endif
10693}
10694
10695/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010696 * "cursor(lnum, col)" function, or
10697 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010698 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010699 * Moves the cursor to the specified line and column.
10700 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010703f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704{
10705 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010706#ifdef FEAT_VIRTUALEDIT
10707 long coladd = 0;
10708#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010709 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010711 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010712 if (argvars[1].v_type == VAR_UNKNOWN)
10713 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010714 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010715 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010716
Bram Moolenaar493c1782014-05-28 14:34:46 +020010717 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010718 {
10719 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010720 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010721 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010722 line = pos.lnum;
10723 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010724#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010725 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010726#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010727 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010728 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010729 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010730 set_curswant = FALSE;
10731 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010732 }
10733 else
10734 {
10735 line = get_tv_lnum(argvars);
10736 col = get_tv_number_chk(&argvars[1], NULL);
10737#ifdef FEAT_VIRTUALEDIT
10738 if (argvars[2].v_type != VAR_UNKNOWN)
10739 coladd = get_tv_number_chk(&argvars[2], NULL);
10740#endif
10741 }
10742 if (line < 0 || col < 0
10743#ifdef FEAT_VIRTUALEDIT
10744 || coladd < 0
10745#endif
10746 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010747 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010748 if (line > 0)
10749 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010750 if (col > 0)
10751 curwin->w_cursor.col = col - 1;
10752#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010753 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010754#endif
10755
10756 /* Make sure the cursor is in a valid position. */
10757 check_cursor();
10758#ifdef FEAT_MBYTE
10759 /* Correct cursor for multi-byte character. */
10760 if (has_mbyte)
10761 mb_adjust_cursor();
10762#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010763
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010764 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010765 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010766}
10767
10768/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010769 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770 */
10771 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010772f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010774 int noref = 0;
10775
10776 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010777 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010778 if (noref < 0 || noref > 1)
10779 EMSG(_(e_invarg));
10780 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010781 {
10782 current_copyID += COPYID_INC;
10783 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785}
10786
10787/*
10788 * "delete()" function
10789 */
10790 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010791f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010792{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010793 char_u nbuf[NUMBUFLEN];
10794 char_u *name;
10795 char_u *flags;
10796
10797 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010798 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010799 return;
10800
10801 name = get_tv_string(&argvars[0]);
10802 if (name == NULL || *name == NUL)
10803 {
10804 EMSG(_(e_invarg));
10805 return;
10806 }
10807
10808 if (argvars[1].v_type != VAR_UNKNOWN)
10809 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010811 flags = (char_u *)"";
10812
10813 if (*flags == NUL)
10814 /* delete a file */
10815 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10816 else if (STRCMP(flags, "d") == 0)
10817 /* delete an empty directory */
10818 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10819 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010820 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010821 rettv->vval.v_number = delete_recursive(name);
10822 else
10823 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010824}
10825
10826/*
10827 * "did_filetype()" function
10828 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010829 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010830f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010831{
10832#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010833 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834#endif
10835}
10836
10837/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010838 * "diff_filler()" function
10839 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010840 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010841f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010842{
10843#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010844 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010845#endif
10846}
10847
10848/*
10849 * "diff_hlID()" function
10850 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010852f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010853{
10854#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010855 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010856 static linenr_T prev_lnum = 0;
10857 static int changedtick = 0;
10858 static int fnum = 0;
10859 static int change_start = 0;
10860 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010861 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010862 int filler_lines;
10863 int col;
10864
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010865 if (lnum < 0) /* ignore type error in {lnum} arg */
10866 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010867 if (lnum != prev_lnum
10868 || changedtick != curbuf->b_changedtick
10869 || fnum != curbuf->b_fnum)
10870 {
10871 /* New line, buffer, change: need to get the values. */
10872 filler_lines = diff_check(curwin, lnum);
10873 if (filler_lines < 0)
10874 {
10875 if (filler_lines == -1)
10876 {
10877 change_start = MAXCOL;
10878 change_end = -1;
10879 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10880 hlID = HLF_ADD; /* added line */
10881 else
10882 hlID = HLF_CHD; /* changed line */
10883 }
10884 else
10885 hlID = HLF_ADD; /* added line */
10886 }
10887 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010888 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010889 prev_lnum = lnum;
10890 changedtick = curbuf->b_changedtick;
10891 fnum = curbuf->b_fnum;
10892 }
10893
10894 if (hlID == HLF_CHD || hlID == HLF_TXD)
10895 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010896 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010897 if (col >= change_start && col <= change_end)
10898 hlID = HLF_TXD; /* changed text */
10899 else
10900 hlID = HLF_CHD; /* changed line */
10901 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010902 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010903#endif
10904}
10905
10906/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010010907 * "disable_char_avail_for_testing({expr})" function
10908 */
10909 static void
10910f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
10911{
10912 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
10913}
10914
10915/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010916 * "empty({expr})" function
10917 */
10918 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010919f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010920{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010010921 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010922
10923 switch (argvars[0].v_type)
10924 {
10925 case VAR_STRING:
10926 case VAR_FUNC:
10927 n = argvars[0].vval.v_string == NULL
10928 || *argvars[0].vval.v_string == NUL;
10929 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010930 case VAR_PARTIAL:
10931 n = FALSE;
10932 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010933 case VAR_NUMBER:
10934 n = argvars[0].vval.v_number == 0;
10935 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010936 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010010937#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010938 n = argvars[0].vval.v_float == 0.0;
10939 break;
10940#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010941 case VAR_LIST:
10942 n = argvars[0].vval.v_list == NULL
10943 || argvars[0].vval.v_list->lv_first == NULL;
10944 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010945 case VAR_DICT:
10946 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010947 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010948 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010949 case VAR_SPECIAL:
10950 n = argvars[0].vval.v_number != VVAL_TRUE;
10951 break;
10952
Bram Moolenaar835dc632016-02-07 14:27:38 +010010953 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010954#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010955 n = argvars[0].vval.v_job == NULL
10956 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
10957 break;
10958#endif
10959 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010960#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010961 n = argvars[0].vval.v_channel == NULL
10962 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010010963 break;
10964#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010010965 case VAR_UNKNOWN:
10966 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
10967 n = TRUE;
10968 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010969 }
10970
10971 rettv->vval.v_number = n;
10972}
10973
10974/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975 * "escape({string}, {chars})" function
10976 */
10977 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010978f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979{
10980 char_u buf[NUMBUFLEN];
10981
Bram Moolenaar758711c2005-02-02 23:11:38 +000010982 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10983 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010984 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985}
10986
10987/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010988 * "eval()" function
10989 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010990 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010991f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010992{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010993 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010994
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010995 s = get_tv_string_chk(&argvars[0]);
10996 if (s != NULL)
10997 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010998
Bram Moolenaar615b9972015-01-14 17:15:05 +010010999 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011000 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11001 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011002 if (p != NULL && !aborting())
11003 EMSG2(_(e_invexpr2), p);
11004 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011005 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011006 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011007 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011008 else if (*s != NUL)
11009 EMSG(_(e_trailing));
11010}
11011
11012/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011013 * "eventhandler()" function
11014 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011015 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011016f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011017{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011018 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019}
11020
11021/*
11022 * "executable()" function
11023 */
11024 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011025f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011026{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011027 char_u *name = get_tv_string(&argvars[0]);
11028
11029 /* Check in $PATH and also check directly if there is a directory name. */
11030 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11031 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011032}
11033
11034/*
11035 * "exepath()" function
11036 */
11037 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011038f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011039{
11040 char_u *p = NULL;
11041
Bram Moolenaarb5971142015-03-21 17:32:19 +010011042 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011043 rettv->v_type = VAR_STRING;
11044 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045}
11046
11047/*
11048 * "exists()" function
11049 */
11050 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011051f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011052{
11053 char_u *p;
11054 char_u *name;
11055 int n = FALSE;
11056 int len = 0;
11057
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011058 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011059 if (*p == '$') /* environment variable */
11060 {
11061 /* first try "normal" environment variables (fast) */
11062 if (mch_getenv(p + 1) != NULL)
11063 n = TRUE;
11064 else
11065 {
11066 /* try expanding things like $VIM and ${HOME} */
11067 p = expand_env_save(p);
11068 if (p != NULL && *p != '$')
11069 n = TRUE;
11070 vim_free(p);
11071 }
11072 }
11073 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011074 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011075 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011076 if (*skipwhite(p) != NUL)
11077 n = FALSE; /* trailing garbage */
11078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079 else if (*p == '*') /* internal or user defined function */
11080 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011081 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011082 }
11083 else if (*p == ':')
11084 {
11085 n = cmd_exists(p + 1);
11086 }
11087 else if (*p == '#')
11088 {
11089#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011090 if (p[1] == '#')
11091 n = autocmd_supported(p + 2);
11092 else
11093 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011094#endif
11095 }
11096 else /* internal variable */
11097 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011098 char_u *tofree;
11099 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011100
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011101 /* get_name_len() takes care of expanding curly braces */
11102 name = p;
11103 len = get_name_len(&p, &tofree, TRUE, FALSE);
11104 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011105 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011106 if (tofree != NULL)
11107 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011108 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011109 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011110 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011111 /* handle d.key, l[idx], f(expr) */
11112 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11113 if (n)
11114 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115 }
11116 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011117 if (*p != NUL)
11118 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011119
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011120 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011121 }
11122
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011123 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011124}
11125
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011126#ifdef FEAT_FLOAT
11127/*
11128 * "exp()" function
11129 */
11130 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011131f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011132{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011133 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011134
11135 rettv->v_type = VAR_FLOAT;
11136 if (get_float_arg(argvars, &f) == OK)
11137 rettv->vval.v_float = exp(f);
11138 else
11139 rettv->vval.v_float = 0.0;
11140}
11141#endif
11142
Bram Moolenaar071d4272004-06-13 20:20:40 +000011143/*
11144 * "expand()" function
11145 */
11146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011147f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011148{
11149 char_u *s;
11150 int len;
11151 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011152 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011154 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011155 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011156
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011157 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011158 if (argvars[1].v_type != VAR_UNKNOWN
11159 && argvars[2].v_type != VAR_UNKNOWN
11160 && get_tv_number_chk(&argvars[2], &error)
11161 && !error)
11162 {
11163 rettv->v_type = VAR_LIST;
11164 rettv->vval.v_list = NULL;
11165 }
11166
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011167 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011168 if (*s == '%' || *s == '#' || *s == '<')
11169 {
11170 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011171 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011172 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011173 if (rettv->v_type == VAR_LIST)
11174 {
11175 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11176 list_append_string(rettv->vval.v_list, result, -1);
11177 else
11178 vim_free(result);
11179 }
11180 else
11181 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011182 }
11183 else
11184 {
11185 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011186 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011187 if (argvars[1].v_type != VAR_UNKNOWN
11188 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011189 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011190 if (!error)
11191 {
11192 ExpandInit(&xpc);
11193 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011194 if (p_wic)
11195 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011196 if (rettv->v_type == VAR_STRING)
11197 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11198 options, WILD_ALL);
11199 else if (rettv_list_alloc(rettv) != FAIL)
11200 {
11201 int i;
11202
11203 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11204 for (i = 0; i < xpc.xp_numfiles; i++)
11205 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11206 ExpandCleanup(&xpc);
11207 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011208 }
11209 else
11210 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011211 }
11212}
11213
11214/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011215 * Go over all entries in "d2" and add them to "d1".
11216 * When "action" is "error" then a duplicate key is an error.
11217 * When "action" is "force" then a duplicate key is overwritten.
11218 * Otherwise duplicate keys are ignored ("action" is "keep").
11219 */
11220 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011221dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011222{
11223 dictitem_T *di1;
11224 hashitem_T *hi2;
11225 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011226 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011227
11228 todo = (int)d2->dv_hashtab.ht_used;
11229 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11230 {
11231 if (!HASHITEM_EMPTY(hi2))
11232 {
11233 --todo;
11234 di1 = dict_find(d1, hi2->hi_key, -1);
11235 if (d1->dv_scope != 0)
11236 {
11237 /* Disallow replacing a builtin function in l: and g:.
11238 * Check the key to be valid when adding to any
11239 * scope. */
11240 if (d1->dv_scope == VAR_DEF_SCOPE
11241 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11242 && var_check_func_name(hi2->hi_key,
11243 di1 == NULL))
11244 break;
11245 if (!valid_varname(hi2->hi_key))
11246 break;
11247 }
11248 if (di1 == NULL)
11249 {
11250 di1 = dictitem_copy(HI2DI(hi2));
11251 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11252 dictitem_free(di1);
11253 }
11254 else if (*action == 'e')
11255 {
11256 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11257 break;
11258 }
11259 else if (*action == 'f' && HI2DI(hi2) != di1)
11260 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011261 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11262 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011263 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011264 clear_tv(&di1->di_tv);
11265 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11266 }
11267 }
11268 }
11269}
11270
11271/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011272 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011273 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011274 */
11275 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011276f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011277{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011278 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011279
Bram Moolenaare9a41262005-01-15 22:18:47 +000011280 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011281 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011282 list_T *l1, *l2;
11283 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011284 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011285 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011286
Bram Moolenaare9a41262005-01-15 22:18:47 +000011287 l1 = argvars[0].vval.v_list;
11288 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011289 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011290 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011291 {
11292 if (argvars[2].v_type != VAR_UNKNOWN)
11293 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011294 before = get_tv_number_chk(&argvars[2], &error);
11295 if (error)
11296 return; /* type error; errmsg already given */
11297
Bram Moolenaar758711c2005-02-02 23:11:38 +000011298 if (before == l1->lv_len)
11299 item = NULL;
11300 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011301 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011302 item = list_find(l1, before);
11303 if (item == NULL)
11304 {
11305 EMSGN(_(e_listidx), before);
11306 return;
11307 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011308 }
11309 }
11310 else
11311 item = NULL;
11312 list_extend(l1, l2, item);
11313
Bram Moolenaare9a41262005-01-15 22:18:47 +000011314 copy_tv(&argvars[0], rettv);
11315 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011316 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011317 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11318 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011319 dict_T *d1, *d2;
11320 char_u *action;
11321 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011322
11323 d1 = argvars[0].vval.v_dict;
11324 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011325 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011326 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011327 {
11328 /* Check the third argument. */
11329 if (argvars[2].v_type != VAR_UNKNOWN)
11330 {
11331 static char *(av[]) = {"keep", "force", "error"};
11332
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011333 action = get_tv_string_chk(&argvars[2]);
11334 if (action == NULL)
11335 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011336 for (i = 0; i < 3; ++i)
11337 if (STRCMP(action, av[i]) == 0)
11338 break;
11339 if (i == 3)
11340 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011341 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011342 return;
11343 }
11344 }
11345 else
11346 action = (char_u *)"force";
11347
Bram Moolenaara9922d62013-05-30 13:01:18 +020011348 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011349
Bram Moolenaare9a41262005-01-15 22:18:47 +000011350 copy_tv(&argvars[0], rettv);
11351 }
11352 }
11353 else
11354 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011355}
11356
11357/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011358 * "feedkeys()" function
11359 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011360 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011361f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011362{
11363 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011364 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011365 char_u *keys, *flags;
11366 char_u nbuf[NUMBUFLEN];
11367 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011368 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011369 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011370
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011371 /* This is not allowed in the sandbox. If the commands would still be
11372 * executed in the sandbox it would be OK, but it probably happens later,
11373 * when "sandbox" is no longer set. */
11374 if (check_secure())
11375 return;
11376
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011377 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011378
11379 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011380 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011381 flags = get_tv_string_buf(&argvars[1], nbuf);
11382 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011383 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011384 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011385 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011386 case 'n': remap = FALSE; break;
11387 case 'm': remap = TRUE; break;
11388 case 't': typed = TRUE; break;
11389 case 'i': insert = TRUE; break;
11390 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011391 }
11392 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011393 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011394
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011395 if (*keys != NUL || execute)
11396 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011397 /* Need to escape K_SPECIAL and CSI before putting the string in the
11398 * typeahead buffer. */
11399 keys_esc = vim_strsave_escape_csi(keys);
11400 if (keys_esc != NULL)
11401 {
11402 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011403 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011404 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011405 if (vgetc_busy)
11406 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011407 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011408 {
11409 int save_msg_scroll = msg_scroll;
11410
11411 /* Avoid a 1 second delay when the keys start Insert mode. */
11412 msg_scroll = FALSE;
Bram Moolenaar9bd547a2016-04-01 21:00:48 +020011413
11414 ++ex_normal_busy;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011415 exec_normal(TRUE);
Bram Moolenaar9bd547a2016-04-01 21:00:48 +020011416 --ex_normal_busy;
Bram Moolenaar9e496852016-03-11 19:31:47 +010011417 msg_scroll |= save_msg_scroll;
11418 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011419 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011420 }
11421}
11422
11423/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011424 * "filereadable()" function
11425 */
11426 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011427f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011428{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011429 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011430 char_u *p;
11431 int n;
11432
Bram Moolenaarc236c162008-07-13 17:41:49 +000011433#ifndef O_NONBLOCK
11434# define O_NONBLOCK 0
11435#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011436 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011437 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11438 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011439 {
11440 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011441 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011442 }
11443 else
11444 n = FALSE;
11445
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011446 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011447}
11448
11449/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011450 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011451 * rights to write into.
11452 */
11453 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011454f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011455{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011456 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011457}
11458
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011459 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011460findfilendir(
11461 typval_T *argvars UNUSED,
11462 typval_T *rettv,
11463 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011464{
11465#ifdef FEAT_SEARCHPATH
11466 char_u *fname;
11467 char_u *fresult = NULL;
11468 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11469 char_u *p;
11470 char_u pathbuf[NUMBUFLEN];
11471 int count = 1;
11472 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011473 int error = FALSE;
11474#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011475
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011476 rettv->vval.v_string = NULL;
11477 rettv->v_type = VAR_STRING;
11478
11479#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011480 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011481
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011482 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011483 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011484 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11485 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011486 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011487 else
11488 {
11489 if (*p != NUL)
11490 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011491
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011492 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011493 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011494 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011495 }
11496
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011497 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11498 error = TRUE;
11499
11500 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011501 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011502 do
11503 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011504 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011505 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011506 fresult = find_file_in_path_option(first ? fname : NULL,
11507 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011508 0, first, path,
11509 find_what,
11510 curbuf->b_ffname,
11511 find_what == FINDFILE_DIR
11512 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011513 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011514
11515 if (fresult != NULL && rettv->v_type == VAR_LIST)
11516 list_append_string(rettv->vval.v_list, fresult, -1);
11517
11518 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011519 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011520
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011521 if (rettv->v_type == VAR_STRING)
11522 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011523#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011524}
11525
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011526static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11527static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011528
11529/*
11530 * Implementation of map() and filter().
11531 */
11532 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011533filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011534{
11535 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011536 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011537 listitem_T *li, *nli;
11538 list_T *l = NULL;
11539 dictitem_T *di;
11540 hashtab_T *ht;
11541 hashitem_T *hi;
11542 dict_T *d = NULL;
11543 typval_T save_val;
11544 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011545 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011546 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011547 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011548 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011549 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011550 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011551 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011552
Bram Moolenaare9a41262005-01-15 22:18:47 +000011553 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011554 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011555 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011556 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011557 return;
11558 }
11559 else if (argvars[0].v_type == VAR_DICT)
11560 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011561 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011562 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011563 return;
11564 }
11565 else
11566 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011567 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011568 return;
11569 }
11570
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011571 expr = get_tv_string_buf_chk(&argvars[1], buf);
11572 /* On type errors, the preceding call has already displayed an error
11573 * message. Avoid a misleading error message for an empty string that
11574 * was not passed as argument. */
11575 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011576 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011577 prepare_vimvar(VV_VAL, &save_val);
11578 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011579
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011580 /* We reset "did_emsg" to be able to detect whether an error
11581 * occurred during evaluation of the expression. */
11582 save_did_emsg = did_emsg;
11583 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011584
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011585 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011586 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011587 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011588 vimvars[VV_KEY].vv_type = VAR_STRING;
11589
11590 ht = &d->dv_hashtab;
11591 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011592 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011593 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011594 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011595 if (!HASHITEM_EMPTY(hi))
11596 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011597 int r;
11598
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011599 --todo;
11600 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011601 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011602 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11603 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011604 break;
11605 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011606 r = filter_map_one(&di->di_tv, expr, map, &rem);
11607 clear_tv(&vimvars[VV_KEY].vv_tv);
11608 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011609 break;
11610 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011611 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011612 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11613 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011614 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011615 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011616 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011617 }
11618 }
11619 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011620 }
11621 else
11622 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011623 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11624
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011625 for (li = l->lv_first; li != NULL; li = nli)
11626 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011627 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011628 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011629 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011630 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011631 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011632 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011633 break;
11634 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011635 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011636 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011637 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011638 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011639
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011640 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011641 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011642
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011643 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011644 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011645
11646 copy_tv(&argvars[0], rettv);
11647}
11648
11649 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011650filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011651{
Bram Moolenaar33570922005-01-25 22:26:29 +000011652 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011653 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011654 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011655
Bram Moolenaar33570922005-01-25 22:26:29 +000011656 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011657 s = expr;
11658 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011659 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011660 if (*s != NUL) /* check for trailing chars after expr */
11661 {
11662 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011663 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011664 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011665 }
11666 if (map)
11667 {
11668 /* map(): replace the list item value */
11669 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011670 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011671 *tv = rettv;
11672 }
11673 else
11674 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011675 int error = FALSE;
11676
Bram Moolenaare9a41262005-01-15 22:18:47 +000011677 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011678 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011679 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011680 /* On type error, nothing has been removed; return FAIL to stop the
11681 * loop. The error message was given by get_tv_number_chk(). */
11682 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011683 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011684 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011685 retval = OK;
11686theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011687 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011688 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011689}
11690
11691/*
11692 * "filter()" function
11693 */
11694 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011695f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011696{
11697 filter_map(argvars, rettv, FALSE);
11698}
11699
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011700/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011701 * "finddir({fname}[, {path}[, {count}]])" function
11702 */
11703 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011704f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011705{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011706 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011707}
11708
11709/*
11710 * "findfile({fname}[, {path}[, {count}]])" function
11711 */
11712 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011713f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011714{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011715 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011716}
11717
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011718#ifdef FEAT_FLOAT
11719/*
11720 * "float2nr({float})" function
11721 */
11722 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011723f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011724{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011725 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011726
11727 if (get_float_arg(argvars, &f) == OK)
11728 {
11729 if (f < -0x7fffffff)
11730 rettv->vval.v_number = -0x7fffffff;
11731 else if (f > 0x7fffffff)
11732 rettv->vval.v_number = 0x7fffffff;
11733 else
11734 rettv->vval.v_number = (varnumber_T)f;
11735 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011736}
11737
11738/*
11739 * "floor({float})" function
11740 */
11741 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011742f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011743{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011744 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011745
11746 rettv->v_type = VAR_FLOAT;
11747 if (get_float_arg(argvars, &f) == OK)
11748 rettv->vval.v_float = floor(f);
11749 else
11750 rettv->vval.v_float = 0.0;
11751}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011752
11753/*
11754 * "fmod()" function
11755 */
11756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011757f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011758{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011759 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011760
11761 rettv->v_type = VAR_FLOAT;
11762 if (get_float_arg(argvars, &fx) == OK
11763 && get_float_arg(&argvars[1], &fy) == OK)
11764 rettv->vval.v_float = fmod(fx, fy);
11765 else
11766 rettv->vval.v_float = 0.0;
11767}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011768#endif
11769
Bram Moolenaar0d660222005-01-07 21:51:51 +000011770/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011771 * "fnameescape({string})" function
11772 */
11773 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011774f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011775{
11776 rettv->vval.v_string = vim_strsave_fnameescape(
11777 get_tv_string(&argvars[0]), FALSE);
11778 rettv->v_type = VAR_STRING;
11779}
11780
11781/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011782 * "fnamemodify({fname}, {mods})" function
11783 */
11784 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011785f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011786{
11787 char_u *fname;
11788 char_u *mods;
11789 int usedlen = 0;
11790 int len;
11791 char_u *fbuf = NULL;
11792 char_u buf[NUMBUFLEN];
11793
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011794 fname = get_tv_string_chk(&argvars[0]);
11795 mods = get_tv_string_buf_chk(&argvars[1], buf);
11796 if (fname == NULL || mods == NULL)
11797 fname = NULL;
11798 else
11799 {
11800 len = (int)STRLEN(fname);
11801 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011803
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011804 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011805 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011806 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011807 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011808 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011809 vim_free(fbuf);
11810}
11811
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011812static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011813
11814/*
11815 * "foldclosed()" function
11816 */
11817 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011818foldclosed_both(
11819 typval_T *argvars UNUSED,
11820 typval_T *rettv,
11821 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011822{
11823#ifdef FEAT_FOLDING
11824 linenr_T lnum;
11825 linenr_T first, last;
11826
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011827 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011828 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11829 {
11830 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11831 {
11832 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011833 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011834 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011835 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011836 return;
11837 }
11838 }
11839#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011840 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011841}
11842
11843/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011844 * "foldclosed()" function
11845 */
11846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011847f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011848{
11849 foldclosed_both(argvars, rettv, FALSE);
11850}
11851
11852/*
11853 * "foldclosedend()" function
11854 */
11855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011856f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011857{
11858 foldclosed_both(argvars, rettv, TRUE);
11859}
11860
11861/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011862 * "foldlevel()" function
11863 */
11864 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011865f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011866{
11867#ifdef FEAT_FOLDING
11868 linenr_T lnum;
11869
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011870 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011871 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011872 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011874}
11875
11876/*
11877 * "foldtext()" function
11878 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011879 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011880f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011881{
11882#ifdef FEAT_FOLDING
11883 linenr_T lnum;
11884 char_u *s;
11885 char_u *r;
11886 int len;
11887 char *txt;
11888#endif
11889
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011890 rettv->v_type = VAR_STRING;
11891 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011892#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011893 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11894 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11895 <= curbuf->b_ml.ml_line_count
11896 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011897 {
11898 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011899 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11900 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011901 {
11902 if (!linewhite(lnum))
11903 break;
11904 ++lnum;
11905 }
11906
11907 /* Find interesting text in this line. */
11908 s = skipwhite(ml_get(lnum));
11909 /* skip C comment-start */
11910 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011911 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011912 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011913 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011914 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011915 {
11916 s = skipwhite(ml_get(lnum + 1));
11917 if (*s == '*')
11918 s = skipwhite(s + 1);
11919 }
11920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011921 txt = _("+-%s%3ld lines: ");
11922 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011923 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011924 + 20 /* for %3ld */
11925 + STRLEN(s))); /* concatenated */
11926 if (r != NULL)
11927 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011928 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11929 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11930 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011931 len = (int)STRLEN(r);
11932 STRCAT(r, s);
11933 /* remove 'foldmarker' and 'commentstring' */
11934 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011935 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011936 }
11937 }
11938#endif
11939}
11940
11941/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011942 * "foldtextresult(lnum)" function
11943 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011944 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011945f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011946{
11947#ifdef FEAT_FOLDING
11948 linenr_T lnum;
11949 char_u *text;
11950 char_u buf[51];
11951 foldinfo_T foldinfo;
11952 int fold_count;
11953#endif
11954
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011955 rettv->v_type = VAR_STRING;
11956 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011957#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011958 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011959 /* treat illegal types and illegal string values for {lnum} the same */
11960 if (lnum < 0)
11961 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011962 fold_count = foldedCount(curwin, lnum, &foldinfo);
11963 if (fold_count > 0)
11964 {
11965 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11966 &foldinfo, buf);
11967 if (text == buf)
11968 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011969 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011970 }
11971#endif
11972}
11973
11974/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011975 * "foreground()" function
11976 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011977 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011978f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011979{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011980#ifdef FEAT_GUI
11981 if (gui.in_use)
11982 gui_mch_set_foreground();
11983#else
11984# ifdef WIN32
11985 win32_set_foreground();
11986# endif
11987#endif
11988}
11989
11990/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011991 * "function()" function
11992 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011993 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011994f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011995{
11996 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011997 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011998 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011999 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012000
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012001 if (argvars[0].v_type == VAR_FUNC)
12002 {
12003 /* function(MyFunc, [arg], dict) */
12004 s = argvars[0].vval.v_string;
12005 }
12006 else if (argvars[0].v_type == VAR_PARTIAL
12007 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012008 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012009 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012010 arg_pt = argvars[0].vval.v_partial;
12011 s = arg_pt->pt_name;
12012 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012013 else
12014 {
12015 /* function('MyFunc', [arg], dict) */
12016 s = get_tv_string(&argvars[0]);
12017 use_string = TRUE;
12018 }
12019
12020 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012021 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012022 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012023 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
12024 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012025 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012026 else
12027 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012028 int dict_idx = 0;
12029 int arg_idx = 0;
12030 list_T *list = NULL;
12031
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012032 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012033 {
12034 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012035 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012036
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012037 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12038 * also be called from another script. Using trans_function_name()
12039 * would also work, but some plugins depend on the name being
12040 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012041 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012042 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
12043 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012044 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012045 STRCPY(name, sid_buf);
12046 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012047 }
12048 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012049 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012050 name = vim_strsave(s);
12051
12052 if (argvars[1].v_type != VAR_UNKNOWN)
12053 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012054 if (argvars[2].v_type != VAR_UNKNOWN)
12055 {
12056 /* function(name, [args], dict) */
12057 arg_idx = 1;
12058 dict_idx = 2;
12059 }
12060 else if (argvars[1].v_type == VAR_DICT)
12061 /* function(name, dict) */
12062 dict_idx = 1;
12063 else
12064 /* function(name, [args]) */
12065 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010012066 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012067 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012068 if (argvars[dict_idx].v_type != VAR_DICT)
12069 {
12070 EMSG(_("E922: expected a dict"));
12071 vim_free(name);
12072 return;
12073 }
12074 if (argvars[dict_idx].vval.v_dict == NULL)
12075 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012076 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012077 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012078 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012079 if (argvars[arg_idx].v_type != VAR_LIST)
12080 {
12081 EMSG(_("E923: Second argument of function() must be a list or a dict"));
12082 vim_free(name);
12083 return;
12084 }
12085 list = argvars[arg_idx].vval.v_list;
12086 if (list == NULL || list->lv_len == 0)
12087 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012088 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012089 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012090 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010012091 {
12092 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012093
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012094 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010012095 if (pt == NULL)
12096 vim_free(name);
12097 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012098 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012099 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012100 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012101 listitem_T *li;
12102 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012103 int arg_len = 0;
12104 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012105
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012106 if (arg_pt != NULL)
12107 arg_len = arg_pt->pt_argc;
12108 if (list != NULL)
12109 lv_len = list->lv_len;
12110 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012111 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012112 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012113 if (pt->pt_argv == NULL)
12114 {
12115 vim_free(pt);
12116 vim_free(name);
12117 return;
12118 }
12119 else
12120 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012121 for (i = 0; i < arg_len; i++)
12122 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
12123 if (lv_len > 0)
12124 for (li = list->lv_first; li != NULL;
12125 li = li->li_next)
12126 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012127 }
12128 }
12129
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012130 /* For "function(dict.func, [], dict)" and "func" is a partial
12131 * use "dict". That is backwards compatible. */
12132 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012133 {
12134 pt->pt_dict = argvars[dict_idx].vval.v_dict;
12135 ++pt->pt_dict->dv_refcount;
12136 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012137 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012138 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012139 pt->pt_dict = arg_pt->pt_dict;
12140 if (pt->pt_dict != NULL)
12141 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012142 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012143
12144 pt->pt_refcount = 1;
12145 pt->pt_name = name;
12146 func_ref(pt->pt_name);
12147 }
12148 rettv->v_type = VAR_PARTIAL;
12149 rettv->vval.v_partial = pt;
12150 }
12151 else
12152 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012153 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012154 rettv->v_type = VAR_FUNC;
12155 rettv->vval.v_string = name;
12156 func_ref(name);
12157 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012158 }
12159}
12160
12161/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012162 * "garbagecollect()" function
12163 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012164 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012165f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012166{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012167 /* This is postponed until we are back at the toplevel, because we may be
12168 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12169 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012170
12171 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12172 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012173}
12174
12175/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012176 * "get()" function
12177 */
12178 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012179f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012180{
Bram Moolenaar33570922005-01-25 22:26:29 +000012181 listitem_T *li;
12182 list_T *l;
12183 dictitem_T *di;
12184 dict_T *d;
12185 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012186
Bram Moolenaare9a41262005-01-15 22:18:47 +000012187 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012188 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012189 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012190 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012191 int error = FALSE;
12192
12193 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12194 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012195 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012196 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012197 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012198 else if (argvars[0].v_type == VAR_DICT)
12199 {
12200 if ((d = argvars[0].vval.v_dict) != NULL)
12201 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012202 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012203 if (di != NULL)
12204 tv = &di->di_tv;
12205 }
12206 }
12207 else
12208 EMSG2(_(e_listdictarg), "get()");
12209
12210 if (tv == NULL)
12211 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012212 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012213 copy_tv(&argvars[2], rettv);
12214 }
12215 else
12216 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012217}
12218
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012219static 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 +000012220
12221/*
12222 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012223 * Return a range (from start to end) of lines in rettv from the specified
12224 * buffer.
12225 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012226 */
12227 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012228get_buffer_lines(
12229 buf_T *buf,
12230 linenr_T start,
12231 linenr_T end,
12232 int retlist,
12233 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012234{
12235 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012236
Bram Moolenaar959a1432013-12-14 12:17:38 +010012237 rettv->v_type = VAR_STRING;
12238 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012239 if (retlist && rettv_list_alloc(rettv) == FAIL)
12240 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012241
12242 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12243 return;
12244
12245 if (!retlist)
12246 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012247 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12248 p = ml_get_buf(buf, start, FALSE);
12249 else
12250 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012251 rettv->vval.v_string = vim_strsave(p);
12252 }
12253 else
12254 {
12255 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012256 return;
12257
12258 if (start < 1)
12259 start = 1;
12260 if (end > buf->b_ml.ml_line_count)
12261 end = buf->b_ml.ml_line_count;
12262 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012263 if (list_append_string(rettv->vval.v_list,
12264 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012265 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012266 }
12267}
12268
12269/*
12270 * "getbufline()" function
12271 */
12272 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012273f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012274{
12275 linenr_T lnum;
12276 linenr_T end;
12277 buf_T *buf;
12278
12279 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12280 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012281 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012282 --emsg_off;
12283
Bram Moolenaar661b1822005-07-28 22:36:45 +000012284 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012285 if (argvars[2].v_type == VAR_UNKNOWN)
12286 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012287 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012288 end = get_tv_lnum_buf(&argvars[2], buf);
12289
Bram Moolenaar342337a2005-07-21 21:11:17 +000012290 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012291}
12292
Bram Moolenaar0d660222005-01-07 21:51:51 +000012293/*
12294 * "getbufvar()" function
12295 */
12296 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012297f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012298{
12299 buf_T *buf;
12300 buf_T *save_curbuf;
12301 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012302 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012303 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012304
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012305 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12306 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012307 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012308 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012309
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012310 rettv->v_type = VAR_STRING;
12311 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012312
12313 if (buf != NULL && varname != NULL)
12314 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012315 /* set curbuf to be our buf, temporarily */
12316 save_curbuf = curbuf;
12317 curbuf = buf;
12318
Bram Moolenaar0d660222005-01-07 21:51:51 +000012319 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012320 {
12321 if (get_option_tv(&varname, rettv, TRUE) == OK)
12322 done = TRUE;
12323 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012324 else if (STRCMP(varname, "changedtick") == 0)
12325 {
12326 rettv->v_type = VAR_NUMBER;
12327 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012328 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012329 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012330 else
12331 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012332 /* Look up the variable. */
12333 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12334 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12335 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012336 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012337 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012338 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012339 done = TRUE;
12340 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012341 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012342
12343 /* restore previous notion of curbuf */
12344 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012345 }
12346
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012347 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12348 /* use the default value */
12349 copy_tv(&argvars[2], rettv);
12350
Bram Moolenaar0d660222005-01-07 21:51:51 +000012351 --emsg_off;
12352}
12353
12354/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012355 * "getchar()" function
12356 */
12357 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012358f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012359{
12360 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012361 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012362
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012363 /* Position the cursor. Needed after a message that ends in a space. */
12364 windgoto(msg_row, msg_col);
12365
Bram Moolenaar071d4272004-06-13 20:20:40 +000012366 ++no_mapping;
12367 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012368 for (;;)
12369 {
12370 if (argvars[0].v_type == VAR_UNKNOWN)
12371 /* getchar(): blocking wait. */
12372 n = safe_vgetc();
12373 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12374 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012375 n = vpeekc_any();
12376 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012377 /* illegal argument or getchar(0) and no char avail: return zero */
12378 n = 0;
12379 else
12380 /* getchar(0) and char avail: return char */
12381 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012382
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012383 if (n == K_IGNORE)
12384 continue;
12385 break;
12386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012387 --no_mapping;
12388 --allow_keys;
12389
Bram Moolenaar219b8702006-11-01 14:32:36 +000012390 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12391 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12392 vimvars[VV_MOUSE_COL].vv_nr = 0;
12393
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012394 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012395 if (IS_SPECIAL(n) || mod_mask != 0)
12396 {
12397 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12398 int i = 0;
12399
12400 /* Turn a special key into three bytes, plus modifier. */
12401 if (mod_mask != 0)
12402 {
12403 temp[i++] = K_SPECIAL;
12404 temp[i++] = KS_MODIFIER;
12405 temp[i++] = mod_mask;
12406 }
12407 if (IS_SPECIAL(n))
12408 {
12409 temp[i++] = K_SPECIAL;
12410 temp[i++] = K_SECOND(n);
12411 temp[i++] = K_THIRD(n);
12412 }
12413#ifdef FEAT_MBYTE
12414 else if (has_mbyte)
12415 i += (*mb_char2bytes)(n, temp + i);
12416#endif
12417 else
12418 temp[i++] = n;
12419 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012420 rettv->v_type = VAR_STRING;
12421 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012422
12423#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012424 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012425 {
12426 int row = mouse_row;
12427 int col = mouse_col;
12428 win_T *win;
12429 linenr_T lnum;
12430# ifdef FEAT_WINDOWS
12431 win_T *wp;
12432# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012433 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012434
12435 if (row >= 0 && col >= 0)
12436 {
12437 /* Find the window at the mouse coordinates and compute the
12438 * text position. */
12439 win = mouse_find_win(&row, &col);
12440 (void)mouse_comp_pos(win, &row, &col, &lnum);
12441# ifdef FEAT_WINDOWS
12442 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012443 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012444# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012445 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012446 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12447 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12448 }
12449 }
12450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012451 }
12452}
12453
12454/*
12455 * "getcharmod()" function
12456 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012457 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012458f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012459{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012460 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012461}
12462
12463/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012464 * "getcharsearch()" function
12465 */
12466 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012467f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012468{
12469 if (rettv_dict_alloc(rettv) != FAIL)
12470 {
12471 dict_T *dict = rettv->vval.v_dict;
12472
12473 dict_add_nr_str(dict, "char", 0L, last_csearch());
12474 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12475 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12476 }
12477}
12478
12479/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012480 * "getcmdline()" function
12481 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012482 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012483f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012484{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012485 rettv->v_type = VAR_STRING;
12486 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012487}
12488
12489/*
12490 * "getcmdpos()" function
12491 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012492 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012493f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012494{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012495 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012496}
12497
12498/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012499 * "getcmdtype()" function
12500 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012502f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012503{
12504 rettv->v_type = VAR_STRING;
12505 rettv->vval.v_string = alloc(2);
12506 if (rettv->vval.v_string != NULL)
12507 {
12508 rettv->vval.v_string[0] = get_cmdline_type();
12509 rettv->vval.v_string[1] = NUL;
12510 }
12511}
12512
12513/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012514 * "getcmdwintype()" function
12515 */
12516 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012517f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012518{
12519 rettv->v_type = VAR_STRING;
12520 rettv->vval.v_string = NULL;
12521#ifdef FEAT_CMDWIN
12522 rettv->vval.v_string = alloc(2);
12523 if (rettv->vval.v_string != NULL)
12524 {
12525 rettv->vval.v_string[0] = cmdwin_type;
12526 rettv->vval.v_string[1] = NUL;
12527 }
12528#endif
12529}
12530
12531/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012532 * "getcwd()" function
12533 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012534 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012535f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012536{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012537 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012538 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012539
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012540 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012541 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012542
12543 wp = find_tabwin(&argvars[0], &argvars[1]);
12544 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012545 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012546 if (wp->w_localdir != NULL)
12547 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12548 else if(globaldir != NULL)
12549 rettv->vval.v_string = vim_strsave(globaldir);
12550 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012551 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012552 cwd = alloc(MAXPATHL);
12553 if (cwd != NULL)
12554 {
12555 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12556 rettv->vval.v_string = vim_strsave(cwd);
12557 vim_free(cwd);
12558 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012559 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012560#ifdef BACKSLASH_IN_FILENAME
12561 if (rettv->vval.v_string != NULL)
12562 slash_adjust(rettv->vval.v_string);
12563#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012564 }
12565}
12566
12567/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012568 * "getfontname()" function
12569 */
12570 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012571f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012572{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012573 rettv->v_type = VAR_STRING;
12574 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012575#ifdef FEAT_GUI
12576 if (gui.in_use)
12577 {
12578 GuiFont font;
12579 char_u *name = NULL;
12580
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012581 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012582 {
12583 /* Get the "Normal" font. Either the name saved by
12584 * hl_set_font_name() or from the font ID. */
12585 font = gui.norm_font;
12586 name = hl_get_font_name();
12587 }
12588 else
12589 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012590 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012591 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12592 return;
12593 font = gui_mch_get_font(name, FALSE);
12594 if (font == NOFONT)
12595 return; /* Invalid font name, return empty string. */
12596 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012597 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012598 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012599 gui_mch_free_font(font);
12600 }
12601#endif
12602}
12603
12604/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012605 * "getfperm({fname})" function
12606 */
12607 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012608f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012609{
12610 char_u *fname;
12611 struct stat st;
12612 char_u *perm = NULL;
12613 char_u flags[] = "rwx";
12614 int i;
12615
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012616 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012617
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012618 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012619 if (mch_stat((char *)fname, &st) >= 0)
12620 {
12621 perm = vim_strsave((char_u *)"---------");
12622 if (perm != NULL)
12623 {
12624 for (i = 0; i < 9; i++)
12625 {
12626 if (st.st_mode & (1 << (8 - i)))
12627 perm[i] = flags[i % 3];
12628 }
12629 }
12630 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012631 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012632}
12633
12634/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012635 * "getfsize({fname})" function
12636 */
12637 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012638f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012639{
12640 char_u *fname;
12641 struct stat st;
12642
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012643 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012644
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012645 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012646
12647 if (mch_stat((char *)fname, &st) >= 0)
12648 {
12649 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012650 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012651 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012652 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012653 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012654
12655 /* non-perfect check for overflow */
12656 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12657 rettv->vval.v_number = -2;
12658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012659 }
12660 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012661 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012662}
12663
12664/*
12665 * "getftime({fname})" function
12666 */
12667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012668f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012669{
12670 char_u *fname;
12671 struct stat st;
12672
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012673 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012674
12675 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012676 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012677 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012678 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012679}
12680
12681/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012682 * "getftype({fname})" function
12683 */
12684 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012685f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012686{
12687 char_u *fname;
12688 struct stat st;
12689 char_u *type = NULL;
12690 char *t;
12691
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012692 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012693
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012694 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012695 if (mch_lstat((char *)fname, &st) >= 0)
12696 {
12697#ifdef S_ISREG
12698 if (S_ISREG(st.st_mode))
12699 t = "file";
12700 else if (S_ISDIR(st.st_mode))
12701 t = "dir";
12702# ifdef S_ISLNK
12703 else if (S_ISLNK(st.st_mode))
12704 t = "link";
12705# endif
12706# ifdef S_ISBLK
12707 else if (S_ISBLK(st.st_mode))
12708 t = "bdev";
12709# endif
12710# ifdef S_ISCHR
12711 else if (S_ISCHR(st.st_mode))
12712 t = "cdev";
12713# endif
12714# ifdef S_ISFIFO
12715 else if (S_ISFIFO(st.st_mode))
12716 t = "fifo";
12717# endif
12718# ifdef S_ISSOCK
12719 else if (S_ISSOCK(st.st_mode))
12720 t = "fifo";
12721# endif
12722 else
12723 t = "other";
12724#else
12725# ifdef S_IFMT
12726 switch (st.st_mode & S_IFMT)
12727 {
12728 case S_IFREG: t = "file"; break;
12729 case S_IFDIR: t = "dir"; break;
12730# ifdef S_IFLNK
12731 case S_IFLNK: t = "link"; break;
12732# endif
12733# ifdef S_IFBLK
12734 case S_IFBLK: t = "bdev"; break;
12735# endif
12736# ifdef S_IFCHR
12737 case S_IFCHR: t = "cdev"; break;
12738# endif
12739# ifdef S_IFIFO
12740 case S_IFIFO: t = "fifo"; break;
12741# endif
12742# ifdef S_IFSOCK
12743 case S_IFSOCK: t = "socket"; break;
12744# endif
12745 default: t = "other";
12746 }
12747# else
12748 if (mch_isdir(fname))
12749 t = "dir";
12750 else
12751 t = "file";
12752# endif
12753#endif
12754 type = vim_strsave((char_u *)t);
12755 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012756 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012757}
12758
12759/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012760 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012761 */
12762 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012763f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012764{
12765 linenr_T lnum;
12766 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012767 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012768
12769 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012770 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012771 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012772 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012773 retlist = FALSE;
12774 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012775 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012776 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012777 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012778 retlist = TRUE;
12779 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012780
Bram Moolenaar342337a2005-07-21 21:11:17 +000012781 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012782}
12783
12784/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012785 * "getmatches()" function
12786 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012787 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012788f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012789{
12790#ifdef FEAT_SEARCH_EXTRA
12791 dict_T *dict;
12792 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012793 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012794
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012795 if (rettv_list_alloc(rettv) == OK)
12796 {
12797 while (cur != NULL)
12798 {
12799 dict = dict_alloc();
12800 if (dict == NULL)
12801 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012802 if (cur->match.regprog == NULL)
12803 {
12804 /* match added with matchaddpos() */
12805 for (i = 0; i < MAXPOSMATCH; ++i)
12806 {
12807 llpos_T *llpos;
12808 char buf[6];
12809 list_T *l;
12810
12811 llpos = &cur->pos.pos[i];
12812 if (llpos->lnum == 0)
12813 break;
12814 l = list_alloc();
12815 if (l == NULL)
12816 break;
12817 list_append_number(l, (varnumber_T)llpos->lnum);
12818 if (llpos->col > 0)
12819 {
12820 list_append_number(l, (varnumber_T)llpos->col);
12821 list_append_number(l, (varnumber_T)llpos->len);
12822 }
12823 sprintf(buf, "pos%d", i + 1);
12824 dict_add_list(dict, buf, l);
12825 }
12826 }
12827 else
12828 {
12829 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12830 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012831 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012832 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12833 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar42356152016-03-31 22:27:40 +020012834# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +020012835 if (cur->conceal_char)
12836 {
12837 char_u buf[MB_MAXBYTES + 1];
12838
12839 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12840 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12841 }
12842# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012843 list_append_dict(rettv->vval.v_list, dict);
12844 cur = cur->next;
12845 }
12846 }
12847#endif
12848}
12849
12850/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012851 * "getpid()" function
12852 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012853 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012854f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012855{
12856 rettv->vval.v_number = mch_get_pid();
12857}
12858
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012859static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012860
12861/*
12862 * "getcurpos()" function
12863 */
12864 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012865f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012866{
12867 getpos_both(argvars, rettv, TRUE);
12868}
12869
Bram Moolenaar18081e32008-02-20 19:11:07 +000012870/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012871 * "getpos(string)" function
12872 */
12873 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012874f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012875{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012876 getpos_both(argvars, rettv, FALSE);
12877}
12878
12879 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012880getpos_both(
12881 typval_T *argvars,
12882 typval_T *rettv,
12883 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012884{
Bram Moolenaara5525202006-03-02 22:52:09 +000012885 pos_T *fp;
12886 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012887 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012888
12889 if (rettv_list_alloc(rettv) == OK)
12890 {
12891 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012892 if (getcurpos)
12893 fp = &curwin->w_cursor;
12894 else
12895 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012896 if (fnum != -1)
12897 list_append_number(l, (varnumber_T)fnum);
12898 else
12899 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012900 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12901 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012902 list_append_number(l, (fp != NULL)
12903 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012904 : (varnumber_T)0);
12905 list_append_number(l,
12906#ifdef FEAT_VIRTUALEDIT
12907 (fp != NULL) ? (varnumber_T)fp->coladd :
12908#endif
12909 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012910 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012911 {
12912 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010012913 list_append_number(l, curwin->w_curswant == MAXCOL ?
12914 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012915 }
Bram Moolenaara5525202006-03-02 22:52:09 +000012916 }
12917 else
12918 rettv->vval.v_number = FALSE;
12919}
12920
12921/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012922 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012923 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012925f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012926{
12927#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012928 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012929#endif
12930
Bram Moolenaar2641f772005-03-25 21:58:17 +000012931#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012932 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012933 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012934 wp = NULL;
12935 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12936 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012937 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012938 if (wp == NULL)
12939 return;
12940 }
12941
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012942 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012943 }
12944#endif
12945}
12946
12947/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012948 * "getreg()" function
12949 */
12950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012951f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012952{
12953 char_u *strregname;
12954 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012955 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012956 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012957 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012958
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012959 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012960 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012961 strregname = get_tv_string_chk(&argvars[0]);
12962 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012963 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012964 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012965 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012966 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12967 return_list = get_tv_number_chk(&argvars[2], &error);
12968 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012970 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012971 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012972
12973 if (error)
12974 return;
12975
Bram Moolenaar071d4272004-06-13 20:20:40 +000012976 regname = (strregname == NULL ? '"' : *strregname);
12977 if (regname == 0)
12978 regname = '"';
12979
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012980 if (return_list)
12981 {
12982 rettv->v_type = VAR_LIST;
12983 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12984 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012985 if (rettv->vval.v_list != NULL)
12986 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012987 }
12988 else
12989 {
12990 rettv->v_type = VAR_STRING;
12991 rettv->vval.v_string = get_reg_contents(regname,
12992 arg2 ? GREG_EXPR_SRC : 0);
12993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012994}
12995
12996/*
12997 * "getregtype()" function
12998 */
12999 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013000f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013001{
13002 char_u *strregname;
13003 int regname;
13004 char_u buf[NUMBUFLEN + 2];
13005 long reglen = 0;
13006
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013007 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013008 {
13009 strregname = get_tv_string_chk(&argvars[0]);
13010 if (strregname == NULL) /* type error; errmsg already given */
13011 {
13012 rettv->v_type = VAR_STRING;
13013 rettv->vval.v_string = NULL;
13014 return;
13015 }
13016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013017 else
13018 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013019 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013020
13021 regname = (strregname == NULL ? '"' : *strregname);
13022 if (regname == 0)
13023 regname = '"';
13024
13025 buf[0] = NUL;
13026 buf[1] = NUL;
13027 switch (get_reg_type(regname, &reglen))
13028 {
13029 case MLINE: buf[0] = 'V'; break;
13030 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013031 case MBLOCK:
13032 buf[0] = Ctrl_V;
13033 sprintf((char *)buf + 1, "%ld", reglen + 1);
13034 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013035 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013036 rettv->v_type = VAR_STRING;
13037 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013038}
13039
13040/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013041 * "gettabvar()" function
13042 */
13043 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013044f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013045{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013046 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013047 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013048 dictitem_T *v;
13049 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013050 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013051
13052 rettv->v_type = VAR_STRING;
13053 rettv->vval.v_string = NULL;
13054
13055 varname = get_tv_string_chk(&argvars[1]);
13056 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13057 if (tp != NULL && varname != NULL)
13058 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013059 /* Set tp to be our tabpage, temporarily. Also set the window to the
13060 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013061 if (switch_win(&oldcurwin, &oldtabpage,
13062 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013063 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013064 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013065 /* look up the variable */
13066 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13067 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13068 if (v != NULL)
13069 {
13070 copy_tv(&v->di_tv, rettv);
13071 done = TRUE;
13072 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013073 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013074
13075 /* restore previous notion of curwin */
13076 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013077 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013078
13079 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013080 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013081}
13082
13083/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013084 * "gettabwinvar()" function
13085 */
13086 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013087f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013088{
13089 getwinvar(argvars, rettv, 1);
13090}
13091
13092/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013093 * "getwinposx()" function
13094 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013095 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013096f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013097{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013098 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013099#ifdef FEAT_GUI
13100 if (gui.in_use)
13101 {
13102 int x, y;
13103
13104 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013105 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013106 }
13107#endif
13108}
13109
13110/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010013111 * "win_findbuf()" function
13112 */
13113 static void
13114f_win_findbuf(typval_T *argvars, typval_T *rettv)
13115{
13116 if (rettv_list_alloc(rettv) != FAIL)
13117 win_findbuf(argvars, rettv->vval.v_list);
13118}
13119
13120/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010013121 * "win_getid()" function
13122 */
13123 static void
13124f_win_getid(typval_T *argvars, typval_T *rettv)
13125{
13126 rettv->vval.v_number = win_getid(argvars);
13127}
13128
13129/*
13130 * "win_gotoid()" function
13131 */
13132 static void
13133f_win_gotoid(typval_T *argvars, typval_T *rettv)
13134{
13135 rettv->vval.v_number = win_gotoid(argvars);
13136}
13137
13138/*
13139 * "win_id2tabwin()" function
13140 */
13141 static void
13142f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13143{
13144 if (rettv_list_alloc(rettv) != FAIL)
13145 win_id2tabwin(argvars, rettv->vval.v_list);
13146}
13147
13148/*
13149 * "win_id2win()" function
13150 */
13151 static void
13152f_win_id2win(typval_T *argvars, typval_T *rettv)
13153{
13154 rettv->vval.v_number = win_id2win(argvars);
13155}
13156
13157/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013158 * "getwinposy()" function
13159 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013160 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013161f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013162{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013163 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013164#ifdef FEAT_GUI
13165 if (gui.in_use)
13166 {
13167 int x, y;
13168
13169 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013170 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013171 }
13172#endif
13173}
13174
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013175/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013176 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013177 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013178 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013179find_win_by_nr(
13180 typval_T *vp,
13181 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013182{
13183#ifdef FEAT_WINDOWS
13184 win_T *wp;
13185#endif
13186 int nr;
13187
13188 nr = get_tv_number_chk(vp, NULL);
13189
13190#ifdef FEAT_WINDOWS
13191 if (nr < 0)
13192 return NULL;
13193 if (nr == 0)
13194 return curwin;
13195
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013196 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13197 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013198 if (--nr <= 0)
13199 break;
13200 return wp;
13201#else
13202 if (nr == 0 || nr == 1)
13203 return curwin;
13204 return NULL;
13205#endif
13206}
13207
Bram Moolenaar071d4272004-06-13 20:20:40 +000013208/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013209 * Find window specified by "wvp" in tabpage "tvp".
13210 */
13211 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013212find_tabwin(
13213 typval_T *wvp, /* VAR_UNKNOWN for current window */
13214 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013215{
13216 win_T *wp = NULL;
13217 tabpage_T *tp = NULL;
13218 long n;
13219
13220 if (wvp->v_type != VAR_UNKNOWN)
13221 {
13222 if (tvp->v_type != VAR_UNKNOWN)
13223 {
13224 n = get_tv_number(tvp);
13225 if (n >= 0)
13226 tp = find_tabpage(n);
13227 }
13228 else
13229 tp = curtab;
13230
13231 if (tp != NULL)
13232 wp = find_win_by_nr(wvp, tp);
13233 }
13234 else
13235 wp = curwin;
13236
13237 return wp;
13238}
13239
13240/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013241 * "getwinvar()" function
13242 */
13243 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013244f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013245{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013246 getwinvar(argvars, rettv, 0);
13247}
13248
13249/*
13250 * getwinvar() and gettabwinvar()
13251 */
13252 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013253getwinvar(
13254 typval_T *argvars,
13255 typval_T *rettv,
13256 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013257{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013258 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013259 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013260 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013261 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013262 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013263#ifdef FEAT_WINDOWS
13264 win_T *oldcurwin;
13265 tabpage_T *oldtabpage;
13266 int need_switch_win;
13267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013268
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013269#ifdef FEAT_WINDOWS
13270 if (off == 1)
13271 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13272 else
13273 tp = curtab;
13274#endif
13275 win = find_win_by_nr(&argvars[off], tp);
13276 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013277 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013278
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013279 rettv->v_type = VAR_STRING;
13280 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013281
13282 if (win != NULL && varname != NULL)
13283 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013284#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013285 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013286 * otherwise the window is not valid. Only do this when needed,
13287 * autocommands get blocked. */
13288 need_switch_win = !(tp == curtab && win == curwin);
13289 if (!need_switch_win
13290 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13291#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013292 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013293 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013294 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013295 if (get_option_tv(&varname, rettv, 1) == OK)
13296 done = TRUE;
13297 }
13298 else
13299 {
13300 /* Look up the variable. */
13301 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13302 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13303 varname, FALSE);
13304 if (v != NULL)
13305 {
13306 copy_tv(&v->di_tv, rettv);
13307 done = TRUE;
13308 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013310 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013311
Bram Moolenaarba117c22015-09-29 16:53:22 +020013312#ifdef FEAT_WINDOWS
13313 if (need_switch_win)
13314 /* restore previous notion of curwin */
13315 restore_win(oldcurwin, oldtabpage, TRUE);
13316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013317 }
13318
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013319 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13320 /* use the default return value */
13321 copy_tv(&argvars[off + 2], rettv);
13322
Bram Moolenaar071d4272004-06-13 20:20:40 +000013323 --emsg_off;
13324}
13325
13326/*
13327 * "glob()" function
13328 */
13329 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013330f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013331{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013332 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013333 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013334 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013335
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013336 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013337 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013338 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013339 if (argvars[1].v_type != VAR_UNKNOWN)
13340 {
13341 if (get_tv_number_chk(&argvars[1], &error))
13342 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013343 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013344 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013345 if (get_tv_number_chk(&argvars[2], &error))
13346 {
13347 rettv->v_type = VAR_LIST;
13348 rettv->vval.v_list = NULL;
13349 }
13350 if (argvars[3].v_type != VAR_UNKNOWN
13351 && get_tv_number_chk(&argvars[3], &error))
13352 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013353 }
13354 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013355 if (!error)
13356 {
13357 ExpandInit(&xpc);
13358 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013359 if (p_wic)
13360 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013361 if (rettv->v_type == VAR_STRING)
13362 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013363 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013364 else if (rettv_list_alloc(rettv) != FAIL)
13365 {
13366 int i;
13367
13368 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13369 NULL, options, WILD_ALL_KEEP);
13370 for (i = 0; i < xpc.xp_numfiles; i++)
13371 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13372
13373 ExpandCleanup(&xpc);
13374 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013375 }
13376 else
13377 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013378}
13379
13380/*
13381 * "globpath()" function
13382 */
13383 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013384f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013385{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013386 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013387 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013388 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013389 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013390 garray_T ga;
13391 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013392
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013393 /* When the optional second argument is non-zero, don't remove matches
13394 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013395 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013396 if (argvars[2].v_type != VAR_UNKNOWN)
13397 {
13398 if (get_tv_number_chk(&argvars[2], &error))
13399 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013400 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013401 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013402 if (get_tv_number_chk(&argvars[3], &error))
13403 {
13404 rettv->v_type = VAR_LIST;
13405 rettv->vval.v_list = NULL;
13406 }
13407 if (argvars[4].v_type != VAR_UNKNOWN
13408 && get_tv_number_chk(&argvars[4], &error))
13409 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013410 }
13411 }
13412 if (file != NULL && !error)
13413 {
13414 ga_init2(&ga, (int)sizeof(char_u *), 10);
13415 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13416 if (rettv->v_type == VAR_STRING)
13417 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13418 else if (rettv_list_alloc(rettv) != FAIL)
13419 for (i = 0; i < ga.ga_len; ++i)
13420 list_append_string(rettv->vval.v_list,
13421 ((char_u **)(ga.ga_data))[i], -1);
13422 ga_clear_strings(&ga);
13423 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013424 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013425 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013426}
13427
13428/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013429 * "glob2regpat()" function
13430 */
13431 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013432f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013433{
13434 char_u *pat = get_tv_string_chk(&argvars[0]);
13435
13436 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013437 rettv->vval.v_string = (pat == NULL)
13438 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013439}
13440
13441/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013442 * "has()" function
13443 */
13444 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013445f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013446{
13447 int i;
13448 char_u *name;
13449 int n = FALSE;
13450 static char *(has_list[]) =
13451 {
13452#ifdef AMIGA
13453 "amiga",
13454# ifdef FEAT_ARP
13455 "arp",
13456# endif
13457#endif
13458#ifdef __BEOS__
13459 "beos",
13460#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013461#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013462 "mac",
13463#endif
13464#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013465 "macunix", /* built with 'darwin' enabled */
13466#endif
13467#if defined(__APPLE__) && __APPLE__ == 1
13468 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013470#ifdef __QNX__
13471 "qnx",
13472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013473#ifdef UNIX
13474 "unix",
13475#endif
13476#ifdef VMS
13477 "vms",
13478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013479#ifdef WIN32
13480 "win32",
13481#endif
13482#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13483 "win32unix",
13484#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013485#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013486 "win64",
13487#endif
13488#ifdef EBCDIC
13489 "ebcdic",
13490#endif
13491#ifndef CASE_INSENSITIVE_FILENAME
13492 "fname_case",
13493#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013494#ifdef HAVE_ACL
13495 "acl",
13496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013497#ifdef FEAT_ARABIC
13498 "arabic",
13499#endif
13500#ifdef FEAT_AUTOCMD
13501 "autocmd",
13502#endif
13503#ifdef FEAT_BEVAL
13504 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013505# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13506 "balloon_multiline",
13507# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013508#endif
13509#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13510 "builtin_terms",
13511# ifdef ALL_BUILTIN_TCAPS
13512 "all_builtin_terms",
13513# endif
13514#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013515#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13516 || defined(FEAT_GUI_W32) \
13517 || defined(FEAT_GUI_MOTIF))
13518 "browsefilter",
13519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013520#ifdef FEAT_BYTEOFF
13521 "byte_offset",
13522#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013523#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013524 "channel",
13525#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013526#ifdef FEAT_CINDENT
13527 "cindent",
13528#endif
13529#ifdef FEAT_CLIENTSERVER
13530 "clientserver",
13531#endif
13532#ifdef FEAT_CLIPBOARD
13533 "clipboard",
13534#endif
13535#ifdef FEAT_CMDL_COMPL
13536 "cmdline_compl",
13537#endif
13538#ifdef FEAT_CMDHIST
13539 "cmdline_hist",
13540#endif
13541#ifdef FEAT_COMMENTS
13542 "comments",
13543#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013544#ifdef FEAT_CONCEAL
13545 "conceal",
13546#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013547#ifdef FEAT_CRYPT
13548 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013549 "crypt-blowfish",
13550 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013551#endif
13552#ifdef FEAT_CSCOPE
13553 "cscope",
13554#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013555#ifdef FEAT_CURSORBIND
13556 "cursorbind",
13557#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013558#ifdef CURSOR_SHAPE
13559 "cursorshape",
13560#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013561#ifdef DEBUG
13562 "debug",
13563#endif
13564#ifdef FEAT_CON_DIALOG
13565 "dialog_con",
13566#endif
13567#ifdef FEAT_GUI_DIALOG
13568 "dialog_gui",
13569#endif
13570#ifdef FEAT_DIFF
13571 "diff",
13572#endif
13573#ifdef FEAT_DIGRAPHS
13574 "digraphs",
13575#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013576#ifdef FEAT_DIRECTX
13577 "directx",
13578#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013579#ifdef FEAT_DND
13580 "dnd",
13581#endif
13582#ifdef FEAT_EMACS_TAGS
13583 "emacs_tags",
13584#endif
13585 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013586 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013587#ifdef FEAT_SEARCH_EXTRA
13588 "extra_search",
13589#endif
13590#ifdef FEAT_FKMAP
13591 "farsi",
13592#endif
13593#ifdef FEAT_SEARCHPATH
13594 "file_in_path",
13595#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013596#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013597 "filterpipe",
13598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013599#ifdef FEAT_FIND_ID
13600 "find_in_path",
13601#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013602#ifdef FEAT_FLOAT
13603 "float",
13604#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013605#ifdef FEAT_FOLDING
13606 "folding",
13607#endif
13608#ifdef FEAT_FOOTER
13609 "footer",
13610#endif
13611#if !defined(USE_SYSTEM) && defined(UNIX)
13612 "fork",
13613#endif
13614#ifdef FEAT_GETTEXT
13615 "gettext",
13616#endif
13617#ifdef FEAT_GUI
13618 "gui",
13619#endif
13620#ifdef FEAT_GUI_ATHENA
13621# ifdef FEAT_GUI_NEXTAW
13622 "gui_neXtaw",
13623# else
13624 "gui_athena",
13625# endif
13626#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013627#ifdef FEAT_GUI_GTK
13628 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013629# ifdef USE_GTK3
13630 "gui_gtk3",
13631# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013632 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013633# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013634#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013635#ifdef FEAT_GUI_GNOME
13636 "gui_gnome",
13637#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013638#ifdef FEAT_GUI_MAC
13639 "gui_mac",
13640#endif
13641#ifdef FEAT_GUI_MOTIF
13642 "gui_motif",
13643#endif
13644#ifdef FEAT_GUI_PHOTON
13645 "gui_photon",
13646#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013647#ifdef FEAT_GUI_W32
13648 "gui_win32",
13649#endif
13650#ifdef FEAT_HANGULIN
13651 "hangul_input",
13652#endif
13653#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13654 "iconv",
13655#endif
13656#ifdef FEAT_INS_EXPAND
13657 "insert_expand",
13658#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013659#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010013660 "job",
13661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013662#ifdef FEAT_JUMPLIST
13663 "jumplist",
13664#endif
13665#ifdef FEAT_KEYMAP
13666 "keymap",
13667#endif
13668#ifdef FEAT_LANGMAP
13669 "langmap",
13670#endif
13671#ifdef FEAT_LIBCALL
13672 "libcall",
13673#endif
13674#ifdef FEAT_LINEBREAK
13675 "linebreak",
13676#endif
13677#ifdef FEAT_LISP
13678 "lispindent",
13679#endif
13680#ifdef FEAT_LISTCMDS
13681 "listcmds",
13682#endif
13683#ifdef FEAT_LOCALMAP
13684 "localmap",
13685#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013686#ifdef FEAT_LUA
13687# ifndef DYNAMIC_LUA
13688 "lua",
13689# endif
13690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013691#ifdef FEAT_MENU
13692 "menu",
13693#endif
13694#ifdef FEAT_SESSION
13695 "mksession",
13696#endif
13697#ifdef FEAT_MODIFY_FNAME
13698 "modify_fname",
13699#endif
13700#ifdef FEAT_MOUSE
13701 "mouse",
13702#endif
13703#ifdef FEAT_MOUSESHAPE
13704 "mouseshape",
13705#endif
13706#if defined(UNIX) || defined(VMS)
13707# ifdef FEAT_MOUSE_DEC
13708 "mouse_dec",
13709# endif
13710# ifdef FEAT_MOUSE_GPM
13711 "mouse_gpm",
13712# endif
13713# ifdef FEAT_MOUSE_JSB
13714 "mouse_jsbterm",
13715# endif
13716# ifdef FEAT_MOUSE_NET
13717 "mouse_netterm",
13718# endif
13719# ifdef FEAT_MOUSE_PTERM
13720 "mouse_pterm",
13721# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013722# ifdef FEAT_MOUSE_SGR
13723 "mouse_sgr",
13724# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013725# ifdef FEAT_SYSMOUSE
13726 "mouse_sysmouse",
13727# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013728# ifdef FEAT_MOUSE_URXVT
13729 "mouse_urxvt",
13730# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013731# ifdef FEAT_MOUSE_XTERM
13732 "mouse_xterm",
13733# endif
13734#endif
13735#ifdef FEAT_MBYTE
13736 "multi_byte",
13737#endif
13738#ifdef FEAT_MBYTE_IME
13739 "multi_byte_ime",
13740#endif
13741#ifdef FEAT_MULTI_LANG
13742 "multi_lang",
13743#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013744#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013745#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013746 "mzscheme",
13747#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013749#ifdef FEAT_OLE
13750 "ole",
13751#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013752 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013753#ifdef FEAT_PATH_EXTRA
13754 "path_extra",
13755#endif
13756#ifdef FEAT_PERL
13757#ifndef DYNAMIC_PERL
13758 "perl",
13759#endif
13760#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013761#ifdef FEAT_PERSISTENT_UNDO
13762 "persistent_undo",
13763#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013764#ifdef FEAT_PYTHON
13765#ifndef DYNAMIC_PYTHON
13766 "python",
13767#endif
13768#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013769#ifdef FEAT_PYTHON3
13770#ifndef DYNAMIC_PYTHON3
13771 "python3",
13772#endif
13773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013774#ifdef FEAT_POSTSCRIPT
13775 "postscript",
13776#endif
13777#ifdef FEAT_PRINTER
13778 "printer",
13779#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013780#ifdef FEAT_PROFILE
13781 "profile",
13782#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013783#ifdef FEAT_RELTIME
13784 "reltime",
13785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013786#ifdef FEAT_QUICKFIX
13787 "quickfix",
13788#endif
13789#ifdef FEAT_RIGHTLEFT
13790 "rightleft",
13791#endif
13792#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13793 "ruby",
13794#endif
13795#ifdef FEAT_SCROLLBIND
13796 "scrollbind",
13797#endif
13798#ifdef FEAT_CMDL_INFO
13799 "showcmd",
13800 "cmdline_info",
13801#endif
13802#ifdef FEAT_SIGNS
13803 "signs",
13804#endif
13805#ifdef FEAT_SMARTINDENT
13806 "smartindent",
13807#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013808#ifdef STARTUPTIME
13809 "startuptime",
13810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013811#ifdef FEAT_STL_OPT
13812 "statusline",
13813#endif
13814#ifdef FEAT_SUN_WORKSHOP
13815 "sun_workshop",
13816#endif
13817#ifdef FEAT_NETBEANS_INTG
13818 "netbeans_intg",
13819#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013820#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013821 "spell",
13822#endif
13823#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013824 "syntax",
13825#endif
13826#if defined(USE_SYSTEM) || !defined(UNIX)
13827 "system",
13828#endif
13829#ifdef FEAT_TAG_BINS
13830 "tag_binary",
13831#endif
13832#ifdef FEAT_TAG_OLDSTATIC
13833 "tag_old_static",
13834#endif
13835#ifdef FEAT_TAG_ANYWHITE
13836 "tag_any_white",
13837#endif
13838#ifdef FEAT_TCL
13839# ifndef DYNAMIC_TCL
13840 "tcl",
13841# endif
13842#endif
13843#ifdef TERMINFO
13844 "terminfo",
13845#endif
13846#ifdef FEAT_TERMRESPONSE
13847 "termresponse",
13848#endif
13849#ifdef FEAT_TEXTOBJ
13850 "textobjects",
13851#endif
13852#ifdef HAVE_TGETENT
13853 "tgetent",
13854#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010013855#ifdef FEAT_TIMERS
13856 "timers",
13857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013858#ifdef FEAT_TITLE
13859 "title",
13860#endif
13861#ifdef FEAT_TOOLBAR
13862 "toolbar",
13863#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013864#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13865 "unnamedplus",
13866#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013867#ifdef FEAT_USR_CMDS
13868 "user-commands", /* was accidentally included in 5.4 */
13869 "user_commands",
13870#endif
13871#ifdef FEAT_VIMINFO
13872 "viminfo",
13873#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010013874#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013875 "vertsplit",
13876#endif
13877#ifdef FEAT_VIRTUALEDIT
13878 "virtualedit",
13879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013880 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013881#ifdef FEAT_VISUALEXTRA
13882 "visualextra",
13883#endif
13884#ifdef FEAT_VREPLACE
13885 "vreplace",
13886#endif
13887#ifdef FEAT_WILDIGN
13888 "wildignore",
13889#endif
13890#ifdef FEAT_WILDMENU
13891 "wildmenu",
13892#endif
13893#ifdef FEAT_WINDOWS
13894 "windows",
13895#endif
13896#ifdef FEAT_WAK
13897 "winaltkeys",
13898#endif
13899#ifdef FEAT_WRITEBACKUP
13900 "writebackup",
13901#endif
13902#ifdef FEAT_XIM
13903 "xim",
13904#endif
13905#ifdef FEAT_XFONTSET
13906 "xfontset",
13907#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013908#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013909 "xpm",
13910 "xpm_w32", /* for backward compatibility */
13911#else
13912# if defined(HAVE_XPM)
13913 "xpm",
13914# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013916#ifdef USE_XSMP
13917 "xsmp",
13918#endif
13919#ifdef USE_XSMP_INTERACT
13920 "xsmp_interact",
13921#endif
13922#ifdef FEAT_XCLIPBOARD
13923 "xterm_clipboard",
13924#endif
13925#ifdef FEAT_XTERM_SAVE
13926 "xterm_save",
13927#endif
13928#if defined(UNIX) && defined(FEAT_X11)
13929 "X11",
13930#endif
13931 NULL
13932 };
13933
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013934 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013935 for (i = 0; has_list[i] != NULL; ++i)
13936 if (STRICMP(name, has_list[i]) == 0)
13937 {
13938 n = TRUE;
13939 break;
13940 }
13941
13942 if (n == FALSE)
13943 {
13944 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013945 {
13946 if (name[5] == '-'
Bram Moolenaar819821c2016-03-26 21:24:14 +010013947 && STRLEN(name) >= 11
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013948 && vim_isdigit(name[6])
13949 && vim_isdigit(name[8])
13950 && vim_isdigit(name[10]))
13951 {
13952 int major = atoi((char *)name + 6);
13953 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013954
13955 /* Expect "patch-9.9.01234". */
13956 n = (major < VIM_VERSION_MAJOR
13957 || (major == VIM_VERSION_MAJOR
13958 && (minor < VIM_VERSION_MINOR
13959 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013960 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013961 }
13962 else
13963 n = has_patch(atoi((char *)name + 5));
13964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013965 else if (STRICMP(name, "vim_starting") == 0)
13966 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013967#ifdef FEAT_MBYTE
13968 else if (STRICMP(name, "multi_byte_encoding") == 0)
13969 n = has_mbyte;
13970#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013971#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13972 else if (STRICMP(name, "balloon_multiline") == 0)
13973 n = multiline_balloon_available();
13974#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013975#ifdef DYNAMIC_TCL
13976 else if (STRICMP(name, "tcl") == 0)
13977 n = tcl_enabled(FALSE);
13978#endif
13979#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13980 else if (STRICMP(name, "iconv") == 0)
13981 n = iconv_enabled(FALSE);
13982#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013983#ifdef DYNAMIC_LUA
13984 else if (STRICMP(name, "lua") == 0)
13985 n = lua_enabled(FALSE);
13986#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013987#ifdef DYNAMIC_MZSCHEME
13988 else if (STRICMP(name, "mzscheme") == 0)
13989 n = mzscheme_enabled(FALSE);
13990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013991#ifdef DYNAMIC_RUBY
13992 else if (STRICMP(name, "ruby") == 0)
13993 n = ruby_enabled(FALSE);
13994#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013995#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013996#ifdef DYNAMIC_PYTHON
13997 else if (STRICMP(name, "python") == 0)
13998 n = python_enabled(FALSE);
13999#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014000#endif
14001#ifdef FEAT_PYTHON3
14002#ifdef DYNAMIC_PYTHON3
14003 else if (STRICMP(name, "python3") == 0)
14004 n = python3_enabled(FALSE);
14005#endif
14006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014007#ifdef DYNAMIC_PERL
14008 else if (STRICMP(name, "perl") == 0)
14009 n = perl_enabled(FALSE);
14010#endif
14011#ifdef FEAT_GUI
14012 else if (STRICMP(name, "gui_running") == 0)
14013 n = (gui.in_use || gui.starting);
14014# ifdef FEAT_GUI_W32
14015 else if (STRICMP(name, "gui_win32s") == 0)
14016 n = gui_is_win32s();
14017# endif
14018# ifdef FEAT_BROWSE
14019 else if (STRICMP(name, "browse") == 0)
14020 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14021# endif
14022#endif
14023#ifdef FEAT_SYN_HL
14024 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014025 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014026#endif
14027#if defined(WIN3264)
14028 else if (STRICMP(name, "win95") == 0)
14029 n = mch_windows95();
14030#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014031#ifdef FEAT_NETBEANS_INTG
14032 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014033 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014034#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014035 }
14036
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014037 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014038}
14039
14040/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014041 * "has_key()" function
14042 */
14043 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014044f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014045{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014046 if (argvars[0].v_type != VAR_DICT)
14047 {
14048 EMSG(_(e_dictreq));
14049 return;
14050 }
14051 if (argvars[0].vval.v_dict == NULL)
14052 return;
14053
14054 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014055 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014056}
14057
14058/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014059 * "haslocaldir()" function
14060 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014061 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014062f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014063{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014064 win_T *wp = NULL;
14065
14066 wp = find_tabwin(&argvars[0], &argvars[1]);
14067 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014068}
14069
14070/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014071 * "hasmapto()" function
14072 */
14073 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014074f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014075{
14076 char_u *name;
14077 char_u *mode;
14078 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014079 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014080
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014081 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014082 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014083 mode = (char_u *)"nvo";
14084 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014085 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014086 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014087 if (argvars[2].v_type != VAR_UNKNOWN)
14088 abbr = get_tv_number(&argvars[2]);
14089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014090
Bram Moolenaar2c932302006-03-18 21:42:09 +000014091 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014092 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014093 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014094 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014095}
14096
14097/*
14098 * "histadd()" function
14099 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014100 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014101f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014102{
14103#ifdef FEAT_CMDHIST
14104 int histype;
14105 char_u *str;
14106 char_u buf[NUMBUFLEN];
14107#endif
14108
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014109 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014110 if (check_restricted() || check_secure())
14111 return;
14112#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014113 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14114 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014115 if (histype >= 0)
14116 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014117 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014118 if (*str != NUL)
14119 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014120 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014121 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014122 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014123 return;
14124 }
14125 }
14126#endif
14127}
14128
14129/*
14130 * "histdel()" function
14131 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014132 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014133f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014134{
14135#ifdef FEAT_CMDHIST
14136 int n;
14137 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014138 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014139
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014140 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14141 if (str == NULL)
14142 n = 0;
14143 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014144 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014145 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014146 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014147 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014148 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014149 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014150 else
14151 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014152 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014153 get_tv_string_buf(&argvars[1], buf));
14154 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014155#endif
14156}
14157
14158/*
14159 * "histget()" function
14160 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014161 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014162f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014163{
14164#ifdef FEAT_CMDHIST
14165 int type;
14166 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014167 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014168
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014169 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14170 if (str == NULL)
14171 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014172 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014173 {
14174 type = get_histtype(str);
14175 if (argvars[1].v_type == VAR_UNKNOWN)
14176 idx = get_history_idx(type);
14177 else
14178 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14179 /* -1 on type error */
14180 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014182#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014183 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014184#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014185 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014186}
14187
14188/*
14189 * "histnr()" function
14190 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014192f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014193{
14194 int i;
14195
14196#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014197 char_u *history = get_tv_string_chk(&argvars[0]);
14198
14199 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014200 if (i >= HIST_CMD && i < HIST_COUNT)
14201 i = get_history_idx(i);
14202 else
14203#endif
14204 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014205 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014206}
14207
14208/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014209 * "highlightID(name)" function
14210 */
14211 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014212f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014213{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014214 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014215}
14216
14217/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014218 * "highlight_exists()" function
14219 */
14220 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014221f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014222{
14223 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14224}
14225
14226/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014227 * "hostname()" function
14228 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014229 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014230f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014231{
14232 char_u hostname[256];
14233
14234 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014235 rettv->v_type = VAR_STRING;
14236 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014237}
14238
14239/*
14240 * iconv() function
14241 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014243f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014244{
14245#ifdef FEAT_MBYTE
14246 char_u buf1[NUMBUFLEN];
14247 char_u buf2[NUMBUFLEN];
14248 char_u *from, *to, *str;
14249 vimconv_T vimconv;
14250#endif
14251
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014252 rettv->v_type = VAR_STRING;
14253 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014254
14255#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014256 str = get_tv_string(&argvars[0]);
14257 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14258 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014259 vimconv.vc_type = CONV_NONE;
14260 convert_setup(&vimconv, from, to);
14261
14262 /* If the encodings are equal, no conversion needed. */
14263 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014264 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014265 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014266 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014267
14268 convert_setup(&vimconv, NULL, NULL);
14269 vim_free(from);
14270 vim_free(to);
14271#endif
14272}
14273
14274/*
14275 * "indent()" function
14276 */
14277 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014278f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014279{
14280 linenr_T lnum;
14281
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014282 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014283 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014284 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014285 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014286 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014287}
14288
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014289/*
14290 * "index()" function
14291 */
14292 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014293f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014294{
Bram Moolenaar33570922005-01-25 22:26:29 +000014295 list_T *l;
14296 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014297 long idx = 0;
14298 int ic = FALSE;
14299
14300 rettv->vval.v_number = -1;
14301 if (argvars[0].v_type != VAR_LIST)
14302 {
14303 EMSG(_(e_listreq));
14304 return;
14305 }
14306 l = argvars[0].vval.v_list;
14307 if (l != NULL)
14308 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014309 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014310 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014311 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014312 int error = FALSE;
14313
Bram Moolenaar758711c2005-02-02 23:11:38 +000014314 /* Start at specified item. Use the cached index that list_find()
14315 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014316 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014317 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014318 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014319 ic = get_tv_number_chk(&argvars[3], &error);
14320 if (error)
14321 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014322 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014323
Bram Moolenaar758711c2005-02-02 23:11:38 +000014324 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014325 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014326 {
14327 rettv->vval.v_number = idx;
14328 break;
14329 }
14330 }
14331}
14332
Bram Moolenaar071d4272004-06-13 20:20:40 +000014333static int inputsecret_flag = 0;
14334
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014335static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014336
Bram Moolenaar071d4272004-06-13 20:20:40 +000014337/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014338 * This function is used by f_input() and f_inputdialog() functions. The third
14339 * argument to f_input() specifies the type of completion to use at the
14340 * prompt. The third argument to f_inputdialog() specifies the value to return
14341 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014342 */
14343 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014344get_user_input(
14345 typval_T *argvars,
14346 typval_T *rettv,
14347 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014348{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014349 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014350 char_u *p = NULL;
14351 int c;
14352 char_u buf[NUMBUFLEN];
14353 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014354 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014355 int xp_type = EXPAND_NOTHING;
14356 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014357
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014358 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014359 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014360
14361#ifdef NO_CONSOLE_INPUT
14362 /* While starting up, there is no place to enter text. */
14363 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014364 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014365#endif
14366
14367 cmd_silent = FALSE; /* Want to see the prompt. */
14368 if (prompt != NULL)
14369 {
14370 /* Only the part of the message after the last NL is considered as
14371 * prompt for the command line */
14372 p = vim_strrchr(prompt, '\n');
14373 if (p == NULL)
14374 p = prompt;
14375 else
14376 {
14377 ++p;
14378 c = *p;
14379 *p = NUL;
14380 msg_start();
14381 msg_clr_eos();
14382 msg_puts_attr(prompt, echo_attr);
14383 msg_didout = FALSE;
14384 msg_starthere();
14385 *p = c;
14386 }
14387 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014388
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014389 if (argvars[1].v_type != VAR_UNKNOWN)
14390 {
14391 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14392 if (defstr != NULL)
14393 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014394
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014395 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014396 {
14397 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014398 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014399 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014400
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014401 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014402 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014403
Bram Moolenaar4463f292005-09-25 22:20:24 +000014404 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14405 if (xp_name == NULL)
14406 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014407
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014408 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014409
Bram Moolenaar4463f292005-09-25 22:20:24 +000014410 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14411 &xp_arg) == FAIL)
14412 return;
14413 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014414 }
14415
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014416 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014417 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014418 int save_ex_normal_busy = ex_normal_busy;
14419 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014420 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014421 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14422 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014423 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014424 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014425 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014426 && argvars[1].v_type != VAR_UNKNOWN
14427 && argvars[2].v_type != VAR_UNKNOWN)
14428 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14429 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014430
14431 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014432
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014433 /* since the user typed this, no need to wait for return */
14434 need_wait_return = FALSE;
14435 msg_didout = FALSE;
14436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014437 cmd_silent = cmd_silent_save;
14438}
14439
14440/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014441 * "input()" function
14442 * Also handles inputsecret() when inputsecret is set.
14443 */
14444 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014445f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014446{
14447 get_user_input(argvars, rettv, FALSE);
14448}
14449
14450/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014451 * "inputdialog()" function
14452 */
14453 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014454f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014455{
14456#if defined(FEAT_GUI_TEXTDIALOG)
14457 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14458 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14459 {
14460 char_u *message;
14461 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014462 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014463
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014464 message = get_tv_string_chk(&argvars[0]);
14465 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014466 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014467 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014468 else
14469 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014470 if (message != NULL && defstr != NULL
14471 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014472 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014473 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014474 else
14475 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014476 if (message != NULL && defstr != NULL
14477 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014478 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014479 rettv->vval.v_string = vim_strsave(
14480 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014481 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014482 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014483 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014484 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014485 }
14486 else
14487#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014488 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014489}
14490
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014491/*
14492 * "inputlist()" function
14493 */
14494 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014495f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014496{
14497 listitem_T *li;
14498 int selected;
14499 int mouse_used;
14500
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014501#ifdef NO_CONSOLE_INPUT
14502 /* While starting up, there is no place to enter text. */
14503 if (no_console_input())
14504 return;
14505#endif
14506 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14507 {
14508 EMSG2(_(e_listarg), "inputlist()");
14509 return;
14510 }
14511
14512 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014513 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014514 lines_left = Rows; /* avoid more prompt */
14515 msg_scroll = TRUE;
14516 msg_clr_eos();
14517
14518 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14519 {
14520 msg_puts(get_tv_string(&li->li_tv));
14521 msg_putchar('\n');
14522 }
14523
14524 /* Ask for choice. */
14525 selected = prompt_for_number(&mouse_used);
14526 if (mouse_used)
14527 selected -= lines_left;
14528
14529 rettv->vval.v_number = selected;
14530}
14531
14532
Bram Moolenaar071d4272004-06-13 20:20:40 +000014533static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14534
14535/*
14536 * "inputrestore()" function
14537 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014538 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014539f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014540{
14541 if (ga_userinput.ga_len > 0)
14542 {
14543 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014544 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14545 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014546 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014547 }
14548 else if (p_verbose > 1)
14549 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014550 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014551 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014552 }
14553}
14554
14555/*
14556 * "inputsave()" function
14557 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014558 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014559f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014560{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014561 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014562 if (ga_grow(&ga_userinput, 1) == OK)
14563 {
14564 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14565 + ga_userinput.ga_len);
14566 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014567 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014568 }
14569 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014570 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014571}
14572
14573/*
14574 * "inputsecret()" function
14575 */
14576 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014577f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014578{
14579 ++cmdline_star;
14580 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014581 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014582 --cmdline_star;
14583 --inputsecret_flag;
14584}
14585
14586/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014587 * "insert()" function
14588 */
14589 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014590f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014591{
14592 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014593 listitem_T *item;
14594 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014595 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014596
14597 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014598 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014599 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014600 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014601 {
14602 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014603 before = get_tv_number_chk(&argvars[2], &error);
14604 if (error)
14605 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014606
Bram Moolenaar758711c2005-02-02 23:11:38 +000014607 if (before == l->lv_len)
14608 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014609 else
14610 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014611 item = list_find(l, before);
14612 if (item == NULL)
14613 {
14614 EMSGN(_(e_listidx), before);
14615 l = NULL;
14616 }
14617 }
14618 if (l != NULL)
14619 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014620 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014621 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014622 }
14623 }
14624}
14625
14626/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014627 * "invert(expr)" function
14628 */
14629 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014630f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014631{
14632 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14633}
14634
14635/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014636 * "isdirectory()" function
14637 */
14638 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014639f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014640{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014641 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014642}
14643
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014644/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014645 * "islocked()" function
14646 */
14647 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014648f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014649{
14650 lval_T lv;
14651 char_u *end;
14652 dictitem_T *di;
14653
14654 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014655 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14656 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014657 if (end != NULL && lv.ll_name != NULL)
14658 {
14659 if (*end != NUL)
14660 EMSG(_(e_trailing));
14661 else
14662 {
14663 if (lv.ll_tv == NULL)
14664 {
14665 if (check_changedtick(lv.ll_name))
14666 rettv->vval.v_number = 1; /* always locked */
14667 else
14668 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014669 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014670 if (di != NULL)
14671 {
14672 /* Consider a variable locked when:
14673 * 1. the variable itself is locked
14674 * 2. the value of the variable is locked.
14675 * 3. the List or Dict value is locked.
14676 */
14677 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14678 || tv_islocked(&di->di_tv));
14679 }
14680 }
14681 }
14682 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014683 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014684 else if (lv.ll_newkey != NULL)
14685 EMSG2(_(e_dictkey), lv.ll_newkey);
14686 else if (lv.ll_list != NULL)
14687 /* List item. */
14688 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14689 else
14690 /* Dictionary item. */
14691 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14692 }
14693 }
14694
14695 clear_lval(&lv);
14696}
14697
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014698#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14699/*
14700 * "isnan()" function
14701 */
14702 static void
14703f_isnan(typval_T *argvars, typval_T *rettv)
14704{
14705 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14706 && isnan(argvars[0].vval.v_float);
14707}
14708#endif
14709
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014710static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014711
14712/*
14713 * Turn a dict into a list:
14714 * "what" == 0: list of keys
14715 * "what" == 1: list of values
14716 * "what" == 2: list of items
14717 */
14718 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014719dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014720{
Bram Moolenaar33570922005-01-25 22:26:29 +000014721 list_T *l2;
14722 dictitem_T *di;
14723 hashitem_T *hi;
14724 listitem_T *li;
14725 listitem_T *li2;
14726 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014727 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014728
Bram Moolenaar8c711452005-01-14 21:53:12 +000014729 if (argvars[0].v_type != VAR_DICT)
14730 {
14731 EMSG(_(e_dictreq));
14732 return;
14733 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014734 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014735 return;
14736
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014737 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014738 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014739
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014740 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014741 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014742 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014743 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014744 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014745 --todo;
14746 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014747
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014748 li = listitem_alloc();
14749 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014750 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014751 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014752
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014753 if (what == 0)
14754 {
14755 /* keys() */
14756 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014757 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014758 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14759 }
14760 else if (what == 1)
14761 {
14762 /* values() */
14763 copy_tv(&di->di_tv, &li->li_tv);
14764 }
14765 else
14766 {
14767 /* items() */
14768 l2 = list_alloc();
14769 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014770 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014771 li->li_tv.vval.v_list = l2;
14772 if (l2 == NULL)
14773 break;
14774 ++l2->lv_refcount;
14775
14776 li2 = listitem_alloc();
14777 if (li2 == NULL)
14778 break;
14779 list_append(l2, li2);
14780 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014781 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014782 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14783
14784 li2 = listitem_alloc();
14785 if (li2 == NULL)
14786 break;
14787 list_append(l2, li2);
14788 copy_tv(&di->di_tv, &li2->li_tv);
14789 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014790 }
14791 }
14792}
14793
14794/*
14795 * "items(dict)" function
14796 */
14797 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014798f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014799{
14800 dict_list(argvars, rettv, 2);
14801}
14802
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014803#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014804/*
14805 * Get the job from the argument.
14806 * Returns NULL if the job is invalid.
14807 */
14808 static job_T *
14809get_job_arg(typval_T *tv)
14810{
14811 job_T *job;
14812
14813 if (tv->v_type != VAR_JOB)
14814 {
14815 EMSG2(_(e_invarg2), get_tv_string(tv));
14816 return NULL;
14817 }
14818 job = tv->vval.v_job;
14819
14820 if (job == NULL)
14821 EMSG(_("E916: not a valid job"));
14822 return job;
14823}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014824
Bram Moolenaar835dc632016-02-07 14:27:38 +010014825/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014826 * "job_getchannel()" function
14827 */
14828 static void
14829f_job_getchannel(typval_T *argvars, typval_T *rettv)
14830{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014831 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014832
Bram Moolenaar65edff82016-02-21 16:40:11 +010014833 if (job != NULL)
14834 {
Bram Moolenaar77073442016-02-13 23:23:53 +010014835 rettv->v_type = VAR_CHANNEL;
14836 rettv->vval.v_channel = job->jv_channel;
14837 if (job->jv_channel != NULL)
14838 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014839 }
14840}
14841
14842/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010014843 * "job_info()" function
14844 */
14845 static void
14846f_job_info(typval_T *argvars, typval_T *rettv)
14847{
14848 job_T *job = get_job_arg(&argvars[0]);
14849
14850 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
14851 job_info(job, rettv->vval.v_dict);
14852}
14853
14854/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010014855 * "job_setoptions()" function
14856 */
14857 static void
14858f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
14859{
14860 job_T *job = get_job_arg(&argvars[0]);
14861 jobopt_T opt;
14862
14863 if (job == NULL)
14864 return;
14865 clear_job_options(&opt);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014866 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014867 return;
14868 job_set_options(job, &opt);
14869}
14870
14871/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014872 * "job_start()" function
14873 */
14874 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010014875f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014876{
Bram Moolenaar835dc632016-02-07 14:27:38 +010014877 rettv->v_type = VAR_JOB;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014878 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014879}
14880
14881/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014882 * "job_status()" function
14883 */
14884 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014885f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014886{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014887 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014888
Bram Moolenaar65edff82016-02-21 16:40:11 +010014889 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014890 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010014891 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010014892 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010014893 }
14894}
14895
14896/*
14897 * "job_stop()" function
14898 */
14899 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014900f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014901{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014902 job_T *job = get_job_arg(&argvars[0]);
14903
14904 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014905 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014906}
14907#endif
14908
Bram Moolenaar071d4272004-06-13 20:20:40 +000014909/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014910 * "join()" function
14911 */
14912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014913f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014914{
14915 garray_T ga;
14916 char_u *sep;
14917
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014918 if (argvars[0].v_type != VAR_LIST)
14919 {
14920 EMSG(_(e_listreq));
14921 return;
14922 }
14923 if (argvars[0].vval.v_list == NULL)
14924 return;
14925 if (argvars[1].v_type == VAR_UNKNOWN)
14926 sep = (char_u *)" ";
14927 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014928 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014929
14930 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014931
14932 if (sep != NULL)
14933 {
14934 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014935 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014936 ga_append(&ga, NUL);
14937 rettv->vval.v_string = (char_u *)ga.ga_data;
14938 }
14939 else
14940 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014941}
14942
14943/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014944 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014945 */
14946 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014947f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014948{
14949 js_read_T reader;
14950
14951 reader.js_buf = get_tv_string(&argvars[0]);
14952 reader.js_fill = NULL;
14953 reader.js_used = 0;
14954 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
14955 EMSG(_(e_invarg));
14956}
14957
14958/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014959 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014960 */
14961 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014962f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014963{
14964 rettv->v_type = VAR_STRING;
14965 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
14966}
14967
14968/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014969 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014970 */
14971 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014972f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014973{
14974 js_read_T reader;
14975
14976 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010014977 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014978 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014979 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014980 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014981}
14982
14983/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014984 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014985 */
14986 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014987f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014988{
14989 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014990 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014991}
14992
14993/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014994 * "keys()" function
14995 */
14996 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014997f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014998{
14999 dict_list(argvars, rettv, 0);
15000}
15001
15002/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015003 * "last_buffer_nr()" function.
15004 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015005 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015006f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015007{
15008 int n = 0;
15009 buf_T *buf;
15010
15011 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15012 if (n < buf->b_fnum)
15013 n = buf->b_fnum;
15014
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015015 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015016}
15017
15018/*
15019 * "len()" function
15020 */
15021 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015022f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015023{
15024 switch (argvars[0].v_type)
15025 {
15026 case VAR_STRING:
15027 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015028 rettv->vval.v_number = (varnumber_T)STRLEN(
15029 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015030 break;
15031 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015032 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015033 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015034 case VAR_DICT:
15035 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15036 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015037 case VAR_UNKNOWN:
15038 case VAR_SPECIAL:
15039 case VAR_FLOAT:
15040 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010015041 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015042 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015043 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015044 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015045 break;
15046 }
15047}
15048
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015049static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015050
15051 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015052libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015053{
15054#ifdef FEAT_LIBCALL
15055 char_u *string_in;
15056 char_u **string_result;
15057 int nr_result;
15058#endif
15059
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015060 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015061 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015062 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015063
15064 if (check_restricted() || check_secure())
15065 return;
15066
15067#ifdef FEAT_LIBCALL
15068 /* The first two args must be strings, otherwise its meaningless */
15069 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15070 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015071 string_in = NULL;
15072 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015073 string_in = argvars[2].vval.v_string;
15074 if (type == VAR_NUMBER)
15075 string_result = NULL;
15076 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015077 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015078 if (mch_libcall(argvars[0].vval.v_string,
15079 argvars[1].vval.v_string,
15080 string_in,
15081 argvars[2].vval.v_number,
15082 string_result,
15083 &nr_result) == OK
15084 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015085 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015086 }
15087#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015088}
15089
15090/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015091 * "libcall()" function
15092 */
15093 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015094f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015095{
15096 libcall_common(argvars, rettv, VAR_STRING);
15097}
15098
15099/*
15100 * "libcallnr()" function
15101 */
15102 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015103f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015104{
15105 libcall_common(argvars, rettv, VAR_NUMBER);
15106}
15107
15108/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015109 * "line(string)" function
15110 */
15111 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015112f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015113{
15114 linenr_T lnum = 0;
15115 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015116 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015117
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015118 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015119 if (fp != NULL)
15120 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015121 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015122}
15123
15124/*
15125 * "line2byte(lnum)" function
15126 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015127 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015128f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015129{
15130#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015131 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015132#else
15133 linenr_T lnum;
15134
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015135 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015136 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015137 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015138 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015139 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15140 if (rettv->vval.v_number >= 0)
15141 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015142#endif
15143}
15144
15145/*
15146 * "lispindent(lnum)" function
15147 */
15148 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015149f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015150{
15151#ifdef FEAT_LISP
15152 pos_T pos;
15153 linenr_T lnum;
15154
15155 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015156 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015157 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15158 {
15159 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015160 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015161 curwin->w_cursor = pos;
15162 }
15163 else
15164#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015165 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015166}
15167
15168/*
15169 * "localtime()" function
15170 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015171 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015172f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015173{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015174 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015175}
15176
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015177static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015178
15179 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015180get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015181{
15182 char_u *keys;
15183 char_u *which;
15184 char_u buf[NUMBUFLEN];
15185 char_u *keys_buf = NULL;
15186 char_u *rhs;
15187 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015188 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015189 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015190 mapblock_T *mp;
15191 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015192
15193 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015194 rettv->v_type = VAR_STRING;
15195 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015196
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015197 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015198 if (*keys == NUL)
15199 return;
15200
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015201 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015202 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015203 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015204 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015205 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015206 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015207 if (argvars[3].v_type != VAR_UNKNOWN)
15208 get_dict = get_tv_number(&argvars[3]);
15209 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015211 else
15212 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015213 if (which == NULL)
15214 return;
15215
Bram Moolenaar071d4272004-06-13 20:20:40 +000015216 mode = get_map_mode(&which, 0);
15217
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015218 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015219 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015220 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015221
15222 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015223 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015224 /* Return a string. */
15225 if (rhs != NULL)
15226 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015227
Bram Moolenaarbd743252010-10-20 21:23:33 +020015228 }
15229 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15230 {
15231 /* Return a dictionary. */
15232 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15233 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15234 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015235
Bram Moolenaarbd743252010-10-20 21:23:33 +020015236 dict_add_nr_str(dict, "lhs", 0L, lhs);
15237 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15238 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15239 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15240 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15241 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15242 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015243 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015244 dict_add_nr_str(dict, "mode", 0L, mapmode);
15245
15246 vim_free(lhs);
15247 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015248 }
15249}
15250
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015251#ifdef FEAT_FLOAT
15252/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015253 * "log()" function
15254 */
15255 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015256f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015257{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015258 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015259
15260 rettv->v_type = VAR_FLOAT;
15261 if (get_float_arg(argvars, &f) == OK)
15262 rettv->vval.v_float = log(f);
15263 else
15264 rettv->vval.v_float = 0.0;
15265}
15266
15267/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015268 * "log10()" function
15269 */
15270 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015271f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015272{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015273 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015274
15275 rettv->v_type = VAR_FLOAT;
15276 if (get_float_arg(argvars, &f) == OK)
15277 rettv->vval.v_float = log10(f);
15278 else
15279 rettv->vval.v_float = 0.0;
15280}
15281#endif
15282
Bram Moolenaar1dced572012-04-05 16:54:08 +020015283#ifdef FEAT_LUA
15284/*
15285 * "luaeval()" function
15286 */
15287 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015288f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015289{
15290 char_u *str;
15291 char_u buf[NUMBUFLEN];
15292
15293 str = get_tv_string_buf(&argvars[0], buf);
15294 do_luaeval(str, argvars + 1, rettv);
15295}
15296#endif
15297
Bram Moolenaar071d4272004-06-13 20:20:40 +000015298/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015299 * "map()" function
15300 */
15301 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015302f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015303{
15304 filter_map(argvars, rettv, TRUE);
15305}
15306
15307/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015308 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015309 */
15310 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015311f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015312{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015313 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015314}
15315
15316/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015317 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015318 */
15319 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015320f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015321{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015322 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015323}
15324
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015325static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015326
15327 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015328find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015329{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015330 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015331 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015332 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015333 char_u *pat;
15334 regmatch_T regmatch;
15335 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015336 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015337 char_u *save_cpo;
15338 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015339 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015340 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015341 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015342 list_T *l = NULL;
15343 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015344 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015345 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015346
15347 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15348 save_cpo = p_cpo;
15349 p_cpo = (char_u *)"";
15350
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015351 rettv->vval.v_number = -1;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015352 if (type == 3 || type == 4)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015353 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015354 /* type 3: return empty list when there are no matches.
15355 * type 4: return ["", -1, -1, -1] */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015356 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015357 goto theend;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015358 if (type == 4
15359 && (list_append_string(rettv->vval.v_list,
15360 (char_u *)"", 0) == FAIL
15361 || list_append_number(rettv->vval.v_list,
15362 (varnumber_T)-1) == FAIL
15363 || list_append_number(rettv->vval.v_list,
15364 (varnumber_T)-1) == FAIL
15365 || list_append_number(rettv->vval.v_list,
15366 (varnumber_T)-1) == FAIL))
15367 {
15368 list_free(rettv->vval.v_list, TRUE);
15369 rettv->vval.v_list = NULL;
15370 goto theend;
15371 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015372 }
15373 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015374 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015375 rettv->v_type = VAR_STRING;
15376 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015378
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015379 if (argvars[0].v_type == VAR_LIST)
15380 {
15381 if ((l = argvars[0].vval.v_list) == NULL)
15382 goto theend;
15383 li = l->lv_first;
15384 }
15385 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015386 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015387 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015388 len = (long)STRLEN(str);
15389 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015390
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015391 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15392 if (pat == NULL)
15393 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015394
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015395 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015396 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015397 int error = FALSE;
15398
15399 start = get_tv_number_chk(&argvars[2], &error);
15400 if (error)
15401 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015402 if (l != NULL)
15403 {
15404 li = list_find(l, start);
15405 if (li == NULL)
15406 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015407 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015408 }
15409 else
15410 {
15411 if (start < 0)
15412 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015413 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015414 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015415 /* When "count" argument is there ignore matches before "start",
15416 * otherwise skip part of the string. Differs when pattern is "^"
15417 * or "\<". */
15418 if (argvars[3].v_type != VAR_UNKNOWN)
15419 startcol = start;
15420 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015421 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015422 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015423 len -= start;
15424 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015425 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015426
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015427 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015428 nth = get_tv_number_chk(&argvars[3], &error);
15429 if (error)
15430 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015431 }
15432
15433 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15434 if (regmatch.regprog != NULL)
15435 {
15436 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015437
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015438 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015439 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015440 if (l != NULL)
15441 {
15442 if (li == NULL)
15443 {
15444 match = FALSE;
15445 break;
15446 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015447 vim_free(tofree);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015448 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015449 if (str == NULL)
15450 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015451 }
15452
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015453 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015454
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015455 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015456 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015457 if (l == NULL && !match)
15458 break;
15459
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015460 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015461 if (l != NULL)
15462 {
15463 li = li->li_next;
15464 ++idx;
15465 }
15466 else
15467 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015468#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015469 startcol = (colnr_T)(regmatch.startp[0]
15470 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015471#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015472 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015473#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015474 if (startcol > (colnr_T)len
15475 || str + startcol <= regmatch.startp[0])
15476 {
15477 match = FALSE;
15478 break;
15479 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015480 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015481 }
15482
15483 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015484 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015485 if (type == 4)
15486 {
15487 listitem_T *li1 = rettv->vval.v_list->lv_first;
15488 listitem_T *li2 = li1->li_next;
15489 listitem_T *li3 = li2->li_next;
15490 listitem_T *li4 = li3->li_next;
15491
15492 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
15493 (int)(regmatch.endp[0] - regmatch.startp[0]));
15494 li3->li_tv.vval.v_number =
15495 (varnumber_T)(regmatch.startp[0] - expr);
15496 li4->li_tv.vval.v_number =
15497 (varnumber_T)(regmatch.endp[0] - expr);
15498 if (l != NULL)
15499 li2->li_tv.vval.v_number = (varnumber_T)idx;
15500 }
15501 else if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015502 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015503 int i;
15504
15505 /* return list with matched string and submatches */
15506 for (i = 0; i < NSUBEXP; ++i)
15507 {
15508 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015509 {
15510 if (list_append_string(rettv->vval.v_list,
15511 (char_u *)"", 0) == FAIL)
15512 break;
15513 }
15514 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015515 regmatch.startp[i],
15516 (int)(regmatch.endp[i] - regmatch.startp[i]))
15517 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015518 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015519 }
15520 }
15521 else if (type == 2)
15522 {
15523 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015524 if (l != NULL)
15525 copy_tv(&li->li_tv, rettv);
15526 else
15527 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015528 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015529 }
15530 else if (l != NULL)
15531 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015532 else
15533 {
15534 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015535 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015536 (varnumber_T)(regmatch.startp[0] - str);
15537 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015538 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015539 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015540 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015541 }
15542 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015543 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015544 }
15545
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015546 if (type == 4 && l == NULL)
15547 /* matchstrpos() without a list: drop the second item. */
15548 listitem_remove(rettv->vval.v_list,
15549 rettv->vval.v_list->lv_first->li_next);
15550
Bram Moolenaar071d4272004-06-13 20:20:40 +000015551theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015552 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015553 p_cpo = save_cpo;
15554}
15555
15556/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015557 * "match()" function
15558 */
15559 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015560f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015561{
15562 find_some_match(argvars, rettv, 1);
15563}
15564
15565/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015566 * "matchadd()" function
15567 */
15568 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015569f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015570{
15571#ifdef FEAT_SEARCH_EXTRA
15572 char_u buf[NUMBUFLEN];
15573 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15574 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15575 int prio = 10; /* default priority */
15576 int id = -1;
15577 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015578 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015579
15580 rettv->vval.v_number = -1;
15581
15582 if (grp == NULL || pat == NULL)
15583 return;
15584 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015585 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015586 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015587 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015588 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015589 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015590 if (argvars[4].v_type != VAR_UNKNOWN)
15591 {
15592 if (argvars[4].v_type != VAR_DICT)
15593 {
15594 EMSG(_(e_dictreq));
15595 return;
15596 }
15597 if (dict_find(argvars[4].vval.v_dict,
15598 (char_u *)"conceal", -1) != NULL)
15599 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15600 (char_u *)"conceal", FALSE);
15601 }
15602 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015603 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015604 if (error == TRUE)
15605 return;
15606 if (id >= 1 && id <= 3)
15607 {
15608 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15609 return;
15610 }
15611
Bram Moolenaar6561d522015-07-21 15:48:27 +020015612 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15613 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015614#endif
15615}
15616
15617/*
15618 * "matchaddpos()" function
15619 */
15620 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015621f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015622{
15623#ifdef FEAT_SEARCH_EXTRA
15624 char_u buf[NUMBUFLEN];
15625 char_u *group;
15626 int prio = 10;
15627 int id = -1;
15628 int error = FALSE;
15629 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015630 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015631
15632 rettv->vval.v_number = -1;
15633
15634 group = get_tv_string_buf_chk(&argvars[0], buf);
15635 if (group == NULL)
15636 return;
15637
15638 if (argvars[1].v_type != VAR_LIST)
15639 {
15640 EMSG2(_(e_listarg), "matchaddpos()");
15641 return;
15642 }
15643 l = argvars[1].vval.v_list;
15644 if (l == NULL)
15645 return;
15646
15647 if (argvars[2].v_type != VAR_UNKNOWN)
15648 {
15649 prio = get_tv_number_chk(&argvars[2], &error);
15650 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015651 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015652 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015653 if (argvars[4].v_type != VAR_UNKNOWN)
15654 {
15655 if (argvars[4].v_type != VAR_DICT)
15656 {
15657 EMSG(_(e_dictreq));
15658 return;
15659 }
15660 if (dict_find(argvars[4].vval.v_dict,
15661 (char_u *)"conceal", -1) != NULL)
15662 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15663 (char_u *)"conceal", FALSE);
15664 }
15665 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015666 }
15667 if (error == TRUE)
15668 return;
15669
15670 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15671 if (id == 1 || id == 2)
15672 {
15673 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15674 return;
15675 }
15676
Bram Moolenaar6561d522015-07-21 15:48:27 +020015677 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15678 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015679#endif
15680}
15681
15682/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015683 * "matcharg()" function
15684 */
15685 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015686f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015687{
15688 if (rettv_list_alloc(rettv) == OK)
15689 {
15690#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015691 int id = get_tv_number(&argvars[0]);
15692 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015693
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015694 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015695 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015696 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15697 {
15698 list_append_string(rettv->vval.v_list,
15699 syn_id2name(m->hlg_id), -1);
15700 list_append_string(rettv->vval.v_list, m->pattern, -1);
15701 }
15702 else
15703 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015704 list_append_string(rettv->vval.v_list, NULL, -1);
15705 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015706 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015707 }
15708#endif
15709 }
15710}
15711
15712/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015713 * "matchdelete()" function
15714 */
15715 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015716f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015717{
15718#ifdef FEAT_SEARCH_EXTRA
15719 rettv->vval.v_number = match_delete(curwin,
15720 (int)get_tv_number(&argvars[0]), TRUE);
15721#endif
15722}
15723
15724/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015725 * "matchend()" function
15726 */
15727 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015728f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015729{
15730 find_some_match(argvars, rettv, 0);
15731}
15732
15733/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015734 * "matchlist()" function
15735 */
15736 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015737f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015738{
15739 find_some_match(argvars, rettv, 3);
15740}
15741
15742/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015743 * "matchstr()" function
15744 */
15745 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015746f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015747{
15748 find_some_match(argvars, rettv, 2);
15749}
15750
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015751/*
15752 * "matchstrpos()" function
15753 */
15754 static void
15755f_matchstrpos(typval_T *argvars, typval_T *rettv)
15756{
15757 find_some_match(argvars, rettv, 4);
15758}
15759
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015760static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015761
15762 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015763max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015764{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015765 long n = 0;
15766 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015767 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015768
15769 if (argvars[0].v_type == VAR_LIST)
15770 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015771 list_T *l;
15772 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015773
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015774 l = argvars[0].vval.v_list;
15775 if (l != NULL)
15776 {
15777 li = l->lv_first;
15778 if (li != NULL)
15779 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015780 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015781 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015782 {
15783 li = li->li_next;
15784 if (li == NULL)
15785 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015786 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015787 if (domax ? i > n : i < n)
15788 n = i;
15789 }
15790 }
15791 }
15792 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015793 else if (argvars[0].v_type == VAR_DICT)
15794 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015795 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015796 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015797 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015798 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015799
15800 d = argvars[0].vval.v_dict;
15801 if (d != NULL)
15802 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015803 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015804 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015805 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015806 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015807 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015808 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015809 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015810 if (first)
15811 {
15812 n = i;
15813 first = FALSE;
15814 }
15815 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015816 n = i;
15817 }
15818 }
15819 }
15820 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015821 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015822 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015823 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015824}
15825
15826/*
15827 * "max()" function
15828 */
15829 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015830f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015831{
15832 max_min(argvars, rettv, TRUE);
15833}
15834
15835/*
15836 * "min()" function
15837 */
15838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015839f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015840{
15841 max_min(argvars, rettv, FALSE);
15842}
15843
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015844static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015845
15846/*
15847 * Create the directory in which "dir" is located, and higher levels when
15848 * needed.
15849 */
15850 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015851mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015852{
15853 char_u *p;
15854 char_u *updir;
15855 int r = FAIL;
15856
15857 /* Get end of directory name in "dir".
15858 * We're done when it's "/" or "c:/". */
15859 p = gettail_sep(dir);
15860 if (p <= get_past_head(dir))
15861 return OK;
15862
15863 /* If the directory exists we're done. Otherwise: create it.*/
15864 updir = vim_strnsave(dir, (int)(p - dir));
15865 if (updir == NULL)
15866 return FAIL;
15867 if (mch_isdir(updir))
15868 r = OK;
15869 else if (mkdir_recurse(updir, prot) == OK)
15870 r = vim_mkdir_emsg(updir, prot);
15871 vim_free(updir);
15872 return r;
15873}
15874
15875#ifdef vim_mkdir
15876/*
15877 * "mkdir()" function
15878 */
15879 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015880f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015881{
15882 char_u *dir;
15883 char_u buf[NUMBUFLEN];
15884 int prot = 0755;
15885
15886 rettv->vval.v_number = FAIL;
15887 if (check_restricted() || check_secure())
15888 return;
15889
15890 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015891 if (*dir == NUL)
15892 rettv->vval.v_number = FAIL;
15893 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015894 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015895 if (*gettail(dir) == NUL)
15896 /* remove trailing slashes */
15897 *gettail_sep(dir) = NUL;
15898
15899 if (argvars[1].v_type != VAR_UNKNOWN)
15900 {
15901 if (argvars[2].v_type != VAR_UNKNOWN)
15902 prot = get_tv_number_chk(&argvars[2], NULL);
15903 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15904 mkdir_recurse(dir, prot);
15905 }
15906 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015907 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015908}
15909#endif
15910
Bram Moolenaar0d660222005-01-07 21:51:51 +000015911/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015912 * "mode()" function
15913 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015914 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015915f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015916{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015917 char_u buf[3];
15918
15919 buf[1] = NUL;
15920 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015921
Bram Moolenaar071d4272004-06-13 20:20:40 +000015922 if (VIsual_active)
15923 {
15924 if (VIsual_select)
15925 buf[0] = VIsual_mode + 's' - 'v';
15926 else
15927 buf[0] = VIsual_mode;
15928 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015929 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015930 || State == CONFIRM)
15931 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015932 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015933 if (State == ASKMORE)
15934 buf[1] = 'm';
15935 else if (State == CONFIRM)
15936 buf[1] = '?';
15937 }
15938 else if (State == EXTERNCMD)
15939 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015940 else if (State & INSERT)
15941 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015942#ifdef FEAT_VREPLACE
15943 if (State & VREPLACE_FLAG)
15944 {
15945 buf[0] = 'R';
15946 buf[1] = 'v';
15947 }
15948 else
15949#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015950 if (State & REPLACE_FLAG)
15951 buf[0] = 'R';
15952 else
15953 buf[0] = 'i';
15954 }
15955 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015956 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015957 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015958 if (exmode_active)
15959 buf[1] = 'v';
15960 }
15961 else if (exmode_active)
15962 {
15963 buf[0] = 'c';
15964 buf[1] = 'e';
15965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015966 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015967 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015968 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015969 if (finish_op)
15970 buf[1] = 'o';
15971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015972
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015973 /* Clear out the minor mode when the argument is not a non-zero number or
15974 * non-empty string. */
15975 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015976 buf[1] = NUL;
15977
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015978 rettv->vval.v_string = vim_strsave(buf);
15979 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015980}
15981
Bram Moolenaar429fa852013-04-15 12:27:36 +020015982#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015983/*
15984 * "mzeval()" function
15985 */
15986 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015987f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015988{
15989 char_u *str;
15990 char_u buf[NUMBUFLEN];
15991
15992 str = get_tv_string_buf(&argvars[0], buf);
15993 do_mzeval(str, rettv);
15994}
Bram Moolenaar75676462013-01-30 14:55:42 +010015995
15996 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015997mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015998{
15999 typval_T argvars[3];
16000
16001 argvars[0].v_type = VAR_STRING;
16002 argvars[0].vval.v_string = name;
16003 copy_tv(args, &argvars[1]);
16004 argvars[2].v_type = VAR_UNKNOWN;
16005 f_call(argvars, rettv);
16006 clear_tv(&argvars[1]);
16007}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016008#endif
16009
Bram Moolenaar071d4272004-06-13 20:20:40 +000016010/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016011 * "nextnonblank()" function
16012 */
16013 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016014f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016015{
16016 linenr_T lnum;
16017
16018 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16019 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016020 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016021 {
16022 lnum = 0;
16023 break;
16024 }
16025 if (*skipwhite(ml_get(lnum)) != NUL)
16026 break;
16027 }
16028 rettv->vval.v_number = lnum;
16029}
16030
16031/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016032 * "nr2char()" function
16033 */
16034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016035f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016036{
16037 char_u buf[NUMBUFLEN];
16038
16039#ifdef FEAT_MBYTE
16040 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016041 {
16042 int utf8 = 0;
16043
16044 if (argvars[1].v_type != VAR_UNKNOWN)
16045 utf8 = get_tv_number_chk(&argvars[1], NULL);
16046 if (utf8)
16047 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16048 else
16049 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016051 else
16052#endif
16053 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016054 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016055 buf[1] = NUL;
16056 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016057 rettv->v_type = VAR_STRING;
16058 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016059}
16060
16061/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016062 * "or(expr, expr)" function
16063 */
16064 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016065f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016066{
16067 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16068 | get_tv_number_chk(&argvars[1], NULL);
16069}
16070
16071/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016072 * "pathshorten()" function
16073 */
16074 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016075f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016076{
16077 char_u *p;
16078
16079 rettv->v_type = VAR_STRING;
16080 p = get_tv_string_chk(&argvars[0]);
16081 if (p == NULL)
16082 rettv->vval.v_string = NULL;
16083 else
16084 {
16085 p = vim_strsave(p);
16086 rettv->vval.v_string = p;
16087 if (p != NULL)
16088 shorten_dir(p);
16089 }
16090}
16091
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016092#ifdef FEAT_PERL
16093/*
16094 * "perleval()" function
16095 */
16096 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016097f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016098{
16099 char_u *str;
16100 char_u buf[NUMBUFLEN];
16101
16102 str = get_tv_string_buf(&argvars[0], buf);
16103 do_perleval(str, rettv);
16104}
16105#endif
16106
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016107#ifdef FEAT_FLOAT
16108/*
16109 * "pow()" function
16110 */
16111 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016112f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016113{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016114 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016115
16116 rettv->v_type = VAR_FLOAT;
16117 if (get_float_arg(argvars, &fx) == OK
16118 && get_float_arg(&argvars[1], &fy) == OK)
16119 rettv->vval.v_float = pow(fx, fy);
16120 else
16121 rettv->vval.v_float = 0.0;
16122}
16123#endif
16124
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016125/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016126 * "prevnonblank()" function
16127 */
16128 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016129f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016130{
16131 linenr_T lnum;
16132
16133 lnum = get_tv_lnum(argvars);
16134 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16135 lnum = 0;
16136 else
16137 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16138 --lnum;
16139 rettv->vval.v_number = lnum;
16140}
16141
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016142/* This dummy va_list is here because:
16143 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16144 * - locally in the function results in a "used before set" warning
16145 * - using va_start() to initialize it gives "function with fixed args" error */
16146static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016147
Bram Moolenaar8c711452005-01-14 21:53:12 +000016148/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016149 * "printf()" function
16150 */
16151 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016152f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016153{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016154 char_u buf[NUMBUFLEN];
16155 int len;
16156 char_u *s;
16157 int saved_did_emsg = did_emsg;
16158 char *fmt;
16159
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016160 rettv->v_type = VAR_STRING;
16161 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016162
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016163 /* Get the required length, allocate the buffer and do it for real. */
16164 did_emsg = FALSE;
16165 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16166 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16167 if (!did_emsg)
16168 {
16169 s = alloc(len + 1);
16170 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016171 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016172 rettv->vval.v_string = s;
16173 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016174 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016175 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016176 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016177}
16178
16179/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016180 * "pumvisible()" function
16181 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016182 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016183f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016184{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016185#ifdef FEAT_INS_EXPAND
16186 if (pum_visible())
16187 rettv->vval.v_number = 1;
16188#endif
16189}
16190
Bram Moolenaardb913952012-06-29 12:54:53 +020016191#ifdef FEAT_PYTHON3
16192/*
16193 * "py3eval()" function
16194 */
16195 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016196f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016197{
16198 char_u *str;
16199 char_u buf[NUMBUFLEN];
16200
16201 str = get_tv_string_buf(&argvars[0], buf);
16202 do_py3eval(str, rettv);
16203}
16204#endif
16205
16206#ifdef FEAT_PYTHON
16207/*
16208 * "pyeval()" function
16209 */
16210 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016211f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016212{
16213 char_u *str;
16214 char_u buf[NUMBUFLEN];
16215
16216 str = get_tv_string_buf(&argvars[0], buf);
16217 do_pyeval(str, rettv);
16218}
16219#endif
16220
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016221/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016222 * "range()" function
16223 */
16224 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016225f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016226{
16227 long start;
16228 long end;
16229 long stride = 1;
16230 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016231 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016232
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016233 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016234 if (argvars[1].v_type == VAR_UNKNOWN)
16235 {
16236 end = start - 1;
16237 start = 0;
16238 }
16239 else
16240 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016241 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016242 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016243 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016244 }
16245
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016246 if (error)
16247 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016248 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016249 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016250 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016251 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016252 else
16253 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016254 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016255 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016256 if (list_append_number(rettv->vval.v_list,
16257 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016258 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016259 }
16260}
16261
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016262/*
16263 * "readfile()" function
16264 */
16265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016266f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016267{
16268 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016269 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016270 char_u *fname;
16271 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016272 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16273 int io_size = sizeof(buf);
16274 int readlen; /* size of last fread() */
16275 char_u *prev = NULL; /* previously read bytes, if any */
16276 long prevlen = 0; /* length of data in prev */
16277 long prevsize = 0; /* size of prev buffer */
16278 long maxline = MAXLNUM;
16279 long cnt = 0;
16280 char_u *p; /* position in buf */
16281 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016282
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016283 if (argvars[1].v_type != VAR_UNKNOWN)
16284 {
16285 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16286 binary = TRUE;
16287 if (argvars[2].v_type != VAR_UNKNOWN)
16288 maxline = get_tv_number(&argvars[2]);
16289 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016290
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016291 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016292 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016293
16294 /* Always open the file in binary mode, library functions have a mind of
16295 * their own about CR-LF conversion. */
16296 fname = get_tv_string(&argvars[0]);
16297 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16298 {
16299 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16300 return;
16301 }
16302
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016303 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016304 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016305 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016306
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016307 /* This for loop processes what was read, but is also entered at end
16308 * of file so that either:
16309 * - an incomplete line gets written
16310 * - a "binary" file gets an empty line at the end if it ends in a
16311 * newline. */
16312 for (p = buf, start = buf;
16313 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16314 ++p)
16315 {
16316 if (*p == '\n' || readlen <= 0)
16317 {
16318 listitem_T *li;
16319 char_u *s = NULL;
16320 long_u len = p - start;
16321
16322 /* Finished a line. Remove CRs before NL. */
16323 if (readlen > 0 && !binary)
16324 {
16325 while (len > 0 && start[len - 1] == '\r')
16326 --len;
16327 /* removal may cross back to the "prev" string */
16328 if (len == 0)
16329 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16330 --prevlen;
16331 }
16332 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016333 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016334 else
16335 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016336 /* Change "prev" buffer to be the right size. This way
16337 * the bytes are only copied once, and very long lines are
16338 * allocated only once. */
16339 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016340 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016341 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016342 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016343 prev = NULL; /* the list will own the string */
16344 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016345 }
16346 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016347 if (s == NULL)
16348 {
16349 do_outofmem_msg((long_u) prevlen + len + 1);
16350 failed = TRUE;
16351 break;
16352 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016353
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016354 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016355 {
16356 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016357 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016358 break;
16359 }
16360 li->li_tv.v_type = VAR_STRING;
16361 li->li_tv.v_lock = 0;
16362 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016363 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016364
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016365 start = p + 1; /* step over newline */
16366 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016367 break;
16368 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016369 else if (*p == NUL)
16370 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016371#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016372 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16373 * when finding the BF and check the previous two bytes. */
16374 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016375 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016376 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16377 * + 1, these may be in the "prev" string. */
16378 char_u back1 = p >= buf + 1 ? p[-1]
16379 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16380 char_u back2 = p >= buf + 2 ? p[-2]
16381 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16382 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016383
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016384 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016385 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016386 char_u *dest = p - 2;
16387
16388 /* Usually a BOM is at the beginning of a file, and so at
16389 * the beginning of a line; then we can just step over it.
16390 */
16391 if (start == dest)
16392 start = p + 1;
16393 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016394 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016395 /* have to shuffle buf to close gap */
16396 int adjust_prevlen = 0;
16397
16398 if (dest < buf)
16399 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016400 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016401 dest = buf;
16402 }
16403 if (readlen > p - buf + 1)
16404 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16405 readlen -= 3 - adjust_prevlen;
16406 prevlen -= adjust_prevlen;
16407 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016408 }
16409 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016410 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016411#endif
16412 } /* for */
16413
16414 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16415 break;
16416 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016417 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016418 /* There's part of a line in buf, store it in "prev". */
16419 if (p - start + prevlen >= prevsize)
16420 {
16421 /* need bigger "prev" buffer */
16422 char_u *newprev;
16423
16424 /* A common use case is ordinary text files and "prev" gets a
16425 * fragment of a line, so the first allocation is made
16426 * small, to avoid repeatedly 'allocing' large and
16427 * 'reallocing' small. */
16428 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016429 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016430 else
16431 {
16432 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016433 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016434 prevsize = grow50pc > growmin ? grow50pc : growmin;
16435 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016436 newprev = prev == NULL ? alloc(prevsize)
16437 : vim_realloc(prev, prevsize);
16438 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016439 {
16440 do_outofmem_msg((long_u)prevsize);
16441 failed = TRUE;
16442 break;
16443 }
16444 prev = newprev;
16445 }
16446 /* Add the line part to end of "prev". */
16447 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016448 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016449 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016450 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016451
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016452 /*
16453 * For a negative line count use only the lines at the end of the file,
16454 * free the rest.
16455 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016456 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016457 while (cnt > -maxline)
16458 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016459 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016460 --cnt;
16461 }
16462
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016463 if (failed)
16464 {
16465 list_free(rettv->vval.v_list, TRUE);
16466 /* readfile doc says an empty list is returned on error */
16467 rettv->vval.v_list = list_alloc();
16468 }
16469
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016470 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016471 fclose(fd);
16472}
16473
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016474#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016475static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016476
16477/*
16478 * Convert a List to proftime_T.
16479 * Return FAIL when there is something wrong.
16480 */
16481 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016482list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016483{
16484 long n1, n2;
16485 int error = FALSE;
16486
16487 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16488 || arg->vval.v_list->lv_len != 2)
16489 return FAIL;
16490 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16491 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16492# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016493 tm->HighPart = n1;
16494 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016495# else
16496 tm->tv_sec = n1;
16497 tm->tv_usec = n2;
16498# endif
16499 return error ? FAIL : OK;
16500}
16501#endif /* FEAT_RELTIME */
16502
16503/*
16504 * "reltime()" function
16505 */
16506 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016507f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016508{
16509#ifdef FEAT_RELTIME
16510 proftime_T res;
16511 proftime_T start;
16512
16513 if (argvars[0].v_type == VAR_UNKNOWN)
16514 {
16515 /* No arguments: get current time. */
16516 profile_start(&res);
16517 }
16518 else if (argvars[1].v_type == VAR_UNKNOWN)
16519 {
16520 if (list2proftime(&argvars[0], &res) == FAIL)
16521 return;
16522 profile_end(&res);
16523 }
16524 else
16525 {
16526 /* Two arguments: compute the difference. */
16527 if (list2proftime(&argvars[0], &start) == FAIL
16528 || list2proftime(&argvars[1], &res) == FAIL)
16529 return;
16530 profile_sub(&res, &start);
16531 }
16532
16533 if (rettv_list_alloc(rettv) == OK)
16534 {
16535 long n1, n2;
16536
16537# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016538 n1 = res.HighPart;
16539 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016540# else
16541 n1 = res.tv_sec;
16542 n2 = res.tv_usec;
16543# endif
16544 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16545 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16546 }
16547#endif
16548}
16549
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016550#ifdef FEAT_FLOAT
16551/*
16552 * "reltimefloat()" function
16553 */
16554 static void
16555f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16556{
16557# ifdef FEAT_RELTIME
16558 proftime_T tm;
16559# endif
16560
16561 rettv->v_type = VAR_FLOAT;
16562 rettv->vval.v_float = 0;
16563# ifdef FEAT_RELTIME
16564 if (list2proftime(&argvars[0], &tm) == OK)
16565 rettv->vval.v_float = profile_float(&tm);
16566# endif
16567}
16568#endif
16569
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016570/*
16571 * "reltimestr()" function
16572 */
16573 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016574f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016575{
16576#ifdef FEAT_RELTIME
16577 proftime_T tm;
16578#endif
16579
16580 rettv->v_type = VAR_STRING;
16581 rettv->vval.v_string = NULL;
16582#ifdef FEAT_RELTIME
16583 if (list2proftime(&argvars[0], &tm) == OK)
16584 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16585#endif
16586}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016587
Bram Moolenaar0d660222005-01-07 21:51:51 +000016588#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016589static void make_connection(void);
16590static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016591
16592 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016593make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016594{
16595 if (X_DISPLAY == NULL
16596# ifdef FEAT_GUI
16597 && !gui.in_use
16598# endif
16599 )
16600 {
16601 x_force_connect = TRUE;
16602 setup_term_clip();
16603 x_force_connect = FALSE;
16604 }
16605}
16606
16607 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016608check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016609{
16610 make_connection();
16611 if (X_DISPLAY == NULL)
16612 {
16613 EMSG(_("E240: No connection to Vim server"));
16614 return FAIL;
16615 }
16616 return OK;
16617}
16618#endif
16619
16620#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016621static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016622
16623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016624remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016625{
16626 char_u *server_name;
16627 char_u *keys;
16628 char_u *r = NULL;
16629 char_u buf[NUMBUFLEN];
16630# ifdef WIN32
16631 HWND w;
16632# else
16633 Window w;
16634# endif
16635
16636 if (check_restricted() || check_secure())
16637 return;
16638
16639# ifdef FEAT_X11
16640 if (check_connection() == FAIL)
16641 return;
16642# endif
16643
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016644 server_name = get_tv_string_chk(&argvars[0]);
16645 if (server_name == NULL)
16646 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016647 keys = get_tv_string_buf(&argvars[1], buf);
16648# ifdef WIN32
16649 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16650# else
16651 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16652 < 0)
16653# endif
16654 {
16655 if (r != NULL)
16656 EMSG(r); /* sending worked but evaluation failed */
16657 else
16658 EMSG2(_("E241: Unable to send to %s"), server_name);
16659 return;
16660 }
16661
16662 rettv->vval.v_string = r;
16663
16664 if (argvars[2].v_type != VAR_UNKNOWN)
16665 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016666 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016667 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016668 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016669
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016670 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016671 v.di_tv.v_type = VAR_STRING;
16672 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016673 idvar = get_tv_string_chk(&argvars[2]);
16674 if (idvar != NULL)
16675 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016676 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016677 }
16678}
16679#endif
16680
16681/*
16682 * "remote_expr()" function
16683 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016684 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016685f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016686{
16687 rettv->v_type = VAR_STRING;
16688 rettv->vval.v_string = NULL;
16689#ifdef FEAT_CLIENTSERVER
16690 remote_common(argvars, rettv, TRUE);
16691#endif
16692}
16693
16694/*
16695 * "remote_foreground()" function
16696 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016698f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016699{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016700#ifdef FEAT_CLIENTSERVER
16701# ifdef WIN32
16702 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016703 {
16704 char_u *server_name = get_tv_string_chk(&argvars[0]);
16705
16706 if (server_name != NULL)
16707 serverForeground(server_name);
16708 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016709# else
16710 /* Send a foreground() expression to the server. */
16711 argvars[1].v_type = VAR_STRING;
16712 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16713 argvars[2].v_type = VAR_UNKNOWN;
16714 remote_common(argvars, rettv, TRUE);
16715 vim_free(argvars[1].vval.v_string);
16716# endif
16717#endif
16718}
16719
Bram Moolenaar0d660222005-01-07 21:51:51 +000016720 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016721f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016722{
16723#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016724 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016725 char_u *s = NULL;
16726# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016727 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016728# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016729 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016730
16731 if (check_restricted() || check_secure())
16732 {
16733 rettv->vval.v_number = -1;
16734 return;
16735 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016736 serverid = get_tv_string_chk(&argvars[0]);
16737 if (serverid == NULL)
16738 {
16739 rettv->vval.v_number = -1;
16740 return; /* type error; errmsg already given */
16741 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016742# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016743 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016744 if (n == 0)
16745 rettv->vval.v_number = -1;
16746 else
16747 {
16748 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16749 rettv->vval.v_number = (s != NULL);
16750 }
16751# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016752 if (check_connection() == FAIL)
16753 return;
16754
16755 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016756 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016757# endif
16758
16759 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16760 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016761 char_u *retvar;
16762
Bram Moolenaar33570922005-01-25 22:26:29 +000016763 v.di_tv.v_type = VAR_STRING;
16764 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016765 retvar = get_tv_string_chk(&argvars[1]);
16766 if (retvar != NULL)
16767 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016768 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016769 }
16770#else
16771 rettv->vval.v_number = -1;
16772#endif
16773}
16774
Bram Moolenaar0d660222005-01-07 21:51:51 +000016775 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016776f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016777{
16778 char_u *r = NULL;
16779
16780#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016781 char_u *serverid = get_tv_string_chk(&argvars[0]);
16782
16783 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016784 {
16785# ifdef WIN32
16786 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016787 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016788
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016789 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016790 if (n != 0)
16791 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16792 if (r == NULL)
16793# else
16794 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016795 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016796# endif
16797 EMSG(_("E277: Unable to read a server reply"));
16798 }
16799#endif
16800 rettv->v_type = VAR_STRING;
16801 rettv->vval.v_string = r;
16802}
16803
16804/*
16805 * "remote_send()" function
16806 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016807 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016808f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016809{
16810 rettv->v_type = VAR_STRING;
16811 rettv->vval.v_string = NULL;
16812#ifdef FEAT_CLIENTSERVER
16813 remote_common(argvars, rettv, FALSE);
16814#endif
16815}
16816
16817/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016818 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016819 */
16820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016821f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016822{
Bram Moolenaar33570922005-01-25 22:26:29 +000016823 list_T *l;
16824 listitem_T *item, *item2;
16825 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016826 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016827 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016828 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016829 dict_T *d;
16830 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016831 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016832
Bram Moolenaar8c711452005-01-14 21:53:12 +000016833 if (argvars[0].v_type == VAR_DICT)
16834 {
16835 if (argvars[2].v_type != VAR_UNKNOWN)
16836 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016837 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016838 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016839 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016840 key = get_tv_string_chk(&argvars[1]);
16841 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016842 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016843 di = dict_find(d, key, -1);
16844 if (di == NULL)
16845 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016846 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16847 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016848 {
16849 *rettv = di->di_tv;
16850 init_tv(&di->di_tv);
16851 dictitem_remove(d, di);
16852 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016853 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016854 }
16855 }
16856 else if (argvars[0].v_type != VAR_LIST)
16857 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016858 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016859 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016860 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016861 int error = FALSE;
16862
16863 idx = get_tv_number_chk(&argvars[1], &error);
16864 if (error)
16865 ; /* type error: do nothing, errmsg already given */
16866 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016867 EMSGN(_(e_listidx), idx);
16868 else
16869 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016870 if (argvars[2].v_type == VAR_UNKNOWN)
16871 {
16872 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016873 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016874 *rettv = item->li_tv;
16875 vim_free(item);
16876 }
16877 else
16878 {
16879 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016880 end = get_tv_number_chk(&argvars[2], &error);
16881 if (error)
16882 ; /* type error: do nothing */
16883 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016884 EMSGN(_(e_listidx), end);
16885 else
16886 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016887 int cnt = 0;
16888
16889 for (li = item; li != NULL; li = li->li_next)
16890 {
16891 ++cnt;
16892 if (li == item2)
16893 break;
16894 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016895 if (li == NULL) /* didn't find "item2" after "item" */
16896 EMSG(_(e_invrange));
16897 else
16898 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016899 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016900 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016901 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016902 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016903 l->lv_first = item;
16904 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016905 item->li_prev = NULL;
16906 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016907 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016908 }
16909 }
16910 }
16911 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016912 }
16913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016914}
16915
16916/*
16917 * "rename({from}, {to})" function
16918 */
16919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016920f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016921{
16922 char_u buf[NUMBUFLEN];
16923
16924 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016925 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016926 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016927 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16928 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016929}
16930
16931/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016932 * "repeat()" function
16933 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016935f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016936{
16937 char_u *p;
16938 int n;
16939 int slen;
16940 int len;
16941 char_u *r;
16942 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016943
16944 n = get_tv_number(&argvars[1]);
16945 if (argvars[0].v_type == VAR_LIST)
16946 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016947 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016948 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016949 if (list_extend(rettv->vval.v_list,
16950 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016951 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016952 }
16953 else
16954 {
16955 p = get_tv_string(&argvars[0]);
16956 rettv->v_type = VAR_STRING;
16957 rettv->vval.v_string = NULL;
16958
16959 slen = (int)STRLEN(p);
16960 len = slen * n;
16961 if (len <= 0)
16962 return;
16963
16964 r = alloc(len + 1);
16965 if (r != NULL)
16966 {
16967 for (i = 0; i < n; i++)
16968 mch_memmove(r + i * slen, p, (size_t)slen);
16969 r[len] = NUL;
16970 }
16971
16972 rettv->vval.v_string = r;
16973 }
16974}
16975
16976/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016977 * "resolve()" function
16978 */
16979 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016980f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016981{
16982 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016983#ifdef HAVE_READLINK
16984 char_u *buf = NULL;
16985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016986
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016987 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016988#ifdef FEAT_SHORTCUT
16989 {
16990 char_u *v = NULL;
16991
16992 v = mch_resolve_shortcut(p);
16993 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016994 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016995 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016996 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016997 }
16998#else
16999# ifdef HAVE_READLINK
17000 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017001 char_u *cpy;
17002 int len;
17003 char_u *remain = NULL;
17004 char_u *q;
17005 int is_relative_to_current = FALSE;
17006 int has_trailing_pathsep = FALSE;
17007 int limit = 100;
17008
17009 p = vim_strsave(p);
17010
17011 if (p[0] == '.' && (vim_ispathsep(p[1])
17012 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17013 is_relative_to_current = TRUE;
17014
17015 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017016 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017017 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017018 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017019 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017021
17022 q = getnextcomp(p);
17023 if (*q != NUL)
17024 {
17025 /* Separate the first path component in "p", and keep the
17026 * remainder (beginning with the path separator). */
17027 remain = vim_strsave(q - 1);
17028 q[-1] = NUL;
17029 }
17030
Bram Moolenaard9462e32011-04-11 21:35:11 +020017031 buf = alloc(MAXPATHL + 1);
17032 if (buf == NULL)
17033 goto fail;
17034
Bram Moolenaar071d4272004-06-13 20:20:40 +000017035 for (;;)
17036 {
17037 for (;;)
17038 {
17039 len = readlink((char *)p, (char *)buf, MAXPATHL);
17040 if (len <= 0)
17041 break;
17042 buf[len] = NUL;
17043
17044 if (limit-- == 0)
17045 {
17046 vim_free(p);
17047 vim_free(remain);
17048 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017049 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017050 goto fail;
17051 }
17052
17053 /* Ensure that the result will have a trailing path separator
17054 * if the argument has one. */
17055 if (remain == NULL && has_trailing_pathsep)
17056 add_pathsep(buf);
17057
17058 /* Separate the first path component in the link value and
17059 * concatenate the remainders. */
17060 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17061 if (*q != NUL)
17062 {
17063 if (remain == NULL)
17064 remain = vim_strsave(q - 1);
17065 else
17066 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017067 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017068 if (cpy != NULL)
17069 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017070 vim_free(remain);
17071 remain = cpy;
17072 }
17073 }
17074 q[-1] = NUL;
17075 }
17076
17077 q = gettail(p);
17078 if (q > p && *q == NUL)
17079 {
17080 /* Ignore trailing path separator. */
17081 q[-1] = NUL;
17082 q = gettail(p);
17083 }
17084 if (q > p && !mch_isFullName(buf))
17085 {
17086 /* symlink is relative to directory of argument */
17087 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17088 if (cpy != NULL)
17089 {
17090 STRCPY(cpy, p);
17091 STRCPY(gettail(cpy), buf);
17092 vim_free(p);
17093 p = cpy;
17094 }
17095 }
17096 else
17097 {
17098 vim_free(p);
17099 p = vim_strsave(buf);
17100 }
17101 }
17102
17103 if (remain == NULL)
17104 break;
17105
17106 /* Append the first path component of "remain" to "p". */
17107 q = getnextcomp(remain + 1);
17108 len = q - remain - (*q != NUL);
17109 cpy = vim_strnsave(p, STRLEN(p) + len);
17110 if (cpy != NULL)
17111 {
17112 STRNCAT(cpy, remain, len);
17113 vim_free(p);
17114 p = cpy;
17115 }
17116 /* Shorten "remain". */
17117 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017118 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017119 else
17120 {
17121 vim_free(remain);
17122 remain = NULL;
17123 }
17124 }
17125
17126 /* If the result is a relative path name, make it explicitly relative to
17127 * the current directory if and only if the argument had this form. */
17128 if (!vim_ispathsep(*p))
17129 {
17130 if (is_relative_to_current
17131 && *p != NUL
17132 && !(p[0] == '.'
17133 && (p[1] == NUL
17134 || vim_ispathsep(p[1])
17135 || (p[1] == '.'
17136 && (p[2] == NUL
17137 || vim_ispathsep(p[2]))))))
17138 {
17139 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017140 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017141 if (cpy != NULL)
17142 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017143 vim_free(p);
17144 p = cpy;
17145 }
17146 }
17147 else if (!is_relative_to_current)
17148 {
17149 /* Strip leading "./". */
17150 q = p;
17151 while (q[0] == '.' && vim_ispathsep(q[1]))
17152 q += 2;
17153 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017154 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017155 }
17156 }
17157
17158 /* Ensure that the result will have no trailing path separator
17159 * if the argument had none. But keep "/" or "//". */
17160 if (!has_trailing_pathsep)
17161 {
17162 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017163 if (after_pathsep(p, q))
17164 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017165 }
17166
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017167 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017168 }
17169# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017170 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017171# endif
17172#endif
17173
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017174 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017175
17176#ifdef HAVE_READLINK
17177fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017178 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017179#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017180 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017181}
17182
17183/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017184 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017185 */
17186 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017187f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017188{
Bram Moolenaar33570922005-01-25 22:26:29 +000017189 list_T *l;
17190 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017191
Bram Moolenaar0d660222005-01-07 21:51:51 +000017192 if (argvars[0].v_type != VAR_LIST)
17193 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017194 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017195 && !tv_check_lock(l->lv_lock,
17196 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017197 {
17198 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017199 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017200 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017201 while (li != NULL)
17202 {
17203 ni = li->li_prev;
17204 list_append(l, li);
17205 li = ni;
17206 }
17207 rettv->vval.v_list = l;
17208 rettv->v_type = VAR_LIST;
17209 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017210 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017212}
17213
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017214#define SP_NOMOVE 0x01 /* don't move cursor */
17215#define SP_REPEAT 0x02 /* repeat to find outer pair */
17216#define SP_RETCOUNT 0x04 /* return matchcount */
17217#define SP_SETPCMARK 0x08 /* set previous context mark */
17218#define SP_START 0x10 /* accept match at start position */
17219#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17220#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017221#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017222
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017223static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017224
17225/*
17226 * Get flags for a search function.
17227 * Possibly sets "p_ws".
17228 * Returns BACKWARD, FORWARD or zero (for an error).
17229 */
17230 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017231get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017232{
17233 int dir = FORWARD;
17234 char_u *flags;
17235 char_u nbuf[NUMBUFLEN];
17236 int mask;
17237
17238 if (varp->v_type != VAR_UNKNOWN)
17239 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017240 flags = get_tv_string_buf_chk(varp, nbuf);
17241 if (flags == NULL)
17242 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017243 while (*flags != NUL)
17244 {
17245 switch (*flags)
17246 {
17247 case 'b': dir = BACKWARD; break;
17248 case 'w': p_ws = TRUE; break;
17249 case 'W': p_ws = FALSE; break;
17250 default: mask = 0;
17251 if (flagsp != NULL)
17252 switch (*flags)
17253 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017254 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017255 case 'e': mask = SP_END; break;
17256 case 'm': mask = SP_RETCOUNT; break;
17257 case 'n': mask = SP_NOMOVE; break;
17258 case 'p': mask = SP_SUBPAT; break;
17259 case 'r': mask = SP_REPEAT; break;
17260 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017261 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017262 }
17263 if (mask == 0)
17264 {
17265 EMSG2(_(e_invarg2), flags);
17266 dir = 0;
17267 }
17268 else
17269 *flagsp |= mask;
17270 }
17271 if (dir == 0)
17272 break;
17273 ++flags;
17274 }
17275 }
17276 return dir;
17277}
17278
Bram Moolenaar071d4272004-06-13 20:20:40 +000017279/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017280 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017281 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017282 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017283search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017284{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017285 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017286 char_u *pat;
17287 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017288 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017289 int save_p_ws = p_ws;
17290 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017291 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017292 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017293 proftime_T tm;
17294#ifdef FEAT_RELTIME
17295 long time_limit = 0;
17296#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017297 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017298 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017299
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017300 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017301 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017302 if (dir == 0)
17303 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017304 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017305 if (flags & SP_START)
17306 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017307 if (flags & SP_END)
17308 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017309 if (flags & SP_COLUMN)
17310 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017311
Bram Moolenaar76929292008-01-06 19:07:36 +000017312 /* Optional arguments: line number to stop searching and timeout. */
17313 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017314 {
17315 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17316 if (lnum_stop < 0)
17317 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017318#ifdef FEAT_RELTIME
17319 if (argvars[3].v_type != VAR_UNKNOWN)
17320 {
17321 time_limit = get_tv_number_chk(&argvars[3], NULL);
17322 if (time_limit < 0)
17323 goto theend;
17324 }
17325#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017326 }
17327
Bram Moolenaar76929292008-01-06 19:07:36 +000017328#ifdef FEAT_RELTIME
17329 /* Set the time limit, if there is one. */
17330 profile_setlimit(time_limit, &tm);
17331#endif
17332
Bram Moolenaar231334e2005-07-25 20:46:57 +000017333 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017334 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017335 * Check to make sure only those flags are set.
17336 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17337 * flags cannot be set. Check for that condition also.
17338 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017339 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017340 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017341 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017342 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017343 goto theend;
17344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017345
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017346 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017347 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017348 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017349 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017350 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017351 if (flags & SP_SUBPAT)
17352 retval = subpatnum;
17353 else
17354 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017355 if (flags & SP_SETPCMARK)
17356 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017357 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017358 if (match_pos != NULL)
17359 {
17360 /* Store the match cursor position */
17361 match_pos->lnum = pos.lnum;
17362 match_pos->col = pos.col + 1;
17363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017364 /* "/$" will put the cursor after the end of the line, may need to
17365 * correct that here */
17366 check_cursor();
17367 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017368
17369 /* If 'n' flag is used: restore cursor position. */
17370 if (flags & SP_NOMOVE)
17371 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017372 else
17373 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017374theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017375 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017376
17377 return retval;
17378}
17379
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017380#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017381
17382/*
17383 * round() is not in C90, use ceil() or floor() instead.
17384 */
17385 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017386vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017387{
17388 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17389}
17390
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017391/*
17392 * "round({float})" function
17393 */
17394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017395f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017396{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017397 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017398
17399 rettv->v_type = VAR_FLOAT;
17400 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017401 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017402 else
17403 rettv->vval.v_float = 0.0;
17404}
17405#endif
17406
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017407/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017408 * "screenattr()" function
17409 */
17410 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017411f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017412{
17413 int row;
17414 int col;
17415 int c;
17416
17417 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17418 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17419 if (row < 0 || row >= screen_Rows
17420 || col < 0 || col >= screen_Columns)
17421 c = -1;
17422 else
17423 c = ScreenAttrs[LineOffset[row] + col];
17424 rettv->vval.v_number = c;
17425}
17426
17427/*
17428 * "screenchar()" function
17429 */
17430 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017431f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017432{
17433 int row;
17434 int col;
17435 int off;
17436 int c;
17437
17438 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17439 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17440 if (row < 0 || row >= screen_Rows
17441 || col < 0 || col >= screen_Columns)
17442 c = -1;
17443 else
17444 {
17445 off = LineOffset[row] + col;
17446#ifdef FEAT_MBYTE
17447 if (enc_utf8 && ScreenLinesUC[off] != 0)
17448 c = ScreenLinesUC[off];
17449 else
17450#endif
17451 c = ScreenLines[off];
17452 }
17453 rettv->vval.v_number = c;
17454}
17455
17456/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017457 * "screencol()" function
17458 *
17459 * First column is 1 to be consistent with virtcol().
17460 */
17461 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017462f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017463{
17464 rettv->vval.v_number = screen_screencol() + 1;
17465}
17466
17467/*
17468 * "screenrow()" function
17469 */
17470 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017471f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017472{
17473 rettv->vval.v_number = screen_screenrow() + 1;
17474}
17475
17476/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017477 * "search()" function
17478 */
17479 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017480f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017481{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017482 int flags = 0;
17483
17484 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017485}
17486
Bram Moolenaar071d4272004-06-13 20:20:40 +000017487/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017488 * "searchdecl()" function
17489 */
17490 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017491f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017492{
17493 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017494 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017495 int error = FALSE;
17496 char_u *name;
17497
17498 rettv->vval.v_number = 1; /* default: FAIL */
17499
17500 name = get_tv_string_chk(&argvars[0]);
17501 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017502 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017503 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017504 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17505 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17506 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017507 if (!error && name != NULL)
17508 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017509 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017510}
17511
17512/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017513 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017514 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017515 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017516searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017517{
17518 char_u *spat, *mpat, *epat;
17519 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017520 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017521 int dir;
17522 int flags = 0;
17523 char_u nbuf1[NUMBUFLEN];
17524 char_u nbuf2[NUMBUFLEN];
17525 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017526 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017527 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017528 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017529
Bram Moolenaar071d4272004-06-13 20:20:40 +000017530 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017531 spat = get_tv_string_chk(&argvars[0]);
17532 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17533 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17534 if (spat == NULL || mpat == NULL || epat == NULL)
17535 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017536
Bram Moolenaar071d4272004-06-13 20:20:40 +000017537 /* Handle the optional fourth argument: flags */
17538 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017539 if (dir == 0)
17540 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017541
17542 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017543 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17544 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017545 if ((flags & (SP_END | SP_SUBPAT)) != 0
17546 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017547 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017548 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017549 goto theend;
17550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017551
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017552 /* Using 'r' implies 'W', otherwise it doesn't work. */
17553 if (flags & SP_REPEAT)
17554 p_ws = FALSE;
17555
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017556 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017557 if (argvars[3].v_type == VAR_UNKNOWN
17558 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017559 skip = (char_u *)"";
17560 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017561 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017562 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017563 if (argvars[5].v_type != VAR_UNKNOWN)
17564 {
17565 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17566 if (lnum_stop < 0)
17567 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017568#ifdef FEAT_RELTIME
17569 if (argvars[6].v_type != VAR_UNKNOWN)
17570 {
17571 time_limit = get_tv_number_chk(&argvars[6], NULL);
17572 if (time_limit < 0)
17573 goto theend;
17574 }
17575#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017576 }
17577 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017578 if (skip == NULL)
17579 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017580
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017581 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017582 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017583
17584theend:
17585 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017586
17587 return retval;
17588}
17589
17590/*
17591 * "searchpair()" function
17592 */
17593 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017594f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017595{
17596 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17597}
17598
17599/*
17600 * "searchpairpos()" function
17601 */
17602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017603f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017604{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017605 pos_T match_pos;
17606 int lnum = 0;
17607 int col = 0;
17608
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017609 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017610 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017611
17612 if (searchpair_cmn(argvars, &match_pos) > 0)
17613 {
17614 lnum = match_pos.lnum;
17615 col = match_pos.col;
17616 }
17617
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017618 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17619 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017620}
17621
17622/*
17623 * Search for a start/middle/end thing.
17624 * Used by searchpair(), see its documentation for the details.
17625 * Returns 0 or -1 for no match,
17626 */
17627 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017628do_searchpair(
17629 char_u *spat, /* start pattern */
17630 char_u *mpat, /* middle pattern */
17631 char_u *epat, /* end pattern */
17632 int dir, /* BACKWARD or FORWARD */
17633 char_u *skip, /* skip expression */
17634 int flags, /* SP_SETPCMARK and other SP_ values */
17635 pos_T *match_pos,
17636 linenr_T lnum_stop, /* stop at this line if not zero */
17637 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017638{
17639 char_u *save_cpo;
17640 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17641 long retval = 0;
17642 pos_T pos;
17643 pos_T firstpos;
17644 pos_T foundpos;
17645 pos_T save_cursor;
17646 pos_T save_pos;
17647 int n;
17648 int r;
17649 int nest = 1;
17650 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017651 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017652 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017653
17654 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17655 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017656 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017657
Bram Moolenaar76929292008-01-06 19:07:36 +000017658#ifdef FEAT_RELTIME
17659 /* Set the time limit, if there is one. */
17660 profile_setlimit(time_limit, &tm);
17661#endif
17662
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017663 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17664 * start/middle/end (pat3, for the top pair). */
17665 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17666 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17667 if (pat2 == NULL || pat3 == NULL)
17668 goto theend;
17669 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17670 if (*mpat == NUL)
17671 STRCPY(pat3, pat2);
17672 else
17673 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17674 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017675 if (flags & SP_START)
17676 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017677
Bram Moolenaar071d4272004-06-13 20:20:40 +000017678 save_cursor = curwin->w_cursor;
17679 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017680 clearpos(&firstpos);
17681 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017682 pat = pat3;
17683 for (;;)
17684 {
17685 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017686 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017687 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17688 /* didn't find it or found the first match again: FAIL */
17689 break;
17690
17691 if (firstpos.lnum == 0)
17692 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017693 if (equalpos(pos, foundpos))
17694 {
17695 /* Found the same position again. Can happen with a pattern that
17696 * has "\zs" at the end and searching backwards. Advance one
17697 * character and try again. */
17698 if (dir == BACKWARD)
17699 decl(&pos);
17700 else
17701 incl(&pos);
17702 }
17703 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017704
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017705 /* clear the start flag to avoid getting stuck here */
17706 options &= ~SEARCH_START;
17707
Bram Moolenaar071d4272004-06-13 20:20:40 +000017708 /* If the skip pattern matches, ignore this match. */
17709 if (*skip != NUL)
17710 {
17711 save_pos = curwin->w_cursor;
17712 curwin->w_cursor = pos;
17713 r = eval_to_bool(skip, &err, NULL, FALSE);
17714 curwin->w_cursor = save_pos;
17715 if (err)
17716 {
17717 /* Evaluating {skip} caused an error, break here. */
17718 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017719 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017720 break;
17721 }
17722 if (r)
17723 continue;
17724 }
17725
17726 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17727 {
17728 /* Found end when searching backwards or start when searching
17729 * forward: nested pair. */
17730 ++nest;
17731 pat = pat2; /* nested, don't search for middle */
17732 }
17733 else
17734 {
17735 /* Found end when searching forward or start when searching
17736 * backward: end of (nested) pair; or found middle in outer pair. */
17737 if (--nest == 1)
17738 pat = pat3; /* outer level, search for middle */
17739 }
17740
17741 if (nest == 0)
17742 {
17743 /* Found the match: return matchcount or line number. */
17744 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017745 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017746 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017747 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017748 if (flags & SP_SETPCMARK)
17749 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017750 curwin->w_cursor = pos;
17751 if (!(flags & SP_REPEAT))
17752 break;
17753 nest = 1; /* search for next unmatched */
17754 }
17755 }
17756
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017757 if (match_pos != NULL)
17758 {
17759 /* Store the match cursor position */
17760 match_pos->lnum = curwin->w_cursor.lnum;
17761 match_pos->col = curwin->w_cursor.col + 1;
17762 }
17763
Bram Moolenaar071d4272004-06-13 20:20:40 +000017764 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017765 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017766 curwin->w_cursor = save_cursor;
17767
17768theend:
17769 vim_free(pat2);
17770 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017771 if (p_cpo == empty_option)
17772 p_cpo = save_cpo;
17773 else
17774 /* Darn, evaluating the {skip} expression changed the value. */
17775 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017776
17777 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017778}
17779
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017780/*
17781 * "searchpos()" function
17782 */
17783 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017784f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017785{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017786 pos_T match_pos;
17787 int lnum = 0;
17788 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017789 int n;
17790 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017791
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017792 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017793 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017794
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017795 n = search_cmn(argvars, &match_pos, &flags);
17796 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017797 {
17798 lnum = match_pos.lnum;
17799 col = match_pos.col;
17800 }
17801
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017802 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17803 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017804 if (flags & SP_SUBPAT)
17805 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017806}
17807
Bram Moolenaar0d660222005-01-07 21:51:51 +000017808 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017809f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017810{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017811#ifdef FEAT_CLIENTSERVER
17812 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017813 char_u *server = get_tv_string_chk(&argvars[0]);
17814 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017815
Bram Moolenaar0d660222005-01-07 21:51:51 +000017816 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017817 if (server == NULL || reply == NULL)
17818 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017819 if (check_restricted() || check_secure())
17820 return;
17821# ifdef FEAT_X11
17822 if (check_connection() == FAIL)
17823 return;
17824# endif
17825
17826 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017827 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017828 EMSG(_("E258: Unable to send to client"));
17829 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017830 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017831 rettv->vval.v_number = 0;
17832#else
17833 rettv->vval.v_number = -1;
17834#endif
17835}
17836
Bram Moolenaar0d660222005-01-07 21:51:51 +000017837 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017838f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017839{
17840 char_u *r = NULL;
17841
17842#ifdef FEAT_CLIENTSERVER
17843# ifdef WIN32
17844 r = serverGetVimNames();
17845# else
17846 make_connection();
17847 if (X_DISPLAY != NULL)
17848 r = serverGetVimNames(X_DISPLAY);
17849# endif
17850#endif
17851 rettv->v_type = VAR_STRING;
17852 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017853}
17854
17855/*
17856 * "setbufvar()" function
17857 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017858 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017859f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017860{
17861 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017862 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017863 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017864 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017865 char_u nbuf[NUMBUFLEN];
17866
17867 if (check_restricted() || check_secure())
17868 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017869 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17870 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017871 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017872 varp = &argvars[2];
17873
17874 if (buf != NULL && varname != NULL && varp != NULL)
17875 {
17876 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017877 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017878
17879 if (*varname == '&')
17880 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017881 long numval;
17882 char_u *strval;
17883 int error = FALSE;
17884
Bram Moolenaar071d4272004-06-13 20:20:40 +000017885 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017886 numval = get_tv_number_chk(varp, &error);
17887 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017888 if (!error && strval != NULL)
17889 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017890 }
17891 else
17892 {
17893 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17894 if (bufvarname != NULL)
17895 {
17896 STRCPY(bufvarname, "b:");
17897 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017898 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017899 vim_free(bufvarname);
17900 }
17901 }
17902
17903 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017904 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017906}
17907
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017908 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017909f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017910{
17911 dict_T *d;
17912 dictitem_T *di;
17913 char_u *csearch;
17914
17915 if (argvars[0].v_type != VAR_DICT)
17916 {
17917 EMSG(_(e_dictreq));
17918 return;
17919 }
17920
17921 if ((d = argvars[0].vval.v_dict) != NULL)
17922 {
17923 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17924 if (csearch != NULL)
17925 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017926#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017927 if (enc_utf8)
17928 {
17929 int pcc[MAX_MCO];
17930 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017931
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017932 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17933 }
17934 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017935#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017936 set_last_csearch(PTR2CHAR(csearch),
17937 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017938 }
17939
17940 di = dict_find(d, (char_u *)"forward", -1);
17941 if (di != NULL)
17942 set_csearch_direction(get_tv_number(&di->di_tv)
17943 ? FORWARD : BACKWARD);
17944
17945 di = dict_find(d, (char_u *)"until", -1);
17946 if (di != NULL)
17947 set_csearch_until(!!get_tv_number(&di->di_tv));
17948 }
17949}
17950
Bram Moolenaar071d4272004-06-13 20:20:40 +000017951/*
17952 * "setcmdpos()" function
17953 */
17954 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017955f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017956{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017957 int pos = (int)get_tv_number(&argvars[0]) - 1;
17958
17959 if (pos >= 0)
17960 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017961}
17962
17963/*
Bram Moolenaar80492532016-03-08 17:08:53 +010017964 * "setfperm({fname}, {mode})" function
17965 */
17966 static void
17967f_setfperm(typval_T *argvars, typval_T *rettv)
17968{
17969 char_u *fname;
17970 char_u modebuf[NUMBUFLEN];
17971 char_u *mode_str;
17972 int i;
17973 int mask;
17974 int mode = 0;
17975
17976 rettv->vval.v_number = 0;
17977 fname = get_tv_string_chk(&argvars[0]);
17978 if (fname == NULL)
17979 return;
17980 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
17981 if (mode_str == NULL)
17982 return;
17983 if (STRLEN(mode_str) != 9)
17984 {
17985 EMSG2(_(e_invarg2), mode_str);
17986 return;
17987 }
17988
17989 mask = 1;
17990 for (i = 8; i >= 0; --i)
17991 {
17992 if (mode_str[i] != '-')
17993 mode |= mask;
17994 mask = mask << 1;
17995 }
17996 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
17997}
17998
17999/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018000 * "setline()" function
18001 */
18002 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018003f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018004{
18005 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018006 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018007 list_T *l = NULL;
18008 listitem_T *li = NULL;
18009 long added = 0;
18010 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018011
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018012 lnum = get_tv_lnum(&argvars[0]);
18013 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018014 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018015 l = argvars[1].vval.v_list;
18016 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018017 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018018 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018019 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018020
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018021 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018022 for (;;)
18023 {
18024 if (l != NULL)
18025 {
18026 /* list argument, get next string */
18027 if (li == NULL)
18028 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018029 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018030 li = li->li_next;
18031 }
18032
18033 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018034 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018035 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018036
18037 /* When coming here from Insert mode, sync undo, so that this can be
18038 * undone separately from what was previously inserted. */
18039 if (u_sync_once == 2)
18040 {
18041 u_sync_once = 1; /* notify that u_sync() was called */
18042 u_sync(TRUE);
18043 }
18044
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018045 if (lnum <= curbuf->b_ml.ml_line_count)
18046 {
18047 /* existing line, replace it */
18048 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18049 {
18050 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018051 if (lnum == curwin->w_cursor.lnum)
18052 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018053 rettv->vval.v_number = 0; /* OK */
18054 }
18055 }
18056 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18057 {
18058 /* lnum is one past the last line, append the line */
18059 ++added;
18060 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18061 rettv->vval.v_number = 0; /* OK */
18062 }
18063
18064 if (l == NULL) /* only one string argument */
18065 break;
18066 ++lnum;
18067 }
18068
18069 if (added > 0)
18070 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018071}
18072
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018073static 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 +000018074
Bram Moolenaar071d4272004-06-13 20:20:40 +000018075/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018076 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018077 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018078 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018079set_qf_ll_list(
18080 win_T *wp UNUSED,
18081 typval_T *list_arg UNUSED,
18082 typval_T *action_arg UNUSED,
18083 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018084{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018085#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018086 char_u *act;
18087 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018088#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018089
Bram Moolenaar2641f772005-03-25 21:58:17 +000018090 rettv->vval.v_number = -1;
18091
18092#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018093 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018094 EMSG(_(e_listreq));
18095 else
18096 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018097 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018098
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018099 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018100 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018101 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018102 if (act == NULL)
18103 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018104 if (*act == 'a' || *act == 'r')
18105 action = *act;
18106 }
18107
Bram Moolenaar81484f42012-12-05 15:16:47 +010018108 if (l != NULL && set_errorlist(wp, l, action,
18109 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018110 rettv->vval.v_number = 0;
18111 }
18112#endif
18113}
18114
18115/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018116 * "setloclist()" function
18117 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018118 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018119f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018120{
18121 win_T *win;
18122
18123 rettv->vval.v_number = -1;
18124
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018125 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018126 if (win != NULL)
18127 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18128}
18129
18130/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018131 * "setmatches()" function
18132 */
18133 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018134f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018135{
18136#ifdef FEAT_SEARCH_EXTRA
18137 list_T *l;
18138 listitem_T *li;
18139 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018140 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018141
18142 rettv->vval.v_number = -1;
18143 if (argvars[0].v_type != VAR_LIST)
18144 {
18145 EMSG(_(e_listreq));
18146 return;
18147 }
18148 if ((l = argvars[0].vval.v_list) != NULL)
18149 {
18150
18151 /* To some extent make sure that we are dealing with a list from
18152 * "getmatches()". */
18153 li = l->lv_first;
18154 while (li != NULL)
18155 {
18156 if (li->li_tv.v_type != VAR_DICT
18157 || (d = li->li_tv.vval.v_dict) == NULL)
18158 {
18159 EMSG(_(e_invarg));
18160 return;
18161 }
18162 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018163 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18164 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018165 && dict_find(d, (char_u *)"priority", -1) != NULL
18166 && dict_find(d, (char_u *)"id", -1) != NULL))
18167 {
18168 EMSG(_(e_invarg));
18169 return;
18170 }
18171 li = li->li_next;
18172 }
18173
18174 clear_matches(curwin);
18175 li = l->lv_first;
18176 while (li != NULL)
18177 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018178 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018179 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018180 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018181 char_u *group;
18182 int priority;
18183 int id;
18184 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018185
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018186 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018187 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18188 {
18189 if (s == NULL)
18190 {
18191 s = list_alloc();
18192 if (s == NULL)
18193 return;
18194 }
18195
18196 /* match from matchaddpos() */
18197 for (i = 1; i < 9; i++)
18198 {
18199 sprintf((char *)buf, (char *)"pos%d", i);
18200 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18201 {
18202 if (di->di_tv.v_type != VAR_LIST)
18203 return;
18204
18205 list_append_tv(s, &di->di_tv);
18206 s->lv_refcount++;
18207 }
18208 else
18209 break;
18210 }
18211 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018212
18213 group = get_dict_string(d, (char_u *)"group", FALSE);
18214 priority = (int)get_dict_number(d, (char_u *)"priority");
18215 id = (int)get_dict_number(d, (char_u *)"id");
18216 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18217 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18218 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018219 if (i == 0)
18220 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018221 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018222 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018223 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018224 }
18225 else
18226 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018227 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018228 list_unref(s);
18229 s = NULL;
18230 }
18231
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018232 li = li->li_next;
18233 }
18234 rettv->vval.v_number = 0;
18235 }
18236#endif
18237}
18238
18239/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018240 * "setpos()" function
18241 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018243f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018244{
18245 pos_T pos;
18246 int fnum;
18247 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018248 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018249
Bram Moolenaar08250432008-02-13 11:42:46 +000018250 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018251 name = get_tv_string_chk(argvars);
18252 if (name != NULL)
18253 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018254 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018255 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018256 if (--pos.col < 0)
18257 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018258 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018259 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018260 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018261 if (fnum == curbuf->b_fnum)
18262 {
18263 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018264 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018265 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018266 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018267 curwin->w_set_curswant = FALSE;
18268 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018269 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018270 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018271 }
18272 else
18273 EMSG(_(e_invarg));
18274 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018275 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18276 {
18277 /* set mark */
18278 if (setmark_pos(name[1], &pos, fnum) == OK)
18279 rettv->vval.v_number = 0;
18280 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018281 else
18282 EMSG(_(e_invarg));
18283 }
18284 }
18285}
18286
18287/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018288 * "setqflist()" function
18289 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018290 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018291f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018292{
18293 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18294}
18295
18296/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018297 * "setreg()" function
18298 */
18299 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018300f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018301{
18302 int regname;
18303 char_u *strregname;
18304 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018305 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018306 int append;
18307 char_u yank_type;
18308 long block_len;
18309
18310 block_len = -1;
18311 yank_type = MAUTO;
18312 append = FALSE;
18313
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018314 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018315 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018316
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018317 if (strregname == NULL)
18318 return; /* type error; errmsg already given */
18319 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018320 if (regname == 0 || regname == '@')
18321 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018322
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018323 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018324 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018325 stropt = get_tv_string_chk(&argvars[2]);
18326 if (stropt == NULL)
18327 return; /* type error */
18328 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018329 switch (*stropt)
18330 {
18331 case 'a': case 'A': /* append */
18332 append = TRUE;
18333 break;
18334 case 'v': case 'c': /* character-wise selection */
18335 yank_type = MCHAR;
18336 break;
18337 case 'V': case 'l': /* line-wise selection */
18338 yank_type = MLINE;
18339 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018340 case 'b': case Ctrl_V: /* block-wise selection */
18341 yank_type = MBLOCK;
18342 if (VIM_ISDIGIT(stropt[1]))
18343 {
18344 ++stropt;
18345 block_len = getdigits(&stropt) - 1;
18346 --stropt;
18347 }
18348 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018349 }
18350 }
18351
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018352 if (argvars[1].v_type == VAR_LIST)
18353 {
18354 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018355 char_u **allocval;
18356 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018357 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018358 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018359 int len = argvars[1].vval.v_list->lv_len;
18360 listitem_T *li;
18361
Bram Moolenaar7d647822014-04-05 21:28:56 +020018362 /* First half: use for pointers to result lines; second half: use for
18363 * pointers to allocated copies. */
18364 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018365 if (lstval == NULL)
18366 return;
18367 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018368 allocval = lstval + len + 2;
18369 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018370
18371 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18372 li = li->li_next)
18373 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018374 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018375 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018376 goto free_lstval;
18377 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018378 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018379 /* Need to make a copy, next get_tv_string_buf_chk() will
18380 * overwrite the string. */
18381 strval = vim_strsave(buf);
18382 if (strval == NULL)
18383 goto free_lstval;
18384 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018385 }
18386 *curval++ = strval;
18387 }
18388 *curval++ = NULL;
18389
18390 write_reg_contents_lst(regname, lstval, -1,
18391 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018392free_lstval:
18393 while (curallocval > allocval)
18394 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018395 vim_free(lstval);
18396 }
18397 else
18398 {
18399 strval = get_tv_string_chk(&argvars[1]);
18400 if (strval == NULL)
18401 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018402 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018403 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018404 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018405 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018406}
18407
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018408/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018409 * "settabvar()" function
18410 */
18411 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018412f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018413{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018414#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018415 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018416 tabpage_T *tp;
18417#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018418 char_u *varname, *tabvarname;
18419 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018420
18421 rettv->vval.v_number = 0;
18422
18423 if (check_restricted() || check_secure())
18424 return;
18425
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018426#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018427 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018428#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018429 varname = get_tv_string_chk(&argvars[1]);
18430 varp = &argvars[2];
18431
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018432 if (varname != NULL && varp != NULL
18433#ifdef FEAT_WINDOWS
18434 && tp != NULL
18435#endif
18436 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018437 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018438#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018439 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018440 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018441#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018442
18443 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18444 if (tabvarname != NULL)
18445 {
18446 STRCPY(tabvarname, "t:");
18447 STRCPY(tabvarname + 2, varname);
18448 set_var(tabvarname, varp, TRUE);
18449 vim_free(tabvarname);
18450 }
18451
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018452#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018453 /* Restore current tabpage */
18454 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018455 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018456#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018457 }
18458}
18459
18460/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018461 * "settabwinvar()" function
18462 */
18463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018464f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018465{
18466 setwinvar(argvars, rettv, 1);
18467}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018468
18469/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018470 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018471 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018473f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018474{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018475 setwinvar(argvars, rettv, 0);
18476}
18477
18478/*
18479 * "setwinvar()" and "settabwinvar()" functions
18480 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018481
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018482 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018483setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018484{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018485 win_T *win;
18486#ifdef FEAT_WINDOWS
18487 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018488 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018489 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018490#endif
18491 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018492 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018493 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018494 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018495
18496 if (check_restricted() || check_secure())
18497 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018498
18499#ifdef FEAT_WINDOWS
18500 if (off == 1)
18501 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18502 else
18503 tp = curtab;
18504#endif
18505 win = find_win_by_nr(&argvars[off], tp);
18506 varname = get_tv_string_chk(&argvars[off + 1]);
18507 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018508
18509 if (win != NULL && varname != NULL && varp != NULL)
18510 {
18511#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018512 need_switch_win = !(tp == curtab && win == curwin);
18513 if (!need_switch_win
18514 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018515#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018516 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018517 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018518 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018519 long numval;
18520 char_u *strval;
18521 int error = FALSE;
18522
18523 ++varname;
18524 numval = get_tv_number_chk(varp, &error);
18525 strval = get_tv_string_buf_chk(varp, nbuf);
18526 if (!error && strval != NULL)
18527 set_option_value(varname, numval, strval, OPT_LOCAL);
18528 }
18529 else
18530 {
18531 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18532 if (winvarname != NULL)
18533 {
18534 STRCPY(winvarname, "w:");
18535 STRCPY(winvarname + 2, varname);
18536 set_var(winvarname, varp, TRUE);
18537 vim_free(winvarname);
18538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018539 }
18540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018541#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018542 if (need_switch_win)
18543 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018544#endif
18545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018546}
18547
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018548#ifdef FEAT_CRYPT
18549/*
18550 * "sha256({string})" function
18551 */
18552 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018553f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018554{
18555 char_u *p;
18556
18557 p = get_tv_string(&argvars[0]);
18558 rettv->vval.v_string = vim_strsave(
18559 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18560 rettv->v_type = VAR_STRING;
18561}
18562#endif /* FEAT_CRYPT */
18563
Bram Moolenaar071d4272004-06-13 20:20:40 +000018564/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018565 * "shellescape({string})" function
18566 */
18567 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018568f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018569{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018570 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018571 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018572 rettv->v_type = VAR_STRING;
18573}
18574
18575/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018576 * shiftwidth() function
18577 */
18578 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018579f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018580{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018581 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018582}
18583
18584/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018585 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018586 */
18587 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018588f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018589{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018590 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018591
Bram Moolenaar0d660222005-01-07 21:51:51 +000018592 p = get_tv_string(&argvars[0]);
18593 rettv->vval.v_string = vim_strsave(p);
18594 simplify_filename(rettv->vval.v_string); /* simplify in place */
18595 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018596}
18597
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018598#ifdef FEAT_FLOAT
18599/*
18600 * "sin()" function
18601 */
18602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018603f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018604{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018605 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018606
18607 rettv->v_type = VAR_FLOAT;
18608 if (get_float_arg(argvars, &f) == OK)
18609 rettv->vval.v_float = sin(f);
18610 else
18611 rettv->vval.v_float = 0.0;
18612}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018613
18614/*
18615 * "sinh()" function
18616 */
18617 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018618f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018619{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018620 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018621
18622 rettv->v_type = VAR_FLOAT;
18623 if (get_float_arg(argvars, &f) == OK)
18624 rettv->vval.v_float = sinh(f);
18625 else
18626 rettv->vval.v_float = 0.0;
18627}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018628#endif
18629
Bram Moolenaar0d660222005-01-07 21:51:51 +000018630static int
18631#ifdef __BORLANDC__
18632 _RTLENTRYF
18633#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018634 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018635static int
18636#ifdef __BORLANDC__
18637 _RTLENTRYF
18638#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018639 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018640
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018641/* struct used in the array that's given to qsort() */
18642typedef struct
18643{
18644 listitem_T *item;
18645 int idx;
18646} sortItem_T;
18647
Bram Moolenaar0b962472016-02-22 22:51:33 +010018648/* struct storing information about current sort */
18649typedef struct
18650{
18651 int item_compare_ic;
18652 int item_compare_numeric;
18653 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018654#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018655 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018656#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018657 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018658 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018659 dict_T *item_compare_selfdict;
18660 int item_compare_func_err;
18661 int item_compare_keep_zero;
18662} sortinfo_T;
18663static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018664static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018665#define ITEM_COMPARE_FAIL 999
18666
Bram Moolenaar071d4272004-06-13 20:20:40 +000018667/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018668 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018669 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018670 static int
18671#ifdef __BORLANDC__
18672_RTLENTRYF
18673#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018674item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018675{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018676 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018677 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018678 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018679 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018680 int res;
18681 char_u numbuf1[NUMBUFLEN];
18682 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018683
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018684 si1 = (sortItem_T *)s1;
18685 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018686 tv1 = &si1->item->li_tv;
18687 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018688
Bram Moolenaar0b962472016-02-22 22:51:33 +010018689 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018690 {
18691 long v1 = get_tv_number(tv1);
18692 long v2 = get_tv_number(tv2);
18693
18694 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18695 }
18696
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018697#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018698 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018699 {
18700 float_T v1 = get_tv_float(tv1);
18701 float_T v2 = get_tv_float(tv2);
18702
18703 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18704 }
18705#endif
18706
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018707 /* tv2string() puts quotes around a string and allocates memory. Don't do
18708 * that for string variables. Use a single quote when comparing with a
18709 * non-string to do what the docs promise. */
18710 if (tv1->v_type == VAR_STRING)
18711 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018712 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018713 p1 = (char_u *)"'";
18714 else
18715 p1 = tv1->vval.v_string;
18716 }
18717 else
18718 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18719 if (tv2->v_type == VAR_STRING)
18720 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018721 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018722 p2 = (char_u *)"'";
18723 else
18724 p2 = tv2->vval.v_string;
18725 }
18726 else
18727 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018728 if (p1 == NULL)
18729 p1 = (char_u *)"";
18730 if (p2 == NULL)
18731 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010018732 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018733 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018734 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018735 res = STRICMP(p1, p2);
18736 else
18737 res = STRCMP(p1, p2);
18738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018739 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018740 {
18741 double n1, n2;
18742 n1 = strtod((char *)p1, (char **)&p1);
18743 n2 = strtod((char *)p2, (char **)&p2);
18744 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18745 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018746
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018747 /* When the result would be zero, compare the item indexes. Makes the
18748 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018749 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018750 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018751
Bram Moolenaar0d660222005-01-07 21:51:51 +000018752 vim_free(tofree1);
18753 vim_free(tofree2);
18754 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018755}
18756
18757 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018758#ifdef __BORLANDC__
18759_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018760#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018761item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018762{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018763 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018764 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018765 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018766 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018767 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018768 char_u *func_name;
18769 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018770
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018771 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018772 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018773 return 0;
18774
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018775 si1 = (sortItem_T *)s1;
18776 si2 = (sortItem_T *)s2;
18777
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018778 if (partial == NULL)
18779 func_name = sortinfo->item_compare_func;
18780 else
18781 func_name = partial->pt_name;
18782
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018783 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018784 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018785 copy_tv(&si1->item->li_tv, &argv[0]);
18786 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018787
18788 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018789 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018790 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018791 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018792 clear_tv(&argv[0]);
18793 clear_tv(&argv[1]);
18794
18795 if (res == FAIL)
18796 res = ITEM_COMPARE_FAIL;
18797 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010018798 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
18799 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018800 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018801 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018802
18803 /* When the result would be zero, compare the pointers themselves. Makes
18804 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018805 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018806 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018807
Bram Moolenaar0d660222005-01-07 21:51:51 +000018808 return res;
18809}
18810
18811/*
18812 * "sort({list})" function
18813 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018815do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018816{
Bram Moolenaar33570922005-01-25 22:26:29 +000018817 list_T *l;
18818 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018819 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018820 sortinfo_T *old_sortinfo;
18821 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018822 long len;
18823 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018824
Bram Moolenaar0b962472016-02-22 22:51:33 +010018825 /* Pointer to current info struct used in compare function. Save and
18826 * restore the current one for nested calls. */
18827 old_sortinfo = sortinfo;
18828 sortinfo = &info;
18829
Bram Moolenaar0d660222005-01-07 21:51:51 +000018830 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018831 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018832 else
18833 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018834 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018835 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018836 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18837 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010018838 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018839 rettv->vval.v_list = l;
18840 rettv->v_type = VAR_LIST;
18841 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018842
Bram Moolenaar0d660222005-01-07 21:51:51 +000018843 len = list_len(l);
18844 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018845 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018846
Bram Moolenaar0b962472016-02-22 22:51:33 +010018847 info.item_compare_ic = FALSE;
18848 info.item_compare_numeric = FALSE;
18849 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018850#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018851 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018852#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018853 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018854 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018855 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018856 if (argvars[1].v_type != VAR_UNKNOWN)
18857 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018858 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018859 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018860 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018861 else if (argvars[1].v_type == VAR_PARTIAL)
18862 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018863 else
18864 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018865 int error = FALSE;
18866
18867 i = get_tv_number_chk(&argvars[1], &error);
18868 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018869 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018870 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018871 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010018872 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018873 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010018874 else if (i != 0)
18875 {
18876 EMSG(_(e_invarg));
18877 goto theend;
18878 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018879 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018880 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010018881 if (*info.item_compare_func == NUL)
18882 {
18883 /* empty string means default sort */
18884 info.item_compare_func = NULL;
18885 }
18886 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018887 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018888 info.item_compare_func = NULL;
18889 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018890 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018891 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018892 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018893 info.item_compare_func = NULL;
18894 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018895 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018896#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018897 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018898 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018899 info.item_compare_func = NULL;
18900 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018901 }
18902#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018903 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018904 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018905 info.item_compare_func = NULL;
18906 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018907 }
18908 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018909 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018910
18911 if (argvars[2].v_type != VAR_UNKNOWN)
18912 {
18913 /* optional third argument: {dict} */
18914 if (argvars[2].v_type != VAR_DICT)
18915 {
18916 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010018917 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018918 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018919 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018920 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018922
Bram Moolenaar0d660222005-01-07 21:51:51 +000018923 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018924 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018925 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018926 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018927
Bram Moolenaar327aa022014-03-25 18:24:23 +010018928 i = 0;
18929 if (sort)
18930 {
18931 /* sort(): ptrs will be the list to sort */
18932 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018933 {
18934 ptrs[i].item = li;
18935 ptrs[i].idx = i;
18936 ++i;
18937 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018938
Bram Moolenaar0b962472016-02-22 22:51:33 +010018939 info.item_compare_func_err = FALSE;
18940 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018941 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018942 if ((info.item_compare_func != NULL
18943 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018944 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018945 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018946 EMSG(_("E702: Sort compare function failed"));
18947 else
18948 {
18949 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018950 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010018951 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018952 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010018953 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018954
Bram Moolenaar0b962472016-02-22 22:51:33 +010018955 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018956 {
18957 /* Clear the List and append the items in sorted order. */
18958 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18959 l->lv_len = 0;
18960 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018961 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018962 }
18963 }
18964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018965 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018966 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018967 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018968
18969 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018970 info.item_compare_func_err = FALSE;
18971 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018972 item_compare_func_ptr = info.item_compare_func != NULL
18973 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010018974 ? item_compare2 : item_compare;
18975
18976 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18977 li = li->li_next)
18978 {
18979 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18980 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018981 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018982 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018983 {
18984 EMSG(_("E882: Uniq compare function failed"));
18985 break;
18986 }
18987 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018988
Bram Moolenaar0b962472016-02-22 22:51:33 +010018989 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018990 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018991 while (--i >= 0)
18992 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018993 li = ptrs[i].item->li_next;
18994 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018995 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018996 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018997 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018998 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018999 list_fix_watch(l, li);
19000 listitem_free(li);
19001 l->lv_len--;
19002 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019003 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019004 }
19005
19006 vim_free(ptrs);
19007 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019008theend:
19009 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019010}
19011
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019012/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019013 * "sort({list})" function
19014 */
19015 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019016f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019017{
19018 do_sort_uniq(argvars, rettv, TRUE);
19019}
19020
19021/*
19022 * "uniq({list})" function
19023 */
19024 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019025f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019026{
19027 do_sort_uniq(argvars, rettv, FALSE);
19028}
19029
19030/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019031 * "soundfold({word})" function
19032 */
19033 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019034f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019035{
19036 char_u *s;
19037
19038 rettv->v_type = VAR_STRING;
19039 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019040#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019041 rettv->vval.v_string = eval_soundfold(s);
19042#else
19043 rettv->vval.v_string = vim_strsave(s);
19044#endif
19045}
19046
19047/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019048 * "spellbadword()" function
19049 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019050 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019051f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019052{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019053 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019054 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019055 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019056
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019057 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019058 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019059
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019060#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019061 if (argvars[0].v_type == VAR_UNKNOWN)
19062 {
19063 /* Find the start and length of the badly spelled word. */
19064 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19065 if (len != 0)
19066 word = ml_get_cursor();
19067 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019068 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019069 {
19070 char_u *str = get_tv_string_chk(&argvars[0]);
19071 int capcol = -1;
19072
19073 if (str != NULL)
19074 {
19075 /* Check the argument for spelling. */
19076 while (*str != NUL)
19077 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019078 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019079 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019080 {
19081 word = str;
19082 break;
19083 }
19084 str += len;
19085 }
19086 }
19087 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019088#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019089
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019090 list_append_string(rettv->vval.v_list, word, len);
19091 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019092 attr == HLF_SPB ? "bad" :
19093 attr == HLF_SPR ? "rare" :
19094 attr == HLF_SPL ? "local" :
19095 attr == HLF_SPC ? "caps" :
19096 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019097}
19098
19099/*
19100 * "spellsuggest()" function
19101 */
19102 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019103f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019104{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019105#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019106 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019107 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019108 int maxcount;
19109 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019110 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019111 listitem_T *li;
19112 int need_capital = FALSE;
19113#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019114
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019115 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019116 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019117
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019118#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019119 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019120 {
19121 str = get_tv_string(&argvars[0]);
19122 if (argvars[1].v_type != VAR_UNKNOWN)
19123 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019124 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019125 if (maxcount <= 0)
19126 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019127 if (argvars[2].v_type != VAR_UNKNOWN)
19128 {
19129 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19130 if (typeerr)
19131 return;
19132 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019133 }
19134 else
19135 maxcount = 25;
19136
Bram Moolenaar4770d092006-01-12 23:22:24 +000019137 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019138
19139 for (i = 0; i < ga.ga_len; ++i)
19140 {
19141 str = ((char_u **)ga.ga_data)[i];
19142
19143 li = listitem_alloc();
19144 if (li == NULL)
19145 vim_free(str);
19146 else
19147 {
19148 li->li_tv.v_type = VAR_STRING;
19149 li->li_tv.v_lock = 0;
19150 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019151 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019152 }
19153 }
19154 ga_clear(&ga);
19155 }
19156#endif
19157}
19158
Bram Moolenaar0d660222005-01-07 21:51:51 +000019159 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019160f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019161{
19162 char_u *str;
19163 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019164 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019165 regmatch_T regmatch;
19166 char_u patbuf[NUMBUFLEN];
19167 char_u *save_cpo;
19168 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019169 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019170 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019171 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019172
19173 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19174 save_cpo = p_cpo;
19175 p_cpo = (char_u *)"";
19176
19177 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019178 if (argvars[1].v_type != VAR_UNKNOWN)
19179 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019180 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19181 if (pat == NULL)
19182 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019183 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019184 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019185 }
19186 if (pat == NULL || *pat == NUL)
19187 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019188
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019189 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019190 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019191 if (typeerr)
19192 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019193
Bram Moolenaar0d660222005-01-07 21:51:51 +000019194 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19195 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019196 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019197 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019198 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019199 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019200 if (*str == NUL)
19201 match = FALSE; /* empty item at the end */
19202 else
19203 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019204 if (match)
19205 end = regmatch.startp[0];
19206 else
19207 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019208 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19209 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019210 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019211 if (list_append_string(rettv->vval.v_list, str,
19212 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019213 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019214 }
19215 if (!match)
19216 break;
19217 /* Advance to just after the match. */
19218 if (regmatch.endp[0] > str)
19219 col = 0;
19220 else
19221 {
19222 /* Don't get stuck at the same match. */
19223#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019224 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019225#else
19226 col = 1;
19227#endif
19228 }
19229 str = regmatch.endp[0];
19230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019231
Bram Moolenaar473de612013-06-08 18:19:48 +020019232 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019234
Bram Moolenaar0d660222005-01-07 21:51:51 +000019235 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019236}
19237
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019238#ifdef FEAT_FLOAT
19239/*
19240 * "sqrt()" function
19241 */
19242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019243f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019244{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019245 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019246
19247 rettv->v_type = VAR_FLOAT;
19248 if (get_float_arg(argvars, &f) == OK)
19249 rettv->vval.v_float = sqrt(f);
19250 else
19251 rettv->vval.v_float = 0.0;
19252}
19253
19254/*
19255 * "str2float()" function
19256 */
19257 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019258f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019259{
19260 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19261
19262 if (*p == '+')
19263 p = skipwhite(p + 1);
19264 (void)string2float(p, &rettv->vval.v_float);
19265 rettv->v_type = VAR_FLOAT;
19266}
19267#endif
19268
Bram Moolenaar2c932302006-03-18 21:42:09 +000019269/*
19270 * "str2nr()" function
19271 */
19272 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019273f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019274{
19275 int base = 10;
19276 char_u *p;
19277 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019278 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019279
19280 if (argvars[1].v_type != VAR_UNKNOWN)
19281 {
19282 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019283 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019284 {
19285 EMSG(_(e_invarg));
19286 return;
19287 }
19288 }
19289
19290 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019291 if (*p == '+')
19292 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019293 switch (base)
19294 {
19295 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19296 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19297 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19298 default: what = 0;
19299 }
19300 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019301 rettv->vval.v_number = n;
19302}
19303
Bram Moolenaar071d4272004-06-13 20:20:40 +000019304#ifdef HAVE_STRFTIME
19305/*
19306 * "strftime({format}[, {time}])" function
19307 */
19308 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019309f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019310{
19311 char_u result_buf[256];
19312 struct tm *curtime;
19313 time_t seconds;
19314 char_u *p;
19315
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019316 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019317
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019318 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019319 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019320 seconds = time(NULL);
19321 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019322 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019323 curtime = localtime(&seconds);
19324 /* MSVC returns NULL for an invalid value of seconds. */
19325 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019326 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019327 else
19328 {
19329# ifdef FEAT_MBYTE
19330 vimconv_T conv;
19331 char_u *enc;
19332
19333 conv.vc_type = CONV_NONE;
19334 enc = enc_locale();
19335 convert_setup(&conv, p_enc, enc);
19336 if (conv.vc_type != CONV_NONE)
19337 p = string_convert(&conv, p, NULL);
19338# endif
19339 if (p != NULL)
19340 (void)strftime((char *)result_buf, sizeof(result_buf),
19341 (char *)p, curtime);
19342 else
19343 result_buf[0] = NUL;
19344
19345# ifdef FEAT_MBYTE
19346 if (conv.vc_type != CONV_NONE)
19347 vim_free(p);
19348 convert_setup(&conv, enc, p_enc);
19349 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019350 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019351 else
19352# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019353 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019354
19355# ifdef FEAT_MBYTE
19356 /* Release conversion descriptors */
19357 convert_setup(&conv, NULL, NULL);
19358 vim_free(enc);
19359# endif
19360 }
19361}
19362#endif
19363
19364/*
19365 * "stridx()" function
19366 */
19367 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019368f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019369{
19370 char_u buf[NUMBUFLEN];
19371 char_u *needle;
19372 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019373 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019374 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019375 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019376
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019377 needle = get_tv_string_chk(&argvars[1]);
19378 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019379 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019380 if (needle == NULL || haystack == NULL)
19381 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019382
Bram Moolenaar33570922005-01-25 22:26:29 +000019383 if (argvars[2].v_type != VAR_UNKNOWN)
19384 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019385 int error = FALSE;
19386
19387 start_idx = get_tv_number_chk(&argvars[2], &error);
19388 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019389 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019390 if (start_idx >= 0)
19391 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019392 }
19393
19394 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19395 if (pos != NULL)
19396 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019397}
19398
19399/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019400 * "string()" function
19401 */
19402 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019403f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019404{
19405 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019406 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019407
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019408 rettv->v_type = VAR_STRING;
Bram Moolenaar24c77a12016-03-24 21:23:06 +010019409 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
19410 get_copyID());
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019411 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019412 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019413 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019414}
19415
19416/*
19417 * "strlen()" function
19418 */
19419 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019420f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019421{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019422 rettv->vval.v_number = (varnumber_T)(STRLEN(
19423 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019424}
19425
19426/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019427 * "strchars()" function
19428 */
19429 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019430f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019431{
19432 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019433 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019434#ifdef FEAT_MBYTE
19435 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019436 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019437#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019438
19439 if (argvars[1].v_type != VAR_UNKNOWN)
19440 skipcc = get_tv_number_chk(&argvars[1], NULL);
19441 if (skipcc < 0 || skipcc > 1)
19442 EMSG(_(e_invarg));
19443 else
19444 {
19445#ifdef FEAT_MBYTE
19446 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19447 while (*s != NUL)
19448 {
19449 func_mb_ptr2char_adv(&s);
19450 ++len;
19451 }
19452 rettv->vval.v_number = len;
19453#else
19454 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19455#endif
19456 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019457}
19458
19459/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019460 * "strdisplaywidth()" function
19461 */
19462 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019463f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019464{
19465 char_u *s = get_tv_string(&argvars[0]);
19466 int col = 0;
19467
19468 if (argvars[1].v_type != VAR_UNKNOWN)
19469 col = get_tv_number(&argvars[1]);
19470
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019471 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019472}
19473
19474/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019475 * "strwidth()" function
19476 */
19477 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019478f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019479{
19480 char_u *s = get_tv_string(&argvars[0]);
19481
19482 rettv->vval.v_number = (varnumber_T)(
19483#ifdef FEAT_MBYTE
19484 mb_string2cells(s, -1)
19485#else
19486 STRLEN(s)
19487#endif
19488 );
19489}
19490
19491/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019492 * "strpart()" function
19493 */
19494 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019495f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019496{
19497 char_u *p;
19498 int n;
19499 int len;
19500 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019501 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019502
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019503 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019504 slen = (int)STRLEN(p);
19505
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019506 n = get_tv_number_chk(&argvars[1], &error);
19507 if (error)
19508 len = 0;
19509 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019510 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019511 else
19512 len = slen - n; /* default len: all bytes that are available. */
19513
19514 /*
19515 * Only return the overlap between the specified part and the actual
19516 * string.
19517 */
19518 if (n < 0)
19519 {
19520 len += n;
19521 n = 0;
19522 }
19523 else if (n > slen)
19524 n = slen;
19525 if (len < 0)
19526 len = 0;
19527 else if (n + len > slen)
19528 len = slen - n;
19529
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019530 rettv->v_type = VAR_STRING;
19531 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019532}
19533
19534/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019535 * "strridx()" function
19536 */
19537 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019538f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019539{
19540 char_u buf[NUMBUFLEN];
19541 char_u *needle;
19542 char_u *haystack;
19543 char_u *rest;
19544 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019545 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019546
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019547 needle = get_tv_string_chk(&argvars[1]);
19548 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019549
19550 rettv->vval.v_number = -1;
19551 if (needle == NULL || haystack == NULL)
19552 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019553
19554 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019555 if (argvars[2].v_type != VAR_UNKNOWN)
19556 {
19557 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019558 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019559 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019560 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019561 }
19562 else
19563 end_idx = haystack_len;
19564
Bram Moolenaar0d660222005-01-07 21:51:51 +000019565 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019566 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019567 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019568 lastmatch = haystack + end_idx;
19569 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019570 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019571 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019572 for (rest = haystack; *rest != '\0'; ++rest)
19573 {
19574 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019575 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019576 break;
19577 lastmatch = rest;
19578 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019579 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019580
19581 if (lastmatch == NULL)
19582 rettv->vval.v_number = -1;
19583 else
19584 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19585}
19586
19587/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019588 * "strtrans()" function
19589 */
19590 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019591f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019592{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019593 rettv->v_type = VAR_STRING;
19594 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019595}
19596
19597/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019598 * "submatch()" function
19599 */
19600 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019601f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019602{
Bram Moolenaar41571762014-04-02 19:00:58 +020019603 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019604 int no;
19605 int retList = 0;
19606
19607 no = (int)get_tv_number_chk(&argvars[0], &error);
19608 if (error)
19609 return;
19610 error = FALSE;
19611 if (argvars[1].v_type != VAR_UNKNOWN)
19612 retList = get_tv_number_chk(&argvars[1], &error);
19613 if (error)
19614 return;
19615
19616 if (retList == 0)
19617 {
19618 rettv->v_type = VAR_STRING;
19619 rettv->vval.v_string = reg_submatch(no);
19620 }
19621 else
19622 {
19623 rettv->v_type = VAR_LIST;
19624 rettv->vval.v_list = reg_submatch_list(no);
19625 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019626}
19627
19628/*
19629 * "substitute()" function
19630 */
19631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019632f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019633{
19634 char_u patbuf[NUMBUFLEN];
19635 char_u subbuf[NUMBUFLEN];
19636 char_u flagsbuf[NUMBUFLEN];
19637
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019638 char_u *str = get_tv_string_chk(&argvars[0]);
19639 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19640 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19641 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19642
Bram Moolenaar0d660222005-01-07 21:51:51 +000019643 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019644 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19645 rettv->vval.v_string = NULL;
19646 else
19647 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019648}
19649
19650/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019651 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019652 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019653 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019654f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019655{
19656 int id = 0;
19657#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019658 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019659 long col;
19660 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019661 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019662
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019663 lnum = get_tv_lnum(argvars); /* -1 on type error */
19664 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19665 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019666
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019667 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019668 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019669 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019670#endif
19671
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019672 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019673}
19674
19675/*
19676 * "synIDattr(id, what [, mode])" function
19677 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019678 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019679f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019680{
19681 char_u *p = NULL;
19682#ifdef FEAT_SYN_HL
19683 int id;
19684 char_u *what;
19685 char_u *mode;
19686 char_u modebuf[NUMBUFLEN];
19687 int modec;
19688
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019689 id = get_tv_number(&argvars[0]);
19690 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019691 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019692 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019693 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019694 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019695 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019696 modec = 0; /* replace invalid with current */
19697 }
19698 else
19699 {
19700#ifdef FEAT_GUI
19701 if (gui.in_use)
19702 modec = 'g';
19703 else
19704#endif
19705 if (t_colors > 1)
19706 modec = 'c';
19707 else
19708 modec = 't';
19709 }
19710
19711
19712 switch (TOLOWER_ASC(what[0]))
19713 {
19714 case 'b':
19715 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19716 p = highlight_color(id, what, modec);
19717 else /* bold */
19718 p = highlight_has_attr(id, HL_BOLD, modec);
19719 break;
19720
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019721 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019722 p = highlight_color(id, what, modec);
19723 break;
19724
19725 case 'i':
19726 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19727 p = highlight_has_attr(id, HL_INVERSE, modec);
19728 else /* italic */
19729 p = highlight_has_attr(id, HL_ITALIC, modec);
19730 break;
19731
19732 case 'n': /* name */
19733 p = get_highlight_name(NULL, id - 1);
19734 break;
19735
19736 case 'r': /* reverse */
19737 p = highlight_has_attr(id, HL_INVERSE, modec);
19738 break;
19739
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019740 case 's':
19741 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19742 p = highlight_color(id, what, modec);
19743 else /* standout */
19744 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019745 break;
19746
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019747 case 'u':
19748 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19749 /* underline */
19750 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19751 else
19752 /* undercurl */
19753 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019754 break;
19755 }
19756
19757 if (p != NULL)
19758 p = vim_strsave(p);
19759#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019760 rettv->v_type = VAR_STRING;
19761 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019762}
19763
19764/*
19765 * "synIDtrans(id)" function
19766 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019767 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019768f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019769{
19770 int id;
19771
19772#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019773 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019774
19775 if (id > 0)
19776 id = syn_get_final_id(id);
19777 else
19778#endif
19779 id = 0;
19780
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019781 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019782}
19783
19784/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019785 * "synconcealed(lnum, col)" function
19786 */
19787 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019788f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019789{
19790#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19791 long lnum;
19792 long col;
19793 int syntax_flags = 0;
19794 int cchar;
19795 int matchid = 0;
19796 char_u str[NUMBUFLEN];
19797#endif
19798
19799 rettv->v_type = VAR_LIST;
19800 rettv->vval.v_list = NULL;
19801
19802#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19803 lnum = get_tv_lnum(argvars); /* -1 on type error */
19804 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19805
19806 vim_memset(str, NUL, sizeof(str));
19807
19808 if (rettv_list_alloc(rettv) != FAIL)
19809 {
19810 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19811 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19812 && curwin->w_p_cole > 0)
19813 {
19814 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19815 syntax_flags = get_syntax_info(&matchid);
19816
19817 /* get the conceal character */
19818 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19819 {
19820 cchar = syn_get_sub_char();
19821 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19822 cchar = lcs_conceal;
19823 if (cchar != NUL)
19824 {
19825# ifdef FEAT_MBYTE
19826 if (has_mbyte)
19827 (*mb_char2bytes)(cchar, str);
19828 else
19829# endif
19830 str[0] = cchar;
19831 }
19832 }
19833 }
19834
19835 list_append_number(rettv->vval.v_list,
19836 (syntax_flags & HL_CONCEAL) != 0);
19837 /* -1 to auto-determine strlen */
19838 list_append_string(rettv->vval.v_list, str, -1);
19839 list_append_number(rettv->vval.v_list, matchid);
19840 }
19841#endif
19842}
19843
19844/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019845 * "synstack(lnum, col)" function
19846 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019847 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019848f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019849{
19850#ifdef FEAT_SYN_HL
19851 long lnum;
19852 long col;
19853 int i;
19854 int id;
19855#endif
19856
19857 rettv->v_type = VAR_LIST;
19858 rettv->vval.v_list = NULL;
19859
19860#ifdef FEAT_SYN_HL
19861 lnum = get_tv_lnum(argvars); /* -1 on type error */
19862 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19863
19864 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019865 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019866 && rettv_list_alloc(rettv) != FAIL)
19867 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019868 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019869 for (i = 0; ; ++i)
19870 {
19871 id = syn_get_stack_item(i);
19872 if (id < 0)
19873 break;
19874 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19875 break;
19876 }
19877 }
19878#endif
19879}
19880
Bram Moolenaar071d4272004-06-13 20:20:40 +000019881 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019882get_cmd_output_as_rettv(
19883 typval_T *argvars,
19884 typval_T *rettv,
19885 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019886{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019887 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019888 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019889 char_u *infile = NULL;
19890 char_u buf[NUMBUFLEN];
19891 int err = FALSE;
19892 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019893 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019894 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019895
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019896 rettv->v_type = VAR_STRING;
19897 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019898 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019899 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019900
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019901 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019902 {
19903 /*
19904 * Write the string to a temp file, to be used for input of the shell
19905 * command.
19906 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019907 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019908 {
19909 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019910 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019911 }
19912
19913 fd = mch_fopen((char *)infile, WRITEBIN);
19914 if (fd == NULL)
19915 {
19916 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019917 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019918 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019919 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019920 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019921 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19922 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019923 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019924 else
19925 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019926 size_t len;
19927
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019928 p = get_tv_string_buf_chk(&argvars[1], buf);
19929 if (p == NULL)
19930 {
19931 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019932 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019933 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019934 len = STRLEN(p);
19935 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019936 err = TRUE;
19937 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019938 if (fclose(fd) != 0)
19939 err = TRUE;
19940 if (err)
19941 {
19942 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019943 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019944 }
19945 }
19946
Bram Moolenaar52a72462014-08-29 15:53:52 +020019947 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19948 * echoes typeahead, that messes up the display. */
19949 if (!msg_silent)
19950 flags += SHELL_COOKED;
19951
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019952 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019953 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019954 int len;
19955 listitem_T *li;
19956 char_u *s = NULL;
19957 char_u *start;
19958 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019959 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019960
Bram Moolenaar52a72462014-08-29 15:53:52 +020019961 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019962 if (res == NULL)
19963 goto errret;
19964
19965 list = list_alloc();
19966 if (list == NULL)
19967 goto errret;
19968
19969 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019970 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019971 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019972 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019973 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019974 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019975
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019976 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019977 if (s == NULL)
19978 goto errret;
19979
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019980 for (p = s; start < end; ++p, ++start)
19981 *p = *start == NUL ? NL : *start;
19982 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019983
19984 li = listitem_alloc();
19985 if (li == NULL)
19986 {
19987 vim_free(s);
19988 goto errret;
19989 }
19990 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019991 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019992 li->li_tv.vval.v_string = s;
19993 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019994 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019995
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019996 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019997 rettv->v_type = VAR_LIST;
19998 rettv->vval.v_list = list;
19999 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020000 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020001 else
20002 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020003 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020004#ifdef USE_CR
20005 /* translate <CR> into <NL> */
20006 if (res != NULL)
20007 {
20008 char_u *s;
20009
20010 for (s = res; *s; ++s)
20011 {
20012 if (*s == CAR)
20013 *s = NL;
20014 }
20015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020016#else
20017# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020018 /* translate <CR><NL> into <NL> */
20019 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020020 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020021 char_u *s, *d;
20022
20023 d = res;
20024 for (s = res; *s; ++s)
20025 {
20026 if (s[0] == CAR && s[1] == NL)
20027 ++s;
20028 *d++ = *s;
20029 }
20030 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020032# endif
20033#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020034 rettv->vval.v_string = res;
20035 res = NULL;
20036 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020037
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020038errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020039 if (infile != NULL)
20040 {
20041 mch_remove(infile);
20042 vim_free(infile);
20043 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020044 if (res != NULL)
20045 vim_free(res);
20046 if (list != NULL)
20047 list_free(list, TRUE);
20048}
20049
20050/*
20051 * "system()" function
20052 */
20053 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020054f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020055{
20056 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20057}
20058
20059/*
20060 * "systemlist()" function
20061 */
20062 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020063f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020064{
20065 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020066}
20067
20068/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020069 * "tabpagebuflist()" function
20070 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020071 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020072f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020073{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020074#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020075 tabpage_T *tp;
20076 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020077
20078 if (argvars[0].v_type == VAR_UNKNOWN)
20079 wp = firstwin;
20080 else
20081 {
20082 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20083 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020084 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020085 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020086 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020087 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020088 for (; wp != NULL; wp = wp->w_next)
20089 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020090 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020091 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020092 }
20093#endif
20094}
20095
20096
20097/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020098 * "tabpagenr()" function
20099 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020100 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020101f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020102{
20103 int nr = 1;
20104#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020105 char_u *arg;
20106
20107 if (argvars[0].v_type != VAR_UNKNOWN)
20108 {
20109 arg = get_tv_string_chk(&argvars[0]);
20110 nr = 0;
20111 if (arg != NULL)
20112 {
20113 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020114 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020115 else
20116 EMSG2(_(e_invexpr2), arg);
20117 }
20118 }
20119 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020120 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020121#endif
20122 rettv->vval.v_number = nr;
20123}
20124
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020125
20126#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020127static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020128
20129/*
20130 * Common code for tabpagewinnr() and winnr().
20131 */
20132 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020133get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020134{
20135 win_T *twin;
20136 int nr = 1;
20137 win_T *wp;
20138 char_u *arg;
20139
20140 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20141 if (argvar->v_type != VAR_UNKNOWN)
20142 {
20143 arg = get_tv_string_chk(argvar);
20144 if (arg == NULL)
20145 nr = 0; /* type error; errmsg already given */
20146 else if (STRCMP(arg, "$") == 0)
20147 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20148 else if (STRCMP(arg, "#") == 0)
20149 {
20150 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20151 if (twin == NULL)
20152 nr = 0;
20153 }
20154 else
20155 {
20156 EMSG2(_(e_invexpr2), arg);
20157 nr = 0;
20158 }
20159 }
20160
20161 if (nr > 0)
20162 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20163 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020164 {
20165 if (wp == NULL)
20166 {
20167 /* didn't find it in this tabpage */
20168 nr = 0;
20169 break;
20170 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020171 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020172 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020173 return nr;
20174}
20175#endif
20176
20177/*
20178 * "tabpagewinnr()" function
20179 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020180 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020181f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020182{
20183 int nr = 1;
20184#ifdef FEAT_WINDOWS
20185 tabpage_T *tp;
20186
20187 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20188 if (tp == NULL)
20189 nr = 0;
20190 else
20191 nr = get_winnr(tp, &argvars[1]);
20192#endif
20193 rettv->vval.v_number = nr;
20194}
20195
20196
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020197/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020198 * "tagfiles()" function
20199 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020200 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020201f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020202{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020203 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020204 tagname_T tn;
20205 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020206
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020207 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020208 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020209 fname = alloc(MAXPATHL);
20210 if (fname == NULL)
20211 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020212
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020213 for (first = TRUE; ; first = FALSE)
20214 if (get_tagfname(&tn, first, fname) == FAIL
20215 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020216 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020217 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020218 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020219}
20220
20221/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020222 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020223 */
20224 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020225f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020226{
20227 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020228
20229 tag_pattern = get_tv_string(&argvars[0]);
20230
20231 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020232 if (*tag_pattern == NUL)
20233 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020234
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020235 if (rettv_list_alloc(rettv) == OK)
20236 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020237}
20238
20239/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020240 * "tempname()" function
20241 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020243f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020244{
20245 static int x = 'A';
20246
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020247 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020248 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020249
20250 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20251 * names. Skip 'I' and 'O', they are used for shell redirection. */
20252 do
20253 {
20254 if (x == 'Z')
20255 x = '0';
20256 else if (x == '9')
20257 x = 'A';
20258 else
20259 {
20260#ifdef EBCDIC
20261 if (x == 'I')
20262 x = 'J';
20263 else if (x == 'R')
20264 x = 'S';
20265 else
20266#endif
20267 ++x;
20268 }
20269 } while (x == 'I' || x == 'O');
20270}
20271
20272/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020273 * "test(list)" function: Just checking the walls...
20274 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020275 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020276f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020277{
20278 /* Used for unit testing. Change the code below to your liking. */
20279#if 0
20280 listitem_T *li;
20281 list_T *l;
20282 char_u *bad, *good;
20283
20284 if (argvars[0].v_type != VAR_LIST)
20285 return;
20286 l = argvars[0].vval.v_list;
20287 if (l == NULL)
20288 return;
20289 li = l->lv_first;
20290 if (li == NULL)
20291 return;
20292 bad = get_tv_string(&li->li_tv);
20293 li = li->li_next;
20294 if (li == NULL)
20295 return;
20296 good = get_tv_string(&li->li_tv);
20297 rettv->vval.v_number = test_edit_score(bad, good);
20298#endif
20299}
20300
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020301#ifdef FEAT_FLOAT
20302/*
20303 * "tan()" function
20304 */
20305 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020306f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020307{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020308 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020309
20310 rettv->v_type = VAR_FLOAT;
20311 if (get_float_arg(argvars, &f) == OK)
20312 rettv->vval.v_float = tan(f);
20313 else
20314 rettv->vval.v_float = 0.0;
20315}
20316
20317/*
20318 * "tanh()" function
20319 */
20320 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020321f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020322{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020323 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020324
20325 rettv->v_type = VAR_FLOAT;
20326 if (get_float_arg(argvars, &f) == OK)
20327 rettv->vval.v_float = tanh(f);
20328 else
20329 rettv->vval.v_float = 0.0;
20330}
20331#endif
20332
Bram Moolenaar975b5272016-03-15 23:10:59 +010020333#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
20334/*
20335 * Get a callback from "arg". It can be a Funcref or a function name.
20336 * When "arg" is zero return an empty string.
20337 * Return NULL for an invalid argument.
20338 */
20339 char_u *
20340get_callback(typval_T *arg, partial_T **pp)
20341{
20342 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
20343 {
20344 *pp = arg->vval.v_partial;
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010020345 ++(*pp)->pt_refcount;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020346 return (*pp)->pt_name;
20347 }
20348 *pp = NULL;
20349 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
20350 return arg->vval.v_string;
20351 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
20352 return (char_u *)"";
20353 EMSG(_("E921: Invalid callback argument"));
20354 return NULL;
20355}
20356#endif
20357
20358#ifdef FEAT_TIMERS
20359/*
20360 * "timer_start(time, callback [, options])" function
20361 */
20362 static void
20363f_timer_start(typval_T *argvars, typval_T *rettv)
20364{
20365 long msec = get_tv_number(&argvars[0]);
20366 timer_T *timer;
20367 int repeat = 0;
20368 char_u *callback;
20369 dict_T *dict;
20370
20371 if (argvars[2].v_type != VAR_UNKNOWN)
20372 {
20373 if (argvars[2].v_type != VAR_DICT
20374 || (dict = argvars[2].vval.v_dict) == NULL)
20375 {
20376 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
20377 return;
20378 }
20379 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
20380 repeat = get_dict_number(dict, (char_u *)"repeat");
20381 }
20382
20383 timer = create_timer(msec, repeat);
20384 callback = get_callback(&argvars[1], &timer->tr_partial);
20385 if (callback == NULL)
20386 {
20387 stop_timer(timer);
20388 rettv->vval.v_number = -1;
20389 }
20390 else
20391 {
20392 timer->tr_callback = vim_strsave(callback);
20393 rettv->vval.v_number = timer->tr_id;
20394 }
20395}
20396
20397/*
20398 * "timer_stop(timer)" function
20399 */
20400 static void
20401f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
20402{
20403 timer_T *timer = find_timer(get_tv_number(&argvars[0]));
20404
20405 if (timer != NULL)
20406 stop_timer(timer);
20407}
20408#endif
20409
Bram Moolenaard52d9742005-08-21 22:20:28 +000020410/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020411 * "tolower(string)" function
20412 */
20413 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020414f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020415{
20416 char_u *p;
20417
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020418 p = vim_strsave(get_tv_string(&argvars[0]));
20419 rettv->v_type = VAR_STRING;
20420 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020421
20422 if (p != NULL)
20423 while (*p != NUL)
20424 {
20425#ifdef FEAT_MBYTE
20426 int l;
20427
20428 if (enc_utf8)
20429 {
20430 int c, lc;
20431
20432 c = utf_ptr2char(p);
20433 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020434 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020435 /* TODO: reallocate string when byte count changes. */
20436 if (utf_char2len(lc) == l)
20437 utf_char2bytes(lc, p);
20438 p += l;
20439 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020440 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020441 p += l; /* skip multi-byte character */
20442 else
20443#endif
20444 {
20445 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20446 ++p;
20447 }
20448 }
20449}
20450
20451/*
20452 * "toupper(string)" function
20453 */
20454 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020455f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020456{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020457 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020458 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020459}
20460
20461/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020462 * "tr(string, fromstr, tostr)" function
20463 */
20464 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020465f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020466{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020467 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020468 char_u *fromstr;
20469 char_u *tostr;
20470 char_u *p;
20471#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020472 int inlen;
20473 int fromlen;
20474 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020475 int idx;
20476 char_u *cpstr;
20477 int cplen;
20478 int first = TRUE;
20479#endif
20480 char_u buf[NUMBUFLEN];
20481 char_u buf2[NUMBUFLEN];
20482 garray_T ga;
20483
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020484 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020485 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20486 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020487
20488 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020489 rettv->v_type = VAR_STRING;
20490 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020491 if (fromstr == NULL || tostr == NULL)
20492 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020493 ga_init2(&ga, (int)sizeof(char), 80);
20494
20495#ifdef FEAT_MBYTE
20496 if (!has_mbyte)
20497#endif
20498 /* not multi-byte: fromstr and tostr must be the same length */
20499 if (STRLEN(fromstr) != STRLEN(tostr))
20500 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020501#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020502error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020503#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020504 EMSG2(_(e_invarg2), fromstr);
20505 ga_clear(&ga);
20506 return;
20507 }
20508
20509 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020510 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020511 {
20512#ifdef FEAT_MBYTE
20513 if (has_mbyte)
20514 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020515 inlen = (*mb_ptr2len)(in_str);
20516 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020517 cplen = inlen;
20518 idx = 0;
20519 for (p = fromstr; *p != NUL; p += fromlen)
20520 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020521 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020522 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020523 {
20524 for (p = tostr; *p != NUL; p += tolen)
20525 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020526 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020527 if (idx-- == 0)
20528 {
20529 cplen = tolen;
20530 cpstr = p;
20531 break;
20532 }
20533 }
20534 if (*p == NUL) /* tostr is shorter than fromstr */
20535 goto error;
20536 break;
20537 }
20538 ++idx;
20539 }
20540
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020541 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020542 {
20543 /* Check that fromstr and tostr have the same number of
20544 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020545 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020546 first = FALSE;
20547 for (p = tostr; *p != NUL; p += tolen)
20548 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020549 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020550 --idx;
20551 }
20552 if (idx != 0)
20553 goto error;
20554 }
20555
Bram Moolenaarcde88542015-08-11 19:14:00 +020020556 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020557 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020558 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020559
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020560 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020561 }
20562 else
20563#endif
20564 {
20565 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020566 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020567 if (p != NULL)
20568 ga_append(&ga, tostr[p - fromstr]);
20569 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020570 ga_append(&ga, *in_str);
20571 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020572 }
20573 }
20574
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020575 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020576 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020577 ga_append(&ga, NUL);
20578
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020579 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020580}
20581
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020582#ifdef FEAT_FLOAT
20583/*
20584 * "trunc({float})" function
20585 */
20586 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020587f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020588{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020589 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020590
20591 rettv->v_type = VAR_FLOAT;
20592 if (get_float_arg(argvars, &f) == OK)
20593 /* trunc() is not in C90, use floor() or ceil() instead. */
20594 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20595 else
20596 rettv->vval.v_float = 0.0;
20597}
20598#endif
20599
Bram Moolenaar8299df92004-07-10 09:47:34 +000020600/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020601 * "type(expr)" function
20602 */
20603 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020604f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020605{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020606 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020607
20608 switch (argvars[0].v_type)
20609 {
20610 case VAR_NUMBER: n = 0; break;
20611 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010020612 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020613 case VAR_FUNC: n = 2; break;
20614 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020615 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020616 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020617 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020618 if (argvars[0].vval.v_number == VVAL_FALSE
20619 || argvars[0].vval.v_number == VVAL_TRUE)
20620 n = 6;
20621 else
20622 n = 7;
20623 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020624 case VAR_JOB: n = 8; break;
20625 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020626 case VAR_UNKNOWN:
20627 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20628 n = -1;
20629 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020630 }
20631 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020632}
20633
20634/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020635 * "undofile(name)" function
20636 */
20637 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020638f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020639{
20640 rettv->v_type = VAR_STRING;
20641#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020642 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020643 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020644
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020645 if (*fname == NUL)
20646 {
20647 /* If there is no file name there will be no undo file. */
20648 rettv->vval.v_string = NULL;
20649 }
20650 else
20651 {
20652 char_u *ffname = FullName_save(fname, FALSE);
20653
20654 if (ffname != NULL)
20655 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20656 vim_free(ffname);
20657 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020658 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020659#else
20660 rettv->vval.v_string = NULL;
20661#endif
20662}
20663
20664/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020665 * "undotree()" function
20666 */
20667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020668f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020669{
20670 if (rettv_dict_alloc(rettv) == OK)
20671 {
20672 dict_T *dict = rettv->vval.v_dict;
20673 list_T *list;
20674
Bram Moolenaar730cde92010-06-27 05:18:54 +020020675 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020676 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020677 dict_add_nr_str(dict, "save_last",
20678 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020679 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20680 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020681 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020682
20683 list = list_alloc();
20684 if (list != NULL)
20685 {
20686 u_eval_tree(curbuf->b_u_oldhead, list);
20687 dict_add_list(dict, "entries", list);
20688 }
20689 }
20690}
20691
20692/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020693 * "values(dict)" function
20694 */
20695 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020696f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020697{
20698 dict_list(argvars, rettv, 1);
20699}
20700
20701/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020702 * "virtcol(string)" function
20703 */
20704 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020705f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020706{
20707 colnr_T vcol = 0;
20708 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020709 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020710
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020711 fp = var2fpos(&argvars[0], FALSE, &fnum);
20712 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20713 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020714 {
20715 getvvcol(curwin, fp, NULL, NULL, &vcol);
20716 ++vcol;
20717 }
20718
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020719 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020720}
20721
20722/*
20723 * "visualmode()" function
20724 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020725 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010020726f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020727{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020728 char_u str[2];
20729
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020730 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020731 str[0] = curbuf->b_visual_mode_eval;
20732 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020733 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020734
20735 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020736 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020737 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020738}
20739
20740/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020741 * "wildmenumode()" function
20742 */
20743 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020744f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020745{
20746#ifdef FEAT_WILDMENU
20747 if (wild_menu_showing)
20748 rettv->vval.v_number = 1;
20749#endif
20750}
20751
20752/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020753 * "winbufnr(nr)" function
20754 */
20755 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020756f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020757{
20758 win_T *wp;
20759
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020760 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020761 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020762 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020763 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020764 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020765}
20766
20767/*
20768 * "wincol()" function
20769 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020770 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020771f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020772{
20773 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020774 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020775}
20776
20777/*
20778 * "winheight(nr)" function
20779 */
20780 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020781f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020782{
20783 win_T *wp;
20784
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020785 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020786 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020787 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020788 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020789 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020790}
20791
20792/*
20793 * "winline()" function
20794 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020795 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020796f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020797{
20798 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020799 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020800}
20801
20802/*
20803 * "winnr()" function
20804 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020805 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020806f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020807{
20808 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020809
Bram Moolenaar071d4272004-06-13 20:20:40 +000020810#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020811 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020812#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020813 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020814}
20815
20816/*
20817 * "winrestcmd()" function
20818 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020819 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020820f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020821{
20822#ifdef FEAT_WINDOWS
20823 win_T *wp;
20824 int winnr = 1;
20825 garray_T ga;
20826 char_u buf[50];
20827
20828 ga_init2(&ga, (int)sizeof(char), 70);
20829 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20830 {
20831 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20832 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020833 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20834 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020835 ++winnr;
20836 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020837 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020838
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020839 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020840#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020841 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020842#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020843 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020844}
20845
20846/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020847 * "winrestview()" function
20848 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020849 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020850f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020851{
20852 dict_T *dict;
20853
20854 if (argvars[0].v_type != VAR_DICT
20855 || (dict = argvars[0].vval.v_dict) == NULL)
20856 EMSG(_(e_invarg));
20857 else
20858 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020859 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20860 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20861 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20862 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020863#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020864 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20865 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020866#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020867 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20868 {
20869 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20870 curwin->w_set_curswant = FALSE;
20871 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020872
Bram Moolenaar82c25852014-05-28 16:47:16 +020020873 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20874 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020875#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020876 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20877 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020878#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020879 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20880 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20881 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20882 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020883
20884 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020885 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010020886# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020020887 win_new_width(curwin, W_WIDTH(curwin));
20888# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020889 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020890
Bram Moolenaarb851a962014-10-31 15:45:52 +010020891 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020892 curwin->w_topline = 1;
20893 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20894 curwin->w_topline = curbuf->b_ml.ml_line_count;
20895#ifdef FEAT_DIFF
20896 check_topfill(curwin, TRUE);
20897#endif
20898 }
20899}
20900
20901/*
20902 * "winsaveview()" function
20903 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020904 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020905f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020906{
20907 dict_T *dict;
20908
Bram Moolenaara800b422010-06-27 01:15:55 +020020909 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020910 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020911 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020912
20913 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20914 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20915#ifdef FEAT_VIRTUALEDIT
20916 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20917#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020918 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020919 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20920
20921 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20922#ifdef FEAT_DIFF
20923 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20924#endif
20925 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20926 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20927}
20928
20929/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020930 * "winwidth(nr)" function
20931 */
20932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020933f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020934{
20935 win_T *wp;
20936
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020937 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020938 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020939 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020940 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010020941#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020942 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020943#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020944 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020945#endif
20946}
20947
Bram Moolenaar071d4272004-06-13 20:20:40 +000020948/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020949 * "wordcount()" function
20950 */
20951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020952f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010020953{
20954 if (rettv_dict_alloc(rettv) == FAIL)
20955 return;
20956 cursor_pos_info(rettv->vval.v_dict);
20957}
20958
20959/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020960 * Write list of strings to file
20961 */
20962 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020963write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020964{
20965 listitem_T *li;
20966 int c;
20967 int ret = OK;
20968 char_u *s;
20969
20970 for (li = list->lv_first; li != NULL; li = li->li_next)
20971 {
20972 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20973 {
20974 if (*s == '\n')
20975 c = putc(NUL, fd);
20976 else
20977 c = putc(*s, fd);
20978 if (c == EOF)
20979 {
20980 ret = FAIL;
20981 break;
20982 }
20983 }
20984 if (!binary || li->li_next != NULL)
20985 if (putc('\n', fd) == EOF)
20986 {
20987 ret = FAIL;
20988 break;
20989 }
20990 if (ret == FAIL)
20991 {
20992 EMSG(_(e_write));
20993 break;
20994 }
20995 }
20996 return ret;
20997}
20998
20999/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021000 * "writefile()" function
21001 */
21002 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021003f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021004{
21005 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021006 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021007 char_u *fname;
21008 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021009 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021010
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021011 if (check_restricted() || check_secure())
21012 return;
21013
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021014 if (argvars[0].v_type != VAR_LIST)
21015 {
21016 EMSG2(_(e_listarg), "writefile()");
21017 return;
21018 }
21019 if (argvars[0].vval.v_list == NULL)
21020 return;
21021
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021022 if (argvars[2].v_type != VAR_UNKNOWN)
21023 {
21024 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21025 binary = TRUE;
21026 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21027 append = TRUE;
21028 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021029
21030 /* Always open the file in binary mode, library functions have a mind of
21031 * their own about CR-LF conversion. */
21032 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021033 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21034 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021035 {
21036 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21037 ret = -1;
21038 }
21039 else
21040 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021041 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21042 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021043 fclose(fd);
21044 }
21045
21046 rettv->vval.v_number = ret;
21047}
21048
21049/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021050 * "xor(expr, expr)" function
21051 */
21052 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021053f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021054{
21055 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21056 ^ get_tv_number_chk(&argvars[1], NULL);
21057}
21058
21059
21060/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021061 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021062 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021063 */
21064 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021065var2fpos(
21066 typval_T *varp,
21067 int dollar_lnum, /* TRUE when $ is last line */
21068 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021069{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021070 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021071 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021072 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021073
Bram Moolenaara5525202006-03-02 22:52:09 +000021074 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021075 if (varp->v_type == VAR_LIST)
21076 {
21077 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021078 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021079 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021080 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021081
21082 l = varp->vval.v_list;
21083 if (l == NULL)
21084 return NULL;
21085
21086 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021087 pos.lnum = list_find_nr(l, 0L, &error);
21088 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021089 return NULL; /* invalid line number */
21090
21091 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021092 pos.col = list_find_nr(l, 1L, &error);
21093 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021094 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021095 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021096
21097 /* We accept "$" for the column number: last column. */
21098 li = list_find(l, 1L);
21099 if (li != NULL && li->li_tv.v_type == VAR_STRING
21100 && li->li_tv.vval.v_string != NULL
21101 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21102 pos.col = len + 1;
21103
Bram Moolenaara5525202006-03-02 22:52:09 +000021104 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021105 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021106 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021107 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021108
Bram Moolenaara5525202006-03-02 22:52:09 +000021109#ifdef FEAT_VIRTUALEDIT
21110 /* Get the virtual offset. Defaults to zero. */
21111 pos.coladd = list_find_nr(l, 2L, &error);
21112 if (error)
21113 pos.coladd = 0;
21114#endif
21115
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021116 return &pos;
21117 }
21118
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021119 name = get_tv_string_chk(varp);
21120 if (name == NULL)
21121 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021122 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021123 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021124 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21125 {
21126 if (VIsual_active)
21127 return &VIsual;
21128 return &curwin->w_cursor;
21129 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021130 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021131 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021132 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021133 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21134 return NULL;
21135 return pp;
21136 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021137
21138#ifdef FEAT_VIRTUALEDIT
21139 pos.coladd = 0;
21140#endif
21141
Bram Moolenaar477933c2007-07-17 14:32:23 +000021142 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021143 {
21144 pos.col = 0;
21145 if (name[1] == '0') /* "w0": first visible line */
21146 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021147 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021148 pos.lnum = curwin->w_topline;
21149 return &pos;
21150 }
21151 else if (name[1] == '$') /* "w$": last visible line */
21152 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021153 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021154 pos.lnum = curwin->w_botline - 1;
21155 return &pos;
21156 }
21157 }
21158 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021159 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021160 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021161 {
21162 pos.lnum = curbuf->b_ml.ml_line_count;
21163 pos.col = 0;
21164 }
21165 else
21166 {
21167 pos.lnum = curwin->w_cursor.lnum;
21168 pos.col = (colnr_T)STRLEN(ml_get_curline());
21169 }
21170 return &pos;
21171 }
21172 return NULL;
21173}
21174
21175/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021176 * Convert list in "arg" into a position and optional file number.
21177 * When "fnump" is NULL there is no file number, only 3 items.
21178 * Note that the column is passed on as-is, the caller may want to decrement
21179 * it to use 1 for the first column.
21180 * Return FAIL when conversion is not possible, doesn't check the position for
21181 * validity.
21182 */
21183 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021184list2fpos(
21185 typval_T *arg,
21186 pos_T *posp,
21187 int *fnump,
21188 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021189{
21190 list_T *l = arg->vval.v_list;
21191 long i = 0;
21192 long n;
21193
Bram Moolenaar493c1782014-05-28 14:34:46 +020021194 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21195 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021196 if (arg->v_type != VAR_LIST
21197 || l == NULL
21198 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021199 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021200 return FAIL;
21201
21202 if (fnump != NULL)
21203 {
21204 n = list_find_nr(l, i++, NULL); /* fnum */
21205 if (n < 0)
21206 return FAIL;
21207 if (n == 0)
21208 n = curbuf->b_fnum; /* current buffer */
21209 *fnump = n;
21210 }
21211
21212 n = list_find_nr(l, i++, NULL); /* lnum */
21213 if (n < 0)
21214 return FAIL;
21215 posp->lnum = n;
21216
21217 n = list_find_nr(l, i++, NULL); /* col */
21218 if (n < 0)
21219 return FAIL;
21220 posp->col = n;
21221
21222#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021223 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021224 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021225 posp->coladd = 0;
21226 else
21227 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021228#endif
21229
Bram Moolenaar493c1782014-05-28 14:34:46 +020021230 if (curswantp != NULL)
21231 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21232
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021233 return OK;
21234}
21235
21236/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021237 * Get the length of an environment variable name.
21238 * Advance "arg" to the first character after the name.
21239 * Return 0 for error.
21240 */
21241 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021242get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021243{
21244 char_u *p;
21245 int len;
21246
21247 for (p = *arg; vim_isIDc(*p); ++p)
21248 ;
21249 if (p == *arg) /* no name found */
21250 return 0;
21251
21252 len = (int)(p - *arg);
21253 *arg = p;
21254 return len;
21255}
21256
21257/*
21258 * Get the length of the name of a function or internal variable.
21259 * "arg" is advanced to the first non-white character after the name.
21260 * Return 0 if something is wrong.
21261 */
21262 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021263get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021264{
21265 char_u *p;
21266 int len;
21267
21268 /* Find the end of the name. */
21269 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021270 {
21271 if (*p == ':')
21272 {
21273 /* "s:" is start of "s:var", but "n:" is not and can be used in
21274 * slice "[n:]". Also "xx:" is not a namespace. */
21275 len = (int)(p - *arg);
21276 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21277 || len > 1)
21278 break;
21279 }
21280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021281 if (p == *arg) /* no name found */
21282 return 0;
21283
21284 len = (int)(p - *arg);
21285 *arg = skipwhite(p);
21286
21287 return len;
21288}
21289
21290/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021291 * Get the length of the name of a variable or function.
21292 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021293 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021294 * Return -1 if curly braces expansion failed.
21295 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021296 * If the name contains 'magic' {}'s, expand them and return the
21297 * expanded name in an allocated string via 'alias' - caller must free.
21298 */
21299 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021300get_name_len(
21301 char_u **arg,
21302 char_u **alias,
21303 int evaluate,
21304 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021305{
21306 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021307 char_u *p;
21308 char_u *expr_start;
21309 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021310
21311 *alias = NULL; /* default to no alias */
21312
21313 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21314 && (*arg)[2] == (int)KE_SNR)
21315 {
21316 /* hard coded <SNR>, already translated */
21317 *arg += 3;
21318 return get_id_len(arg) + 3;
21319 }
21320 len = eval_fname_script(*arg);
21321 if (len > 0)
21322 {
21323 /* literal "<SID>", "s:" or "<SNR>" */
21324 *arg += len;
21325 }
21326
Bram Moolenaar071d4272004-06-13 20:20:40 +000021327 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021328 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021329 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021330 p = find_name_end(*arg, &expr_start, &expr_end,
21331 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021332 if (expr_start != NULL)
21333 {
21334 char_u *temp_string;
21335
21336 if (!evaluate)
21337 {
21338 len += (int)(p - *arg);
21339 *arg = skipwhite(p);
21340 return len;
21341 }
21342
21343 /*
21344 * Include any <SID> etc in the expanded string:
21345 * Thus the -len here.
21346 */
21347 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21348 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021349 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021350 *alias = temp_string;
21351 *arg = skipwhite(p);
21352 return (int)STRLEN(temp_string);
21353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021354
21355 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021356 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021357 EMSG2(_(e_invexpr2), *arg);
21358
21359 return len;
21360}
21361
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021362/*
21363 * Find the end of a variable or function name, taking care of magic braces.
21364 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21365 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021366 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021367 * Return a pointer to just after the name. Equal to "arg" if there is no
21368 * valid name.
21369 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021370 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021371find_name_end(
21372 char_u *arg,
21373 char_u **expr_start,
21374 char_u **expr_end,
21375 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021376{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021377 int mb_nest = 0;
21378 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021379 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021380 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021381
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021382 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021383 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021384 *expr_start = NULL;
21385 *expr_end = NULL;
21386 }
21387
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021388 /* Quick check for valid starting character. */
21389 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21390 return arg;
21391
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021392 for (p = arg; *p != NUL
21393 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021394 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021395 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021396 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021397 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021398 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021399 if (*p == '\'')
21400 {
21401 /* skip over 'string' to avoid counting [ and ] inside it. */
21402 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21403 ;
21404 if (*p == NUL)
21405 break;
21406 }
21407 else if (*p == '"')
21408 {
21409 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21410 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21411 if (*p == '\\' && p[1] != NUL)
21412 ++p;
21413 if (*p == NUL)
21414 break;
21415 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021416 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21417 {
21418 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021419 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021420 len = (int)(p - arg);
21421 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021422 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021423 break;
21424 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021425
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021426 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021427 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021428 if (*p == '[')
21429 ++br_nest;
21430 else if (*p == ']')
21431 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021432 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021433
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021434 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021435 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021436 if (*p == '{')
21437 {
21438 mb_nest++;
21439 if (expr_start != NULL && *expr_start == NULL)
21440 *expr_start = p;
21441 }
21442 else if (*p == '}')
21443 {
21444 mb_nest--;
21445 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21446 *expr_end = p;
21447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021449 }
21450
21451 return p;
21452}
21453
21454/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021455 * Expands out the 'magic' {}'s in a variable/function name.
21456 * Note that this can call itself recursively, to deal with
21457 * constructs like foo{bar}{baz}{bam}
21458 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21459 * "in_start" ^
21460 * "expr_start" ^
21461 * "expr_end" ^
21462 * "in_end" ^
21463 *
21464 * Returns a new allocated string, which the caller must free.
21465 * Returns NULL for failure.
21466 */
21467 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021468make_expanded_name(
21469 char_u *in_start,
21470 char_u *expr_start,
21471 char_u *expr_end,
21472 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021473{
21474 char_u c1;
21475 char_u *retval = NULL;
21476 char_u *temp_result;
21477 char_u *nextcmd = NULL;
21478
21479 if (expr_end == NULL || in_end == NULL)
21480 return NULL;
21481 *expr_start = NUL;
21482 *expr_end = NUL;
21483 c1 = *in_end;
21484 *in_end = NUL;
21485
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021486 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021487 if (temp_result != NULL && nextcmd == NULL)
21488 {
21489 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21490 + (in_end - expr_end) + 1));
21491 if (retval != NULL)
21492 {
21493 STRCPY(retval, in_start);
21494 STRCAT(retval, temp_result);
21495 STRCAT(retval, expr_end + 1);
21496 }
21497 }
21498 vim_free(temp_result);
21499
21500 *in_end = c1; /* put char back for error messages */
21501 *expr_start = '{';
21502 *expr_end = '}';
21503
21504 if (retval != NULL)
21505 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021506 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021507 if (expr_start != NULL)
21508 {
21509 /* Further expansion! */
21510 temp_result = make_expanded_name(retval, expr_start,
21511 expr_end, temp_result);
21512 vim_free(retval);
21513 retval = temp_result;
21514 }
21515 }
21516
21517 return retval;
21518}
21519
21520/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021521 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021522 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021523 */
21524 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021525eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021526{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021527 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21528}
21529
21530/*
21531 * Return TRUE if character "c" can be used as the first character in a
21532 * variable or function name (excluding '{' and '}').
21533 */
21534 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021535eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021536{
21537 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021538}
21539
21540/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021541 * Set number v: variable to "val".
21542 */
21543 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021544set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021545{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021546 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021547}
21548
21549/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021550 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021551 */
21552 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021553get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021554{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021555 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021556}
21557
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021558/*
21559 * Get string v: variable value. Uses a static buffer, can only be used once.
21560 */
21561 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021562get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021563{
21564 return get_tv_string(&vimvars[idx].vv_tv);
21565}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021566
Bram Moolenaar071d4272004-06-13 20:20:40 +000021567/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021568 * Get List v: variable value. Caller must take care of reference count when
21569 * needed.
21570 */
21571 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021572get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021573{
21574 return vimvars[idx].vv_list;
21575}
21576
21577/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021578 * Set v:char to character "c".
21579 */
21580 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021581set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021582{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021583 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021584
21585#ifdef FEAT_MBYTE
21586 if (has_mbyte)
21587 buf[(*mb_char2bytes)(c, buf)] = NUL;
21588 else
21589#endif
21590 {
21591 buf[0] = c;
21592 buf[1] = NUL;
21593 }
21594 set_vim_var_string(VV_CHAR, buf, -1);
21595}
21596
21597/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021598 * Set v:count to "count" and v:count1 to "count1".
21599 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021600 */
21601 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021602set_vcount(
21603 long count,
21604 long count1,
21605 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021606{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021607 if (set_prevcount)
21608 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021609 vimvars[VV_COUNT].vv_nr = count;
21610 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021611}
21612
21613/*
21614 * Set string v: variable to a copy of "val".
21615 */
21616 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021617set_vim_var_string(
21618 int idx,
21619 char_u *val,
21620 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021621{
Bram Moolenaara542c682016-01-31 16:28:04 +010021622 clear_tv(&vimvars[idx].vv_di.di_tv);
21623 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021624 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021625 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021626 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021627 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021628 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021629 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021630}
21631
21632/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021633 * Set List v: variable to "val".
21634 */
21635 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021636set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021637{
Bram Moolenaara542c682016-01-31 16:28:04 +010021638 clear_tv(&vimvars[idx].vv_di.di_tv);
21639 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021640 vimvars[idx].vv_list = val;
21641 if (val != NULL)
21642 ++val->lv_refcount;
21643}
21644
21645/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021646 * Set Dictionary v: variable to "val".
21647 */
21648 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021649set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021650{
21651 int todo;
21652 hashitem_T *hi;
21653
Bram Moolenaara542c682016-01-31 16:28:04 +010021654 clear_tv(&vimvars[idx].vv_di.di_tv);
21655 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021656 vimvars[idx].vv_dict = val;
21657 if (val != NULL)
21658 {
21659 ++val->dv_refcount;
21660
21661 /* Set readonly */
21662 todo = (int)val->dv_hashtab.ht_used;
21663 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21664 {
21665 if (HASHITEM_EMPTY(hi))
21666 continue;
21667 --todo;
21668 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21669 }
21670 }
21671}
21672
21673/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021674 * Set v:register if needed.
21675 */
21676 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021677set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021678{
21679 char_u regname;
21680
21681 if (c == 0 || c == ' ')
21682 regname = '"';
21683 else
21684 regname = c;
21685 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021686 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021687 set_vim_var_string(VV_REG, &regname, 1);
21688}
21689
21690/*
21691 * Get or set v:exception. If "oldval" == NULL, return the current value.
21692 * Otherwise, restore the value to "oldval" and return NULL.
21693 * Must always be called in pairs to save and restore v:exception! Does not
21694 * take care of memory allocations.
21695 */
21696 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021697v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021698{
21699 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021700 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021701
Bram Moolenaare9a41262005-01-15 22:18:47 +000021702 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021703 return NULL;
21704}
21705
21706/*
21707 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21708 * Otherwise, restore the value to "oldval" and return NULL.
21709 * Must always be called in pairs to save and restore v:throwpoint! Does not
21710 * take care of memory allocations.
21711 */
21712 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021713v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021714{
21715 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021716 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021717
Bram Moolenaare9a41262005-01-15 22:18:47 +000021718 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021719 return NULL;
21720}
21721
21722#if defined(FEAT_AUTOCMD) || defined(PROTO)
21723/*
21724 * Set v:cmdarg.
21725 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21726 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21727 * Must always be called in pairs!
21728 */
21729 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021730set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021731{
21732 char_u *oldval;
21733 char_u *newval;
21734 unsigned len;
21735
Bram Moolenaare9a41262005-01-15 22:18:47 +000021736 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021737 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021738 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021739 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021740 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021741 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021742 }
21743
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021744 if (eap->force_bin == FORCE_BIN)
21745 len = 6;
21746 else if (eap->force_bin == FORCE_NOBIN)
21747 len = 8;
21748 else
21749 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021750
21751 if (eap->read_edit)
21752 len += 7;
21753
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021754 if (eap->force_ff != 0)
21755 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21756# ifdef FEAT_MBYTE
21757 if (eap->force_enc != 0)
21758 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021759 if (eap->bad_char != 0)
21760 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021761# endif
21762
21763 newval = alloc(len + 1);
21764 if (newval == NULL)
21765 return NULL;
21766
21767 if (eap->force_bin == FORCE_BIN)
21768 sprintf((char *)newval, " ++bin");
21769 else if (eap->force_bin == FORCE_NOBIN)
21770 sprintf((char *)newval, " ++nobin");
21771 else
21772 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021773
21774 if (eap->read_edit)
21775 STRCAT(newval, " ++edit");
21776
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021777 if (eap->force_ff != 0)
21778 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21779 eap->cmd + eap->force_ff);
21780# ifdef FEAT_MBYTE
21781 if (eap->force_enc != 0)
21782 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21783 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021784 if (eap->bad_char == BAD_KEEP)
21785 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21786 else if (eap->bad_char == BAD_DROP)
21787 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21788 else if (eap->bad_char != 0)
21789 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021790# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021791 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021792 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021793}
21794#endif
21795
21796/*
21797 * Get the value of internal variable "name".
21798 * Return OK or FAIL.
21799 */
21800 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021801get_var_tv(
21802 char_u *name,
21803 int len, /* length of "name" */
21804 typval_T *rettv, /* NULL when only checking existence */
21805 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21806 int verbose, /* may give error message */
21807 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021808{
21809 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021810 typval_T *tv = NULL;
21811 typval_T atv;
21812 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021813 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021814
21815 /* truncate the name, so that we can use strcmp() */
21816 cc = name[len];
21817 name[len] = NUL;
21818
21819 /*
21820 * Check for "b:changedtick".
21821 */
21822 if (STRCMP(name, "b:changedtick") == 0)
21823 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021824 atv.v_type = VAR_NUMBER;
21825 atv.vval.v_number = curbuf->b_changedtick;
21826 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021827 }
21828
21829 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021830 * Check for user-defined variables.
21831 */
21832 else
21833 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021834 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021835 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021836 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021837 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021838 if (dip != NULL)
21839 *dip = v;
21840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021841 }
21842
Bram Moolenaare9a41262005-01-15 22:18:47 +000021843 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021844 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021845 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021846 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021847 ret = FAIL;
21848 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021849 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021850 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021851
21852 name[len] = cc;
21853
21854 return ret;
21855}
21856
21857/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021858 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21859 * Also handle function call with Funcref variable: func(expr)
21860 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21861 */
21862 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021863handle_subscript(
21864 char_u **arg,
21865 typval_T *rettv,
21866 int evaluate, /* do more than finding the end */
21867 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021868{
21869 int ret = OK;
21870 dict_T *selfdict = NULL;
21871 char_u *s;
21872 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021873 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021874
21875 while (ret == OK
21876 && (**arg == '['
21877 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021878 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
21879 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021880 && !vim_iswhite(*(*arg - 1)))
21881 {
21882 if (**arg == '(')
21883 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010021884 partial_T *pt = NULL;
21885
Bram Moolenaard9fba312005-06-26 22:34:35 +000021886 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021887 if (evaluate)
21888 {
21889 functv = *rettv;
21890 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021891
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021892 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021893 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021894 {
21895 pt = functv.vval.v_partial;
21896 s = pt->pt_name;
21897 }
21898 else
21899 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021900 }
21901 else
21902 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021903 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021904 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021905 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000021906
21907 /* Clear the funcref afterwards, so that deleting it while
21908 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021909 if (evaluate)
21910 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021911
21912 /* Stop the expression evaluation when immediately aborting on
21913 * error, or when an interrupt occurred or an exception was thrown
21914 * but not caught. */
21915 if (aborting())
21916 {
21917 if (ret == OK)
21918 clear_tv(rettv);
21919 ret = FAIL;
21920 }
21921 dict_unref(selfdict);
21922 selfdict = NULL;
21923 }
21924 else /* **arg == '[' || **arg == '.' */
21925 {
21926 dict_unref(selfdict);
21927 if (rettv->v_type == VAR_DICT)
21928 {
21929 selfdict = rettv->vval.v_dict;
21930 if (selfdict != NULL)
21931 ++selfdict->dv_refcount;
21932 }
21933 else
21934 selfdict = NULL;
21935 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21936 {
21937 clear_tv(rettv);
21938 ret = FAIL;
21939 }
21940 }
21941 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021942
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021943 if ((rettv->v_type == VAR_FUNC || rettv->v_type == VAR_PARTIAL)
21944 && selfdict != NULL)
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021945 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021946 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
21947 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010021948 char_u *tofree = NULL;
21949 ufunc_T *fp;
21950 char_u fname_buf[FLEN_FIXED + 1];
21951 int error;
21952
21953 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021954 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010021955 fp = find_func(fname);
21956 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021957
21958 /* Turn "dict.Func" into a partial for "Func" with "dict". */
Bram Moolenaar65639032016-03-16 21:40:30 +010021959 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021960 {
Bram Moolenaar65639032016-03-16 21:40:30 +010021961 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
21962
21963 if (pt != NULL)
21964 {
21965 pt->pt_refcount = 1;
21966 pt->pt_dict = selfdict;
21967 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021968 if (rettv->v_type == VAR_FUNC)
21969 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010021970 /* Just a function: Take over the function name and use
21971 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021972 pt->pt_name = rettv->vval.v_string;
21973 }
21974 else
21975 {
21976 partial_T *ret_pt = rettv->vval.v_partial;
21977 int i;
21978
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010021979 /* Partial: copy the function name, use selfdict and copy
21980 * args. Can't take over name or args, the partial might
21981 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021982 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010021983 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021984 if (ret_pt->pt_argc > 0)
21985 {
21986 pt->pt_argv = (typval_T *)alloc(
21987 sizeof(typval_T) * ret_pt->pt_argc);
21988 if (pt->pt_argv == NULL)
21989 /* out of memory: drop the arguments */
21990 pt->pt_argc = 0;
21991 else
21992 {
21993 pt->pt_argc = ret_pt->pt_argc;
21994 for (i = 0; i < pt->pt_argc; i++)
21995 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
21996 }
21997 }
21998 partial_unref(ret_pt);
21999 }
Bram Moolenaar65639032016-03-16 21:40:30 +010022000 rettv->v_type = VAR_PARTIAL;
22001 rettv->vval.v_partial = pt;
22002 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022003 }
22004 }
22005
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022006 dict_unref(selfdict);
22007 return ret;
22008}
22009
22010/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022011 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022012 * value).
22013 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022014 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022015alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022016{
Bram Moolenaar33570922005-01-25 22:26:29 +000022017 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022018}
22019
22020/*
22021 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022022 * The string "s" must have been allocated, it is consumed.
22023 * Return NULL for out of memory, the variable otherwise.
22024 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022025 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022026alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022027{
Bram Moolenaar33570922005-01-25 22:26:29 +000022028 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022029
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022030 rettv = alloc_tv();
22031 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022032 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022033 rettv->v_type = VAR_STRING;
22034 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022035 }
22036 else
22037 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022038 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022039}
22040
22041/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022042 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022043 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022044 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022045free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022046{
22047 if (varp != NULL)
22048 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022049 switch (varp->v_type)
22050 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022051 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022052 func_unref(varp->vval.v_string);
22053 /*FALLTHROUGH*/
22054 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022055 vim_free(varp->vval.v_string);
22056 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022057 case VAR_PARTIAL:
22058 partial_unref(varp->vval.v_partial);
22059 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022060 case VAR_LIST:
22061 list_unref(varp->vval.v_list);
22062 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022063 case VAR_DICT:
22064 dict_unref(varp->vval.v_dict);
22065 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022066 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022067#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022068 job_unref(varp->vval.v_job);
22069 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022070#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022071 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022072#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022073 channel_unref(varp->vval.v_channel);
22074 break;
22075#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022076 case VAR_NUMBER:
22077 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022078 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022079 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022080 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022082 vim_free(varp);
22083 }
22084}
22085
22086/*
22087 * Free the memory for a variable value and set the value to NULL or 0.
22088 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022089 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022090clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022091{
22092 if (varp != NULL)
22093 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022094 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022095 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022096 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022097 func_unref(varp->vval.v_string);
22098 /*FALLTHROUGH*/
22099 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022100 vim_free(varp->vval.v_string);
22101 varp->vval.v_string = NULL;
22102 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022103 case VAR_PARTIAL:
22104 partial_unref(varp->vval.v_partial);
22105 varp->vval.v_partial = NULL;
22106 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022107 case VAR_LIST:
22108 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022109 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022110 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022111 case VAR_DICT:
22112 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022113 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022114 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022115 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022116 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022117 varp->vval.v_number = 0;
22118 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022119 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022120#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022121 varp->vval.v_float = 0.0;
22122 break;
22123#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022124 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022125#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022126 job_unref(varp->vval.v_job);
22127 varp->vval.v_job = NULL;
22128#endif
22129 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022130 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022131#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022132 channel_unref(varp->vval.v_channel);
22133 varp->vval.v_channel = NULL;
22134#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022135 case VAR_UNKNOWN:
22136 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022137 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022138 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022139 }
22140}
22141
22142/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022143 * Set the value of a variable to NULL without freeing items.
22144 */
22145 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022146init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022147{
22148 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022149 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022150}
22151
22152/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022153 * Get the number value of a variable.
22154 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022155 * For incompatible types, return 0.
22156 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22157 * caller of incompatible types: it sets *denote to TRUE if "denote"
22158 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022159 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022160 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022161get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022162{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022163 int error = FALSE;
22164
22165 return get_tv_number_chk(varp, &error); /* return 0L on error */
22166}
22167
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022168 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022169get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022170{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022171 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022172
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022173 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022174 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022175 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022176 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022177 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022178#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022179 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022180 break;
22181#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022182 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022183 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022184 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022185 break;
22186 case VAR_STRING:
22187 if (varp->vval.v_string != NULL)
22188 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022189 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022190 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022191 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022192 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022193 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022194 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022195 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022196 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022197 case VAR_SPECIAL:
22198 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22199 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022200 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022201#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022202 EMSG(_("E910: Using a Job as a Number"));
22203 break;
22204#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022205 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022206#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022207 EMSG(_("E913: Using a Channel as a Number"));
22208 break;
22209#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022210 case VAR_UNKNOWN:
22211 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022212 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022213 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022214 if (denote == NULL) /* useful for values that must be unsigned */
22215 n = -1;
22216 else
22217 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022218 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022219}
22220
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022221#ifdef FEAT_FLOAT
22222 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022223get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022224{
22225 switch (varp->v_type)
22226 {
22227 case VAR_NUMBER:
22228 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022229 case VAR_FLOAT:
22230 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022231 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022232 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022233 EMSG(_("E891: Using a Funcref as a Float"));
22234 break;
22235 case VAR_STRING:
22236 EMSG(_("E892: Using a String as a Float"));
22237 break;
22238 case VAR_LIST:
22239 EMSG(_("E893: Using a List as a Float"));
22240 break;
22241 case VAR_DICT:
22242 EMSG(_("E894: Using a Dictionary as a Float"));
22243 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022244 case VAR_SPECIAL:
22245 EMSG(_("E907: Using a special value as a Float"));
22246 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022247 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022248# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022249 EMSG(_("E911: Using a Job as a Float"));
22250 break;
22251# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022252 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022253# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022254 EMSG(_("E914: Using a Channel as a Float"));
22255 break;
22256# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022257 case VAR_UNKNOWN:
22258 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022259 break;
22260 }
22261 return 0;
22262}
22263#endif
22264
Bram Moolenaar071d4272004-06-13 20:20:40 +000022265/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022266 * Get the lnum from the first argument.
22267 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022268 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022269 */
22270 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022271get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022272{
Bram Moolenaar33570922005-01-25 22:26:29 +000022273 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022274 linenr_T lnum;
22275
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022276 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022277 if (lnum == 0) /* no valid number, try using line() */
22278 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022279 rettv.v_type = VAR_NUMBER;
22280 f_line(argvars, &rettv);
22281 lnum = rettv.vval.v_number;
22282 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022283 }
22284 return lnum;
22285}
22286
22287/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022288 * Get the lnum from the first argument.
22289 * Also accepts "$", then "buf" is used.
22290 * Returns 0 on error.
22291 */
22292 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022293get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022294{
22295 if (argvars[0].v_type == VAR_STRING
22296 && argvars[0].vval.v_string != NULL
22297 && argvars[0].vval.v_string[0] == '$'
22298 && buf != NULL)
22299 return buf->b_ml.ml_line_count;
22300 return get_tv_number_chk(&argvars[0], NULL);
22301}
22302
22303/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022304 * Get the string value of a variable.
22305 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022306 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22307 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022308 * If the String variable has never been set, return an empty string.
22309 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022310 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22311 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022312 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022313 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022314get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022315{
22316 static char_u mybuf[NUMBUFLEN];
22317
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022318 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022319}
22320
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022321 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022322get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022323{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022324 char_u *res = get_tv_string_buf_chk(varp, buf);
22325
22326 return res != NULL ? res : (char_u *)"";
22327}
22328
Bram Moolenaar7d647822014-04-05 21:28:56 +020022329/*
22330 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22331 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022332 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022333get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022334{
22335 static char_u mybuf[NUMBUFLEN];
22336
22337 return get_tv_string_buf_chk(varp, mybuf);
22338}
22339
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022340 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022341get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022342{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022343 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022344 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022345 case VAR_NUMBER:
22346 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22347 return buf;
22348 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022349 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022350 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022351 break;
22352 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022353 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022354 break;
22355 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022356 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022357 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022358 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022359#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022360 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022361 break;
22362#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022363 case VAR_STRING:
22364 if (varp->vval.v_string != NULL)
22365 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022366 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022367 case VAR_SPECIAL:
22368 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22369 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022370 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022371#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022372 {
22373 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022374 char *status;
22375
22376 if (job == NULL)
22377 return (char_u *)"no process";
22378 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022379 : job->jv_status == JOB_ENDED ? "dead"
22380 : "run";
22381# ifdef UNIX
22382 vim_snprintf((char *)buf, NUMBUFLEN,
22383 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022384# elif defined(WIN32)
22385 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022386 "process %ld %s",
22387 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022388 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022389# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022390 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022391 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22392# endif
22393 return buf;
22394 }
22395#endif
22396 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022397 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022398#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022399 {
22400 channel_T *channel = varp->vval.v_channel;
22401 char *status = channel_status(channel);
22402
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022403 if (channel == NULL)
22404 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22405 else
22406 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022407 "channel %d %s", channel->ch_id, status);
22408 return buf;
22409 }
22410#endif
22411 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022412 case VAR_UNKNOWN:
22413 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022414 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022415 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022416 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022417}
22418
22419/*
22420 * Find variable "name" in the list of variables.
22421 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022422 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022423 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022424 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022425 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022426 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022427find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022428{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022429 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022430 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022431
Bram Moolenaara7043832005-01-21 11:56:39 +000022432 ht = find_var_ht(name, &varname);
22433 if (htp != NULL)
22434 *htp = ht;
22435 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022436 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022437 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022438}
22439
22440/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022441 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022442 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022443 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022444 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022445find_var_in_ht(
22446 hashtab_T *ht,
22447 int htname,
22448 char_u *varname,
22449 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022450{
Bram Moolenaar33570922005-01-25 22:26:29 +000022451 hashitem_T *hi;
22452
22453 if (*varname == NUL)
22454 {
22455 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022456 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022457 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022458 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022459 case 'g': return &globvars_var;
22460 case 'v': return &vimvars_var;
22461 case 'b': return &curbuf->b_bufvar;
22462 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022463#ifdef FEAT_WINDOWS
22464 case 't': return &curtab->tp_winvar;
22465#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022466 case 'l': return current_funccal == NULL
22467 ? NULL : &current_funccal->l_vars_var;
22468 case 'a': return current_funccal == NULL
22469 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022470 }
22471 return NULL;
22472 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022473
22474 hi = hash_find(ht, varname);
22475 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022476 {
22477 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022478 * worked find the variable again. Don't auto-load a script if it was
22479 * loaded already, otherwise it would be loaded every time when
22480 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022481 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022482 {
22483 /* Note: script_autoload() may make "hi" invalid. It must either
22484 * be obtained again or not used. */
22485 if (!script_autoload(varname, FALSE) || aborting())
22486 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022487 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022488 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022489 if (HASHITEM_EMPTY(hi))
22490 return NULL;
22491 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022492 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022493}
22494
22495/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022496 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022497 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022498 * Set "varname" to the start of name without ':'.
22499 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022500 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022501find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022502{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022503 hashitem_T *hi;
22504
Bram Moolenaar73627d02015-08-11 15:46:09 +020022505 if (name[0] == NUL)
22506 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022507 if (name[1] != ':')
22508 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022509 /* The name must not start with a colon or #. */
22510 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022511 return NULL;
22512 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022513
22514 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022515 hi = hash_find(&compat_hashtab, name);
22516 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022517 return &compat_hashtab;
22518
Bram Moolenaar071d4272004-06-13 20:20:40 +000022519 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022520 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022521 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022522 }
22523 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022524 if (*name == 'g') /* global variable */
22525 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022526 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22527 */
22528 if (vim_strchr(name + 2, ':') != NULL
22529 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022530 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022531 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022532 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022533 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022534 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022535#ifdef FEAT_WINDOWS
22536 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022537 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022538#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022539 if (*name == 'v') /* v: variable */
22540 return &vimvarht;
22541 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022542 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022543 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022544 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022545 if (*name == 's' /* script variable */
22546 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22547 return &SCRIPT_VARS(current_SID);
22548 return NULL;
22549}
22550
22551/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022552 * Get function call environment based on bactrace debug level
22553 */
22554 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022555get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022556{
22557 int i;
22558 funccall_T *funccal;
22559 funccall_T *temp_funccal;
22560
22561 funccal = current_funccal;
22562 if (debug_backtrace_level > 0)
22563 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022564 for (i = 0; i < debug_backtrace_level; i++)
22565 {
22566 temp_funccal = funccal->caller;
22567 if (temp_funccal)
22568 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022569 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022570 /* backtrace level overflow. reset to max */
22571 debug_backtrace_level = i;
22572 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022573 }
22574 return funccal;
22575}
22576
22577/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022578 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022579 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022580 * Returns NULL when it doesn't exist.
22581 */
22582 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022583get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022584{
Bram Moolenaar33570922005-01-25 22:26:29 +000022585 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022586
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022587 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022588 if (v == NULL)
22589 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022590 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022591}
22592
22593/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022594 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022595 * sourcing this script and when executing functions defined in the script.
22596 */
22597 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022598new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022599{
Bram Moolenaara7043832005-01-21 11:56:39 +000022600 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022601 hashtab_T *ht;
22602 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022603
Bram Moolenaar071d4272004-06-13 20:20:40 +000022604 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22605 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022606 /* Re-allocating ga_data means that an ht_array pointing to
22607 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022608 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022609 for (i = 1; i <= ga_scripts.ga_len; ++i)
22610 {
22611 ht = &SCRIPT_VARS(i);
22612 if (ht->ht_mask == HT_INIT_SIZE - 1)
22613 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022614 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022615 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022616 }
22617
Bram Moolenaar071d4272004-06-13 20:20:40 +000022618 while (ga_scripts.ga_len < id)
22619 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022620 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022621 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022622 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022623 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022624 }
22625 }
22626}
22627
22628/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022629 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22630 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022631 */
22632 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022633init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022634{
Bram Moolenaar33570922005-01-25 22:26:29 +000022635 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022636 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022637 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022638 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022639 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022640 dict_var->di_tv.vval.v_dict = dict;
22641 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022642 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022643 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22644 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022645}
22646
22647/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022648 * Unreference a dictionary initialized by init_var_dict().
22649 */
22650 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022651unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022652{
22653 /* Now the dict needs to be freed if no one else is using it, go back to
22654 * normal reference counting. */
22655 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22656 dict_unref(dict);
22657}
22658
22659/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022660 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022661 * Frees all allocated variables and the value they contain.
22662 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022663 */
22664 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022665vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022666{
22667 vars_clear_ext(ht, TRUE);
22668}
22669
22670/*
22671 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22672 */
22673 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022674vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022675{
Bram Moolenaara7043832005-01-21 11:56:39 +000022676 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022677 hashitem_T *hi;
22678 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022679
Bram Moolenaar33570922005-01-25 22:26:29 +000022680 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022681 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022682 for (hi = ht->ht_array; todo > 0; ++hi)
22683 {
22684 if (!HASHITEM_EMPTY(hi))
22685 {
22686 --todo;
22687
Bram Moolenaar33570922005-01-25 22:26:29 +000022688 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022689 * ht_array might change then. hash_clear() takes care of it
22690 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022691 v = HI2DI(hi);
22692 if (free_val)
22693 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022694 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022695 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022696 }
22697 }
22698 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022699 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022700}
22701
Bram Moolenaara7043832005-01-21 11:56:39 +000022702/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022703 * Delete a variable from hashtab "ht" at item "hi".
22704 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022705 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022706 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022707delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022708{
Bram Moolenaar33570922005-01-25 22:26:29 +000022709 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022710
22711 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022712 clear_tv(&di->di_tv);
22713 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022714}
22715
22716/*
22717 * List the value of one internal variable.
22718 */
22719 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022720list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022721{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022722 char_u *tofree;
22723 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022724 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022725
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022726 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022727 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022728 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022729 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022730}
22731
Bram Moolenaar071d4272004-06-13 20:20:40 +000022732 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022733list_one_var_a(
22734 char_u *prefix,
22735 char_u *name,
22736 int type,
22737 char_u *string,
22738 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022739{
Bram Moolenaar31859182007-08-14 20:41:13 +000022740 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22741 msg_start();
22742 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022743 if (name != NULL) /* "a:" vars don't have a name stored */
22744 msg_puts(name);
22745 msg_putchar(' ');
22746 msg_advance(22);
22747 if (type == VAR_NUMBER)
22748 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022749 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022750 msg_putchar('*');
22751 else if (type == VAR_LIST)
22752 {
22753 msg_putchar('[');
22754 if (*string == '[')
22755 ++string;
22756 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022757 else if (type == VAR_DICT)
22758 {
22759 msg_putchar('{');
22760 if (*string == '{')
22761 ++string;
22762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022763 else
22764 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022765
Bram Moolenaar071d4272004-06-13 20:20:40 +000022766 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022767
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022768 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022769 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022770 if (*first)
22771 {
22772 msg_clr_eos();
22773 *first = FALSE;
22774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022775}
22776
22777/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022778 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022779 * If the variable already exists, the value is updated.
22780 * Otherwise the variable is created.
22781 */
22782 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022783set_var(
22784 char_u *name,
22785 typval_T *tv,
22786 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022787{
Bram Moolenaar33570922005-01-25 22:26:29 +000022788 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022789 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022790 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022791
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022792 ht = find_var_ht(name, &varname);
22793 if (ht == NULL || *varname == NUL)
22794 {
22795 EMSG2(_(e_illvar), name);
22796 return;
22797 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022798 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022799
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022800 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
22801 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022802 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022803
Bram Moolenaar33570922005-01-25 22:26:29 +000022804 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022805 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022806 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022807 if (var_check_ro(v->di_flags, name, FALSE)
22808 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022809 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022810
22811 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022812 * Handle setting internal v: variables separately where needed to
22813 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022814 */
22815 if (ht == &vimvarht)
22816 {
22817 if (v->di_tv.v_type == VAR_STRING)
22818 {
22819 vim_free(v->di_tv.vval.v_string);
22820 if (copy || tv->v_type != VAR_STRING)
22821 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22822 else
22823 {
22824 /* Take over the string to avoid an extra alloc/free. */
22825 v->di_tv.vval.v_string = tv->vval.v_string;
22826 tv->vval.v_string = NULL;
22827 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022828 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022829 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022830 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022831 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022832 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022833 if (STRCMP(varname, "searchforward") == 0)
22834 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022835#ifdef FEAT_SEARCH_EXTRA
22836 else if (STRCMP(varname, "hlsearch") == 0)
22837 {
22838 no_hlsearch = !v->di_tv.vval.v_number;
22839 redraw_all_later(SOME_VALID);
22840 }
22841#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022842 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022843 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022844 else if (v->di_tv.v_type != tv->v_type)
22845 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022846 }
22847
22848 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022849 }
22850 else /* add a new variable */
22851 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022852 /* Can't add "v:" variable. */
22853 if (ht == &vimvarht)
22854 {
22855 EMSG2(_(e_illvar), name);
22856 return;
22857 }
22858
Bram Moolenaar92124a32005-06-17 22:03:40 +000022859 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022860 if (!valid_varname(varname))
22861 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022862
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022863 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22864 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022865 if (v == NULL)
22866 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022867 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022868 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022869 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022870 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022871 return;
22872 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022873 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022874 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022875
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022876 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022877 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022878 else
22879 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022880 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022881 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022882 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022884}
22885
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022886/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022887 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022888 * Also give an error message.
22889 */
22890 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022891var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022892{
22893 if (flags & DI_FLAGS_RO)
22894 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022895 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022896 return TRUE;
22897 }
22898 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22899 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022900 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022901 return TRUE;
22902 }
22903 return FALSE;
22904}
22905
22906/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022907 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22908 * Also give an error message.
22909 */
22910 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022911var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022912{
22913 if (flags & DI_FLAGS_FIX)
22914 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022915 EMSG2(_("E795: Cannot delete variable %s"),
22916 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022917 return TRUE;
22918 }
22919 return FALSE;
22920}
22921
22922/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022923 * Check if a funcref is assigned to a valid variable name.
22924 * Return TRUE and give an error if not.
22925 */
22926 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022927var_check_func_name(
22928 char_u *name, /* points to start of variable name */
22929 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022930{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022931 /* Allow for w: b: s: and t:. */
22932 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022933 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22934 ? name[2] : name[0]))
22935 {
22936 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22937 name);
22938 return TRUE;
22939 }
22940 /* Don't allow hiding a function. When "v" is not NULL we might be
22941 * assigning another function to the same var, the type is checked
22942 * below. */
22943 if (new_var && function_exists(name))
22944 {
22945 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22946 name);
22947 return TRUE;
22948 }
22949 return FALSE;
22950}
22951
22952/*
22953 * Check if a variable name is valid.
22954 * Return FALSE and give an error if not.
22955 */
22956 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022957valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022958{
22959 char_u *p;
22960
22961 for (p = varname; *p != NUL; ++p)
22962 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22963 && *p != AUTOLOAD_CHAR)
22964 {
22965 EMSG2(_(e_illvar), varname);
22966 return FALSE;
22967 }
22968 return TRUE;
22969}
22970
22971/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022972 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022973 * Also give an error message, using "name" or _("name") when use_gettext is
22974 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022975 */
22976 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022977tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022978{
22979 if (lock & VAR_LOCKED)
22980 {
22981 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022982 name == NULL ? (char_u *)_("Unknown")
22983 : use_gettext ? (char_u *)_(name)
22984 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022985 return TRUE;
22986 }
22987 if (lock & VAR_FIXED)
22988 {
22989 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022990 name == NULL ? (char_u *)_("Unknown")
22991 : use_gettext ? (char_u *)_(name)
22992 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022993 return TRUE;
22994 }
22995 return FALSE;
22996}
22997
22998/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022999 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023000 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023001 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023002 * It is OK for "from" and "to" to point to the same item. This is used to
23003 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023004 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023005 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023006copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023007{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023008 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023009 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023010 switch (from->v_type)
23011 {
23012 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023013 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023014 to->vval.v_number = from->vval.v_number;
23015 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023016 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023017#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023018 to->vval.v_float = from->vval.v_float;
23019 break;
23020#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023021 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023022#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023023 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023024 if (to->vval.v_job != NULL)
23025 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023026 break;
23027#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023028 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023029#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023030 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023031 if (to->vval.v_channel != NULL)
23032 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023033 break;
23034#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023035 case VAR_STRING:
23036 case VAR_FUNC:
23037 if (from->vval.v_string == NULL)
23038 to->vval.v_string = NULL;
23039 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023040 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023041 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023042 if (from->v_type == VAR_FUNC)
23043 func_ref(to->vval.v_string);
23044 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023045 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023046 case VAR_PARTIAL:
23047 if (from->vval.v_partial == NULL)
23048 to->vval.v_partial = NULL;
23049 else
23050 {
23051 to->vval.v_partial = from->vval.v_partial;
23052 ++to->vval.v_partial->pt_refcount;
23053 }
23054 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023055 case VAR_LIST:
23056 if (from->vval.v_list == NULL)
23057 to->vval.v_list = NULL;
23058 else
23059 {
23060 to->vval.v_list = from->vval.v_list;
23061 ++to->vval.v_list->lv_refcount;
23062 }
23063 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023064 case VAR_DICT:
23065 if (from->vval.v_dict == NULL)
23066 to->vval.v_dict = NULL;
23067 else
23068 {
23069 to->vval.v_dict = from->vval.v_dict;
23070 ++to->vval.v_dict->dv_refcount;
23071 }
23072 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023073 case VAR_UNKNOWN:
23074 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023075 break;
23076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023077}
23078
23079/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023080 * Make a copy of an item.
23081 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023082 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23083 * reference to an already copied list/dict can be used.
23084 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023085 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023086 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023087item_copy(
23088 typval_T *from,
23089 typval_T *to,
23090 int deep,
23091 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023092{
23093 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023094 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023095
Bram Moolenaar33570922005-01-25 22:26:29 +000023096 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023097 {
23098 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023099 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023100 }
23101 ++recurse;
23102
23103 switch (from->v_type)
23104 {
23105 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023106 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023107 case VAR_STRING:
23108 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023109 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010023110 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023111 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023112 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023113 copy_tv(from, to);
23114 break;
23115 case VAR_LIST:
23116 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023117 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023118 if (from->vval.v_list == NULL)
23119 to->vval.v_list = NULL;
23120 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23121 {
23122 /* use the copy made earlier */
23123 to->vval.v_list = from->vval.v_list->lv_copylist;
23124 ++to->vval.v_list->lv_refcount;
23125 }
23126 else
23127 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23128 if (to->vval.v_list == NULL)
23129 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023130 break;
23131 case VAR_DICT:
23132 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023133 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023134 if (from->vval.v_dict == NULL)
23135 to->vval.v_dict = NULL;
23136 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23137 {
23138 /* use the copy made earlier */
23139 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23140 ++to->vval.v_dict->dv_refcount;
23141 }
23142 else
23143 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23144 if (to->vval.v_dict == NULL)
23145 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023146 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023147 case VAR_UNKNOWN:
23148 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023149 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023150 }
23151 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023152 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023153}
23154
23155/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023156 * ":echo expr1 ..." print each argument separated with a space, add a
23157 * newline at the end.
23158 * ":echon expr1 ..." print each argument plain.
23159 */
23160 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023161ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023162{
23163 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023164 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023165 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023166 char_u *p;
23167 int needclr = TRUE;
23168 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023169 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023170
23171 if (eap->skip)
23172 ++emsg_skip;
23173 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23174 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023175 /* If eval1() causes an error message the text from the command may
23176 * still need to be cleared. E.g., "echo 22,44". */
23177 need_clr_eos = needclr;
23178
Bram Moolenaar071d4272004-06-13 20:20:40 +000023179 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023180 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023181 {
23182 /*
23183 * Report the invalid expression unless the expression evaluation
23184 * has been cancelled due to an aborting error, an interrupt, or an
23185 * exception.
23186 */
23187 if (!aborting())
23188 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023189 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023190 break;
23191 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023192 need_clr_eos = FALSE;
23193
Bram Moolenaar071d4272004-06-13 20:20:40 +000023194 if (!eap->skip)
23195 {
23196 if (atstart)
23197 {
23198 atstart = FALSE;
23199 /* Call msg_start() after eval1(), evaluating the expression
23200 * may cause a message to appear. */
23201 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023202 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023203 /* Mark the saved text as finishing the line, so that what
23204 * follows is displayed on a new line when scrolling back
23205 * at the more prompt. */
23206 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023207 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023209 }
23210 else if (eap->cmdidx == CMD_echo)
23211 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023212 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023213 if (p != NULL)
23214 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023215 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023216 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023217 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023218 if (*p != TAB && needclr)
23219 {
23220 /* remove any text still there from the command */
23221 msg_clr_eos();
23222 needclr = FALSE;
23223 }
23224 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023225 }
23226 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023227 {
23228#ifdef FEAT_MBYTE
23229 if (has_mbyte)
23230 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023231 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023232
23233 (void)msg_outtrans_len_attr(p, i, echo_attr);
23234 p += i - 1;
23235 }
23236 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023237#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023238 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023240 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023241 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023242 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023243 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023244 arg = skipwhite(arg);
23245 }
23246 eap->nextcmd = check_nextcmd(arg);
23247
23248 if (eap->skip)
23249 --emsg_skip;
23250 else
23251 {
23252 /* remove text that may still be there from the command */
23253 if (needclr)
23254 msg_clr_eos();
23255 if (eap->cmdidx == CMD_echo)
23256 msg_end();
23257 }
23258}
23259
23260/*
23261 * ":echohl {name}".
23262 */
23263 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023264ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023265{
23266 int id;
23267
23268 id = syn_name2id(eap->arg);
23269 if (id == 0)
23270 echo_attr = 0;
23271 else
23272 echo_attr = syn_id2attr(id);
23273}
23274
23275/*
23276 * ":execute expr1 ..." execute the result of an expression.
23277 * ":echomsg expr1 ..." Print a message
23278 * ":echoerr expr1 ..." Print an error
23279 * Each gets spaces around each argument and a newline at the end for
23280 * echo commands
23281 */
23282 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023283ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023284{
23285 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023286 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023287 int ret = OK;
23288 char_u *p;
23289 garray_T ga;
23290 int len;
23291 int save_did_emsg;
23292
23293 ga_init2(&ga, 1, 80);
23294
23295 if (eap->skip)
23296 ++emsg_skip;
23297 while (*arg != NUL && *arg != '|' && *arg != '\n')
23298 {
23299 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023300 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023301 {
23302 /*
23303 * Report the invalid expression unless the expression evaluation
23304 * has been cancelled due to an aborting error, an interrupt, or an
23305 * exception.
23306 */
23307 if (!aborting())
23308 EMSG2(_(e_invexpr2), p);
23309 ret = FAIL;
23310 break;
23311 }
23312
23313 if (!eap->skip)
23314 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023315 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023316 len = (int)STRLEN(p);
23317 if (ga_grow(&ga, len + 2) == FAIL)
23318 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023319 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023320 ret = FAIL;
23321 break;
23322 }
23323 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023324 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023325 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023326 ga.ga_len += len;
23327 }
23328
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023329 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023330 arg = skipwhite(arg);
23331 }
23332
23333 if (ret != FAIL && ga.ga_data != NULL)
23334 {
23335 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023336 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023337 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023338 out_flush();
23339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023340 else if (eap->cmdidx == CMD_echoerr)
23341 {
23342 /* We don't want to abort following commands, restore did_emsg. */
23343 save_did_emsg = did_emsg;
23344 EMSG((char_u *)ga.ga_data);
23345 if (!force_abort)
23346 did_emsg = save_did_emsg;
23347 }
23348 else if (eap->cmdidx == CMD_execute)
23349 do_cmdline((char_u *)ga.ga_data,
23350 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23351 }
23352
23353 ga_clear(&ga);
23354
23355 if (eap->skip)
23356 --emsg_skip;
23357
23358 eap->nextcmd = check_nextcmd(arg);
23359}
23360
23361/*
23362 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23363 * "arg" points to the "&" or '+' when called, to "option" when returning.
23364 * Returns NULL when no option name found. Otherwise pointer to the char
23365 * after the option name.
23366 */
23367 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023368find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023369{
23370 char_u *p = *arg;
23371
23372 ++p;
23373 if (*p == 'g' && p[1] == ':')
23374 {
23375 *opt_flags = OPT_GLOBAL;
23376 p += 2;
23377 }
23378 else if (*p == 'l' && p[1] == ':')
23379 {
23380 *opt_flags = OPT_LOCAL;
23381 p += 2;
23382 }
23383 else
23384 *opt_flags = 0;
23385
23386 if (!ASCII_ISALPHA(*p))
23387 return NULL;
23388 *arg = p;
23389
23390 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23391 p += 4; /* termcap option */
23392 else
23393 while (ASCII_ISALPHA(*p))
23394 ++p;
23395 return p;
23396}
23397
23398/*
23399 * ":function"
23400 */
23401 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023402ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023403{
23404 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023405 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023406 int j;
23407 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023408 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023409 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023410 char_u *name = NULL;
23411 char_u *p;
23412 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023413 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023414 garray_T newargs;
23415 garray_T newlines;
23416 int varargs = FALSE;
23417 int mustend = FALSE;
23418 int flags = 0;
23419 ufunc_T *fp;
23420 int indent;
23421 int nesting;
23422 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023423 dictitem_T *v;
23424 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023425 static int func_nr = 0; /* number for nameless function */
23426 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023427 hashtab_T *ht;
23428 int todo;
23429 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023430 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023431
23432 /*
23433 * ":function" without argument: list functions.
23434 */
23435 if (ends_excmd(*eap->arg))
23436 {
23437 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023438 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023439 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023440 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023441 {
23442 if (!HASHITEM_EMPTY(hi))
23443 {
23444 --todo;
23445 fp = HI2UF(hi);
23446 if (!isdigit(*fp->uf_name))
23447 list_func_head(fp, FALSE);
23448 }
23449 }
23450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023451 eap->nextcmd = check_nextcmd(eap->arg);
23452 return;
23453 }
23454
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023455 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023456 * ":function /pat": list functions matching pattern.
23457 */
23458 if (*eap->arg == '/')
23459 {
23460 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23461 if (!eap->skip)
23462 {
23463 regmatch_T regmatch;
23464
23465 c = *p;
23466 *p = NUL;
23467 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23468 *p = c;
23469 if (regmatch.regprog != NULL)
23470 {
23471 regmatch.rm_ic = p_ic;
23472
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023473 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023474 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23475 {
23476 if (!HASHITEM_EMPTY(hi))
23477 {
23478 --todo;
23479 fp = HI2UF(hi);
23480 if (!isdigit(*fp->uf_name)
23481 && vim_regexec(&regmatch, fp->uf_name, 0))
23482 list_func_head(fp, FALSE);
23483 }
23484 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023485 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023486 }
23487 }
23488 if (*p == '/')
23489 ++p;
23490 eap->nextcmd = check_nextcmd(p);
23491 return;
23492 }
23493
23494 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023495 * Get the function name. There are these situations:
23496 * func normal function name
23497 * "name" == func, "fudi.fd_dict" == NULL
23498 * dict.func new dictionary entry
23499 * "name" == NULL, "fudi.fd_dict" set,
23500 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23501 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023502 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023503 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23504 * dict.func existing dict entry that's not a Funcref
23505 * "name" == NULL, "fudi.fd_dict" set,
23506 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023507 * s:func script-local function name
23508 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023509 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023510 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010023511 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023512 paren = (vim_strchr(p, '(') != NULL);
23513 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023514 {
23515 /*
23516 * Return on an invalid expression in braces, unless the expression
23517 * evaluation has been cancelled due to an aborting error, an
23518 * interrupt, or an exception.
23519 */
23520 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023521 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023522 if (!eap->skip && fudi.fd_newkey != NULL)
23523 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023524 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023525 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023527 else
23528 eap->skip = TRUE;
23529 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023530
Bram Moolenaar071d4272004-06-13 20:20:40 +000023531 /* An error in a function call during evaluation of an expression in magic
23532 * braces should not cause the function not to be defined. */
23533 saved_did_emsg = did_emsg;
23534 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023535
23536 /*
23537 * ":function func" with only function name: list function.
23538 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023539 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023540 {
23541 if (!ends_excmd(*skipwhite(p)))
23542 {
23543 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023544 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023545 }
23546 eap->nextcmd = check_nextcmd(p);
23547 if (eap->nextcmd != NULL)
23548 *p = NUL;
23549 if (!eap->skip && !got_int)
23550 {
23551 fp = find_func(name);
23552 if (fp != NULL)
23553 {
23554 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023555 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023556 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023557 if (FUNCLINE(fp, j) == NULL)
23558 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023559 msg_putchar('\n');
23560 msg_outnum((long)(j + 1));
23561 if (j < 9)
23562 msg_putchar(' ');
23563 if (j < 99)
23564 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023565 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023566 out_flush(); /* show a line at a time */
23567 ui_breakcheck();
23568 }
23569 if (!got_int)
23570 {
23571 msg_putchar('\n');
23572 msg_puts((char_u *)" endfunction");
23573 }
23574 }
23575 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023576 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023577 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023578 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023579 }
23580
23581 /*
23582 * ":function name(arg1, arg2)" Define function.
23583 */
23584 p = skipwhite(p);
23585 if (*p != '(')
23586 {
23587 if (!eap->skip)
23588 {
23589 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023590 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023591 }
23592 /* attempt to continue by skipping some text */
23593 if (vim_strchr(p, '(') != NULL)
23594 p = vim_strchr(p, '(');
23595 }
23596 p = skipwhite(p + 1);
23597
23598 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23599 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23600
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023601 if (!eap->skip)
23602 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023603 /* Check the name of the function. Unless it's a dictionary function
23604 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023605 if (name != NULL)
23606 arg = name;
23607 else
23608 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023609 if (arg != NULL && (fudi.fd_di == NULL
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +010023610 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
23611 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023612 {
23613 if (*arg == K_SPECIAL)
23614 j = 3;
23615 else
23616 j = 0;
23617 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23618 : eval_isnamec(arg[j])))
23619 ++j;
23620 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023621 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023622 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023623 /* Disallow using the g: dict. */
23624 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23625 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023626 }
23627
Bram Moolenaar071d4272004-06-13 20:20:40 +000023628 /*
23629 * Isolate the arguments: "arg1, arg2, ...)"
23630 */
23631 while (*p != ')')
23632 {
23633 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23634 {
23635 varargs = TRUE;
23636 p += 3;
23637 mustend = TRUE;
23638 }
23639 else
23640 {
23641 arg = p;
23642 while (ASCII_ISALNUM(*p) || *p == '_')
23643 ++p;
23644 if (arg == p || isdigit(*arg)
23645 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23646 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23647 {
23648 if (!eap->skip)
23649 EMSG2(_("E125: Illegal argument: %s"), arg);
23650 break;
23651 }
23652 if (ga_grow(&newargs, 1) == FAIL)
23653 goto erret;
23654 c = *p;
23655 *p = NUL;
23656 arg = vim_strsave(arg);
23657 if (arg == NULL)
23658 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023659
23660 /* Check for duplicate argument name. */
23661 for (i = 0; i < newargs.ga_len; ++i)
23662 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23663 {
23664 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023665 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023666 goto erret;
23667 }
23668
Bram Moolenaar071d4272004-06-13 20:20:40 +000023669 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23670 *p = c;
23671 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023672 if (*p == ',')
23673 ++p;
23674 else
23675 mustend = TRUE;
23676 }
23677 p = skipwhite(p);
23678 if (mustend && *p != ')')
23679 {
23680 if (!eap->skip)
23681 EMSG2(_(e_invarg2), eap->arg);
23682 break;
23683 }
23684 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023685 if (*p != ')')
23686 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023687 ++p; /* skip the ')' */
23688
Bram Moolenaare9a41262005-01-15 22:18:47 +000023689 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023690 for (;;)
23691 {
23692 p = skipwhite(p);
23693 if (STRNCMP(p, "range", 5) == 0)
23694 {
23695 flags |= FC_RANGE;
23696 p += 5;
23697 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023698 else if (STRNCMP(p, "dict", 4) == 0)
23699 {
23700 flags |= FC_DICT;
23701 p += 4;
23702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023703 else if (STRNCMP(p, "abort", 5) == 0)
23704 {
23705 flags |= FC_ABORT;
23706 p += 5;
23707 }
23708 else
23709 break;
23710 }
23711
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023712 /* When there is a line break use what follows for the function body.
23713 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23714 if (*p == '\n')
23715 line_arg = p + 1;
23716 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023717 EMSG(_(e_trailing));
23718
23719 /*
23720 * Read the body of the function, until ":endfunction" is found.
23721 */
23722 if (KeyTyped)
23723 {
23724 /* Check if the function already exists, don't let the user type the
23725 * whole function before telling him it doesn't work! For a script we
23726 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023727 if (!eap->skip && !eap->forceit)
23728 {
23729 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23730 EMSG(_(e_funcdict));
23731 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023732 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023734
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023735 if (!eap->skip && did_emsg)
23736 goto erret;
23737
Bram Moolenaar071d4272004-06-13 20:20:40 +000023738 msg_putchar('\n'); /* don't overwrite the function name */
23739 cmdline_row = msg_row;
23740 }
23741
23742 indent = 2;
23743 nesting = 0;
23744 for (;;)
23745 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023746 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023747 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023748 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023749 saved_wait_return = FALSE;
23750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023751 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023752 sourcing_lnum_off = sourcing_lnum;
23753
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023754 if (line_arg != NULL)
23755 {
23756 /* Use eap->arg, split up in parts by line breaks. */
23757 theline = line_arg;
23758 p = vim_strchr(theline, '\n');
23759 if (p == NULL)
23760 line_arg += STRLEN(line_arg);
23761 else
23762 {
23763 *p = NUL;
23764 line_arg = p + 1;
23765 }
23766 }
23767 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023768 theline = getcmdline(':', 0L, indent);
23769 else
23770 theline = eap->getline(':', eap->cookie, indent);
23771 if (KeyTyped)
23772 lines_left = Rows - 1;
23773 if (theline == NULL)
23774 {
23775 EMSG(_("E126: Missing :endfunction"));
23776 goto erret;
23777 }
23778
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023779 /* Detect line continuation: sourcing_lnum increased more than one. */
23780 if (sourcing_lnum > sourcing_lnum_off + 1)
23781 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23782 else
23783 sourcing_lnum_off = 0;
23784
Bram Moolenaar071d4272004-06-13 20:20:40 +000023785 if (skip_until != NULL)
23786 {
23787 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23788 * don't check for ":endfunc". */
23789 if (STRCMP(theline, skip_until) == 0)
23790 {
23791 vim_free(skip_until);
23792 skip_until = NULL;
23793 }
23794 }
23795 else
23796 {
23797 /* skip ':' and blanks*/
23798 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23799 ;
23800
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023801 /* Check for "endfunction". */
23802 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023803 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023804 if (line_arg == NULL)
23805 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023806 break;
23807 }
23808
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023809 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023810 * at "end". */
23811 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23812 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023813 else if (STRNCMP(p, "if", 2) == 0
23814 || STRNCMP(p, "wh", 2) == 0
23815 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023816 || STRNCMP(p, "try", 3) == 0)
23817 indent += 2;
23818
23819 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023820 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023821 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023822 if (*p == '!')
23823 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023824 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010023825 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010023826 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023827 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023828 ++nesting;
23829 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023830 }
23831 }
23832
23833 /* Check for ":append" or ":insert". */
23834 p = skip_range(p, NULL);
23835 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23836 || (p[0] == 'i'
23837 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23838 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23839 skip_until = vim_strsave((char_u *)".");
23840
23841 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23842 arg = skipwhite(skiptowhite(p));
23843 if (arg[0] == '<' && arg[1] =='<'
23844 && ((p[0] == 'p' && p[1] == 'y'
23845 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23846 || (p[0] == 'p' && p[1] == 'e'
23847 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23848 || (p[0] == 't' && p[1] == 'c'
23849 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023850 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23851 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023852 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23853 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023854 || (p[0] == 'm' && p[1] == 'z'
23855 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023856 ))
23857 {
23858 /* ":python <<" continues until a dot, like ":append" */
23859 p = skipwhite(arg + 2);
23860 if (*p == NUL)
23861 skip_until = vim_strsave((char_u *)".");
23862 else
23863 skip_until = vim_strsave(p);
23864 }
23865 }
23866
23867 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023868 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023869 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023870 if (line_arg == NULL)
23871 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023872 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023873 }
23874
23875 /* Copy the line to newly allocated memory. get_one_sourceline()
23876 * allocates 250 bytes per line, this saves 80% on average. The cost
23877 * is an extra alloc/free. */
23878 p = vim_strsave(theline);
23879 if (p != NULL)
23880 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023881 if (line_arg == NULL)
23882 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023883 theline = p;
23884 }
23885
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023886 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23887
23888 /* Add NULL lines for continuation lines, so that the line count is
23889 * equal to the index in the growarray. */
23890 while (sourcing_lnum_off-- > 0)
23891 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023892
23893 /* Check for end of eap->arg. */
23894 if (line_arg != NULL && *line_arg == NUL)
23895 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023896 }
23897
23898 /* Don't define the function when skipping commands or when an error was
23899 * detected. */
23900 if (eap->skip || did_emsg)
23901 goto erret;
23902
23903 /*
23904 * If there are no errors, add the function
23905 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023906 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023907 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023908 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023909 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023910 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023911 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023912 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023913 goto erret;
23914 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023915
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023916 fp = find_func(name);
23917 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023918 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023919 if (!eap->forceit)
23920 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023921 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023922 goto erret;
23923 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023924 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023925 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023926 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023927 name);
23928 goto erret;
23929 }
23930 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023931 ga_clear_strings(&(fp->uf_args));
23932 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023933 vim_free(name);
23934 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023936 }
23937 else
23938 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023939 char numbuf[20];
23940
23941 fp = NULL;
23942 if (fudi.fd_newkey == NULL && !eap->forceit)
23943 {
23944 EMSG(_(e_funcdict));
23945 goto erret;
23946 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023947 if (fudi.fd_di == NULL)
23948 {
23949 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023950 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023951 goto erret;
23952 }
23953 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023954 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023955 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023956
23957 /* Give the function a sequential number. Can only be used with a
23958 * Funcref! */
23959 vim_free(name);
23960 sprintf(numbuf, "%d", ++func_nr);
23961 name = vim_strsave((char_u *)numbuf);
23962 if (name == NULL)
23963 goto erret;
23964 }
23965
23966 if (fp == NULL)
23967 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023968 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023969 {
23970 int slen, plen;
23971 char_u *scriptname;
23972
23973 /* Check that the autoload name matches the script name. */
23974 j = FAIL;
23975 if (sourcing_name != NULL)
23976 {
23977 scriptname = autoload_name(name);
23978 if (scriptname != NULL)
23979 {
23980 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023981 plen = (int)STRLEN(p);
23982 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023983 if (slen > plen && fnamecmp(p,
23984 sourcing_name + slen - plen) == 0)
23985 j = OK;
23986 vim_free(scriptname);
23987 }
23988 }
23989 if (j == FAIL)
23990 {
23991 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23992 goto erret;
23993 }
23994 }
23995
23996 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023997 if (fp == NULL)
23998 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023999
24000 if (fudi.fd_dict != NULL)
24001 {
24002 if (fudi.fd_di == NULL)
24003 {
24004 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024005 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024006 if (fudi.fd_di == NULL)
24007 {
24008 vim_free(fp);
24009 goto erret;
24010 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024011 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24012 {
24013 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024014 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024015 goto erret;
24016 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024017 }
24018 else
24019 /* overwrite existing dict entry */
24020 clear_tv(&fudi.fd_di->di_tv);
24021 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024022 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024023 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024024 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024025
24026 /* behave like "dict" was used */
24027 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024028 }
24029
Bram Moolenaar071d4272004-06-13 20:20:40 +000024030 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024031 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024032 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24033 {
24034 vim_free(fp);
24035 goto erret;
24036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024037 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024038 fp->uf_args = newargs;
24039 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024040#ifdef FEAT_PROFILE
24041 fp->uf_tml_count = NULL;
24042 fp->uf_tml_total = NULL;
24043 fp->uf_tml_self = NULL;
24044 fp->uf_profiling = FALSE;
24045 if (prof_def_func())
24046 func_do_profile(fp);
24047#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024048 fp->uf_varargs = varargs;
24049 fp->uf_flags = flags;
24050 fp->uf_calls = 0;
24051 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024052 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024053
24054erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024055 ga_clear_strings(&newargs);
24056 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024057ret_free:
24058 vim_free(skip_until);
24059 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024060 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024061 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024062 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024063}
24064
24065/*
24066 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024067 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024068 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024069 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024070 * TFN_INT: internal function name OK
24071 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024072 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024073 * Advances "pp" to just after the function name (if no error).
24074 */
24075 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024076trans_function_name(
24077 char_u **pp,
24078 int skip, /* only find the end, don't evaluate */
24079 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010024080 funcdict_T *fdp, /* return: info about dictionary used */
24081 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024082{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024083 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024084 char_u *start;
24085 char_u *end;
24086 int lead;
24087 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024088 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024089 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024090
24091 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024092 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024093 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024094
24095 /* Check for hard coded <SNR>: already translated function ID (from a user
24096 * command). */
24097 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24098 && (*pp)[2] == (int)KE_SNR)
24099 {
24100 *pp += 3;
24101 len = get_id_len(pp) + 3;
24102 return vim_strnsave(start, len);
24103 }
24104
24105 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24106 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024107 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024108 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024109 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024110
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024111 /* Note that TFN_ flags use the same values as GLV_ flags. */
24112 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024113 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024114 if (end == start)
24115 {
24116 if (!skip)
24117 EMSG(_("E129: Function name required"));
24118 goto theend;
24119 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024120 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024121 {
24122 /*
24123 * Report an invalid expression in braces, unless the expression
24124 * evaluation has been cancelled due to an aborting error, an
24125 * interrupt, or an exception.
24126 */
24127 if (!aborting())
24128 {
24129 if (end != NULL)
24130 EMSG2(_(e_invarg2), start);
24131 }
24132 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024133 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024134 goto theend;
24135 }
24136
24137 if (lv.ll_tv != NULL)
24138 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024139 if (fdp != NULL)
24140 {
24141 fdp->fd_dict = lv.ll_dict;
24142 fdp->fd_newkey = lv.ll_newkey;
24143 lv.ll_newkey = NULL;
24144 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024145 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024146 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24147 {
24148 name = vim_strsave(lv.ll_tv->vval.v_string);
24149 *pp = end;
24150 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010024151 else if (lv.ll_tv->v_type == VAR_PARTIAL
24152 && lv.ll_tv->vval.v_partial != NULL)
24153 {
24154 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
24155 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010024156 if (partial != NULL)
24157 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010024158 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024159 else
24160 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024161 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24162 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024163 EMSG(_(e_funcref));
24164 else
24165 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024166 name = NULL;
24167 }
24168 goto theend;
24169 }
24170
24171 if (lv.ll_name == NULL)
24172 {
24173 /* Error found, but continue after the function name. */
24174 *pp = end;
24175 goto theend;
24176 }
24177
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024178 /* Check if the name is a Funcref. If so, use the value. */
24179 if (lv.ll_exp_name != NULL)
24180 {
24181 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010024182 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010024183 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024184 if (name == lv.ll_exp_name)
24185 name = NULL;
24186 }
24187 else
24188 {
24189 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010024190 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024191 if (name == *pp)
24192 name = NULL;
24193 }
24194 if (name != NULL)
24195 {
24196 name = vim_strsave(name);
24197 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024198 if (STRNCMP(name, "<SNR>", 5) == 0)
24199 {
24200 /* Change "<SNR>" to the byte sequence. */
24201 name[0] = K_SPECIAL;
24202 name[1] = KS_EXTRA;
24203 name[2] = (int)KE_SNR;
24204 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24205 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024206 goto theend;
24207 }
24208
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024209 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024210 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024211 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024212 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24213 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24214 {
24215 /* When there was "s:" already or the name expanded to get a
24216 * leading "s:" then remove it. */
24217 lv.ll_name += 2;
24218 len -= 2;
24219 lead = 2;
24220 }
24221 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024222 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024223 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024224 /* skip over "s:" and "g:" */
24225 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024226 lv.ll_name += 2;
24227 len = (int)(end - lv.ll_name);
24228 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024229
24230 /*
24231 * Copy the function name to allocated memory.
24232 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24233 * Accept <SNR>123_name() outside a script.
24234 */
24235 if (skip)
24236 lead = 0; /* do nothing */
24237 else if (lead > 0)
24238 {
24239 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024240 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24241 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024242 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024243 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024244 if (current_SID <= 0)
24245 {
24246 EMSG(_(e_usingsid));
24247 goto theend;
24248 }
24249 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24250 lead += (int)STRLEN(sid_buf);
24251 }
24252 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024253 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024254 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024255 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024256 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024257 goto theend;
24258 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024259 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024260 {
24261 char_u *cp = vim_strchr(lv.ll_name, ':');
24262
24263 if (cp != NULL && cp < end)
24264 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024265 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024266 goto theend;
24267 }
24268 }
24269
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024270 name = alloc((unsigned)(len + lead + 1));
24271 if (name != NULL)
24272 {
24273 if (lead > 0)
24274 {
24275 name[0] = K_SPECIAL;
24276 name[1] = KS_EXTRA;
24277 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024278 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024279 STRCPY(name + 3, sid_buf);
24280 }
24281 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024282 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024283 }
24284 *pp = end;
24285
24286theend:
24287 clear_lval(&lv);
24288 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024289}
24290
24291/*
24292 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24293 * Return 2 if "p" starts with "s:".
24294 * Return 0 otherwise.
24295 */
24296 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024297eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024298{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024299 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24300 * the standard library function. */
24301 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24302 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024303 return 5;
24304 if (p[0] == 's' && p[1] == ':')
24305 return 2;
24306 return 0;
24307}
24308
24309/*
24310 * Return TRUE if "p" starts with "<SID>" or "s:".
24311 * Only works if eval_fname_script() returned non-zero for "p"!
24312 */
24313 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024314eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024315{
24316 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24317}
24318
24319/*
24320 * List the head of the function: "name(arg1, arg2)".
24321 */
24322 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024323list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024324{
24325 int j;
24326
24327 msg_start();
24328 if (indent)
24329 MSG_PUTS(" ");
24330 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024331 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024332 {
24333 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024334 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024335 }
24336 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024337 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024338 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024339 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024340 {
24341 if (j)
24342 MSG_PUTS(", ");
24343 msg_puts(FUNCARG(fp, j));
24344 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024345 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024346 {
24347 if (j)
24348 MSG_PUTS(", ");
24349 MSG_PUTS("...");
24350 }
24351 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024352 if (fp->uf_flags & FC_ABORT)
24353 MSG_PUTS(" abort");
24354 if (fp->uf_flags & FC_RANGE)
24355 MSG_PUTS(" range");
24356 if (fp->uf_flags & FC_DICT)
24357 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024358 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024359 if (p_verbose > 0)
24360 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024361}
24362
24363/*
24364 * Find a function by name, return pointer to it in ufuncs.
24365 * Return NULL for unknown function.
24366 */
24367 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024368find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024369{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024370 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024371
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024372 hi = hash_find(&func_hashtab, name);
24373 if (!HASHITEM_EMPTY(hi))
24374 return HI2UF(hi);
24375 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024376}
24377
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024378#if defined(EXITFREE) || defined(PROTO)
24379 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024380free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024381{
24382 hashitem_T *hi;
24383
24384 /* Need to start all over every time, because func_free() may change the
24385 * hash table. */
24386 while (func_hashtab.ht_used > 0)
24387 for (hi = func_hashtab.ht_array; ; ++hi)
24388 if (!HASHITEM_EMPTY(hi))
24389 {
24390 func_free(HI2UF(hi));
24391 break;
24392 }
24393}
24394#endif
24395
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024396 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024397translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024398{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024399 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024400 return find_internal_func(name) >= 0;
24401 return find_func(name) != NULL;
24402}
24403
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024404/*
24405 * Return TRUE if a function "name" exists.
24406 */
24407 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024408function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024409{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024410 char_u *nm = name;
24411 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024412 int n = FALSE;
24413
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024414 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010024415 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024416 nm = skipwhite(nm);
24417
24418 /* Only accept "funcname", "funcname ", "funcname (..." and
24419 * "funcname(...", not "funcname!...". */
24420 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024421 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024422 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024423 return n;
24424}
24425
Bram Moolenaara1544c02013-05-30 12:35:52 +020024426 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024427get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024428{
24429 char_u *nm = name;
24430 char_u *p;
24431
Bram Moolenaar65639032016-03-16 21:40:30 +010024432 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020024433
24434 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024435 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024436 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024437
Bram Moolenaara1544c02013-05-30 12:35:52 +020024438 vim_free(p);
24439 return NULL;
24440}
24441
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024442/*
24443 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024444 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24445 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024446 */
24447 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024448builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024449{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024450 char_u *p;
24451
24452 if (!ASCII_ISLOWER(name[0]))
24453 return FALSE;
24454 p = vim_strchr(name, AUTOLOAD_CHAR);
24455 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024456}
24457
Bram Moolenaar05159a02005-02-26 23:04:13 +000024458#if defined(FEAT_PROFILE) || defined(PROTO)
24459/*
24460 * Start profiling function "fp".
24461 */
24462 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024463func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024464{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024465 int len = fp->uf_lines.ga_len;
24466
24467 if (len == 0)
24468 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024469 fp->uf_tm_count = 0;
24470 profile_zero(&fp->uf_tm_self);
24471 profile_zero(&fp->uf_tm_total);
24472 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024473 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024474 if (fp->uf_tml_total == NULL)
24475 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024476 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024477 if (fp->uf_tml_self == NULL)
24478 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024479 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024480 fp->uf_tml_idx = -1;
24481 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24482 || fp->uf_tml_self == NULL)
24483 return; /* out of memory */
24484
24485 fp->uf_profiling = TRUE;
24486}
24487
24488/*
24489 * Dump the profiling results for all functions in file "fd".
24490 */
24491 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024492func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024493{
24494 hashitem_T *hi;
24495 int todo;
24496 ufunc_T *fp;
24497 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024498 ufunc_T **sorttab;
24499 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024500
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024501 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024502 if (todo == 0)
24503 return; /* nothing to dump */
24504
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024505 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024506
Bram Moolenaar05159a02005-02-26 23:04:13 +000024507 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24508 {
24509 if (!HASHITEM_EMPTY(hi))
24510 {
24511 --todo;
24512 fp = HI2UF(hi);
24513 if (fp->uf_profiling)
24514 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024515 if (sorttab != NULL)
24516 sorttab[st_len++] = fp;
24517
Bram Moolenaar05159a02005-02-26 23:04:13 +000024518 if (fp->uf_name[0] == K_SPECIAL)
24519 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24520 else
24521 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24522 if (fp->uf_tm_count == 1)
24523 fprintf(fd, "Called 1 time\n");
24524 else
24525 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24526 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24527 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24528 fprintf(fd, "\n");
24529 fprintf(fd, "count total (s) self (s)\n");
24530
24531 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24532 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024533 if (FUNCLINE(fp, i) == NULL)
24534 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024535 prof_func_line(fd, fp->uf_tml_count[i],
24536 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024537 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24538 }
24539 fprintf(fd, "\n");
24540 }
24541 }
24542 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024543
24544 if (sorttab != NULL && st_len > 0)
24545 {
24546 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24547 prof_total_cmp);
24548 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24549 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24550 prof_self_cmp);
24551 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24552 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024553
24554 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024555}
Bram Moolenaar73830342005-02-28 22:48:19 +000024556
24557 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024558prof_sort_list(
24559 FILE *fd,
24560 ufunc_T **sorttab,
24561 int st_len,
24562 char *title,
24563 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024564{
24565 int i;
24566 ufunc_T *fp;
24567
24568 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24569 fprintf(fd, "count total (s) self (s) function\n");
24570 for (i = 0; i < 20 && i < st_len; ++i)
24571 {
24572 fp = sorttab[i];
24573 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24574 prefer_self);
24575 if (fp->uf_name[0] == K_SPECIAL)
24576 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24577 else
24578 fprintf(fd, " %s()\n", fp->uf_name);
24579 }
24580 fprintf(fd, "\n");
24581}
24582
24583/*
24584 * Print the count and times for one function or function line.
24585 */
24586 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024587prof_func_line(
24588 FILE *fd,
24589 int count,
24590 proftime_T *total,
24591 proftime_T *self,
24592 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024593{
24594 if (count > 0)
24595 {
24596 fprintf(fd, "%5d ", count);
24597 if (prefer_self && profile_equal(total, self))
24598 fprintf(fd, " ");
24599 else
24600 fprintf(fd, "%s ", profile_msg(total));
24601 if (!prefer_self && profile_equal(total, self))
24602 fprintf(fd, " ");
24603 else
24604 fprintf(fd, "%s ", profile_msg(self));
24605 }
24606 else
24607 fprintf(fd, " ");
24608}
24609
24610/*
24611 * Compare function for total time sorting.
24612 */
24613 static int
24614#ifdef __BORLANDC__
24615_RTLENTRYF
24616#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024617prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024618{
24619 ufunc_T *p1, *p2;
24620
24621 p1 = *(ufunc_T **)s1;
24622 p2 = *(ufunc_T **)s2;
24623 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24624}
24625
24626/*
24627 * Compare function for self time sorting.
24628 */
24629 static int
24630#ifdef __BORLANDC__
24631_RTLENTRYF
24632#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024633prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024634{
24635 ufunc_T *p1, *p2;
24636
24637 p1 = *(ufunc_T **)s1;
24638 p2 = *(ufunc_T **)s2;
24639 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24640}
24641
Bram Moolenaar05159a02005-02-26 23:04:13 +000024642#endif
24643
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024644/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024645 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024646 * Return TRUE if a package was loaded.
24647 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024648 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024649script_autoload(
24650 char_u *name,
24651 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024652{
24653 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024654 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024655 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024656 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024657
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024658 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024659 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024660 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024661 return FALSE;
24662
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024663 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024664
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024665 /* Find the name in the list of previously loaded package names. Skip
24666 * "autoload/", it's always the same. */
24667 for (i = 0; i < ga_loaded.ga_len; ++i)
24668 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24669 break;
24670 if (!reload && i < ga_loaded.ga_len)
24671 ret = FALSE; /* was loaded already */
24672 else
24673 {
24674 /* Remember the name if it wasn't loaded already. */
24675 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24676 {
24677 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24678 tofree = NULL;
24679 }
24680
24681 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010024682 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024683 ret = TRUE;
24684 }
24685
24686 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024687 return ret;
24688}
24689
24690/*
24691 * Return the autoload script name for a function or variable name.
24692 * Returns NULL when out of memory.
24693 */
24694 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024695autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024696{
24697 char_u *p;
24698 char_u *scriptname;
24699
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024700 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024701 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24702 if (scriptname == NULL)
24703 return FALSE;
24704 STRCPY(scriptname, "autoload/");
24705 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024706 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024707 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024708 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024709 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024710 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024711}
24712
Bram Moolenaar071d4272004-06-13 20:20:40 +000024713#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24714
24715/*
24716 * Function given to ExpandGeneric() to obtain the list of user defined
24717 * function names.
24718 */
24719 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024720get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024721{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024722 static long_u done;
24723 static hashitem_T *hi;
24724 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024725
24726 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024727 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024728 done = 0;
24729 hi = func_hashtab.ht_array;
24730 }
24731 if (done < func_hashtab.ht_used)
24732 {
24733 if (done++ > 0)
24734 ++hi;
24735 while (HASHITEM_EMPTY(hi))
24736 ++hi;
24737 fp = HI2UF(hi);
24738
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024739 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024740 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024741
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024742 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24743 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024744
24745 cat_func_name(IObuff, fp);
24746 if (xp->xp_context != EXPAND_USER_FUNC)
24747 {
24748 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024749 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024750 STRCAT(IObuff, ")");
24751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024752 return IObuff;
24753 }
24754 return NULL;
24755}
24756
24757#endif /* FEAT_CMDL_COMPL */
24758
24759/*
24760 * Copy the function name of "fp" to buffer "buf".
24761 * "buf" must be able to hold the function name plus three bytes.
24762 * Takes care of script-local function names.
24763 */
24764 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024765cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024766{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024767 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024768 {
24769 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024770 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024771 }
24772 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024773 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024774}
24775
24776/*
24777 * ":delfunction {name}"
24778 */
24779 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024780ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024781{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024782 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024783 char_u *p;
24784 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024785 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024786
24787 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010024788 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024789 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024790 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024791 {
24792 if (fudi.fd_dict != NULL && !eap->skip)
24793 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024794 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024796 if (!ends_excmd(*skipwhite(p)))
24797 {
24798 vim_free(name);
24799 EMSG(_(e_trailing));
24800 return;
24801 }
24802 eap->nextcmd = check_nextcmd(p);
24803 if (eap->nextcmd != NULL)
24804 *p = NUL;
24805
24806 if (!eap->skip)
24807 fp = find_func(name);
24808 vim_free(name);
24809
24810 if (!eap->skip)
24811 {
24812 if (fp == NULL)
24813 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024814 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024815 return;
24816 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024817 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024818 {
24819 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24820 return;
24821 }
24822
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024823 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024824 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024825 /* Delete the dict item that refers to the function, it will
24826 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024827 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024828 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024829 else
24830 func_free(fp);
24831 }
24832}
24833
24834/*
24835 * Free a function and remove it from the list of functions.
24836 */
24837 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024838func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024839{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024840 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024841
24842 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024843 ga_clear_strings(&(fp->uf_args));
24844 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024845#ifdef FEAT_PROFILE
24846 vim_free(fp->uf_tml_count);
24847 vim_free(fp->uf_tml_total);
24848 vim_free(fp->uf_tml_self);
24849#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024850
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024851 /* remove the function from the function hashtable */
24852 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24853 if (HASHITEM_EMPTY(hi))
24854 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024855 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024856 hash_remove(&func_hashtab, hi);
24857
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024858 vim_free(fp);
24859}
24860
24861/*
24862 * Unreference a Function: decrement the reference count and free it when it
24863 * becomes zero. Only for numbered functions.
24864 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024865 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024866func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024867{
24868 ufunc_T *fp;
24869
24870 if (name != NULL && isdigit(*name))
24871 {
24872 fp = find_func(name);
24873 if (fp == NULL)
24874 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024875 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024876 {
24877 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024878 * when "uf_calls" becomes zero. */
24879 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024880 func_free(fp);
24881 }
24882 }
24883}
24884
24885/*
24886 * Count a reference to a Function.
24887 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024888 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024889func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024890{
24891 ufunc_T *fp;
24892
24893 if (name != NULL && isdigit(*name))
24894 {
24895 fp = find_func(name);
24896 if (fp == NULL)
24897 EMSG2(_(e_intern2), "func_ref()");
24898 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024899 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024900 }
24901}
24902
24903/*
24904 * Call a user function.
24905 */
24906 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024907call_user_func(
24908 ufunc_T *fp, /* pointer to function */
24909 int argcount, /* nr of args */
24910 typval_T *argvars, /* arguments */
24911 typval_T *rettv, /* return value */
24912 linenr_T firstline, /* first line of range */
24913 linenr_T lastline, /* last line of range */
24914 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024915{
Bram Moolenaar33570922005-01-25 22:26:29 +000024916 char_u *save_sourcing_name;
24917 linenr_T save_sourcing_lnum;
24918 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024919 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024920 int save_did_emsg;
24921 static int depth = 0;
24922 dictitem_T *v;
24923 int fixvar_idx = 0; /* index in fixvar[] */
24924 int i;
24925 int ai;
24926 char_u numbuf[NUMBUFLEN];
24927 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024928 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024929#ifdef FEAT_PROFILE
24930 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024931 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024932#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024933
24934 /* If depth of calling is getting too high, don't execute the function */
24935 if (depth >= p_mfd)
24936 {
24937 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024938 rettv->v_type = VAR_NUMBER;
24939 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024940 return;
24941 }
24942 ++depth;
24943
24944 line_breakcheck(); /* check for CTRL-C hit */
24945
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024946 fc = (funccall_T *)alloc(sizeof(funccall_T));
24947 fc->caller = current_funccal;
24948 current_funccal = fc;
24949 fc->func = fp;
24950 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024951 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024952 fc->linenr = 0;
24953 fc->returned = FALSE;
24954 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024955 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024956 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24957 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024958
Bram Moolenaar33570922005-01-25 22:26:29 +000024959 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024960 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024961 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24962 * each argument variable and saves a lot of time.
24963 */
24964 /*
24965 * Init l: variables.
24966 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024967 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024968 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024969 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024970 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24971 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024972 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024973 name = v->di_key;
24974 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024975 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024976 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024977 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024978 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024979 v->di_tv.vval.v_dict = selfdict;
24980 ++selfdict->dv_refcount;
24981 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024982
Bram Moolenaar33570922005-01-25 22:26:29 +000024983 /*
24984 * Init a: variables.
24985 * Set a:0 to "argcount".
24986 * Set a:000 to a list with room for the "..." arguments.
24987 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024988 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024989 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024990 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024991 /* Use "name" to avoid a warning from some compiler that checks the
24992 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024993 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024994 name = v->di_key;
24995 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024996 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024997 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024998 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024999 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025000 v->di_tv.vval.v_list = &fc->l_varlist;
25001 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25002 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25003 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025004
25005 /*
25006 * Set a:firstline to "firstline" and a:lastline to "lastline".
25007 * Set a:name to named arguments.
25008 * Set a:N to the "..." arguments.
25009 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025010 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025011 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025012 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025013 (varnumber_T)lastline);
25014 for (i = 0; i < argcount; ++i)
25015 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025016 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025017 if (ai < 0)
25018 /* named argument a:name */
25019 name = FUNCARG(fp, i);
25020 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025021 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025022 /* "..." argument a:1, a:2, etc. */
25023 sprintf((char *)numbuf, "%d", ai + 1);
25024 name = numbuf;
25025 }
25026 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25027 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025028 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025029 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25030 }
25031 else
25032 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025033 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25034 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025035 if (v == NULL)
25036 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025037 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025038 }
25039 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025040 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025041
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025042 /* Note: the values are copied directly to avoid alloc/free.
25043 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025044 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025045 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025046
25047 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25048 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025049 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25050 fc->l_listitems[ai].li_tv = argvars[i];
25051 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025052 }
25053 }
25054
Bram Moolenaar071d4272004-06-13 20:20:40 +000025055 /* Don't redraw while executing the function. */
25056 ++RedrawingDisabled;
25057 save_sourcing_name = sourcing_name;
25058 save_sourcing_lnum = sourcing_lnum;
25059 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025060 /* need space for function name + ("function " + 3) or "[number]" */
25061 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25062 + STRLEN(fp->uf_name) + 20;
25063 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025064 if (sourcing_name != NULL)
25065 {
25066 if (save_sourcing_name != NULL
25067 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025068 sprintf((char *)sourcing_name, "%s[%d]..",
25069 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025070 else
25071 STRCPY(sourcing_name, "function ");
25072 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25073
25074 if (p_verbose >= 12)
25075 {
25076 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025077 verbose_enter_scroll();
25078
Bram Moolenaar555b2802005-05-19 21:08:39 +000025079 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025080 if (p_verbose >= 14)
25081 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025082 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025083 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025084 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025085 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025086
25087 msg_puts((char_u *)"(");
25088 for (i = 0; i < argcount; ++i)
25089 {
25090 if (i > 0)
25091 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025092 if (argvars[i].v_type == VAR_NUMBER)
25093 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025094 else
25095 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025096 /* Do not want errors such as E724 here. */
25097 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025098 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025099 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025100 if (s != NULL)
25101 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025102 if (vim_strsize(s) > MSG_BUF_CLEN)
25103 {
25104 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25105 s = buf;
25106 }
25107 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025108 vim_free(tofree);
25109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025110 }
25111 }
25112 msg_puts((char_u *)")");
25113 }
25114 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025115
25116 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025117 --no_wait_return;
25118 }
25119 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025120#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025121 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025122 {
25123 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25124 func_do_profile(fp);
25125 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025126 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025127 {
25128 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025129 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025130 profile_zero(&fp->uf_tm_children);
25131 }
25132 script_prof_save(&wait_start);
25133 }
25134#endif
25135
Bram Moolenaar071d4272004-06-13 20:20:40 +000025136 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025137 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025138 save_did_emsg = did_emsg;
25139 did_emsg = FALSE;
25140
25141 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025142 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025143 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25144
25145 --RedrawingDisabled;
25146
25147 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025148 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025149 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025150 clear_tv(rettv);
25151 rettv->v_type = VAR_NUMBER;
25152 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025153 }
25154
Bram Moolenaar05159a02005-02-26 23:04:13 +000025155#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025156 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025157 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025158 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025159 profile_end(&call_start);
25160 profile_sub_wait(&wait_start, &call_start);
25161 profile_add(&fp->uf_tm_total, &call_start);
25162 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025163 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025164 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025165 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25166 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025167 }
25168 }
25169#endif
25170
Bram Moolenaar071d4272004-06-13 20:20:40 +000025171 /* when being verbose, mention the return value */
25172 if (p_verbose >= 12)
25173 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025174 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025175 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025176
Bram Moolenaar071d4272004-06-13 20:20:40 +000025177 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025178 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025179 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025180 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025181 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025182 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025183 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025184 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025185 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025186 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025187 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025188
Bram Moolenaar555b2802005-05-19 21:08:39 +000025189 /* The value may be very long. Skip the middle part, so that we
25190 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025191 * truncate it at the end. Don't want errors such as E724 here. */
25192 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025193 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025194 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025195 if (s != NULL)
25196 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025197 if (vim_strsize(s) > MSG_BUF_CLEN)
25198 {
25199 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25200 s = buf;
25201 }
25202 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025203 vim_free(tofree);
25204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025205 }
25206 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025207
25208 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025209 --no_wait_return;
25210 }
25211
25212 vim_free(sourcing_name);
25213 sourcing_name = save_sourcing_name;
25214 sourcing_lnum = save_sourcing_lnum;
25215 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025216#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025217 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025218 script_prof_restore(&wait_start);
25219#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025220
25221 if (p_verbose >= 12 && sourcing_name != NULL)
25222 {
25223 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025224 verbose_enter_scroll();
25225
Bram Moolenaar555b2802005-05-19 21:08:39 +000025226 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025227 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025228
25229 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025230 --no_wait_return;
25231 }
25232
25233 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025234 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025235 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025236
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025237 /* If the a:000 list and the l: and a: dicts are not referenced we can
25238 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025239 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25240 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25241 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25242 {
25243 free_funccal(fc, FALSE);
25244 }
25245 else
25246 {
25247 hashitem_T *hi;
25248 listitem_T *li;
25249 int todo;
25250
25251 /* "fc" is still in use. This can happen when returning "a:000" or
25252 * assigning "l:" to a global variable.
25253 * Link "fc" in the list for garbage collection later. */
25254 fc->caller = previous_funccal;
25255 previous_funccal = fc;
25256
25257 /* Make a copy of the a: variables, since we didn't do that above. */
25258 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25259 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25260 {
25261 if (!HASHITEM_EMPTY(hi))
25262 {
25263 --todo;
25264 v = HI2DI(hi);
25265 copy_tv(&v->di_tv, &v->di_tv);
25266 }
25267 }
25268
25269 /* Make a copy of the a:000 items, since we didn't do that above. */
25270 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25271 copy_tv(&li->li_tv, &li->li_tv);
25272 }
25273}
25274
25275/*
25276 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025277 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025278 */
25279 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025280can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025281{
25282 return (fc->l_varlist.lv_copyID != copyID
25283 && fc->l_vars.dv_copyID != copyID
25284 && fc->l_avars.dv_copyID != copyID);
25285}
25286
25287/*
25288 * Free "fc" and what it contains.
25289 */
25290 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025291free_funccal(
25292 funccall_T *fc,
25293 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025294{
25295 listitem_T *li;
25296
25297 /* The a: variables typevals may not have been allocated, only free the
25298 * allocated variables. */
25299 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25300
25301 /* free all l: variables */
25302 vars_clear(&fc->l_vars.dv_hashtab);
25303
25304 /* Free the a:000 variables if they were allocated. */
25305 if (free_val)
25306 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25307 clear_tv(&li->li_tv);
25308
25309 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025310}
25311
25312/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025313 * Add a number variable "name" to dict "dp" with value "nr".
25314 */
25315 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025316add_nr_var(
25317 dict_T *dp,
25318 dictitem_T *v,
25319 char *name,
25320 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025321{
25322 STRCPY(v->di_key, name);
25323 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25324 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25325 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025326 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025327 v->di_tv.vval.v_number = nr;
25328}
25329
25330/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025331 * ":return [expr]"
25332 */
25333 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025334ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025335{
25336 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025337 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025338 int returning = FALSE;
25339
25340 if (current_funccal == NULL)
25341 {
25342 EMSG(_("E133: :return not inside a function"));
25343 return;
25344 }
25345
25346 if (eap->skip)
25347 ++emsg_skip;
25348
25349 eap->nextcmd = NULL;
25350 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025351 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025352 {
25353 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025354 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025355 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025356 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025357 }
25358 /* It's safer to return also on error. */
25359 else if (!eap->skip)
25360 {
25361 /*
25362 * Return unless the expression evaluation has been cancelled due to an
25363 * aborting error, an interrupt, or an exception.
25364 */
25365 if (!aborting())
25366 returning = do_return(eap, FALSE, TRUE, NULL);
25367 }
25368
25369 /* When skipping or the return gets pending, advance to the next command
25370 * in this line (!returning). Otherwise, ignore the rest of the line.
25371 * Following lines will be ignored by get_func_line(). */
25372 if (returning)
25373 eap->nextcmd = NULL;
25374 else if (eap->nextcmd == NULL) /* no argument */
25375 eap->nextcmd = check_nextcmd(arg);
25376
25377 if (eap->skip)
25378 --emsg_skip;
25379}
25380
25381/*
25382 * Return from a function. Possibly makes the return pending. Also called
25383 * for a pending return at the ":endtry" or after returning from an extra
25384 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025385 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025386 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025387 * FALSE when the return gets pending.
25388 */
25389 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025390do_return(
25391 exarg_T *eap,
25392 int reanimate,
25393 int is_cmd,
25394 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025395{
25396 int idx;
25397 struct condstack *cstack = eap->cstack;
25398
25399 if (reanimate)
25400 /* Undo the return. */
25401 current_funccal->returned = FALSE;
25402
25403 /*
25404 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25405 * not in its finally clause (which then is to be executed next) is found.
25406 * In this case, make the ":return" pending for execution at the ":endtry".
25407 * Otherwise, return normally.
25408 */
25409 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25410 if (idx >= 0)
25411 {
25412 cstack->cs_pending[idx] = CSTP_RETURN;
25413
25414 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025415 /* A pending return again gets pending. "rettv" points to an
25416 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025417 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025418 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025419 else
25420 {
25421 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025422 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025423 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025424 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025425
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025426 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025427 {
25428 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025429 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025430 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025431 else
25432 EMSG(_(e_outofmem));
25433 }
25434 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025435 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025436
25437 if (reanimate)
25438 {
25439 /* The pending return value could be overwritten by a ":return"
25440 * without argument in a finally clause; reset the default
25441 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025442 current_funccal->rettv->v_type = VAR_NUMBER;
25443 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025444 }
25445 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025446 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025447 }
25448 else
25449 {
25450 current_funccal->returned = TRUE;
25451
25452 /* If the return is carried out now, store the return value. For
25453 * a return immediately after reanimation, the value is already
25454 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025455 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025456 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025457 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025458 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025459 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025460 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025461 }
25462 }
25463
25464 return idx < 0;
25465}
25466
25467/*
25468 * Free the variable with a pending return value.
25469 */
25470 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025471discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025472{
Bram Moolenaar33570922005-01-25 22:26:29 +000025473 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025474}
25475
25476/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025477 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025478 * is an allocated string. Used by report_pending() for verbose messages.
25479 */
25480 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025481get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025482{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025483 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025484 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025485 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025486
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025487 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025488 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025489 if (s == NULL)
25490 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025491
25492 STRCPY(IObuff, ":return ");
25493 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25494 if (STRLEN(s) + 8 >= IOSIZE)
25495 STRCPY(IObuff + IOSIZE - 4, "...");
25496 vim_free(tofree);
25497 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025498}
25499
25500/*
25501 * Get next function line.
25502 * Called by do_cmdline() to get the next line.
25503 * Returns allocated string, or NULL for end of function.
25504 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025505 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025506get_func_line(
25507 int c UNUSED,
25508 void *cookie,
25509 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025510{
Bram Moolenaar33570922005-01-25 22:26:29 +000025511 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025512 ufunc_T *fp = fcp->func;
25513 char_u *retval;
25514 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025515
25516 /* If breakpoints have been added/deleted need to check for it. */
25517 if (fcp->dbg_tick != debug_tick)
25518 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025519 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025520 sourcing_lnum);
25521 fcp->dbg_tick = debug_tick;
25522 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025523#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025524 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025525 func_line_end(cookie);
25526#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025527
Bram Moolenaar05159a02005-02-26 23:04:13 +000025528 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025529 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25530 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025531 retval = NULL;
25532 else
25533 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025534 /* Skip NULL lines (continuation lines). */
25535 while (fcp->linenr < gap->ga_len
25536 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25537 ++fcp->linenr;
25538 if (fcp->linenr >= gap->ga_len)
25539 retval = NULL;
25540 else
25541 {
25542 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25543 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025544#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025545 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025546 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025547#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025549 }
25550
25551 /* Did we encounter a breakpoint? */
25552 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25553 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025554 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025555 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025556 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025557 sourcing_lnum);
25558 fcp->dbg_tick = debug_tick;
25559 }
25560
25561 return retval;
25562}
25563
Bram Moolenaar05159a02005-02-26 23:04:13 +000025564#if defined(FEAT_PROFILE) || defined(PROTO)
25565/*
25566 * Called when starting to read a function line.
25567 * "sourcing_lnum" must be correct!
25568 * When skipping lines it may not actually be executed, but we won't find out
25569 * until later and we need to store the time now.
25570 */
25571 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025572func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025573{
25574 funccall_T *fcp = (funccall_T *)cookie;
25575 ufunc_T *fp = fcp->func;
25576
25577 if (fp->uf_profiling && sourcing_lnum >= 1
25578 && sourcing_lnum <= fp->uf_lines.ga_len)
25579 {
25580 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025581 /* Skip continuation lines. */
25582 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25583 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025584 fp->uf_tml_execed = FALSE;
25585 profile_start(&fp->uf_tml_start);
25586 profile_zero(&fp->uf_tml_children);
25587 profile_get_wait(&fp->uf_tml_wait);
25588 }
25589}
25590
25591/*
25592 * Called when actually executing a function line.
25593 */
25594 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025595func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025596{
25597 funccall_T *fcp = (funccall_T *)cookie;
25598 ufunc_T *fp = fcp->func;
25599
25600 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25601 fp->uf_tml_execed = TRUE;
25602}
25603
25604/*
25605 * Called when done with a function line.
25606 */
25607 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025608func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025609{
25610 funccall_T *fcp = (funccall_T *)cookie;
25611 ufunc_T *fp = fcp->func;
25612
25613 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25614 {
25615 if (fp->uf_tml_execed)
25616 {
25617 ++fp->uf_tml_count[fp->uf_tml_idx];
25618 profile_end(&fp->uf_tml_start);
25619 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025620 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025621 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25622 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025623 }
25624 fp->uf_tml_idx = -1;
25625 }
25626}
25627#endif
25628
Bram Moolenaar071d4272004-06-13 20:20:40 +000025629/*
25630 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025631 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025632 */
25633 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025634func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025635{
Bram Moolenaar33570922005-01-25 22:26:29 +000025636 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025637
25638 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25639 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025640 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025641 || fcp->returned);
25642}
25643
25644/*
25645 * return TRUE if cookie indicates a function which "abort"s on errors.
25646 */
25647 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025648func_has_abort(
25649 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025650{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025651 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025652}
25653
25654#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25655typedef enum
25656{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025657 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25658 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25659 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025660} var_flavour_T;
25661
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025662static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025663
25664 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025665var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025666{
25667 char_u *p = varname;
25668
25669 if (ASCII_ISUPPER(*p))
25670 {
25671 while (*(++p))
25672 if (ASCII_ISLOWER(*p))
25673 return VAR_FLAVOUR_SESSION;
25674 return VAR_FLAVOUR_VIMINFO;
25675 }
25676 else
25677 return VAR_FLAVOUR_DEFAULT;
25678}
25679#endif
25680
25681#if defined(FEAT_VIMINFO) || defined(PROTO)
25682/*
25683 * Restore global vars that start with a capital from the viminfo file
25684 */
25685 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025686read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025687{
25688 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025689 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025690 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025691 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025692
25693 if (!writing && (find_viminfo_parameter('!') != NULL))
25694 {
25695 tab = vim_strchr(virp->vir_line + 1, '\t');
25696 if (tab != NULL)
25697 {
25698 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025699 switch (*tab)
25700 {
25701 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025702#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025703 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025704#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025705 case 'D': type = VAR_DICT; break;
25706 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025707 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025709
25710 tab = vim_strchr(tab, '\t');
25711 if (tab != NULL)
25712 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025713 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025714 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025715 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025716 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025717#ifdef FEAT_FLOAT
25718 else if (type == VAR_FLOAT)
25719 (void)string2float(tab + 1, &tv.vval.v_float);
25720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025721 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025722 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025723 if (type == VAR_DICT || type == VAR_LIST)
25724 {
25725 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25726
25727 if (etv == NULL)
25728 /* Failed to parse back the dict or list, use it as a
25729 * string. */
25730 tv.v_type = VAR_STRING;
25731 else
25732 {
25733 vim_free(tv.vval.v_string);
25734 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025735 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025736 }
25737 }
25738
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025739 /* when in a function use global variables */
25740 save_funccal = current_funccal;
25741 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025742 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025743 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025744
25745 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025746 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025747 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25748 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025749 }
25750 }
25751 }
25752
25753 return viminfo_readline(virp);
25754}
25755
25756/*
25757 * Write global vars that start with a capital to the viminfo file
25758 */
25759 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025760write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025761{
Bram Moolenaar33570922005-01-25 22:26:29 +000025762 hashitem_T *hi;
25763 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025764 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025765 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025766 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025767 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025768 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025769
25770 if (find_viminfo_parameter('!') == NULL)
25771 return;
25772
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025773 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025774
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025775 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025776 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025777 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025778 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025779 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025780 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025781 this_var = HI2DI(hi);
25782 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025783 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025784 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025785 {
25786 case VAR_STRING: s = "STR"; break;
25787 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025788 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025789 case VAR_DICT: s = "DIC"; break;
25790 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025791 case VAR_SPECIAL: s = "XPL"; break;
25792
25793 case VAR_UNKNOWN:
25794 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010025795 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025796 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025797 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025798 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025799 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025800 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025801 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025802 if (p != NULL)
25803 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025804 vim_free(tofree);
25805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025806 }
25807 }
25808}
25809#endif
25810
25811#if defined(FEAT_SESSION) || defined(PROTO)
25812 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025813store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025814{
Bram Moolenaar33570922005-01-25 22:26:29 +000025815 hashitem_T *hi;
25816 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025817 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025818 char_u *p, *t;
25819
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025820 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025821 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025822 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025823 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025824 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025825 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025826 this_var = HI2DI(hi);
25827 if ((this_var->di_tv.v_type == VAR_NUMBER
25828 || this_var->di_tv.v_type == VAR_STRING)
25829 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025830 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025831 /* Escape special characters with a backslash. Turn a LF and
25832 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025833 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025834 (char_u *)"\\\"\n\r");
25835 if (p == NULL) /* out of memory */
25836 break;
25837 for (t = p; *t != NUL; ++t)
25838 if (*t == '\n')
25839 *t = 'n';
25840 else if (*t == '\r')
25841 *t = 'r';
25842 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025843 this_var->di_key,
25844 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25845 : ' ',
25846 p,
25847 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25848 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025849 || put_eol(fd) == FAIL)
25850 {
25851 vim_free(p);
25852 return FAIL;
25853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025854 vim_free(p);
25855 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025856#ifdef FEAT_FLOAT
25857 else if (this_var->di_tv.v_type == VAR_FLOAT
25858 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25859 {
25860 float_T f = this_var->di_tv.vval.v_float;
25861 int sign = ' ';
25862
25863 if (f < 0)
25864 {
25865 f = -f;
25866 sign = '-';
25867 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025868 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025869 this_var->di_key, sign, f) < 0)
25870 || put_eol(fd) == FAIL)
25871 return FAIL;
25872 }
25873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025874 }
25875 }
25876 return OK;
25877}
25878#endif
25879
Bram Moolenaar661b1822005-07-28 22:36:45 +000025880/*
25881 * Display script name where an item was last set.
25882 * Should only be invoked when 'verbose' is non-zero.
25883 */
25884 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025885last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025886{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025887 char_u *p;
25888
Bram Moolenaar661b1822005-07-28 22:36:45 +000025889 if (scriptID != 0)
25890 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025891 p = home_replace_save(NULL, get_scriptname(scriptID));
25892 if (p != NULL)
25893 {
25894 verbose_enter();
25895 MSG_PUTS(_("\n\tLast set from "));
25896 MSG_PUTS(p);
25897 vim_free(p);
25898 verbose_leave();
25899 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025900 }
25901}
25902
Bram Moolenaard812df62008-11-09 12:46:09 +000025903/*
25904 * List v:oldfiles in a nice way.
25905 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025906 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025907ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025908{
25909 list_T *l = vimvars[VV_OLDFILES].vv_list;
25910 listitem_T *li;
25911 int nr = 0;
25912
25913 if (l == NULL)
25914 msg((char_u *)_("No old files"));
25915 else
25916 {
25917 msg_start();
25918 msg_scroll = TRUE;
25919 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25920 {
25921 msg_outnum((long)++nr);
25922 MSG_PUTS(": ");
25923 msg_outtrans(get_tv_string(&li->li_tv));
25924 msg_putchar('\n');
25925 out_flush(); /* output one line at a time */
25926 ui_breakcheck();
25927 }
25928 /* Assume "got_int" was set to truncate the listing. */
25929 got_int = FALSE;
25930
25931#ifdef FEAT_BROWSE_CMD
25932 if (cmdmod.browse)
25933 {
25934 quit_more = FALSE;
25935 nr = prompt_for_number(FALSE);
25936 msg_starthere();
25937 if (nr > 0)
25938 {
25939 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25940 (long)nr);
25941
25942 if (p != NULL)
25943 {
25944 p = expand_env_save(p);
25945 eap->arg = p;
25946 eap->cmdidx = CMD_edit;
25947 cmdmod.browse = FALSE;
25948 do_exedit(eap, NULL);
25949 vim_free(p);
25950 }
25951 }
25952 }
25953#endif
25954 }
25955}
25956
Bram Moolenaar53744302015-07-17 17:38:22 +020025957/* reset v:option_new, v:option_old and v:option_type */
25958 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025959reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025960{
25961 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25962 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25963 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25964}
25965
25966
Bram Moolenaar071d4272004-06-13 20:20:40 +000025967#endif /* FEAT_EVAL */
25968
Bram Moolenaar071d4272004-06-13 20:20:40 +000025969
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025970#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025971
25972#ifdef WIN3264
25973/*
25974 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25975 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025976static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25977static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25978static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025979
25980/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025981 * Get the short path (8.3) for the filename in "fnamep".
25982 * Only works for a valid file name.
25983 * When the path gets longer "fnamep" is changed and the allocated buffer
25984 * is put in "bufp".
25985 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25986 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025987 */
25988 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025989get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025990{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025991 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025992 char_u *newbuf;
25993
25994 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025995 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025996 if (l > len - 1)
25997 {
25998 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025999 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026000 newbuf = vim_strnsave(*fnamep, l);
26001 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026002 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026003
26004 vim_free(*bufp);
26005 *fnamep = *bufp = newbuf;
26006
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026007 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026008 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026009 }
26010
26011 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026012 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026013}
26014
26015/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026016 * Get the short path (8.3) for the filename in "fname". The converted
26017 * path is returned in "bufp".
26018 *
26019 * Some of the directories specified in "fname" may not exist. This function
26020 * will shorten the existing directories at the beginning of the path and then
26021 * append the remaining non-existing path.
26022 *
26023 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026024 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026025 * bufp - Pointer to an allocated buffer for the filename.
26026 * fnamelen - Length of the filename pointed to by fname
26027 *
26028 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026029 */
26030 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026031shortpath_for_invalid_fname(
26032 char_u **fname,
26033 char_u **bufp,
26034 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026035{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026036 char_u *short_fname, *save_fname, *pbuf_unused;
26037 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026038 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026039 int old_len, len;
26040 int new_len, sfx_len;
26041 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026042
26043 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026044 old_len = *fnamelen;
26045 save_fname = vim_strnsave(*fname, old_len);
26046 pbuf_unused = NULL;
26047 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026048
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026049 endp = save_fname + old_len - 1; /* Find the end of the copy */
26050 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026051
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026052 /*
26053 * Try shortening the supplied path till it succeeds by removing one
26054 * directory at a time from the tail of the path.
26055 */
26056 len = 0;
26057 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026058 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026059 /* go back one path-separator */
26060 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26061 --endp;
26062 if (endp <= save_fname)
26063 break; /* processed the complete path */
26064
26065 /*
26066 * Replace the path separator with a NUL and try to shorten the
26067 * resulting path.
26068 */
26069 ch = *endp;
26070 *endp = 0;
26071 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026072 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026073 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26074 {
26075 retval = FAIL;
26076 goto theend;
26077 }
26078 *endp = ch; /* preserve the string */
26079
26080 if (len > 0)
26081 break; /* successfully shortened the path */
26082
26083 /* failed to shorten the path. Skip the path separator */
26084 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026085 }
26086
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026087 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026088 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026089 /*
26090 * Succeeded in shortening the path. Now concatenate the shortened
26091 * path with the remaining path at the tail.
26092 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026093
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026094 /* Compute the length of the new path. */
26095 sfx_len = (int)(save_endp - endp) + 1;
26096 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026097
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026098 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026099 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026100 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026101 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026102 /* There is not enough space in the currently allocated string,
26103 * copy it to a buffer big enough. */
26104 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026105 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026106 {
26107 retval = FAIL;
26108 goto theend;
26109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026110 }
26111 else
26112 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026113 /* Transfer short_fname to the main buffer (it's big enough),
26114 * unless get_short_pathname() did its work in-place. */
26115 *fname = *bufp = save_fname;
26116 if (short_fname != save_fname)
26117 vim_strncpy(save_fname, short_fname, len);
26118 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026119 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026120
26121 /* concat the not-shortened part of the path */
26122 vim_strncpy(*fname + len, endp, sfx_len);
26123 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026124 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026125
26126theend:
26127 vim_free(pbuf_unused);
26128 vim_free(save_fname);
26129
26130 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026131}
26132
26133/*
26134 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026135 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026136 */
26137 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026138shortpath_for_partial(
26139 char_u **fnamep,
26140 char_u **bufp,
26141 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026142{
26143 int sepcount, len, tflen;
26144 char_u *p;
26145 char_u *pbuf, *tfname;
26146 int hasTilde;
26147
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026148 /* Count up the path separators from the RHS.. so we know which part
26149 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026150 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026151 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026152 if (vim_ispathsep(*p))
26153 ++sepcount;
26154
26155 /* Need full path first (use expand_env() to remove a "~/") */
26156 hasTilde = (**fnamep == '~');
26157 if (hasTilde)
26158 pbuf = tfname = expand_env_save(*fnamep);
26159 else
26160 pbuf = tfname = FullName_save(*fnamep, FALSE);
26161
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026162 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026163
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026164 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26165 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026166
26167 if (len == 0)
26168 {
26169 /* Don't have a valid filename, so shorten the rest of the
26170 * path if we can. This CAN give us invalid 8.3 filenames, but
26171 * there's not a lot of point in guessing what it might be.
26172 */
26173 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026174 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26175 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026176 }
26177
26178 /* Count the paths backward to find the beginning of the desired string. */
26179 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026180 {
26181#ifdef FEAT_MBYTE
26182 if (has_mbyte)
26183 p -= mb_head_off(tfname, p);
26184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026185 if (vim_ispathsep(*p))
26186 {
26187 if (sepcount == 0 || (hasTilde && sepcount == 1))
26188 break;
26189 else
26190 sepcount --;
26191 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026193 if (hasTilde)
26194 {
26195 --p;
26196 if (p >= tfname)
26197 *p = '~';
26198 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026199 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026200 }
26201 else
26202 ++p;
26203
26204 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26205 vim_free(*bufp);
26206 *fnamelen = (int)STRLEN(p);
26207 *bufp = pbuf;
26208 *fnamep = p;
26209
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026210 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026211}
26212#endif /* WIN3264 */
26213
26214/*
26215 * Adjust a filename, according to a string of modifiers.
26216 * *fnamep must be NUL terminated when called. When returning, the length is
26217 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026218 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026219 * When there is an error, *fnamep is set to NULL.
26220 */
26221 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026222modify_fname(
26223 char_u *src, /* string with modifiers */
26224 int *usedlen, /* characters after src that are used */
26225 char_u **fnamep, /* file name so far */
26226 char_u **bufp, /* buffer for allocated file name or NULL */
26227 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026228{
26229 int valid = 0;
26230 char_u *tail;
26231 char_u *s, *p, *pbuf;
26232 char_u dirname[MAXPATHL];
26233 int c;
26234 int has_fullname = 0;
26235#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026236 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026237 int has_shortname = 0;
26238#endif
26239
26240repeat:
26241 /* ":p" - full path/file_name */
26242 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26243 {
26244 has_fullname = 1;
26245
26246 valid |= VALID_PATH;
26247 *usedlen += 2;
26248
26249 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26250 if ((*fnamep)[0] == '~'
26251#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26252 && ((*fnamep)[1] == '/'
26253# ifdef BACKSLASH_IN_FILENAME
26254 || (*fnamep)[1] == '\\'
26255# endif
26256 || (*fnamep)[1] == NUL)
26257
26258#endif
26259 )
26260 {
26261 *fnamep = expand_env_save(*fnamep);
26262 vim_free(*bufp); /* free any allocated file name */
26263 *bufp = *fnamep;
26264 if (*fnamep == NULL)
26265 return -1;
26266 }
26267
26268 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026269 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026270 {
26271 if (vim_ispathsep(*p)
26272 && p[1] == '.'
26273 && (p[2] == NUL
26274 || vim_ispathsep(p[2])
26275 || (p[2] == '.'
26276 && (p[3] == NUL || vim_ispathsep(p[3])))))
26277 break;
26278 }
26279
26280 /* FullName_save() is slow, don't use it when not needed. */
26281 if (*p != NUL || !vim_isAbsName(*fnamep))
26282 {
26283 *fnamep = FullName_save(*fnamep, *p != NUL);
26284 vim_free(*bufp); /* free any allocated file name */
26285 *bufp = *fnamep;
26286 if (*fnamep == NULL)
26287 return -1;
26288 }
26289
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026290#ifdef WIN3264
26291# if _WIN32_WINNT >= 0x0500
26292 if (vim_strchr(*fnamep, '~') != NULL)
26293 {
26294 /* Expand 8.3 filename to full path. Needed to make sure the same
26295 * file does not have two different names.
26296 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26297 p = alloc(_MAX_PATH + 1);
26298 if (p != NULL)
26299 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026300 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026301 {
26302 vim_free(*bufp);
26303 *bufp = *fnamep = p;
26304 }
26305 else
26306 vim_free(p);
26307 }
26308 }
26309# endif
26310#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026311 /* Append a path separator to a directory. */
26312 if (mch_isdir(*fnamep))
26313 {
26314 /* Make room for one or two extra characters. */
26315 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26316 vim_free(*bufp); /* free any allocated file name */
26317 *bufp = *fnamep;
26318 if (*fnamep == NULL)
26319 return -1;
26320 add_pathsep(*fnamep);
26321 }
26322 }
26323
26324 /* ":." - path relative to the current directory */
26325 /* ":~" - path relative to the home directory */
26326 /* ":8" - shortname path - postponed till after */
26327 while (src[*usedlen] == ':'
26328 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26329 {
26330 *usedlen += 2;
26331 if (c == '8')
26332 {
26333#ifdef WIN3264
26334 has_shortname = 1; /* Postpone this. */
26335#endif
26336 continue;
26337 }
26338 pbuf = NULL;
26339 /* Need full path first (use expand_env() to remove a "~/") */
26340 if (!has_fullname)
26341 {
26342 if (c == '.' && **fnamep == '~')
26343 p = pbuf = expand_env_save(*fnamep);
26344 else
26345 p = pbuf = FullName_save(*fnamep, FALSE);
26346 }
26347 else
26348 p = *fnamep;
26349
26350 has_fullname = 0;
26351
26352 if (p != NULL)
26353 {
26354 if (c == '.')
26355 {
26356 mch_dirname(dirname, MAXPATHL);
26357 s = shorten_fname(p, dirname);
26358 if (s != NULL)
26359 {
26360 *fnamep = s;
26361 if (pbuf != NULL)
26362 {
26363 vim_free(*bufp); /* free any allocated file name */
26364 *bufp = pbuf;
26365 pbuf = NULL;
26366 }
26367 }
26368 }
26369 else
26370 {
26371 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26372 /* Only replace it when it starts with '~' */
26373 if (*dirname == '~')
26374 {
26375 s = vim_strsave(dirname);
26376 if (s != NULL)
26377 {
26378 *fnamep = s;
26379 vim_free(*bufp);
26380 *bufp = s;
26381 }
26382 }
26383 }
26384 vim_free(pbuf);
26385 }
26386 }
26387
26388 tail = gettail(*fnamep);
26389 *fnamelen = (int)STRLEN(*fnamep);
26390
26391 /* ":h" - head, remove "/file_name", can be repeated */
26392 /* Don't remove the first "/" or "c:\" */
26393 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26394 {
26395 valid |= VALID_HEAD;
26396 *usedlen += 2;
26397 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026398 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026399 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026400 *fnamelen = (int)(tail - *fnamep);
26401#ifdef VMS
26402 if (*fnamelen > 0)
26403 *fnamelen += 1; /* the path separator is part of the path */
26404#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026405 if (*fnamelen == 0)
26406 {
26407 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26408 p = vim_strsave((char_u *)".");
26409 if (p == NULL)
26410 return -1;
26411 vim_free(*bufp);
26412 *bufp = *fnamep = tail = p;
26413 *fnamelen = 1;
26414 }
26415 else
26416 {
26417 while (tail > s && !after_pathsep(s, tail))
26418 mb_ptr_back(*fnamep, tail);
26419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026420 }
26421
26422 /* ":8" - shortname */
26423 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26424 {
26425 *usedlen += 2;
26426#ifdef WIN3264
26427 has_shortname = 1;
26428#endif
26429 }
26430
26431#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026432 /*
26433 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026434 */
26435 if (has_shortname)
26436 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026437 /* Copy the string if it is shortened by :h and when it wasn't copied
26438 * yet, because we are going to change it in place. Avoids changing
26439 * the buffer name for "%:8". */
26440 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026441 {
26442 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026443 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026444 return -1;
26445 vim_free(*bufp);
26446 *bufp = *fnamep = p;
26447 }
26448
26449 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026450 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026451 if (!has_fullname && !vim_isAbsName(*fnamep))
26452 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026453 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026454 return -1;
26455 }
26456 else
26457 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026458 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026459
Bram Moolenaardc935552011-08-17 15:23:23 +020026460 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026461 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026462 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026463 return -1;
26464
26465 if (l == 0)
26466 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026467 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026468 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026469 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026470 return -1;
26471 }
26472 *fnamelen = l;
26473 }
26474 }
26475#endif /* WIN3264 */
26476
26477 /* ":t" - tail, just the basename */
26478 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26479 {
26480 *usedlen += 2;
26481 *fnamelen -= (int)(tail - *fnamep);
26482 *fnamep = tail;
26483 }
26484
26485 /* ":e" - extension, can be repeated */
26486 /* ":r" - root, without extension, can be repeated */
26487 while (src[*usedlen] == ':'
26488 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26489 {
26490 /* find a '.' in the tail:
26491 * - for second :e: before the current fname
26492 * - otherwise: The last '.'
26493 */
26494 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26495 s = *fnamep - 2;
26496 else
26497 s = *fnamep + *fnamelen - 1;
26498 for ( ; s > tail; --s)
26499 if (s[0] == '.')
26500 break;
26501 if (src[*usedlen + 1] == 'e') /* :e */
26502 {
26503 if (s > tail)
26504 {
26505 *fnamelen += (int)(*fnamep - (s + 1));
26506 *fnamep = s + 1;
26507#ifdef VMS
26508 /* cut version from the extension */
26509 s = *fnamep + *fnamelen - 1;
26510 for ( ; s > *fnamep; --s)
26511 if (s[0] == ';')
26512 break;
26513 if (s > *fnamep)
26514 *fnamelen = s - *fnamep;
26515#endif
26516 }
26517 else if (*fnamep <= tail)
26518 *fnamelen = 0;
26519 }
26520 else /* :r */
26521 {
26522 if (s > tail) /* remove one extension */
26523 *fnamelen = (int)(s - *fnamep);
26524 }
26525 *usedlen += 2;
26526 }
26527
26528 /* ":s?pat?foo?" - substitute */
26529 /* ":gs?pat?foo?" - global substitute */
26530 if (src[*usedlen] == ':'
26531 && (src[*usedlen + 1] == 's'
26532 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26533 {
26534 char_u *str;
26535 char_u *pat;
26536 char_u *sub;
26537 int sep;
26538 char_u *flags;
26539 int didit = FALSE;
26540
26541 flags = (char_u *)"";
26542 s = src + *usedlen + 2;
26543 if (src[*usedlen + 1] == 'g')
26544 {
26545 flags = (char_u *)"g";
26546 ++s;
26547 }
26548
26549 sep = *s++;
26550 if (sep)
26551 {
26552 /* find end of pattern */
26553 p = vim_strchr(s, sep);
26554 if (p != NULL)
26555 {
26556 pat = vim_strnsave(s, (int)(p - s));
26557 if (pat != NULL)
26558 {
26559 s = p + 1;
26560 /* find end of substitution */
26561 p = vim_strchr(s, sep);
26562 if (p != NULL)
26563 {
26564 sub = vim_strnsave(s, (int)(p - s));
26565 str = vim_strnsave(*fnamep, *fnamelen);
26566 if (sub != NULL && str != NULL)
26567 {
26568 *usedlen = (int)(p + 1 - src);
26569 s = do_string_sub(str, pat, sub, flags);
26570 if (s != NULL)
26571 {
26572 *fnamep = s;
26573 *fnamelen = (int)STRLEN(s);
26574 vim_free(*bufp);
26575 *bufp = s;
26576 didit = TRUE;
26577 }
26578 }
26579 vim_free(sub);
26580 vim_free(str);
26581 }
26582 vim_free(pat);
26583 }
26584 }
26585 /* after using ":s", repeat all the modifiers */
26586 if (didit)
26587 goto repeat;
26588 }
26589 }
26590
Bram Moolenaar26df0922014-02-23 23:39:13 +010026591 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26592 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010026593 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010026594 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010026595 if (c != NUL)
26596 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010026597 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010026598 if (c != NUL)
26599 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010026600 if (p == NULL)
26601 return -1;
26602 vim_free(*bufp);
26603 *bufp = *fnamep = p;
26604 *fnamelen = (int)STRLEN(p);
26605 *usedlen += 2;
26606 }
26607
Bram Moolenaar071d4272004-06-13 20:20:40 +000026608 return valid;
26609}
26610
26611/*
26612 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26613 * "flags" can be "g" to do a global substitute.
26614 * Returns an allocated string, NULL for error.
26615 */
26616 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026617do_string_sub(
26618 char_u *str,
26619 char_u *pat,
26620 char_u *sub,
26621 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026622{
26623 int sublen;
26624 regmatch_T regmatch;
26625 int i;
26626 int do_all;
26627 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026628 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026629 garray_T ga;
26630 char_u *ret;
26631 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026632 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026633
26634 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26635 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026636 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026637
26638 ga_init2(&ga, 1, 200);
26639
26640 do_all = (flags[0] == 'g');
26641
26642 regmatch.rm_ic = p_ic;
26643 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26644 if (regmatch.regprog != NULL)
26645 {
26646 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026647 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026648 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26649 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026650 /* Skip empty match except for first match. */
26651 if (regmatch.startp[0] == regmatch.endp[0])
26652 {
26653 if (zero_width == regmatch.startp[0])
26654 {
26655 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026656 i = MB_PTR2LEN(tail);
26657 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26658 (size_t)i);
26659 ga.ga_len += i;
26660 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026661 continue;
26662 }
26663 zero_width = regmatch.startp[0];
26664 }
26665
Bram Moolenaar071d4272004-06-13 20:20:40 +000026666 /*
26667 * Get some space for a temporary buffer to do the substitution
26668 * into. It will contain:
26669 * - The text up to where the match is.
26670 * - The substituted text.
26671 * - The text after the match.
26672 */
26673 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026674 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026675 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26676 {
26677 ga_clear(&ga);
26678 break;
26679 }
26680
26681 /* copy the text up to where the match is */
26682 i = (int)(regmatch.startp[0] - tail);
26683 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26684 /* add the substituted text */
26685 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26686 + ga.ga_len + i, TRUE, TRUE, FALSE);
26687 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026688 tail = regmatch.endp[0];
26689 if (*tail == NUL)
26690 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026691 if (!do_all)
26692 break;
26693 }
26694
26695 if (ga.ga_data != NULL)
26696 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26697
Bram Moolenaar473de612013-06-08 18:19:48 +020026698 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026699 }
26700
26701 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26702 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026703 if (p_cpo == empty_option)
26704 p_cpo = save_cpo;
26705 else
26706 /* Darn, evaluating {sub} expression changed the value. */
26707 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026708
26709 return ret;
26710}
26711
26712#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */