blob: a51225d56c826df07353be243e51c695a69267a1 [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 Moolenaar21decdd2016-04-22 10:00:58 +0200101#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +0200102static char *e_stringreq = N_("E928: String required");
Bram Moolenaar21decdd2016-04-22 10:00:58 +0200103#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +0000104static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000105static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
106static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
107static char *e_funcdict = N_("E717: Dictionary entry already exists");
108static char *e_funcref = N_("E718: Funcref required");
109static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
110static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000111static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000112static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200113#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200114static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200115#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000116
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +0100117#define NAMESPACE_CHAR (char_u *)"abglstvw"
118
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200119static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
122/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000123 * Old Vim variables such as "v:version" are also available without the "v:".
124 * Also in functions. We need a special hashtable for them.
125 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000126static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000127
128/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 * When recursively copying lists and dicts we need to remember which ones we
130 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000132 */
133static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000134#define COPYID_INC 2
135#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000136
Bram Moolenaar8502c702014-06-17 12:51:16 +0200137/* Abort conversion to string after a recursion error. */
138static int did_echo_string_emsg = FALSE;
139
Bram Moolenaard9fba312005-06-26 22:34:35 +0000140/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000141 * Array to hold the hashtab with variables local to each sourced script.
142 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000144typedef struct
145{
146 dictitem_T sv_var;
147 dict_T sv_dict;
148} scriptvar_T;
149
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200150static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
151#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
152#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153
154static int echo_attr = 0; /* attributes used for ":echo" */
155
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000156/* Values for trans_function_name() argument: */
157#define TFN_INT 1 /* internal function name OK */
158#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100159#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
160
161/* Values for get_lval() flags argument: */
162#define GLV_QUIET TFN_QUIET /* no error messages */
163#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000164
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165/*
166 * Structure to hold info for a user function.
167 */
168typedef struct ufunc ufunc_T;
169
170struct ufunc
171{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000172 int uf_varargs; /* variable nr of arguments */
173 int uf_flags;
174 int uf_calls; /* nr of active calls */
175 garray_T uf_args; /* arguments */
176 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000177#ifdef FEAT_PROFILE
178 int uf_profiling; /* TRUE when func is being profiled */
179 /* profiling the function as a whole */
180 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000181 proftime_T uf_tm_total; /* time spent in function + children */
182 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000183 proftime_T uf_tm_children; /* time spent in children this call */
184 /* profiling the function per line */
185 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000186 proftime_T *uf_tml_total; /* time spent in a line + children */
187 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000188 proftime_T uf_tml_start; /* start time for current line */
189 proftime_T uf_tml_children; /* time spent in children for this line */
190 proftime_T uf_tml_wait; /* start wait time for current line */
191 int uf_tml_idx; /* index of line being timed; -1 if none */
192 int uf_tml_execed; /* line being timed was executed */
193#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000194 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000196 int uf_refcount; /* for numbered function: reference count */
197 char_u uf_name[1]; /* name of function (actually longer); can
198 start with <SNR>123_ (<SNR> is K_SPECIAL
199 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200};
201
202/* function flags */
203#define FC_ABORT 1 /* abort function on error */
204#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000205#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206
207/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000208 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000210static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000212/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000213static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
214
Bram Moolenaar5f436fc2016-03-22 22:34:03 +0100215/* List heads for garbage collection. Although there can be a reference loop
216 * from partial to dict to partial, we don't need to keep track of the partial,
217 * since it will get freed when the dict is unused and gets freed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000218static dict_T *first_dict = NULL; /* list of all dicts */
219static list_T *first_list = NULL; /* list of all lists */
220
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000221/* From user function to hashitem and back. */
222static ufunc_T dumuf;
223#define UF2HIKEY(fp) ((fp)->uf_name)
224#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
225#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
226
227#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
228#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229
Bram Moolenaar33570922005-01-25 22:26:29 +0000230#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
231#define VAR_SHORT_LEN 20 /* short variable name length */
232#define FIXVAR_CNT 12 /* number of fixed variables */
233
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000235typedef struct funccall_S funccall_T;
236
237struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238{
239 ufunc_T *func; /* function being called */
240 int linenr; /* next line to be executed */
241 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000242 struct /* fixed variables for arguments */
243 {
244 dictitem_T var; /* variable (without room for name) */
245 char_u room[VAR_SHORT_LEN]; /* room for the name */
246 } fixvar[FIXVAR_CNT];
247 dict_T l_vars; /* l: local function variables */
248 dictitem_T l_vars_var; /* variable for l: scope */
249 dict_T l_avars; /* a: argument variables */
250 dictitem_T l_avars_var; /* variable for a: scope */
251 list_T l_varlist; /* list for a:000 */
252 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
253 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 linenr_T breakpoint; /* next line with breakpoint or zero */
255 int dbg_tick; /* debug_tick when breakpoint was set */
256 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000257#ifdef FEAT_PROFILE
258 proftime_T prof_child; /* time spent in a child */
259#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000260 funccall_T *caller; /* calling function or NULL */
261};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262
263/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000264 * Info used by a ":for" loop.
265 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000266typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000267{
268 int fi_semicolon; /* TRUE if ending in '; var]' */
269 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000270 listwatch_T fi_lw; /* keep an eye on the item used. */
271 list_T *fi_list; /* list being used */
272} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000273
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000274/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000275 * Struct used by trans_function_name()
276 */
277typedef struct
278{
Bram Moolenaar33570922005-01-25 22:26:29 +0000279 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000280 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000281 dictitem_T *fd_di; /* Dictionary item used */
282} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000283
Bram Moolenaara7043832005-01-21 11:56:39 +0000284
285/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000286 * Array to hold the value of v: variables.
287 * The value is in a dictitem, so that it can also be used in the v: scope.
288 * The reason to use this table anyway is for very quick access to the
289 * variables with the VV_ defines.
290 */
291#include "version.h"
292
293/* values for vv_flags: */
294#define VV_COMPAT 1 /* compatible, also used without "v:" */
295#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000296#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000297
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100298#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
Bram Moolenaar33570922005-01-25 22:26:29 +0000299
300static struct vimvar
301{
302 char *vv_name; /* name of variable, without v: */
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100303 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000304 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
305} vimvars[VV_LEN] =
306{
307 /*
308 * The order here must match the VV_ defines in vim.h!
309 * Initializing a union does not work, leave tv.vval empty to get zero's.
310 */
311 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
312 {VV_NAME("count1", VAR_NUMBER), VV_RO},
313 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
314 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
315 {VV_NAME("warningmsg", VAR_STRING), 0},
316 {VV_NAME("statusmsg", VAR_STRING), 0},
317 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
318 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
319 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
320 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
321 {VV_NAME("termresponse", VAR_STRING), VV_RO},
322 {VV_NAME("fname", VAR_STRING), VV_RO},
323 {VV_NAME("lang", VAR_STRING), VV_RO},
324 {VV_NAME("lc_time", VAR_STRING), VV_RO},
325 {VV_NAME("ctype", VAR_STRING), VV_RO},
326 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
327 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
328 {VV_NAME("fname_in", VAR_STRING), VV_RO},
329 {VV_NAME("fname_out", VAR_STRING), VV_RO},
330 {VV_NAME("fname_new", VAR_STRING), VV_RO},
331 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
332 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
333 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
334 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
335 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
336 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
337 {VV_NAME("progname", VAR_STRING), VV_RO},
338 {VV_NAME("servername", VAR_STRING), VV_RO},
339 {VV_NAME("dying", VAR_NUMBER), VV_RO},
340 {VV_NAME("exception", VAR_STRING), VV_RO},
341 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
342 {VV_NAME("register", VAR_STRING), VV_RO},
343 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
344 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000345 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
346 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000347 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000348 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
349 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000350 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
Bram Moolenaarc9721bd2016-06-04 17:41:03 +0200352 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000353 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
354 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
355 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000356 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000357 {VV_NAME("swapname", VAR_STRING), VV_RO},
358 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000359 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200360 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000361 {VV_NAME("mouse_win", VAR_NUMBER), 0},
Bram Moolenaar511972d2016-06-04 18:09:59 +0200362 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000363 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
364 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000365 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000366 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100367 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000368 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200369 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200370 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200371 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200372 {VV_NAME("option_new", VAR_STRING), VV_RO},
373 {VV_NAME("option_old", VAR_STRING), VV_RO},
374 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100375 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100376 {VV_NAME("false", VAR_SPECIAL), VV_RO},
377 {VV_NAME("true", VAR_SPECIAL), VV_RO},
378 {VV_NAME("null", VAR_SPECIAL), VV_RO},
379 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100380 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200381 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000382};
383
384/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000385#define vv_type vv_di.di_tv.v_type
386#define vv_nr vv_di.di_tv.vval.v_number
387#define vv_float vv_di.di_tv.vval.v_float
388#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000389#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200390#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000391#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000392
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200393static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000394#define vimvarht vimvardict.dv_hashtab
395
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100396static void prepare_vimvar(int idx, typval_T *save_tv);
397static void restore_vimvar(int idx, typval_T *save_tv);
398static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
399static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
400static char_u *skip_var_one(char_u *arg);
401static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
402static void list_glob_vars(int *first);
403static void list_buf_vars(int *first);
404static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000405#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100406static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000407#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100408static void list_vim_vars(int *first);
409static void list_script_vars(int *first);
410static void list_func_vars(int *first);
411static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
412static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
413static int check_changedtick(char_u *arg);
414static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
415static void clear_lval(lval_T *lp);
416static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
417static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
418static void list_fix_watch(list_T *l, listitem_T *item);
419static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
420static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
421static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
422static void item_lock(typval_T *tv, int deep, int lock);
423static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000424
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100425static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
426static int eval1(char_u **arg, typval_T *rettv, int evaluate);
427static int eval2(char_u **arg, typval_T *rettv, int evaluate);
428static int eval3(char_u **arg, typval_T *rettv, int evaluate);
429static int eval4(char_u **arg, typval_T *rettv, int evaluate);
430static int eval5(char_u **arg, typval_T *rettv, int evaluate);
431static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
432static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000433
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100434static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
435static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
436static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
437static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
438static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200439static void list_free_contents(list_T *l);
440static void list_free_list(list_T *l);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100441static long list_len(list_T *l);
442static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
443static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
444static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
445static long list_find_nr(list_T *l, long idx, int *errorp);
446static long list_idx_of_item(list_T *l, listitem_T *item);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100447static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
448static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
449static list_T *list_copy(list_T *orig, int deep, int copyID);
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200450static char_u *list2string(typval_T *tv, int copyID, int restore_copyID);
451static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int restore_copyID, int copyID, garray_T *join_gap);
452static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo_style, int restore_copyID, int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100453static int free_unref_items(int copyID);
454static dictitem_T *dictitem_copy(dictitem_T *org);
455static void dictitem_remove(dict_T *dict, dictitem_T *item);
456static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
457static long dict_len(dict_T *d);
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200458static char_u *dict2string(typval_T *tv, int copyID, int restore_copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100459static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200460static char_u *echo_string_core(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID, int echo_style, int restore_copyID, int dict_val);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100461static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100462static char_u *string_quote(char_u *str, int function);
463static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
464static int find_internal_func(char_u *name);
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100465static char_u *deref_func_name(char_u *name, int *lenp, partial_T **partial, int no_autoload);
466static 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 +0100467static void emsg_funcname(char *ermsg, char_u *name);
468static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000469
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200470static void dict_free_contents(dict_T *d);
471static void dict_free_dict(dict_T *d);
472
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000473#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100474static void f_abs(typval_T *argvars, typval_T *rettv);
475static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000476#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100477static void f_add(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100478static void f_and(typval_T *argvars, typval_T *rettv);
479static void f_append(typval_T *argvars, typval_T *rettv);
480static void f_argc(typval_T *argvars, typval_T *rettv);
481static void f_argidx(typval_T *argvars, typval_T *rettv);
482static void f_arglistid(typval_T *argvars, typval_T *rettv);
483static void f_argv(typval_T *argvars, typval_T *rettv);
484static void f_assert_equal(typval_T *argvars, typval_T *rettv);
485static void f_assert_exception(typval_T *argvars, typval_T *rettv);
486static void f_assert_fails(typval_T *argvars, typval_T *rettv);
487static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaarea6553b2016-03-27 15:13:38 +0200488static void f_assert_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +0200489static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
490static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100491static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000492#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100493static void f_asin(typval_T *argvars, typval_T *rettv);
494static void f_atan(typval_T *argvars, typval_T *rettv);
495static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000496#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100497static void f_browse(typval_T *argvars, typval_T *rettv);
498static void f_browsedir(typval_T *argvars, typval_T *rettv);
499static void f_bufexists(typval_T *argvars, typval_T *rettv);
500static void f_buflisted(typval_T *argvars, typval_T *rettv);
501static void f_bufloaded(typval_T *argvars, typval_T *rettv);
502static void f_bufname(typval_T *argvars, typval_T *rettv);
503static void f_bufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb3619a92016-06-04 17:58:52 +0200504static void f_bufwinid(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100505static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
506static void f_byte2line(typval_T *argvars, typval_T *rettv);
507static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
508static void f_byteidx(typval_T *argvars, typval_T *rettv);
509static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
510static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000511#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100512static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000513#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100514#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100515static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100516static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
517static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100518static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100519static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100520static void f_ch_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100521static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100522static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
523static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100524static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100525static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100526static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
527static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100528static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100529static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100530#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100531static void f_changenr(typval_T *argvars, typval_T *rettv);
532static void f_char2nr(typval_T *argvars, typval_T *rettv);
533static void f_cindent(typval_T *argvars, typval_T *rettv);
534static void f_clearmatches(typval_T *argvars, typval_T *rettv);
535static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000536#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100537static void f_complete(typval_T *argvars, typval_T *rettv);
538static void f_complete_add(typval_T *argvars, typval_T *rettv);
539static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000540#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100541static void f_confirm(typval_T *argvars, typval_T *rettv);
542static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000543#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100544static void f_cos(typval_T *argvars, typval_T *rettv);
545static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000546#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100547static void f_count(typval_T *argvars, typval_T *rettv);
548static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
549static void f_cursor(typval_T *argsvars, typval_T *rettv);
550static void f_deepcopy(typval_T *argvars, typval_T *rettv);
551static void f_delete(typval_T *argvars, typval_T *rettv);
552static void f_did_filetype(typval_T *argvars, typval_T *rettv);
553static void f_diff_filler(typval_T *argvars, typval_T *rettv);
554static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
555static void f_empty(typval_T *argvars, typval_T *rettv);
556static void f_escape(typval_T *argvars, typval_T *rettv);
557static void f_eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200558static void f_evalcmd(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100559static void f_eventhandler(typval_T *argvars, typval_T *rettv);
560static void f_executable(typval_T *argvars, typval_T *rettv);
561static void f_exepath(typval_T *argvars, typval_T *rettv);
562static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200563#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100564static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200565#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100566static void f_expand(typval_T *argvars, typval_T *rettv);
567static void f_extend(typval_T *argvars, typval_T *rettv);
568static void f_feedkeys(typval_T *argvars, typval_T *rettv);
569static void f_filereadable(typval_T *argvars, typval_T *rettv);
570static void f_filewritable(typval_T *argvars, typval_T *rettv);
571static void f_filter(typval_T *argvars, typval_T *rettv);
572static void f_finddir(typval_T *argvars, typval_T *rettv);
573static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000574#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100575static void f_float2nr(typval_T *argvars, typval_T *rettv);
576static void f_floor(typval_T *argvars, typval_T *rettv);
577static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000578#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100579static void f_fnameescape(typval_T *argvars, typval_T *rettv);
580static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
581static void f_foldclosed(typval_T *argvars, typval_T *rettv);
582static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
583static void f_foldlevel(typval_T *argvars, typval_T *rettv);
584static void f_foldtext(typval_T *argvars, typval_T *rettv);
585static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
586static void f_foreground(typval_T *argvars, typval_T *rettv);
587static void f_function(typval_T *argvars, typval_T *rettv);
588static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
589static void f_get(typval_T *argvars, typval_T *rettv);
590static void f_getbufline(typval_T *argvars, typval_T *rettv);
591static void f_getbufvar(typval_T *argvars, typval_T *rettv);
592static void f_getchar(typval_T *argvars, typval_T *rettv);
593static void f_getcharmod(typval_T *argvars, typval_T *rettv);
594static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
595static void f_getcmdline(typval_T *argvars, typval_T *rettv);
596static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
597static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
598static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
599static void f_getcwd(typval_T *argvars, typval_T *rettv);
600static void f_getfontname(typval_T *argvars, typval_T *rettv);
601static void f_getfperm(typval_T *argvars, typval_T *rettv);
602static void f_getfsize(typval_T *argvars, typval_T *rettv);
603static void f_getftime(typval_T *argvars, typval_T *rettv);
604static void f_getftype(typval_T *argvars, typval_T *rettv);
605static void f_getline(typval_T *argvars, typval_T *rettv);
606static void f_getmatches(typval_T *argvars, typval_T *rettv);
607static void f_getpid(typval_T *argvars, typval_T *rettv);
608static void f_getcurpos(typval_T *argvars, typval_T *rettv);
609static void f_getpos(typval_T *argvars, typval_T *rettv);
610static void f_getqflist(typval_T *argvars, typval_T *rettv);
611static void f_getreg(typval_T *argvars, typval_T *rettv);
612static void f_getregtype(typval_T *argvars, typval_T *rettv);
613static void f_gettabvar(typval_T *argvars, typval_T *rettv);
614static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
615static void f_getwinposx(typval_T *argvars, typval_T *rettv);
616static void f_getwinposy(typval_T *argvars, typval_T *rettv);
617static void f_getwinvar(typval_T *argvars, typval_T *rettv);
618static void f_glob(typval_T *argvars, typval_T *rettv);
619static void f_globpath(typval_T *argvars, typval_T *rettv);
620static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
621static void f_has(typval_T *argvars, typval_T *rettv);
622static void f_has_key(typval_T *argvars, typval_T *rettv);
623static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
624static void f_hasmapto(typval_T *argvars, typval_T *rettv);
625static void f_histadd(typval_T *argvars, typval_T *rettv);
626static void f_histdel(typval_T *argvars, typval_T *rettv);
627static void f_histget(typval_T *argvars, typval_T *rettv);
628static void f_histnr(typval_T *argvars, typval_T *rettv);
629static void f_hlID(typval_T *argvars, typval_T *rettv);
630static void f_hlexists(typval_T *argvars, typval_T *rettv);
631static void f_hostname(typval_T *argvars, typval_T *rettv);
632static void f_iconv(typval_T *argvars, typval_T *rettv);
633static void f_indent(typval_T *argvars, typval_T *rettv);
634static void f_index(typval_T *argvars, typval_T *rettv);
635static void f_input(typval_T *argvars, typval_T *rettv);
636static void f_inputdialog(typval_T *argvars, typval_T *rettv);
637static void f_inputlist(typval_T *argvars, typval_T *rettv);
638static void f_inputrestore(typval_T *argvars, typval_T *rettv);
639static void f_inputsave(typval_T *argvars, typval_T *rettv);
640static void f_inputsecret(typval_T *argvars, typval_T *rettv);
641static void f_insert(typval_T *argvars, typval_T *rettv);
642static void f_invert(typval_T *argvars, typval_T *rettv);
643static void f_isdirectory(typval_T *argvars, typval_T *rettv);
644static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100645#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
646static void f_isnan(typval_T *argvars, typval_T *rettv);
647#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100648static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100649#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100650static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8950a562016-03-12 15:22:55 +0100651static void f_job_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar65edff82016-02-21 16:40:11 +0100652static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100653static void f_job_start(typval_T *argvars, typval_T *rettv);
654static void f_job_stop(typval_T *argvars, typval_T *rettv);
655static void f_job_status(typval_T *argvars, typval_T *rettv);
656#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100657static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100658static void f_js_decode(typval_T *argvars, typval_T *rettv);
659static void f_js_encode(typval_T *argvars, typval_T *rettv);
660static void f_json_decode(typval_T *argvars, typval_T *rettv);
661static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100662static void f_keys(typval_T *argvars, typval_T *rettv);
663static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
664static void f_len(typval_T *argvars, typval_T *rettv);
665static void f_libcall(typval_T *argvars, typval_T *rettv);
666static void f_libcallnr(typval_T *argvars, typval_T *rettv);
667static void f_line(typval_T *argvars, typval_T *rettv);
668static void f_line2byte(typval_T *argvars, typval_T *rettv);
669static void f_lispindent(typval_T *argvars, typval_T *rettv);
670static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000671#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100672static void f_log(typval_T *argvars, typval_T *rettv);
673static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000674#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200675#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100676static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200677#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100678static void f_map(typval_T *argvars, typval_T *rettv);
679static void f_maparg(typval_T *argvars, typval_T *rettv);
680static void f_mapcheck(typval_T *argvars, typval_T *rettv);
681static void f_match(typval_T *argvars, typval_T *rettv);
682static void f_matchadd(typval_T *argvars, typval_T *rettv);
683static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
684static void f_matcharg(typval_T *argvars, typval_T *rettv);
685static void f_matchdelete(typval_T *argvars, typval_T *rettv);
686static void f_matchend(typval_T *argvars, typval_T *rettv);
687static void f_matchlist(typval_T *argvars, typval_T *rettv);
688static void f_matchstr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +0200689static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100690static void f_max(typval_T *argvars, typval_T *rettv);
691static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000692#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100693static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000694#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100695static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100696#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100697static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100698#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100699static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
700static void f_nr2char(typval_T *argvars, typval_T *rettv);
701static void f_or(typval_T *argvars, typval_T *rettv);
702static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100703#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100704static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100705#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000706#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100707static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000708#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100709static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
710static void f_printf(typval_T *argvars, typval_T *rettv);
711static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200712#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100713static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200714#endif
715#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100716static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200717#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100718static void f_range(typval_T *argvars, typval_T *rettv);
719static void f_readfile(typval_T *argvars, typval_T *rettv);
720static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100721#ifdef FEAT_FLOAT
722static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
723#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100724static void f_reltimestr(typval_T *argvars, typval_T *rettv);
725static void f_remote_expr(typval_T *argvars, typval_T *rettv);
726static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
727static void f_remote_peek(typval_T *argvars, typval_T *rettv);
728static void f_remote_read(typval_T *argvars, typval_T *rettv);
729static void f_remote_send(typval_T *argvars, typval_T *rettv);
730static void f_remove(typval_T *argvars, typval_T *rettv);
731static void f_rename(typval_T *argvars, typval_T *rettv);
732static void f_repeat(typval_T *argvars, typval_T *rettv);
733static void f_resolve(typval_T *argvars, typval_T *rettv);
734static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000735#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100736static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000737#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100738static void f_screenattr(typval_T *argvars, typval_T *rettv);
739static void f_screenchar(typval_T *argvars, typval_T *rettv);
740static void f_screencol(typval_T *argvars, typval_T *rettv);
741static void f_screenrow(typval_T *argvars, typval_T *rettv);
742static void f_search(typval_T *argvars, typval_T *rettv);
743static void f_searchdecl(typval_T *argvars, typval_T *rettv);
744static void f_searchpair(typval_T *argvars, typval_T *rettv);
745static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
746static void f_searchpos(typval_T *argvars, typval_T *rettv);
747static void f_server2client(typval_T *argvars, typval_T *rettv);
748static void f_serverlist(typval_T *argvars, typval_T *rettv);
749static void f_setbufvar(typval_T *argvars, typval_T *rettv);
750static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
751static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100752static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100753static void f_setline(typval_T *argvars, typval_T *rettv);
754static void f_setloclist(typval_T *argvars, typval_T *rettv);
755static void f_setmatches(typval_T *argvars, typval_T *rettv);
756static void f_setpos(typval_T *argvars, typval_T *rettv);
757static void f_setqflist(typval_T *argvars, typval_T *rettv);
758static void f_setreg(typval_T *argvars, typval_T *rettv);
759static void f_settabvar(typval_T *argvars, typval_T *rettv);
760static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
761static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100762#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100763static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100764#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100765static void f_shellescape(typval_T *argvars, typval_T *rettv);
766static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
767static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000768#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100769static void f_sin(typval_T *argvars, typval_T *rettv);
770static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000771#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100772static void f_sort(typval_T *argvars, typval_T *rettv);
773static void f_soundfold(typval_T *argvars, typval_T *rettv);
774static void f_spellbadword(typval_T *argvars, typval_T *rettv);
775static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
776static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000777#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100778static void f_sqrt(typval_T *argvars, typval_T *rettv);
779static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000780#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100781static void f_str2nr(typval_T *argvars, typval_T *rettv);
782static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000783#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100784static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000785#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200786static void f_strgetchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100787static void f_stridx(typval_T *argvars, typval_T *rettv);
788static void f_string(typval_T *argvars, typval_T *rettv);
789static void f_strlen(typval_T *argvars, typval_T *rettv);
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200790static void f_strcharpart(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100791static void f_strpart(typval_T *argvars, typval_T *rettv);
792static void f_strridx(typval_T *argvars, typval_T *rettv);
793static void f_strtrans(typval_T *argvars, typval_T *rettv);
794static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
795static void f_strwidth(typval_T *argvars, typval_T *rettv);
796static void f_submatch(typval_T *argvars, typval_T *rettv);
797static void f_substitute(typval_T *argvars, typval_T *rettv);
798static void f_synID(typval_T *argvars, typval_T *rettv);
799static void f_synIDattr(typval_T *argvars, typval_T *rettv);
800static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
801static void f_synstack(typval_T *argvars, typval_T *rettv);
802static void f_synconcealed(typval_T *argvars, typval_T *rettv);
803static void f_system(typval_T *argvars, typval_T *rettv);
804static void f_systemlist(typval_T *argvars, typval_T *rettv);
805static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
806static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
807static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
808static void f_taglist(typval_T *argvars, typval_T *rettv);
809static void f_tagfiles(typval_T *argvars, typval_T *rettv);
810static void f_tempname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200811static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
812static void f_test_disable_char_avail(typval_T *argvars, typval_T *rettv);
Bram Moolenaar574860b2016-05-24 17:33:34 +0200813static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
814#ifdef FEAT_JOB_CHANNEL
815static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
816#endif
817static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
818#ifdef FEAT_JOB_CHANNEL
819static void f_test_null_job(typval_T *argvars, typval_T *rettv);
820#endif
821static void f_test_null_list(typval_T *argvars, typval_T *rettv);
822static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
823static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +0200824static void f_test_settime(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200825#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100826static void f_tan(typval_T *argvars, typval_T *rettv);
827static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200828#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +0100829#ifdef FEAT_TIMERS
830static void f_timer_start(typval_T *argvars, typval_T *rettv);
831static void f_timer_stop(typval_T *argvars, typval_T *rettv);
832#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100833static void f_tolower(typval_T *argvars, typval_T *rettv);
834static void f_toupper(typval_T *argvars, typval_T *rettv);
835static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000836#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100837static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000838#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100839static void f_type(typval_T *argvars, typval_T *rettv);
840static void f_undofile(typval_T *argvars, typval_T *rettv);
841static void f_undotree(typval_T *argvars, typval_T *rettv);
842static void f_uniq(typval_T *argvars, typval_T *rettv);
843static void f_values(typval_T *argvars, typval_T *rettv);
844static void f_virtcol(typval_T *argvars, typval_T *rettv);
845static void f_visualmode(typval_T *argvars, typval_T *rettv);
846static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +0100847static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar86edef62016-03-13 18:07:30 +0100848static void f_win_getid(typval_T *argvars, typval_T *rettv);
849static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
850static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
851static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100852static void f_winbufnr(typval_T *argvars, typval_T *rettv);
853static void f_wincol(typval_T *argvars, typval_T *rettv);
854static void f_winheight(typval_T *argvars, typval_T *rettv);
855static void f_winline(typval_T *argvars, typval_T *rettv);
856static void f_winnr(typval_T *argvars, typval_T *rettv);
857static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
858static void f_winrestview(typval_T *argvars, typval_T *rettv);
859static void f_winsaveview(typval_T *argvars, typval_T *rettv);
860static void f_winwidth(typval_T *argvars, typval_T *rettv);
861static void f_writefile(typval_T *argvars, typval_T *rettv);
862static void f_wordcount(typval_T *argvars, typval_T *rettv);
863static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000864
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100865static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
866static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
867static int get_env_len(char_u **arg);
868static int get_id_len(char_u **arg);
869static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
870static 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 +0000871#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
872#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
873 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100874static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
875static int eval_isnamec(int c);
876static int eval_isnamec1(int c);
877static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
878static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100879static typval_T *alloc_string_tv(char_u *string);
880static void init_tv(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100881#ifdef FEAT_FLOAT
882static float_T get_tv_float(typval_T *varp);
883#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100884static linenr_T get_tv_lnum(typval_T *argvars);
885static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100886static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
887static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
888static hashtab_T *find_var_ht(char_u *name, char_u **varname);
889static funccall_T *get_funccal(void);
890static void vars_clear_ext(hashtab_T *ht, int free_val);
891static void delete_var(hashtab_T *ht, hashitem_T *hi);
892static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
893static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
894static void set_var(char_u *name, typval_T *varp, int copy);
895static int var_check_ro(int flags, char_u *name, int use_gettext);
896static int var_check_fixed(int flags, char_u *name, int use_gettext);
897static int var_check_func_name(char_u *name, int new_var);
898static int valid_varname(char_u *varname);
899static int tv_check_lock(int lock, char_u *name, int use_gettext);
900static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
901static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar65639032016-03-16 21:40:30 +0100902static 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 +0100903static int eval_fname_script(char_u *p);
904static int eval_fname_sid(char_u *p);
905static void list_func_head(ufunc_T *fp, int indent);
906static ufunc_T *find_func(char_u *name);
907static int function_exists(char_u *name);
908static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000909#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100910static void func_do_profile(ufunc_T *fp);
911static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
912static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000913static int
914# ifdef __BORLANDC__
915 _RTLENTRYF
916# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100917 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000918static int
919# ifdef __BORLANDC__
920 _RTLENTRYF
921# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100922 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000923#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100924static int script_autoload(char_u *name, int reload);
925static char_u *autoload_name(char_u *name);
926static void cat_func_name(char_u *buf, ufunc_T *fp);
927static void func_free(ufunc_T *fp);
928static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
929static int can_free_funccal(funccall_T *fc, int copyID) ;
930static void free_funccal(funccall_T *fc, int free_val);
931static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
932static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
933static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
934static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
935static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
936static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
937static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
938static int write_list(FILE *fd, list_T *list, int binary);
939static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000940
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200941
942#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100943static int compare_func_name(const void *s1, const void *s2);
944static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200945#endif
946
Bram Moolenaar33570922005-01-25 22:26:29 +0000947/*
948 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000949 */
950 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100951eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000952{
Bram Moolenaar33570922005-01-25 22:26:29 +0000953 int i;
954 struct vimvar *p;
955
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200956 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
957 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200958 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000959 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000960 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000961
962 for (i = 0; i < VV_LEN; ++i)
963 {
964 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200965 if (STRLEN(p->vv_name) > 16)
966 {
967 EMSG("INTERNAL: name too long, increase size of dictitem16_T");
968 getout(1);
969 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000970 STRCPY(p->vv_di.di_key, p->vv_name);
971 if (p->vv_flags & VV_RO)
972 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
973 else if (p->vv_flags & VV_RO_SBX)
974 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
975 else
976 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000977
978 /* add to v: scope dict, unless the value is not always available */
979 if (p->vv_type != VAR_UNKNOWN)
980 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000981 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000982 /* add to compat scope dict */
983 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000984 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100985 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
986
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000987 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100988 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200989 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100990 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100991
992 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
993 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
994 set_vim_var_nr(VV_NONE, VVAL_NONE);
995 set_vim_var_nr(VV_NULL, VVAL_NULL);
996
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200997 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200998
999#ifdef EBCDIC
1000 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +01001001 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +02001002 */
1003 sortFunctions();
1004#endif
Bram Moolenaara7043832005-01-21 11:56:39 +00001005}
1006
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001007#if defined(EXITFREE) || defined(PROTO)
1008 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001009eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001010{
1011 int i;
1012 struct vimvar *p;
1013
1014 for (i = 0; i < VV_LEN; ++i)
1015 {
1016 p = &vimvars[i];
1017 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +00001018 {
Bram Moolenaar12193212008-11-09 16:22:01 +00001019 vim_free(p->vv_str);
1020 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00001021 }
1022 else if (p->vv_di.di_tv.v_type == VAR_LIST)
1023 {
1024 list_unref(p->vv_list);
1025 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +00001026 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001027 }
1028 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +00001029 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001030 hash_clear(&compat_hashtab);
1031
Bram Moolenaard9fba312005-06-26 22:34:35 +00001032 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001033# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02001034 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001035# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001036
1037 /* global variables */
1038 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +00001039
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001040 /* autoloaded script names */
1041 ga_clear_strings(&ga_loaded);
1042
Bram Moolenaarcca74132013-09-25 21:00:28 +02001043 /* Script-local variables. First clear all the variables and in a second
1044 * loop free the scriptvar_T, because a variable in one script might hold
1045 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001046 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001047 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001048 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001049 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001050 ga_clear(&ga_scripts);
1051
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001052 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001053 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001054
1055 /* functions */
1056 free_all_functions();
1057 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001058}
1059#endif
1060
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001061/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 * Return the name of the executed function.
1063 */
1064 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001065func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001067 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068}
1069
1070/*
1071 * Return the address holding the next breakpoint line for a funccall cookie.
1072 */
1073 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001074func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075{
Bram Moolenaar33570922005-01-25 22:26:29 +00001076 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077}
1078
1079/*
1080 * Return the address holding the debug tick for a funccall cookie.
1081 */
1082 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001083func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084{
Bram Moolenaar33570922005-01-25 22:26:29 +00001085 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086}
1087
1088/*
1089 * Return the nesting level for a funccall cookie.
1090 */
1091 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001092func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093{
Bram Moolenaar33570922005-01-25 22:26:29 +00001094 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095}
1096
1097/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001098funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001100/* pointer to list of previously used funccal, still around because some
1101 * item in it is still being used. */
1102funccall_T *previous_funccal = NULL;
1103
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104/*
1105 * Return TRUE when a function was ended by a ":return" command.
1106 */
1107 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001108current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109{
1110 return current_funccal->returned;
1111}
1112
1113
1114/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 * Set an internal variable to a string value. Creates the variable if it does
1116 * not already exist.
1117 */
1118 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001119set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001121 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001122 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123
1124 val = vim_strsave(value);
1125 if (val != NULL)
1126 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001127 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001128 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001130 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001131 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 }
1133 }
1134}
1135
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001136static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +02001137#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001138static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001139static char_u *redir_endp = NULL;
1140static char_u *redir_varname = NULL;
1141
1142/*
1143 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001144 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001145 * Returns OK if successfully completed the setup. FAIL otherwise.
1146 */
1147 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001148var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001149{
1150 int save_emsg;
1151 int err;
1152 typval_T tv;
1153
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001154 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001155 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001156 {
1157 EMSG(_(e_invarg));
1158 return FAIL;
1159 }
1160
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001161 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001162 redir_varname = vim_strsave(name);
1163 if (redir_varname == NULL)
1164 return FAIL;
1165
1166 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1167 if (redir_lval == NULL)
1168 {
1169 var_redir_stop();
1170 return FAIL;
1171 }
1172
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001173 /* The output is stored in growarray "redir_ga" until redirection ends. */
1174 ga_init2(&redir_ga, (int)sizeof(char), 500);
1175
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001176 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001177 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001178 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001179 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1180 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001181 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001182 if (redir_endp != NULL && *redir_endp != NUL)
1183 /* Trailing characters are present after the variable name */
1184 EMSG(_(e_trailing));
1185 else
1186 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001187 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001188 var_redir_stop();
1189 return FAIL;
1190 }
1191
1192 /* check if we can write to the variable: set it to or append an empty
1193 * string */
1194 save_emsg = did_emsg;
1195 did_emsg = FALSE;
1196 tv.v_type = VAR_STRING;
1197 tv.vval.v_string = (char_u *)"";
1198 if (append)
1199 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1200 else
1201 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001202 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001203 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001204 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001205 if (err)
1206 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001207 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001208 var_redir_stop();
1209 return FAIL;
1210 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001211
1212 return OK;
1213}
1214
1215/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001216 * Append "value[value_len]" to the variable set by var_redir_start().
1217 * The actual appending is postponed until redirection ends, because the value
1218 * appended may in fact be the string we write to, changing it may cause freed
1219 * memory to be used:
1220 * :redir => foo
1221 * :let foo
1222 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001223 */
1224 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001225var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001226{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001227 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001228
1229 if (redir_lval == NULL)
1230 return;
1231
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001232 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001233 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001234 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001235 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001236
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001237 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001238 {
1239 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001240 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001241 }
1242 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001243 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001244}
1245
1246/*
1247 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001248 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001249 */
1250 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001251var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001252{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001253 typval_T tv;
1254
Bram Moolenaar1e5e1232016-07-07 17:33:02 +02001255 if (EVALCMD_BUSY)
1256 {
1257 redir_lval = NULL;
1258 return;
1259 }
1260
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001261 if (redir_lval != NULL)
1262 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001263 /* If there was no error: assign the text to the variable. */
1264 if (redir_endp != NULL)
1265 {
1266 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1267 tv.v_type = VAR_STRING;
1268 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001269 /* Call get_lval() again, if it's inside a Dict or List it may
1270 * have changed. */
1271 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001272 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001273 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1274 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1275 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001276 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001277
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001278 /* free the collected output */
1279 vim_free(redir_ga.ga_data);
1280 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001281
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001282 vim_free(redir_lval);
1283 redir_lval = NULL;
1284 }
1285 vim_free(redir_varname);
1286 redir_varname = NULL;
1287}
1288
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289# if defined(FEAT_MBYTE) || defined(PROTO)
1290 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001291eval_charconvert(
1292 char_u *enc_from,
1293 char_u *enc_to,
1294 char_u *fname_from,
1295 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296{
1297 int err = FALSE;
1298
1299 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1300 set_vim_var_string(VV_CC_TO, enc_to, -1);
1301 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1302 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1303 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1304 err = TRUE;
1305 set_vim_var_string(VV_CC_FROM, NULL, -1);
1306 set_vim_var_string(VV_CC_TO, NULL, -1);
1307 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1308 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1309
1310 if (err)
1311 return FAIL;
1312 return OK;
1313}
1314# endif
1315
1316# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1317 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001318eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319{
1320 int err = FALSE;
1321
1322 set_vim_var_string(VV_FNAME_IN, fname, -1);
1323 set_vim_var_string(VV_CMDARG, args, -1);
1324 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1325 err = TRUE;
1326 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1327 set_vim_var_string(VV_CMDARG, NULL, -1);
1328
1329 if (err)
1330 {
1331 mch_remove(fname);
1332 return FAIL;
1333 }
1334 return OK;
1335}
1336# endif
1337
1338# if defined(FEAT_DIFF) || defined(PROTO)
1339 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001340eval_diff(
1341 char_u *origfile,
1342 char_u *newfile,
1343 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344{
1345 int err = FALSE;
1346
1347 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1348 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1349 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1350 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1351 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1352 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1353 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1354}
1355
1356 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001357eval_patch(
1358 char_u *origfile,
1359 char_u *difffile,
1360 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361{
1362 int err;
1363
1364 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1365 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1366 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1367 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1368 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1369 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1370 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1371}
1372# endif
1373
1374/*
1375 * Top level evaluation function, returning a boolean.
1376 * Sets "error" to TRUE if there was an error.
1377 * Return TRUE or FALSE.
1378 */
1379 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001380eval_to_bool(
1381 char_u *arg,
1382 int *error,
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 Moolenaar22fcfad2016-07-01 18:17:26 +02001387 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388
1389 if (skip)
1390 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001391 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 else
1394 {
1395 *error = FALSE;
1396 if (!skip)
1397 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001398 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001399 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 }
1401 }
1402 if (skip)
1403 --emsg_skip;
1404
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001405 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406}
1407
1408/*
1409 * Top level evaluation function, returning a string. If "skip" is TRUE,
1410 * only parsing to "nextcmd" is done, without reporting errors. Return
1411 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1412 */
1413 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001414eval_to_string_skip(
1415 char_u *arg,
1416 char_u **nextcmd,
1417 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418{
Bram Moolenaar33570922005-01-25 22:26:29 +00001419 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 char_u *retval;
1421
1422 if (skip)
1423 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001424 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 retval = NULL;
1426 else
1427 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001428 retval = vim_strsave(get_tv_string(&tv));
1429 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 }
1431 if (skip)
1432 --emsg_skip;
1433
1434 return retval;
1435}
1436
1437/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001438 * Skip over an expression at "*pp".
1439 * Return FAIL for an error, OK otherwise.
1440 */
1441 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001442skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001443{
Bram Moolenaar33570922005-01-25 22:26:29 +00001444 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001445
1446 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001447 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001448}
1449
1450/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001452 * When "convert" is TRUE convert a List into a sequence of lines and convert
1453 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 * Return pointer to allocated memory, or NULL for failure.
1455 */
1456 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001457eval_to_string(
1458 char_u *arg,
1459 char_u **nextcmd,
1460 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461{
Bram Moolenaar33570922005-01-25 22:26:29 +00001462 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001464 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001465#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001466 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001469 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 retval = NULL;
1471 else
1472 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001473 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001474 {
1475 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001476 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001477 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02001478 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001479 if (tv.vval.v_list->lv_len > 0)
1480 ga_append(&ga, NL);
1481 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001482 ga_append(&ga, NUL);
1483 retval = (char_u *)ga.ga_data;
1484 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001485#ifdef FEAT_FLOAT
1486 else if (convert && tv.v_type == VAR_FLOAT)
1487 {
1488 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1489 retval = vim_strsave(numbuf);
1490 }
1491#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001492 else
1493 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001494 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 }
1496
1497 return retval;
1498}
1499
1500/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001501 * Call eval_to_string() without using current local variables and using
1502 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 */
1504 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001505eval_to_string_safe(
1506 char_u *arg,
1507 char_u **nextcmd,
1508 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509{
1510 char_u *retval;
1511 void *save_funccalp;
1512
1513 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001514 if (use_sandbox)
1515 ++sandbox;
1516 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001517 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001518 if (use_sandbox)
1519 --sandbox;
1520 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 restore_funccal(save_funccalp);
1522 return retval;
1523}
1524
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525/*
1526 * Top level evaluation function, returning a number.
1527 * Evaluates "expr" silently.
1528 * Returns -1 for an error.
1529 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001530 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001531eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532{
Bram Moolenaar33570922005-01-25 22:26:29 +00001533 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001534 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001535 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536
1537 ++emsg_off;
1538
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001539 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 retval = -1;
1541 else
1542 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001543 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001544 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 }
1546 --emsg_off;
1547
1548 return retval;
1549}
1550
Bram Moolenaara40058a2005-07-11 22:42:07 +00001551/*
1552 * Prepare v: variable "idx" to be used.
1553 * Save the current typeval in "save_tv".
1554 * When not used yet add the variable to the v: hashtable.
1555 */
1556 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001557prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001558{
1559 *save_tv = vimvars[idx].vv_tv;
1560 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1561 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1562}
1563
1564/*
1565 * Restore v: variable "idx" to typeval "save_tv".
1566 * When no longer defined, remove the variable from the v: hashtable.
1567 */
1568 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001569restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001570{
1571 hashitem_T *hi;
1572
Bram Moolenaara40058a2005-07-11 22:42:07 +00001573 vimvars[idx].vv_tv = *save_tv;
1574 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1575 {
1576 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1577 if (HASHITEM_EMPTY(hi))
1578 EMSG2(_(e_intern2), "restore_vimvar()");
1579 else
1580 hash_remove(&vimvarht, hi);
1581 }
1582}
1583
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001584#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001585/*
1586 * Evaluate an expression to a list with suggestions.
1587 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001588 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001589 */
1590 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001591eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001592{
1593 typval_T save_val;
1594 typval_T rettv;
1595 list_T *list = NULL;
1596 char_u *p = skipwhite(expr);
1597
1598 /* Set "v:val" to the bad word. */
1599 prepare_vimvar(VV_VAL, &save_val);
1600 vimvars[VV_VAL].vv_type = VAR_STRING;
1601 vimvars[VV_VAL].vv_str = badword;
1602 if (p_verbose == 0)
1603 ++emsg_off;
1604
1605 if (eval1(&p, &rettv, TRUE) == OK)
1606 {
1607 if (rettv.v_type != VAR_LIST)
1608 clear_tv(&rettv);
1609 else
1610 list = rettv.vval.v_list;
1611 }
1612
1613 if (p_verbose == 0)
1614 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001615 restore_vimvar(VV_VAL, &save_val);
1616
1617 return list;
1618}
1619
1620/*
1621 * "list" is supposed to contain two items: a word and a number. Return the
1622 * word in "pp" and the number as the return value.
1623 * Return -1 if anything isn't right.
1624 * Used to get the good word and score from the eval_spell_expr() result.
1625 */
1626 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001627get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001628{
1629 listitem_T *li;
1630
1631 li = list->lv_first;
1632 if (li == NULL)
1633 return -1;
1634 *pp = get_tv_string(&li->li_tv);
1635
1636 li = li->li_next;
1637 if (li == NULL)
1638 return -1;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001639 return (int)get_tv_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001640}
1641#endif
1642
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001643/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001644 * Top level evaluation function.
1645 * Returns an allocated typval_T with the result.
1646 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001647 */
1648 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001649eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001650{
1651 typval_T *tv;
1652
1653 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001654 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001655 {
1656 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001657 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001658 }
1659
1660 return tv;
1661}
1662
1663
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001665 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001666 * Uses argv[argc] for the function arguments. Only Number and String
1667 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001668 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001670 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001671call_vim_function(
1672 char_u *func,
1673 int argc,
1674 char_u **argv,
1675 int safe, /* use the sandbox */
1676 int str_arg_only, /* all arguments are strings */
1677 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678{
Bram Moolenaar33570922005-01-25 22:26:29 +00001679 typval_T *argvars;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001680 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 int len;
1682 int i;
1683 int doesrange;
1684 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001685 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001687 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001689 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690
1691 for (i = 0; i < argc; i++)
1692 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001693 /* Pass a NULL or empty argument as an empty string */
1694 if (argv[i] == NULL || *argv[i] == NUL)
1695 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001696 argvars[i].v_type = VAR_STRING;
1697 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001698 continue;
1699 }
1700
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001701 if (str_arg_only)
1702 len = 0;
1703 else
1704 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001705 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 if (len != 0 && len == (int)STRLEN(argv[i]))
1707 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001708 argvars[i].v_type = VAR_NUMBER;
1709 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 }
1711 else
1712 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001713 argvars[i].v_type = VAR_STRING;
1714 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 }
1716 }
1717
1718 if (safe)
1719 {
1720 save_funccalp = save_funccal();
1721 ++sandbox;
1722 }
1723
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001724 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1725 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001727 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 if (safe)
1729 {
1730 --sandbox;
1731 restore_funccal(save_funccalp);
1732 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001733 vim_free(argvars);
1734
1735 if (ret == FAIL)
1736 clear_tv(rettv);
1737
1738 return ret;
1739}
1740
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001741/*
1742 * Call vimL function "func" and return the result as a number.
1743 * Returns -1 when calling the function fails.
1744 * Uses argv[argc] for the function arguments.
1745 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001746 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001747call_func_retnr(
1748 char_u *func,
1749 int argc,
1750 char_u **argv,
1751 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001752{
1753 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001754 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001755
1756 /* All arguments are passed as strings, no conversion to number. */
1757 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1758 return -1;
1759
1760 retval = get_tv_number_chk(&rettv, NULL);
1761 clear_tv(&rettv);
1762 return retval;
1763}
1764
1765#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1766 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1767
Bram Moolenaar4f688582007-07-24 12:34:30 +00001768# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001769/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001770 * Call vimL function "func" and return the result as a string.
1771 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001772 * Uses argv[argc] for the function arguments.
1773 */
1774 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001775call_func_retstr(
1776 char_u *func,
1777 int argc,
1778 char_u **argv,
1779 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001780{
1781 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001782 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001783
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001784 /* All arguments are passed as strings, no conversion to number. */
1785 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001786 return NULL;
1787
1788 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001789 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 return retval;
1791}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001792# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001793
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001794/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001795 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001796 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001797 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001798 */
1799 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001800call_func_retlist(
1801 char_u *func,
1802 int argc,
1803 char_u **argv,
1804 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001805{
1806 typval_T rettv;
1807
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001808 /* All arguments are passed as strings, no conversion to number. */
1809 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001810 return NULL;
1811
1812 if (rettv.v_type != VAR_LIST)
1813 {
1814 clear_tv(&rettv);
1815 return NULL;
1816 }
1817
1818 return rettv.vval.v_list;
1819}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820#endif
1821
1822/*
1823 * Save the current function call pointer, and set it to NULL.
1824 * Used when executing autocommands and for ":source".
1825 */
1826 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001827save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001829 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 current_funccal = NULL;
1832 return (void *)fc;
1833}
1834
1835 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001836restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001838 funccall_T *fc = (funccall_T *)vfc;
1839
1840 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841}
1842
Bram Moolenaar05159a02005-02-26 23:04:13 +00001843#if defined(FEAT_PROFILE) || defined(PROTO)
1844/*
1845 * Prepare profiling for entering a child or something else that is not
1846 * counted for the script/function itself.
1847 * Should always be called in pair with prof_child_exit().
1848 */
1849 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001850prof_child_enter(
1851 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001852{
1853 funccall_T *fc = current_funccal;
1854
1855 if (fc != NULL && fc->func->uf_profiling)
1856 profile_start(&fc->prof_child);
1857 script_prof_save(tm);
1858}
1859
1860/*
1861 * Take care of time spent in a child.
1862 * Should always be called after prof_child_enter().
1863 */
1864 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001865prof_child_exit(
1866 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001867{
1868 funccall_T *fc = current_funccal;
1869
1870 if (fc != NULL && fc->func->uf_profiling)
1871 {
1872 profile_end(&fc->prof_child);
1873 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1874 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1875 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1876 }
1877 script_prof_restore(tm);
1878}
1879#endif
1880
1881
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882#ifdef FEAT_FOLDING
1883/*
1884 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1885 * it in "*cp". Doesn't give error messages.
1886 */
1887 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001888eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889{
Bram Moolenaar33570922005-01-25 22:26:29 +00001890 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001891 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001893 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1894 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895
1896 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001897 if (use_sandbox)
1898 ++sandbox;
1899 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 retval = 0;
1903 else
1904 {
1905 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001906 if (tv.v_type == VAR_NUMBER)
1907 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001908 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 retval = 0;
1910 else
1911 {
1912 /* If the result is a string, check if there is a non-digit before
1913 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001914 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 if (!VIM_ISDIGIT(*s) && *s != '-')
1916 *cp = *s++;
1917 retval = atol((char *)s);
1918 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001919 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 }
1921 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001922 if (use_sandbox)
1923 --sandbox;
1924 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001926 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927}
1928#endif
1929
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001931 * ":let" list all variable values
1932 * ":let var1 var2" list variable values
1933 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001934 * ":let var += expr" assignment command.
1935 * ":let var -= expr" assignment command.
1936 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001937 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 */
1939 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001940ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941{
1942 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001943 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001944 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001946 int var_count = 0;
1947 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001948 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001949 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001950 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951
Bram Moolenaardb552d602006-03-23 22:59:57 +00001952 argend = skip_var_list(arg, &var_count, &semicolon);
1953 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001954 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001955 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1956 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001957 expr = skipwhite(argend);
1958 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1959 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001961 /*
1962 * ":let" without "=": list variables
1963 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001964 if (*arg == '[')
1965 EMSG(_(e_invarg));
1966 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001967 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001968 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001969 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001970 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001971 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001972 list_glob_vars(&first);
1973 list_buf_vars(&first);
1974 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001975#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001976 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001977#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001978 list_script_vars(&first);
1979 list_func_vars(&first);
1980 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 eap->nextcmd = check_nextcmd(arg);
1983 }
1984 else
1985 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001986 op[0] = '=';
1987 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001988 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001989 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001990 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1991 op[0] = *expr; /* +=, -= or .= */
1992 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001993 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001994 else
1995 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001996
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 if (eap->skip)
1998 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001999 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 if (eap->skip)
2001 {
2002 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002003 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 --emsg_skip;
2005 }
2006 else if (i != FAIL)
2007 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002008 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002009 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002010 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 }
2012 }
2013}
2014
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002015/*
2016 * Assign the typevalue "tv" to the variable or variables at "arg_start".
2017 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002018 * When "nextchars" is not NULL it points to a string with characters that
2019 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
2020 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002021 * Returns OK or FAIL;
2022 */
2023 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002024ex_let_vars(
2025 char_u *arg_start,
2026 typval_T *tv,
2027 int copy, /* copy values from "tv", don't move */
2028 int semicolon, /* from skip_var_list() */
2029 int var_count, /* from skip_var_list() */
2030 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002031{
2032 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00002033 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002034 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00002035 listitem_T *item;
2036 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002037
2038 if (*arg != '[')
2039 {
2040 /*
2041 * ":let var = expr" or ":for var in list"
2042 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002043 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002044 return FAIL;
2045 return OK;
2046 }
2047
2048 /*
2049 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2050 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002051 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002052 {
2053 EMSG(_(e_listreq));
2054 return FAIL;
2055 }
2056
2057 i = list_len(l);
2058 if (semicolon == 0 && var_count < i)
2059 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002060 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002061 return FAIL;
2062 }
2063 if (var_count - semicolon > i)
2064 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002065 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002066 return FAIL;
2067 }
2068
2069 item = l->lv_first;
2070 while (*arg != ']')
2071 {
2072 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002073 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002074 item = item->li_next;
2075 if (arg == NULL)
2076 return FAIL;
2077
2078 arg = skipwhite(arg);
2079 if (*arg == ';')
2080 {
2081 /* Put the rest of the list (may be empty) in the var after ';'.
2082 * Create a new list for this. */
2083 l = list_alloc();
2084 if (l == NULL)
2085 return FAIL;
2086 while (item != NULL)
2087 {
2088 list_append_tv(l, &item->li_tv);
2089 item = item->li_next;
2090 }
2091
2092 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002093 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002094 ltv.vval.v_list = l;
2095 l->lv_refcount = 1;
2096
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002097 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2098 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002099 clear_tv(&ltv);
2100 if (arg == NULL)
2101 return FAIL;
2102 break;
2103 }
2104 else if (*arg != ',' && *arg != ']')
2105 {
2106 EMSG2(_(e_intern2), "ex_let_vars()");
2107 return FAIL;
2108 }
2109 }
2110
2111 return OK;
2112}
2113
2114/*
2115 * Skip over assignable variable "var" or list of variables "[var, var]".
2116 * Used for ":let varvar = expr" and ":for varvar in expr".
2117 * For "[var, var]" increment "*var_count" for each variable.
2118 * for "[var, var; var]" set "semicolon".
2119 * Return NULL for an error.
2120 */
2121 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002122skip_var_list(
2123 char_u *arg,
2124 int *var_count,
2125 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002126{
2127 char_u *p, *s;
2128
2129 if (*arg == '[')
2130 {
2131 /* "[var, var]": find the matching ']'. */
2132 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002133 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002134 {
2135 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2136 s = skip_var_one(p);
2137 if (s == p)
2138 {
2139 EMSG2(_(e_invarg2), p);
2140 return NULL;
2141 }
2142 ++*var_count;
2143
2144 p = skipwhite(s);
2145 if (*p == ']')
2146 break;
2147 else if (*p == ';')
2148 {
2149 if (*semicolon == 1)
2150 {
2151 EMSG(_("Double ; in list of variables"));
2152 return NULL;
2153 }
2154 *semicolon = 1;
2155 }
2156 else if (*p != ',')
2157 {
2158 EMSG2(_(e_invarg2), p);
2159 return NULL;
2160 }
2161 }
2162 return p + 1;
2163 }
2164 else
2165 return skip_var_one(arg);
2166}
2167
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002168/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002169 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002170 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002171 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002172 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002173skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002174{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002175 if (*arg == '@' && arg[1] != NUL)
2176 return arg + 2;
2177 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2178 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002179}
2180
Bram Moolenaara7043832005-01-21 11:56:39 +00002181/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002182 * List variables for hashtab "ht" with prefix "prefix".
2183 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002184 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002186list_hashtable_vars(
2187 hashtab_T *ht,
2188 char_u *prefix,
2189 int empty,
2190 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002191{
Bram Moolenaar33570922005-01-25 22:26:29 +00002192 hashitem_T *hi;
2193 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002194 int todo;
2195
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002196 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002197 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2198 {
2199 if (!HASHITEM_EMPTY(hi))
2200 {
2201 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002202 di = HI2DI(hi);
2203 if (empty || di->di_tv.v_type != VAR_STRING
2204 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002205 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002206 }
2207 }
2208}
2209
2210/*
2211 * List global variables.
2212 */
2213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002214list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002215{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002216 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002217}
2218
2219/*
2220 * List buffer variables.
2221 */
2222 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002223list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002224{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002225 char_u numbuf[NUMBUFLEN];
2226
Bram Moolenaar429fa852013-04-15 12:27:36 +02002227 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002228 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002229
2230 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002231 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2232 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002233}
2234
2235/*
2236 * List window variables.
2237 */
2238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002239list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002240{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002241 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002242 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002243}
2244
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002245#ifdef FEAT_WINDOWS
2246/*
2247 * List tab page variables.
2248 */
2249 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002250list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002251{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002252 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002253 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002254}
2255#endif
2256
Bram Moolenaara7043832005-01-21 11:56:39 +00002257/*
2258 * List Vim variables.
2259 */
2260 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002261list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002262{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002263 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002264}
2265
2266/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002267 * List script-local variables, if there is a script.
2268 */
2269 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002270list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002271{
2272 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002273 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2274 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002275}
2276
2277/*
2278 * List function variables, if there is a function.
2279 */
2280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002281list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002282{
2283 if (current_funccal != NULL)
2284 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002285 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002286}
2287
2288/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002289 * List variables in "arg".
2290 */
2291 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002292list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002293{
2294 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002295 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002296 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002297 char_u *name_start;
2298 char_u *arg_subsc;
2299 char_u *tofree;
2300 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002301
2302 while (!ends_excmd(*arg) && !got_int)
2303 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002304 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002305 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002306 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002307 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2308 {
2309 emsg_severe = TRUE;
2310 EMSG(_(e_trailing));
2311 break;
2312 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002313 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002314 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002316 /* get_name_len() takes care of expanding curly braces */
2317 name_start = name = arg;
2318 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2319 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002320 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002321 /* This is mainly to keep test 49 working: when expanding
2322 * curly braces fails overrule the exception error message. */
2323 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002324 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002325 emsg_severe = TRUE;
2326 EMSG2(_(e_invarg2), arg);
2327 break;
2328 }
2329 error = TRUE;
2330 }
2331 else
2332 {
2333 if (tofree != NULL)
2334 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002335 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002336 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002337 else
2338 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002339 /* handle d.key, l[idx], f(expr) */
2340 arg_subsc = arg;
2341 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002342 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002343 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002344 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002345 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002346 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002347 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002348 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002349 case 'g': list_glob_vars(first); break;
2350 case 'b': list_buf_vars(first); break;
2351 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002352#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002353 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002354#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002355 case 'v': list_vim_vars(first); break;
2356 case 's': list_script_vars(first); break;
2357 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002358 default:
2359 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002360 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002361 }
2362 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002363 {
2364 char_u numbuf[NUMBUFLEN];
2365 char_u *tf;
2366 int c;
2367 char_u *s;
2368
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002369 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002370 c = *arg;
2371 *arg = NUL;
2372 list_one_var_a((char_u *)"",
2373 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002374 tv.v_type,
2375 s == NULL ? (char_u *)"" : s,
2376 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002377 *arg = c;
2378 vim_free(tf);
2379 }
2380 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002381 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002382 }
2383 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002384
2385 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002386 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002387
2388 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002389 }
2390
2391 return arg;
2392}
2393
2394/*
2395 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2396 * Returns a pointer to the char just after the var name.
2397 * Returns NULL if there is an error.
2398 */
2399 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002400ex_let_one(
2401 char_u *arg, /* points to variable name */
2402 typval_T *tv, /* value to assign to variable */
2403 int copy, /* copy value from "tv" */
2404 char_u *endchars, /* valid chars after variable name or NULL */
2405 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002406{
2407 int c1;
2408 char_u *name;
2409 char_u *p;
2410 char_u *arg_end = NULL;
2411 int len;
2412 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002413 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002414
2415 /*
2416 * ":let $VAR = expr": Set environment variable.
2417 */
2418 if (*arg == '$')
2419 {
2420 /* Find the end of the name. */
2421 ++arg;
2422 name = arg;
2423 len = get_env_len(&arg);
2424 if (len == 0)
2425 EMSG2(_(e_invarg2), name - 1);
2426 else
2427 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002428 if (op != NULL && (*op == '+' || *op == '-'))
2429 EMSG2(_(e_letwrong), op);
2430 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002431 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002432 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002433 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002434 {
2435 c1 = name[len];
2436 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002437 p = get_tv_string_chk(tv);
2438 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002439 {
2440 int mustfree = FALSE;
2441 char_u *s = vim_getenv(name, &mustfree);
2442
2443 if (s != NULL)
2444 {
2445 p = tofree = concat_str(s, p);
2446 if (mustfree)
2447 vim_free(s);
2448 }
2449 }
2450 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002451 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002452 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002453 if (STRICMP(name, "HOME") == 0)
2454 init_homedir();
2455 else if (didset_vim && STRICMP(name, "VIM") == 0)
2456 didset_vim = FALSE;
2457 else if (didset_vimruntime
2458 && STRICMP(name, "VIMRUNTIME") == 0)
2459 didset_vimruntime = FALSE;
2460 arg_end = arg;
2461 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002463 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002464 }
2465 }
2466 }
2467
2468 /*
2469 * ":let &option = expr": Set option value.
2470 * ":let &l:option = expr": Set local option value.
2471 * ":let &g:option = expr": Set global option value.
2472 */
2473 else if (*arg == '&')
2474 {
2475 /* Find the end of the name. */
2476 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002477 if (p == NULL || (endchars != NULL
2478 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002479 EMSG(_(e_letunexp));
2480 else
2481 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002482 long n;
2483 int opt_type;
2484 long numval;
2485 char_u *stringval = NULL;
2486 char_u *s;
2487
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002488 c1 = *p;
2489 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002490
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002491 n = (long)get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002492 s = get_tv_string_chk(tv); /* != NULL if number or string */
2493 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002494 {
2495 opt_type = get_option_value(arg, &numval,
2496 &stringval, opt_flags);
2497 if ((opt_type == 1 && *op == '.')
2498 || (opt_type == 0 && *op != '.'))
2499 EMSG2(_(e_letwrong), op);
2500 else
2501 {
2502 if (opt_type == 1) /* number */
2503 {
2504 if (*op == '+')
2505 n = numval + n;
2506 else
2507 n = numval - n;
2508 }
2509 else if (opt_type == 0 && stringval != NULL) /* string */
2510 {
2511 s = concat_str(stringval, s);
2512 vim_free(stringval);
2513 stringval = s;
2514 }
2515 }
2516 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002517 if (s != NULL)
2518 {
2519 set_option_value(arg, n, s, opt_flags);
2520 arg_end = p;
2521 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002522 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002523 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002524 }
2525 }
2526
2527 /*
2528 * ":let @r = expr": Set register contents.
2529 */
2530 else if (*arg == '@')
2531 {
2532 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002533 if (op != NULL && (*op == '+' || *op == '-'))
2534 EMSG2(_(e_letwrong), op);
2535 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002536 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002537 EMSG(_(e_letunexp));
2538 else
2539 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002540 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002541 char_u *s;
2542
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002543 p = get_tv_string_chk(tv);
2544 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002545 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002546 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002547 if (s != NULL)
2548 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002549 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002550 vim_free(s);
2551 }
2552 }
2553 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002554 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002555 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002556 arg_end = arg + 1;
2557 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002558 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002559 }
2560 }
2561
2562 /*
2563 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002565 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002566 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002567 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002568 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002569
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002570 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002571 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002572 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2574 EMSG(_(e_letunexp));
2575 else
2576 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002577 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578 arg_end = p;
2579 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002580 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002581 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002582 }
2583
2584 else
2585 EMSG2(_(e_invarg2), arg);
2586
2587 return arg_end;
2588}
2589
2590/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002591 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2592 */
2593 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002594check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002595{
2596 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2597 {
2598 EMSG2(_(e_readonlyvar), arg);
2599 return TRUE;
2600 }
2601 return FALSE;
2602}
2603
2604/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 * Get an lval: variable, Dict item or List item that can be assigned a value
2606 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2607 * "name.key", "name.key[expr]" etc.
2608 * Indexing only works if "name" is an existing List or Dictionary.
2609 * "name" points to the start of the name.
2610 * If "rettv" is not NULL it points to the value to be assigned.
2611 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2612 * wrong; must end in space or cmd separator.
2613 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002614 * flags:
2615 * GLV_QUIET: do not give error messages
2616 * GLV_NO_AUTOLOAD: do not use script autoloading
2617 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002619 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002620 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002621 */
2622 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002623get_lval(
2624 char_u *name,
2625 typval_T *rettv,
2626 lval_T *lp,
2627 int unlet,
2628 int skip,
2629 int flags, /* GLV_ values */
2630 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002631{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002632 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002633 char_u *expr_start, *expr_end;
2634 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002635 dictitem_T *v;
2636 typval_T var1;
2637 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002639 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002640 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002641 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002642 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002643 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002644
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002646 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647
2648 if (skip)
2649 {
2650 /* When skipping just find the end of the name. */
2651 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002652 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 }
2654
2655 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002656 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 if (expr_start != NULL)
2658 {
2659 /* Don't expand the name when we already know there is an error. */
2660 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2661 && *p != '[' && *p != '.')
2662 {
2663 EMSG(_(e_trailing));
2664 return NULL;
2665 }
2666
2667 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2668 if (lp->ll_exp_name == NULL)
2669 {
2670 /* Report an invalid expression in braces, unless the
2671 * expression evaluation has been cancelled due to an
2672 * aborting error, an interrupt, or an exception. */
2673 if (!aborting() && !quiet)
2674 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002675 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002676 EMSG2(_(e_invarg2), name);
2677 return NULL;
2678 }
2679 }
2680 lp->ll_name = lp->ll_exp_name;
2681 }
2682 else
2683 lp->ll_name = name;
2684
2685 /* Without [idx] or .key we are done. */
2686 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2687 return p;
2688
2689 cc = *p;
2690 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002691 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 if (v == NULL && !quiet)
2693 EMSG2(_(e_undefvar), lp->ll_name);
2694 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002695 if (v == NULL)
2696 return NULL;
2697
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 /*
2699 * Loop until no more [idx] or .key is following.
2700 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002701 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002703 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2705 && !(lp->ll_tv->v_type == VAR_DICT
2706 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002707 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 if (!quiet)
2709 EMSG(_("E689: Can only index a List or Dictionary"));
2710 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002711 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002713 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 if (!quiet)
2715 EMSG(_("E708: [:] must come last"));
2716 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002717 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002718
Bram Moolenaar8c711452005-01-14 21:53:12 +00002719 len = -1;
2720 if (*p == '.')
2721 {
2722 key = p + 1;
2723 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2724 ;
2725 if (len == 0)
2726 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002727 if (!quiet)
2728 EMSG(_(e_emptykey));
2729 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 }
2731 p = key + len;
2732 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002733 else
2734 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002735 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002736 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002737 if (*p == ':')
2738 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002739 else
2740 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002741 empty1 = FALSE;
2742 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002744 if (get_tv_string_chk(&var1) == NULL)
2745 {
2746 /* not a number or string */
2747 clear_tv(&var1);
2748 return NULL;
2749 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002750 }
2751
2752 /* Optionally get the second index [ :expr]. */
2753 if (*p == ':')
2754 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002756 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002757 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002758 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002759 if (!empty1)
2760 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002762 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 if (rettv != NULL && (rettv->v_type != VAR_LIST
2764 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002765 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002766 if (!quiet)
2767 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002768 if (!empty1)
2769 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002770 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002771 }
2772 p = skipwhite(p + 1);
2773 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002774 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002775 else
2776 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002777 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002778 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2779 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002780 if (!empty1)
2781 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002784 if (get_tv_string_chk(&var2) == NULL)
2785 {
2786 /* not a number or string */
2787 if (!empty1)
2788 clear_tv(&var1);
2789 clear_tv(&var2);
2790 return NULL;
2791 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002792 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002793 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002794 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002795 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002797
Bram Moolenaar8c711452005-01-14 21:53:12 +00002798 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002799 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 if (!quiet)
2801 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002802 if (!empty1)
2803 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002804 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002805 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 }
2808
2809 /* Skip to past ']'. */
2810 ++p;
2811 }
2812
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002813 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002814 {
2815 if (len == -1)
2816 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002817 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002818 key = get_tv_string_chk(&var1); /* is number or string */
2819 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002820 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002821 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002823 }
2824 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002825 lp->ll_list = NULL;
2826 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002827 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002828
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002829 /* When assigning to a scope dictionary check that a function and
2830 * variable name is valid (only variable name unless it is l: or
2831 * g: dictionary). Disallow overwriting a builtin function. */
2832 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002833 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002834 int prevval;
2835 int wrong;
2836
2837 if (len != -1)
2838 {
2839 prevval = key[len];
2840 key[len] = NUL;
2841 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002842 else
2843 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002844 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2845 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002846 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002847 || !valid_varname(key);
2848 if (len != -1)
2849 key[len] = prevval;
2850 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002851 return NULL;
2852 }
2853
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002854 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002855 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002856 /* Can't add "v:" variable. */
2857 if (lp->ll_dict == &vimvardict)
2858 {
2859 EMSG2(_(e_illvar), name);
2860 return NULL;
2861 }
2862
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002863 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002864 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002865 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002867 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002868 if (len == -1)
2869 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002871 }
2872 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002873 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002874 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002876 if (len == -1)
2877 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002879 p = NULL;
2880 break;
2881 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002882 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002883 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002884 return NULL;
2885
Bram Moolenaar8c711452005-01-14 21:53:12 +00002886 if (len == -1)
2887 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002889 }
2890 else
2891 {
2892 /*
2893 * Get the number and item for the only or first index of the List.
2894 */
2895 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002897 else
2898 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002899 lp->ll_n1 = (long)get_tv_number(&var1);
2900 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002901 clear_tv(&var1);
2902 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002903 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 lp->ll_list = lp->ll_tv->vval.v_list;
2905 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2906 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002907 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002908 if (lp->ll_n1 < 0)
2909 {
2910 lp->ll_n1 = 0;
2911 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2912 }
2913 }
2914 if (lp->ll_li == NULL)
2915 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002916 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002917 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002918 if (!quiet)
2919 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002921 }
2922
2923 /*
2924 * May need to find the item or absolute index for the second
2925 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002926 * When no index given: "lp->ll_empty2" is TRUE.
2927 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002928 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002930 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002931 lp->ll_n2 = (long)get_tv_number(&var2);
2932 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002933 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002934 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002935 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002936 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002937 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002938 {
2939 if (!quiet)
2940 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002941 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002942 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002943 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002944 }
2945
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002946 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2947 if (lp->ll_n1 < 0)
2948 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2949 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002950 {
2951 if (!quiet)
2952 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002953 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002954 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002955 }
2956
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002957 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002958 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002959 }
2960
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002961 return p;
2962}
2963
2964/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002965 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002966 */
2967 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002968clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002969{
2970 vim_free(lp->ll_exp_name);
2971 vim_free(lp->ll_newkey);
2972}
2973
2974/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002975 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002976 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002977 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002978 */
2979 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002980set_var_lval(
2981 lval_T *lp,
2982 char_u *endp,
2983 typval_T *rettv,
2984 int copy,
2985 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002986{
2987 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002988 listitem_T *ri;
2989 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002990
2991 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002992 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002993 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002994 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002995 cc = *endp;
2996 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002997 if (op != NULL && *op != '=')
2998 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002999 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003000
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003001 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02003002 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003003 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02003004 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003005 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02003006 if ((di == NULL
3007 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
3008 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
3009 FALSE)))
3010 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003011 set_var(lp->ll_name, &tv, FALSE);
3012 clear_tv(&tv);
3013 }
3014 }
3015 else
3016 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003017 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003018 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003019 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003020 else if (tv_check_lock(lp->ll_newkey == NULL
3021 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02003022 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003023 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003024 else if (lp->ll_range)
3025 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003026 listitem_T *ll_li = lp->ll_li;
3027 int ll_n1 = lp->ll_n1;
3028
3029 /*
3030 * Check whether any of the list items is locked
3031 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01003032 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003033 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02003034 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003035 return;
3036 ri = ri->li_next;
3037 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
3038 break;
3039 ll_li = ll_li->li_next;
3040 ++ll_n1;
3041 }
3042
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003043 /*
3044 * Assign the List values to the list items.
3045 */
3046 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003047 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003048 if (op != NULL && *op != '=')
3049 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3050 else
3051 {
3052 clear_tv(&lp->ll_li->li_tv);
3053 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3054 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003055 ri = ri->li_next;
3056 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3057 break;
3058 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003059 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003060 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003061 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003062 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003063 ri = NULL;
3064 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003065 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003066 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003067 lp->ll_li = lp->ll_li->li_next;
3068 ++lp->ll_n1;
3069 }
3070 if (ri != NULL)
3071 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003072 else if (lp->ll_empty2
3073 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003074 : lp->ll_n1 != lp->ll_n2)
3075 EMSG(_("E711: List value has not enough items"));
3076 }
3077 else
3078 {
3079 /*
3080 * Assign to a List or Dictionary item.
3081 */
3082 if (lp->ll_newkey != NULL)
3083 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003084 if (op != NULL && *op != '=')
3085 {
3086 EMSG2(_(e_letwrong), op);
3087 return;
3088 }
3089
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003090 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003091 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003092 if (di == NULL)
3093 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003094 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3095 {
3096 vim_free(di);
3097 return;
3098 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003099 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003100 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003101 else if (op != NULL && *op != '=')
3102 {
3103 tv_op(lp->ll_tv, rettv, op);
3104 return;
3105 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003106 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003107 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003108
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003109 /*
3110 * Assign the value to the variable or list item.
3111 */
3112 if (copy)
3113 copy_tv(rettv, lp->ll_tv);
3114 else
3115 {
3116 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003117 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003118 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003119 }
3120 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003121}
3122
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003123/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003124 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3125 * Returns OK or FAIL.
3126 */
3127 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003128tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003129{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003130 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003131 char_u numbuf[NUMBUFLEN];
3132 char_u *s;
3133
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003134 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3135 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3136 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003137 {
3138 switch (tv1->v_type)
3139 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003140 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003141 case VAR_DICT:
3142 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003143 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003144 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003145 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003146 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003147 break;
3148
3149 case VAR_LIST:
3150 if (*op != '+' || tv2->v_type != VAR_LIST)
3151 break;
3152 /* List += List */
3153 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3154 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3155 return OK;
3156
3157 case VAR_NUMBER:
3158 case VAR_STRING:
3159 if (tv2->v_type == VAR_LIST)
3160 break;
3161 if (*op == '+' || *op == '-')
3162 {
3163 /* nr += nr or nr -= nr*/
3164 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003165#ifdef FEAT_FLOAT
3166 if (tv2->v_type == VAR_FLOAT)
3167 {
3168 float_T f = n;
3169
3170 if (*op == '+')
3171 f += tv2->vval.v_float;
3172 else
3173 f -= tv2->vval.v_float;
3174 clear_tv(tv1);
3175 tv1->v_type = VAR_FLOAT;
3176 tv1->vval.v_float = f;
3177 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003178 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003179#endif
3180 {
3181 if (*op == '+')
3182 n += get_tv_number(tv2);
3183 else
3184 n -= get_tv_number(tv2);
3185 clear_tv(tv1);
3186 tv1->v_type = VAR_NUMBER;
3187 tv1->vval.v_number = n;
3188 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003189 }
3190 else
3191 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003192 if (tv2->v_type == VAR_FLOAT)
3193 break;
3194
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003195 /* str .= str */
3196 s = get_tv_string(tv1);
3197 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3198 clear_tv(tv1);
3199 tv1->v_type = VAR_STRING;
3200 tv1->vval.v_string = s;
3201 }
3202 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003203
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003204 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003205#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003206 {
3207 float_T f;
3208
3209 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3210 && tv2->v_type != VAR_NUMBER
3211 && tv2->v_type != VAR_STRING))
3212 break;
3213 if (tv2->v_type == VAR_FLOAT)
3214 f = tv2->vval.v_float;
3215 else
3216 f = get_tv_number(tv2);
3217 if (*op == '+')
3218 tv1->vval.v_float += f;
3219 else
3220 tv1->vval.v_float -= f;
3221 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003222#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003223 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003224 }
3225 }
3226
3227 EMSG2(_(e_letwrong), op);
3228 return FAIL;
3229}
3230
3231/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003232 * Add a watcher to a list.
3233 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003234 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003235list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003236{
3237 lw->lw_next = l->lv_watch;
3238 l->lv_watch = lw;
3239}
3240
3241/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003242 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003243 * No warning when it isn't found...
3244 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003245 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003246list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003247{
Bram Moolenaar33570922005-01-25 22:26:29 +00003248 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003249
3250 lwp = &l->lv_watch;
3251 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3252 {
3253 if (lw == lwrem)
3254 {
3255 *lwp = lw->lw_next;
3256 break;
3257 }
3258 lwp = &lw->lw_next;
3259 }
3260}
3261
3262/*
3263 * Just before removing an item from a list: advance watchers to the next
3264 * item.
3265 */
3266 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003267list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003268{
Bram Moolenaar33570922005-01-25 22:26:29 +00003269 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003270
3271 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3272 if (lw->lw_item == item)
3273 lw->lw_item = item->li_next;
3274}
3275
3276/*
3277 * Evaluate the expression used in a ":for var in expr" command.
3278 * "arg" points to "var".
3279 * Set "*errp" to TRUE for an error, FALSE otherwise;
3280 * Return a pointer that holds the info. Null when there is an error.
3281 */
3282 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003283eval_for_line(
3284 char_u *arg,
3285 int *errp,
3286 char_u **nextcmdp,
3287 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003288{
Bram Moolenaar33570922005-01-25 22:26:29 +00003289 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003290 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003291 typval_T tv;
3292 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003293
3294 *errp = TRUE; /* default: there is an error */
3295
Bram Moolenaar33570922005-01-25 22:26:29 +00003296 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003297 if (fi == NULL)
3298 return NULL;
3299
3300 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3301 if (expr == NULL)
3302 return fi;
3303
3304 expr = skipwhite(expr);
3305 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3306 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003307 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003308 return fi;
3309 }
3310
3311 if (skip)
3312 ++emsg_skip;
3313 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3314 {
3315 *errp = FALSE;
3316 if (!skip)
3317 {
3318 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003319 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003320 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003321 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003322 clear_tv(&tv);
3323 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003324 else if (l == NULL)
3325 {
3326 /* a null list is like an empty list: do nothing */
3327 clear_tv(&tv);
3328 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003329 else
3330 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003331 /* No need to increment the refcount, it's already set for the
3332 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003333 fi->fi_list = l;
3334 list_add_watch(l, &fi->fi_lw);
3335 fi->fi_lw.lw_item = l->lv_first;
3336 }
3337 }
3338 }
3339 if (skip)
3340 --emsg_skip;
3341
3342 return fi;
3343}
3344
3345/*
3346 * Use the first item in a ":for" list. Advance to the next.
3347 * Assign the values to the variable (list). "arg" points to the first one.
3348 * Return TRUE when a valid item was found, FALSE when at end of list or
3349 * something wrong.
3350 */
3351 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003352next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003353{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003354 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003355 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003356 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003357
3358 item = fi->fi_lw.lw_item;
3359 if (item == NULL)
3360 result = FALSE;
3361 else
3362 {
3363 fi->fi_lw.lw_item = item->li_next;
3364 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3365 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3366 }
3367 return result;
3368}
3369
3370/*
3371 * Free the structure used to store info used by ":for".
3372 */
3373 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003374free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003375{
Bram Moolenaar33570922005-01-25 22:26:29 +00003376 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003377
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003378 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003379 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003380 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003381 list_unref(fi->fi_list);
3382 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003383 vim_free(fi);
3384}
3385
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3387
3388 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003389set_context_for_expression(
3390 expand_T *xp,
3391 char_u *arg,
3392 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393{
3394 int got_eq = FALSE;
3395 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003396 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003398 if (cmdidx == CMD_let)
3399 {
3400 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003401 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003402 {
3403 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003404 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003405 {
3406 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003407 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003408 if (vim_iswhite(*p))
3409 break;
3410 }
3411 return;
3412 }
3413 }
3414 else
3415 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3416 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 while ((xp->xp_pattern = vim_strpbrk(arg,
3418 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3419 {
3420 c = *xp->xp_pattern;
3421 if (c == '&')
3422 {
3423 c = xp->xp_pattern[1];
3424 if (c == '&')
3425 {
3426 ++xp->xp_pattern;
3427 xp->xp_context = cmdidx != CMD_let || got_eq
3428 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3429 }
3430 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003431 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003433 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3434 xp->xp_pattern += 2;
3435
3436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 }
3438 else if (c == '$')
3439 {
3440 /* environment variable */
3441 xp->xp_context = EXPAND_ENV_VARS;
3442 }
3443 else if (c == '=')
3444 {
3445 got_eq = TRUE;
3446 xp->xp_context = EXPAND_EXPRESSION;
3447 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003448 else if (c == '#'
3449 && xp->xp_context == EXPAND_EXPRESSION)
3450 {
3451 /* Autoload function/variable contains '#'. */
3452 break;
3453 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003454 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 && xp->xp_context == EXPAND_FUNCTIONS
3456 && vim_strchr(xp->xp_pattern, '(') == NULL)
3457 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003458 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 break;
3460 }
3461 else if (cmdidx != CMD_let || got_eq)
3462 {
3463 if (c == '"') /* string */
3464 {
3465 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3466 if (c == '\\' && xp->xp_pattern[1] != NUL)
3467 ++xp->xp_pattern;
3468 xp->xp_context = EXPAND_NOTHING;
3469 }
3470 else if (c == '\'') /* literal string */
3471 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003472 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3474 /* skip */ ;
3475 xp->xp_context = EXPAND_NOTHING;
3476 }
3477 else if (c == '|')
3478 {
3479 if (xp->xp_pattern[1] == '|')
3480 {
3481 ++xp->xp_pattern;
3482 xp->xp_context = EXPAND_EXPRESSION;
3483 }
3484 else
3485 xp->xp_context = EXPAND_COMMANDS;
3486 }
3487 else
3488 xp->xp_context = EXPAND_EXPRESSION;
3489 }
3490 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003491 /* Doesn't look like something valid, expand as an expression
3492 * anyway. */
3493 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 arg = xp->xp_pattern;
3495 if (*arg != NUL)
3496 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3497 /* skip */ ;
3498 }
3499 xp->xp_pattern = arg;
3500}
3501
3502#endif /* FEAT_CMDL_COMPL */
3503
3504/*
3505 * ":1,25call func(arg1, arg2)" function call.
3506 */
3507 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003508ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509{
3510 char_u *arg = eap->arg;
3511 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003513 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003515 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 linenr_T lnum;
3517 int doesrange;
3518 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003519 funcdict_T fudi;
Bram Moolenaar9e63f612016-03-17 23:13:28 +01003520 partial_T *partial = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003522 if (eap->skip)
3523 {
3524 /* trans_function_name() doesn't work well when skipping, use eval0()
3525 * instead to skip to any following command, e.g. for:
3526 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003527 ++emsg_skip;
3528 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3529 clear_tv(&rettv);
3530 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003531 return;
3532 }
3533
Bram Moolenaar65639032016-03-16 21:40:30 +01003534 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003535 if (fudi.fd_newkey != NULL)
3536 {
3537 /* Still need to give an error message for missing key. */
3538 EMSG2(_(e_dictkey), fudi.fd_newkey);
3539 vim_free(fudi.fd_newkey);
3540 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003541 if (tofree == NULL)
3542 return;
3543
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003544 /* Increase refcount on dictionary, it could get deleted when evaluating
3545 * the arguments. */
3546 if (fudi.fd_dict != NULL)
3547 ++fudi.fd_dict->dv_refcount;
3548
Bram Moolenaar65639032016-03-16 21:40:30 +01003549 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3550 * contents. For VAR_PARTIAL get its partial, unless we already have one
3551 * from trans_function_name(). */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003552 len = (int)STRLEN(tofree);
Bram Moolenaar65639032016-03-16 21:40:30 +01003553 name = deref_func_name(tofree, &len,
3554 partial != NULL ? NULL : &partial, FALSE);
3555
Bram Moolenaar532c7802005-01-27 14:44:31 +00003556 /* Skip white space to allow ":call func ()". Not good, but required for
3557 * backward compatibility. */
3558 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003559 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560
3561 if (*startarg != '(')
3562 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003563 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 goto end;
3565 }
3566
3567 /*
3568 * When skipping, evaluate the function once, to find the end of the
3569 * arguments.
3570 * When the function takes a range, this is discovered after the first
3571 * call, and the loop is broken.
3572 */
3573 if (eap->skip)
3574 {
3575 ++emsg_skip;
3576 lnum = eap->line2; /* do it once, also with an invalid range */
3577 }
3578 else
3579 lnum = eap->line1;
3580 for ( ; lnum <= eap->line2; ++lnum)
3581 {
3582 if (!eap->skip && eap->addr_count > 0)
3583 {
3584 curwin->w_cursor.lnum = lnum;
3585 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003586#ifdef FEAT_VIRTUALEDIT
3587 curwin->w_cursor.coladd = 0;
3588#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 }
3590 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003591 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003592 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003593 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 {
3595 failed = TRUE;
3596 break;
3597 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003598
3599 /* Handle a function returning a Funcref, Dictionary or List. */
3600 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3601 {
3602 failed = TRUE;
3603 break;
3604 }
3605
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003606 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 if (doesrange || eap->skip)
3608 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003609
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003611 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003612 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003613 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 if (aborting())
3615 break;
3616 }
3617 if (eap->skip)
3618 --emsg_skip;
3619
3620 if (!failed)
3621 {
3622 /* Check for trailing illegal characters and a following command. */
3623 if (!ends_excmd(*arg))
3624 {
3625 emsg_severe = TRUE;
3626 EMSG(_(e_trailing));
3627 }
3628 else
3629 eap->nextcmd = check_nextcmd(arg);
3630 }
3631
3632end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003633 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003634 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635}
3636
3637/*
3638 * ":unlet[!] var1 ... " command.
3639 */
3640 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003641ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003643 ex_unletlock(eap, eap->arg, 0);
3644}
3645
3646/*
3647 * ":lockvar" and ":unlockvar" commands
3648 */
3649 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003650ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003651{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003653 int deep = 2;
3654
3655 if (eap->forceit)
3656 deep = -1;
3657 else if (vim_isdigit(*arg))
3658 {
3659 deep = getdigits(&arg);
3660 arg = skipwhite(arg);
3661 }
3662
3663 ex_unletlock(eap, arg, deep);
3664}
3665
3666/*
3667 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3668 */
3669 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003670ex_unletlock(
3671 exarg_T *eap,
3672 char_u *argstart,
3673 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003674{
3675 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003678 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679
3680 do
3681 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003682 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003683 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003684 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003685 if (lv.ll_name == NULL)
3686 error = TRUE; /* error but continue parsing */
3687 if (name_end == NULL || (!vim_iswhite(*name_end)
3688 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003690 if (name_end != NULL)
3691 {
3692 emsg_severe = TRUE;
3693 EMSG(_(e_trailing));
3694 }
3695 if (!(eap->skip || error))
3696 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 break;
3698 }
3699
3700 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003701 {
3702 if (eap->cmdidx == CMD_unlet)
3703 {
3704 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3705 error = TRUE;
3706 }
3707 else
3708 {
3709 if (do_lock_var(&lv, name_end, deep,
3710 eap->cmdidx == CMD_lockvar) == FAIL)
3711 error = TRUE;
3712 }
3713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003715 if (!eap->skip)
3716 clear_lval(&lv);
3717
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 arg = skipwhite(name_end);
3719 } while (!ends_excmd(*arg));
3720
3721 eap->nextcmd = check_nextcmd(arg);
3722}
3723
Bram Moolenaar8c711452005-01-14 21:53:12 +00003724 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003725do_unlet_var(
3726 lval_T *lp,
3727 char_u *name_end,
3728 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003729{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003730 int ret = OK;
3731 int cc;
3732
3733 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003734 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003735 cc = *name_end;
3736 *name_end = NUL;
3737
3738 /* Normal name or expanded name. */
3739 if (check_changedtick(lp->ll_name))
3740 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003741 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003742 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003743 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003744 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003745 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003746 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003747 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003748 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003749 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003750 else if (lp->ll_range)
3751 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003752 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003753 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003754 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003755
3756 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3757 {
3758 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003759 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003760 return FAIL;
3761 ll_li = li;
3762 ++ll_n1;
3763 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003764
3765 /* Delete a range of List items. */
3766 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3767 {
3768 li = lp->ll_li->li_next;
3769 listitem_remove(lp->ll_list, lp->ll_li);
3770 lp->ll_li = li;
3771 ++lp->ll_n1;
3772 }
3773 }
3774 else
3775 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003776 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003777 /* unlet a List item. */
3778 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003779 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003780 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003781 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003782 }
3783
3784 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003785}
3786
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787/*
3788 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003789 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 */
3791 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003792do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793{
Bram Moolenaar33570922005-01-25 22:26:29 +00003794 hashtab_T *ht;
3795 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003796 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003797 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003798 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799
Bram Moolenaar33570922005-01-25 22:26:29 +00003800 ht = find_var_ht(name, &varname);
3801 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003803 if (ht == &globvarht)
3804 d = &globvardict;
3805 else if (current_funccal != NULL
3806 && ht == &current_funccal->l_vars.dv_hashtab)
3807 d = &current_funccal->l_vars;
3808 else if (ht == &compat_hashtab)
3809 d = &vimvardict;
3810 else
3811 {
3812 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3813 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3814 }
3815 if (d == NULL)
3816 {
3817 EMSG2(_(e_intern2), "do_unlet()");
3818 return FAIL;
3819 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003820 hi = hash_find(ht, varname);
3821 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003822 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003823 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003824 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003825 || var_check_ro(di->di_flags, name, FALSE)
3826 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003827 return FAIL;
3828
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003829 delete_var(ht, hi);
3830 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003833 if (forceit)
3834 return OK;
3835 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 return FAIL;
3837}
3838
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003839/*
3840 * Lock or unlock variable indicated by "lp".
3841 * "deep" is the levels to go (-1 for unlimited);
3842 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3843 */
3844 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003845do_lock_var(
3846 lval_T *lp,
3847 char_u *name_end,
3848 int deep,
3849 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003850{
3851 int ret = OK;
3852 int cc;
3853 dictitem_T *di;
3854
3855 if (deep == 0) /* nothing to do */
3856 return OK;
3857
3858 if (lp->ll_tv == NULL)
3859 {
3860 cc = *name_end;
3861 *name_end = NUL;
3862
3863 /* Normal name or expanded name. */
3864 if (check_changedtick(lp->ll_name))
3865 ret = FAIL;
3866 else
3867 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003868 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003869 if (di == NULL)
3870 ret = FAIL;
3871 else
3872 {
3873 if (lock)
3874 di->di_flags |= DI_FLAGS_LOCK;
3875 else
3876 di->di_flags &= ~DI_FLAGS_LOCK;
3877 item_lock(&di->di_tv, deep, lock);
3878 }
3879 }
3880 *name_end = cc;
3881 }
3882 else if (lp->ll_range)
3883 {
3884 listitem_T *li = lp->ll_li;
3885
3886 /* (un)lock a range of List items. */
3887 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3888 {
3889 item_lock(&li->li_tv, deep, lock);
3890 li = li->li_next;
3891 ++lp->ll_n1;
3892 }
3893 }
3894 else if (lp->ll_list != NULL)
3895 /* (un)lock a List item. */
3896 item_lock(&lp->ll_li->li_tv, deep, lock);
3897 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003898 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003899 item_lock(&lp->ll_di->di_tv, deep, lock);
3900
3901 return ret;
3902}
3903
3904/*
3905 * Lock or unlock an item. "deep" is nr of levels to go.
3906 */
3907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003908item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003909{
3910 static int recurse = 0;
3911 list_T *l;
3912 listitem_T *li;
3913 dict_T *d;
3914 hashitem_T *hi;
3915 int todo;
3916
3917 if (recurse >= DICT_MAXNEST)
3918 {
3919 EMSG(_("E743: variable nested too deep for (un)lock"));
3920 return;
3921 }
3922 if (deep == 0)
3923 return;
3924 ++recurse;
3925
3926 /* lock/unlock the item itself */
3927 if (lock)
3928 tv->v_lock |= VAR_LOCKED;
3929 else
3930 tv->v_lock &= ~VAR_LOCKED;
3931
3932 switch (tv->v_type)
3933 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003934 case VAR_UNKNOWN:
3935 case VAR_NUMBER:
3936 case VAR_STRING:
3937 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003938 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003939 case VAR_FLOAT:
3940 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003941 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003942 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003943 break;
3944
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003945 case VAR_LIST:
3946 if ((l = tv->vval.v_list) != NULL)
3947 {
3948 if (lock)
3949 l->lv_lock |= VAR_LOCKED;
3950 else
3951 l->lv_lock &= ~VAR_LOCKED;
3952 if (deep < 0 || deep > 1)
3953 /* recursive: lock/unlock the items the List contains */
3954 for (li = l->lv_first; li != NULL; li = li->li_next)
3955 item_lock(&li->li_tv, deep - 1, lock);
3956 }
3957 break;
3958 case VAR_DICT:
3959 if ((d = tv->vval.v_dict) != NULL)
3960 {
3961 if (lock)
3962 d->dv_lock |= VAR_LOCKED;
3963 else
3964 d->dv_lock &= ~VAR_LOCKED;
3965 if (deep < 0 || deep > 1)
3966 {
3967 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003968 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003969 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3970 {
3971 if (!HASHITEM_EMPTY(hi))
3972 {
3973 --todo;
3974 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3975 }
3976 }
3977 }
3978 }
3979 }
3980 --recurse;
3981}
3982
Bram Moolenaara40058a2005-07-11 22:42:07 +00003983/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003984 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3985 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003986 */
3987 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003988tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003989{
3990 return (tv->v_lock & VAR_LOCKED)
3991 || (tv->v_type == VAR_LIST
3992 && tv->vval.v_list != NULL
3993 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3994 || (tv->v_type == VAR_DICT
3995 && tv->vval.v_dict != NULL
3996 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3997}
3998
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
4000/*
4001 * Delete all "menutrans_" variables.
4002 */
4003 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004004del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005{
Bram Moolenaar33570922005-01-25 22:26:29 +00004006 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004007 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008
Bram Moolenaar33570922005-01-25 22:26:29 +00004009 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004010 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00004011 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00004012 {
4013 if (!HASHITEM_EMPTY(hi))
4014 {
4015 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00004016 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
4017 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00004018 }
4019 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004020 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021}
4022#endif
4023
4024#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4025
4026/*
4027 * Local string buffer for the next two functions to store a variable name
4028 * with its prefix. Allocated in cat_prefix_varname(), freed later in
4029 * get_user_var_name().
4030 */
4031
Bram Moolenaar48e697e2016-01-23 22:17:30 +01004032static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033
4034static char_u *varnamebuf = NULL;
4035static int varnamebuflen = 0;
4036
4037/*
4038 * Function to concatenate a prefix and a variable name.
4039 */
4040 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004041cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042{
4043 int len;
4044
4045 len = (int)STRLEN(name) + 3;
4046 if (len > varnamebuflen)
4047 {
4048 vim_free(varnamebuf);
4049 len += 10; /* some additional space */
4050 varnamebuf = alloc(len);
4051 if (varnamebuf == NULL)
4052 {
4053 varnamebuflen = 0;
4054 return NULL;
4055 }
4056 varnamebuflen = len;
4057 }
4058 *varnamebuf = prefix;
4059 varnamebuf[1] = ':';
4060 STRCPY(varnamebuf + 2, name);
4061 return varnamebuf;
4062}
4063
4064/*
4065 * Function given to ExpandGeneric() to obtain the list of user defined
4066 * (global/buffer/window/built-in) variable names.
4067 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004069get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004071 static long_u gdone;
4072 static long_u bdone;
4073 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004074#ifdef FEAT_WINDOWS
4075 static long_u tdone;
4076#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004077 static int vidx;
4078 static hashitem_T *hi;
4079 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080
4081 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004082 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004083 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004084#ifdef FEAT_WINDOWS
4085 tdone = 0;
4086#endif
4087 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004088
4089 /* Global variables */
4090 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004092 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004093 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004094 else
4095 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004096 while (HASHITEM_EMPTY(hi))
4097 ++hi;
4098 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4099 return cat_prefix_varname('g', hi->hi_key);
4100 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004102
4103 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004104 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004105 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004107 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004108 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004109 else
4110 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004111 while (HASHITEM_EMPTY(hi))
4112 ++hi;
4113 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004115 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004117 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 return (char_u *)"b:changedtick";
4119 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004120
4121 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004122 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004123 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004125 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004126 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004127 else
4128 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004129 while (HASHITEM_EMPTY(hi))
4130 ++hi;
4131 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004133
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004134#ifdef FEAT_WINDOWS
4135 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004136 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004137 if (tdone < ht->ht_used)
4138 {
4139 if (tdone++ == 0)
4140 hi = ht->ht_array;
4141 else
4142 ++hi;
4143 while (HASHITEM_EMPTY(hi))
4144 ++hi;
4145 return cat_prefix_varname('t', hi->hi_key);
4146 }
4147#endif
4148
Bram Moolenaar33570922005-01-25 22:26:29 +00004149 /* v: variables */
4150 if (vidx < VV_LEN)
4151 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152
4153 vim_free(varnamebuf);
4154 varnamebuf = NULL;
4155 varnamebuflen = 0;
4156 return NULL;
4157}
4158
4159#endif /* FEAT_CMDL_COMPL */
4160
4161/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004162 * Return TRUE if "pat" matches "text".
4163 * Does not use 'cpo' and always uses 'magic'.
4164 */
4165 static int
4166pattern_match(char_u *pat, char_u *text, int ic)
4167{
4168 int matches = FALSE;
4169 char_u *save_cpo;
4170 regmatch_T regmatch;
4171
4172 /* avoid 'l' flag in 'cpoptions' */
4173 save_cpo = p_cpo;
4174 p_cpo = (char_u *)"";
4175 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
4176 if (regmatch.regprog != NULL)
4177 {
4178 regmatch.rm_ic = ic;
4179 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
4180 vim_regfree(regmatch.regprog);
4181 }
4182 p_cpo = save_cpo;
4183 return matches;
4184}
4185
4186/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 * types for expressions.
4188 */
4189typedef enum
4190{
4191 TYPE_UNKNOWN = 0
4192 , TYPE_EQUAL /* == */
4193 , TYPE_NEQUAL /* != */
4194 , TYPE_GREATER /* > */
4195 , TYPE_GEQUAL /* >= */
4196 , TYPE_SMALLER /* < */
4197 , TYPE_SEQUAL /* <= */
4198 , TYPE_MATCH /* =~ */
4199 , TYPE_NOMATCH /* !~ */
4200} exptype_T;
4201
4202/*
4203 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004204 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4206 */
4207
4208/*
4209 * Handle zero level expression.
4210 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004211 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004212 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 * Return OK or FAIL.
4214 */
4215 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004216eval0(
4217 char_u *arg,
4218 typval_T *rettv,
4219 char_u **nextcmd,
4220 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221{
4222 int ret;
4223 char_u *p;
4224
4225 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004226 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 if (ret == FAIL || !ends_excmd(*p))
4228 {
4229 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004230 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 /*
4232 * Report the invalid expression unless the expression evaluation has
4233 * been cancelled due to an aborting error, an interrupt, or an
4234 * exception.
4235 */
4236 if (!aborting())
4237 EMSG2(_(e_invexpr2), arg);
4238 ret = FAIL;
4239 }
4240 if (nextcmd != NULL)
4241 *nextcmd = check_nextcmd(p);
4242
4243 return ret;
4244}
4245
4246/*
4247 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004248 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 *
4250 * "arg" must point to the first non-white of the expression.
4251 * "arg" is advanced to the next non-white after the recognized expression.
4252 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004253 * Note: "rettv.v_lock" is not set.
4254 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 * Return OK or FAIL.
4256 */
4257 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004258eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259{
4260 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004261 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262
4263 /*
4264 * Get the first variable.
4265 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004266 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 return FAIL;
4268
4269 if ((*arg)[0] == '?')
4270 {
4271 result = FALSE;
4272 if (evaluate)
4273 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004274 int error = FALSE;
4275
4276 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004278 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004279 if (error)
4280 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 }
4282
4283 /*
4284 * Get the second variable.
4285 */
4286 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004287 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 return FAIL;
4289
4290 /*
4291 * Check for the ":".
4292 */
4293 if ((*arg)[0] != ':')
4294 {
4295 EMSG(_("E109: Missing ':' after '?'"));
4296 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004297 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 return FAIL;
4299 }
4300
4301 /*
4302 * Get the third variable.
4303 */
4304 *arg = skipwhite(*arg + 1);
4305 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4306 {
4307 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004308 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 return FAIL;
4310 }
4311 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004312 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 }
4314
4315 return OK;
4316}
4317
4318/*
4319 * Handle first level expression:
4320 * expr2 || expr2 || expr2 logical OR
4321 *
4322 * "arg" must point to the first non-white of the expression.
4323 * "arg" is advanced to the next non-white after the recognized expression.
4324 *
4325 * Return OK or FAIL.
4326 */
4327 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004328eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329{
Bram Moolenaar33570922005-01-25 22:26:29 +00004330 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 long result;
4332 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004333 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334
4335 /*
4336 * Get the first variable.
4337 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004338 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 return FAIL;
4340
4341 /*
4342 * Repeat until there is no following "||".
4343 */
4344 first = TRUE;
4345 result = FALSE;
4346 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4347 {
4348 if (evaluate && first)
4349 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004350 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004352 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004353 if (error)
4354 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 first = FALSE;
4356 }
4357
4358 /*
4359 * Get the second variable.
4360 */
4361 *arg = skipwhite(*arg + 2);
4362 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4363 return FAIL;
4364
4365 /*
4366 * Compute the result.
4367 */
4368 if (evaluate && !result)
4369 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004370 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004372 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004373 if (error)
4374 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 }
4376 if (evaluate)
4377 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004378 rettv->v_type = VAR_NUMBER;
4379 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 }
4381 }
4382
4383 return OK;
4384}
4385
4386/*
4387 * Handle second level expression:
4388 * expr3 && expr3 && expr3 logical AND
4389 *
4390 * "arg" must point to the first non-white of the expression.
4391 * "arg" is advanced to the next non-white after the recognized expression.
4392 *
4393 * Return OK or FAIL.
4394 */
4395 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004396eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397{
Bram Moolenaar33570922005-01-25 22:26:29 +00004398 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 long result;
4400 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004401 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402
4403 /*
4404 * Get the first variable.
4405 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004406 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 return FAIL;
4408
4409 /*
4410 * Repeat until there is no following "&&".
4411 */
4412 first = TRUE;
4413 result = TRUE;
4414 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4415 {
4416 if (evaluate && first)
4417 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004418 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004420 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004421 if (error)
4422 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 first = FALSE;
4424 }
4425
4426 /*
4427 * Get the second variable.
4428 */
4429 *arg = skipwhite(*arg + 2);
4430 if (eval4(arg, &var2, evaluate && result) == FAIL)
4431 return FAIL;
4432
4433 /*
4434 * Compute the result.
4435 */
4436 if (evaluate && result)
4437 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004438 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004440 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004441 if (error)
4442 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 }
4444 if (evaluate)
4445 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004446 rettv->v_type = VAR_NUMBER;
4447 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 }
4449 }
4450
4451 return OK;
4452}
4453
4454/*
4455 * Handle third level expression:
4456 * var1 == var2
4457 * var1 =~ var2
4458 * var1 != var2
4459 * var1 !~ var2
4460 * var1 > var2
4461 * var1 >= var2
4462 * var1 < var2
4463 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004464 * var1 is var2
4465 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 *
4467 * "arg" must point to the first non-white of the expression.
4468 * "arg" is advanced to the next non-white after the recognized expression.
4469 *
4470 * Return OK or FAIL.
4471 */
4472 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004473eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474{
Bram Moolenaar33570922005-01-25 22:26:29 +00004475 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 char_u *p;
4477 int i;
4478 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004479 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 int len = 2;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004481 varnumber_T n1, n2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 char_u *s1, *s2;
4483 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485
4486 /*
4487 * Get the first variable.
4488 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004489 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 return FAIL;
4491
4492 p = *arg;
4493 switch (p[0])
4494 {
4495 case '=': if (p[1] == '=')
4496 type = TYPE_EQUAL;
4497 else if (p[1] == '~')
4498 type = TYPE_MATCH;
4499 break;
4500 case '!': if (p[1] == '=')
4501 type = TYPE_NEQUAL;
4502 else if (p[1] == '~')
4503 type = TYPE_NOMATCH;
4504 break;
4505 case '>': if (p[1] != '=')
4506 {
4507 type = TYPE_GREATER;
4508 len = 1;
4509 }
4510 else
4511 type = TYPE_GEQUAL;
4512 break;
4513 case '<': if (p[1] != '=')
4514 {
4515 type = TYPE_SMALLER;
4516 len = 1;
4517 }
4518 else
4519 type = TYPE_SEQUAL;
4520 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004521 case 'i': if (p[1] == 's')
4522 {
4523 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4524 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004525 i = p[len];
4526 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004527 {
4528 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4529 type_is = TRUE;
4530 }
4531 }
4532 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 }
4534
4535 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004536 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 */
4538 if (type != TYPE_UNKNOWN)
4539 {
4540 /* extra question mark appended: ignore case */
4541 if (p[len] == '?')
4542 {
4543 ic = TRUE;
4544 ++len;
4545 }
4546 /* extra '#' appended: match case */
4547 else if (p[len] == '#')
4548 {
4549 ic = FALSE;
4550 ++len;
4551 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004552 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 else
4554 ic = p_ic;
4555
4556 /*
4557 * Get the second variable.
4558 */
4559 *arg = skipwhite(p + len);
4560 if (eval5(arg, &var2, evaluate) == FAIL)
4561 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004562 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 return FAIL;
4564 }
4565
4566 if (evaluate)
4567 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004568 if (type_is && rettv->v_type != var2.v_type)
4569 {
4570 /* For "is" a different type always means FALSE, for "notis"
4571 * it means TRUE. */
4572 n1 = (type == TYPE_NEQUAL);
4573 }
4574 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4575 {
4576 if (type_is)
4577 {
4578 n1 = (rettv->v_type == var2.v_type
4579 && rettv->vval.v_list == var2.vval.v_list);
4580 if (type == TYPE_NEQUAL)
4581 n1 = !n1;
4582 }
4583 else if (rettv->v_type != var2.v_type
4584 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4585 {
4586 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004587 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004588 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004589 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004590 clear_tv(rettv);
4591 clear_tv(&var2);
4592 return FAIL;
4593 }
4594 else
4595 {
4596 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004597 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4598 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004599 if (type == TYPE_NEQUAL)
4600 n1 = !n1;
4601 }
4602 }
4603
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004604 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4605 {
4606 if (type_is)
4607 {
4608 n1 = (rettv->v_type == var2.v_type
4609 && rettv->vval.v_dict == var2.vval.v_dict);
4610 if (type == TYPE_NEQUAL)
4611 n1 = !n1;
4612 }
4613 else if (rettv->v_type != var2.v_type
4614 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4615 {
4616 if (rettv->v_type != var2.v_type)
4617 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4618 else
4619 EMSG(_("E736: Invalid operation for Dictionary"));
4620 clear_tv(rettv);
4621 clear_tv(&var2);
4622 return FAIL;
4623 }
4624 else
4625 {
4626 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004627 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4628 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004629 if (type == TYPE_NEQUAL)
4630 n1 = !n1;
4631 }
4632 }
4633
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004634 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4635 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004636 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004637 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004638 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004639 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004640 clear_tv(rettv);
4641 clear_tv(&var2);
4642 return FAIL;
4643 }
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02004644 if ((rettv->v_type == VAR_PARTIAL
4645 && rettv->vval.v_partial == NULL)
4646 || (var2.v_type == VAR_PARTIAL
4647 && var2.vval.v_partial == NULL))
4648 /* when a partial is NULL assume not equal */
4649 n1 = FALSE;
4650 else if (type_is)
4651 {
4652 if (rettv->v_type == VAR_FUNC && var2.v_type == VAR_FUNC)
4653 /* strings are considered the same if their value is
4654 * the same */
4655 n1 = tv_equal(rettv, &var2, ic, FALSE);
4656 else if (rettv->v_type == VAR_PARTIAL
4657 && var2.v_type == VAR_PARTIAL)
4658 n1 = (rettv->vval.v_partial == var2.vval.v_partial);
4659 else
4660 n1 = FALSE;
4661 }
4662 else
4663 n1 = tv_equal(rettv, &var2, ic, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004664 if (type == TYPE_NEQUAL)
4665 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004666 }
4667
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004668#ifdef FEAT_FLOAT
4669 /*
4670 * If one of the two variables is a float, compare as a float.
4671 * When using "=~" or "!~", always compare as string.
4672 */
4673 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4674 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4675 {
4676 float_T f1, f2;
4677
4678 if (rettv->v_type == VAR_FLOAT)
4679 f1 = rettv->vval.v_float;
4680 else
4681 f1 = get_tv_number(rettv);
4682 if (var2.v_type == VAR_FLOAT)
4683 f2 = var2.vval.v_float;
4684 else
4685 f2 = get_tv_number(&var2);
4686 n1 = FALSE;
4687 switch (type)
4688 {
4689 case TYPE_EQUAL: n1 = (f1 == f2); break;
4690 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4691 case TYPE_GREATER: n1 = (f1 > f2); break;
4692 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4693 case TYPE_SMALLER: n1 = (f1 < f2); break;
4694 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4695 case TYPE_UNKNOWN:
4696 case TYPE_MATCH:
4697 case TYPE_NOMATCH: break; /* avoid gcc warning */
4698 }
4699 }
4700#endif
4701
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 /*
4703 * If one of the two variables is a number, compare as a number.
4704 * When using "=~" or "!~", always compare as string.
4705 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004706 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4708 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004709 n1 = get_tv_number(rettv);
4710 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 switch (type)
4712 {
4713 case TYPE_EQUAL: n1 = (n1 == n2); break;
4714 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4715 case TYPE_GREATER: n1 = (n1 > n2); break;
4716 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4717 case TYPE_SMALLER: n1 = (n1 < n2); break;
4718 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4719 case TYPE_UNKNOWN:
4720 case TYPE_MATCH:
4721 case TYPE_NOMATCH: break; /* avoid gcc warning */
4722 }
4723 }
4724 else
4725 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004726 s1 = get_tv_string_buf(rettv, buf1);
4727 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4729 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4730 else
4731 i = 0;
4732 n1 = FALSE;
4733 switch (type)
4734 {
4735 case TYPE_EQUAL: n1 = (i == 0); break;
4736 case TYPE_NEQUAL: n1 = (i != 0); break;
4737 case TYPE_GREATER: n1 = (i > 0); break;
4738 case TYPE_GEQUAL: n1 = (i >= 0); break;
4739 case TYPE_SMALLER: n1 = (i < 0); break;
4740 case TYPE_SEQUAL: n1 = (i <= 0); break;
4741
4742 case TYPE_MATCH:
4743 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004744 n1 = pattern_match(s2, s1, ic);
4745 if (type == TYPE_NOMATCH)
4746 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 break;
4748
4749 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4750 }
4751 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004752 clear_tv(rettv);
4753 clear_tv(&var2);
4754 rettv->v_type = VAR_NUMBER;
4755 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 }
4757 }
4758
4759 return OK;
4760}
4761
4762/*
4763 * Handle fourth level expression:
4764 * + number addition
4765 * - number subtraction
4766 * . string concatenation
4767 *
4768 * "arg" must point to the first non-white of the expression.
4769 * "arg" is advanced to the next non-white after the recognized expression.
4770 *
4771 * Return OK or FAIL.
4772 */
4773 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004774eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775{
Bram Moolenaar33570922005-01-25 22:26:29 +00004776 typval_T var2;
4777 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004779 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004780#ifdef FEAT_FLOAT
4781 float_T f1 = 0, f2 = 0;
4782#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 char_u *s1, *s2;
4784 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4785 char_u *p;
4786
4787 /*
4788 * Get the first variable.
4789 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004790 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 return FAIL;
4792
4793 /*
4794 * Repeat computing, until no '+', '-' or '.' is following.
4795 */
4796 for (;;)
4797 {
4798 op = **arg;
4799 if (op != '+' && op != '-' && op != '.')
4800 break;
4801
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004802 if ((op != '+' || rettv->v_type != VAR_LIST)
4803#ifdef FEAT_FLOAT
4804 && (op == '.' || rettv->v_type != VAR_FLOAT)
4805#endif
4806 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004807 {
4808 /* For "list + ...", an illegal use of the first operand as
4809 * a number cannot be determined before evaluating the 2nd
4810 * operand: if this is also a list, all is ok.
4811 * For "something . ...", "something - ..." or "non-list + ...",
4812 * we know that the first operand needs to be a string or number
4813 * without evaluating the 2nd operand. So check before to avoid
4814 * side effects after an error. */
4815 if (evaluate && get_tv_string_chk(rettv) == NULL)
4816 {
4817 clear_tv(rettv);
4818 return FAIL;
4819 }
4820 }
4821
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 /*
4823 * Get the second variable.
4824 */
4825 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004826 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004828 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 return FAIL;
4830 }
4831
4832 if (evaluate)
4833 {
4834 /*
4835 * Compute the result.
4836 */
4837 if (op == '.')
4838 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004839 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4840 s2 = get_tv_string_buf_chk(&var2, buf2);
4841 if (s2 == NULL) /* type error ? */
4842 {
4843 clear_tv(rettv);
4844 clear_tv(&var2);
4845 return FAIL;
4846 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004847 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004848 clear_tv(rettv);
4849 rettv->v_type = VAR_STRING;
4850 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004852 else if (op == '+' && rettv->v_type == VAR_LIST
4853 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004854 {
4855 /* concatenate Lists */
4856 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4857 &var3) == FAIL)
4858 {
4859 clear_tv(rettv);
4860 clear_tv(&var2);
4861 return FAIL;
4862 }
4863 clear_tv(rettv);
4864 *rettv = var3;
4865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 else
4867 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004868 int error = FALSE;
4869
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004870#ifdef FEAT_FLOAT
4871 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004872 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004873 f1 = rettv->vval.v_float;
4874 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004875 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004876 else
4877#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004878 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004879 n1 = get_tv_number_chk(rettv, &error);
4880 if (error)
4881 {
4882 /* This can only happen for "list + non-list". For
4883 * "non-list + ..." or "something - ...", we returned
4884 * before evaluating the 2nd operand. */
4885 clear_tv(rettv);
4886 return FAIL;
4887 }
4888#ifdef FEAT_FLOAT
4889 if (var2.v_type == VAR_FLOAT)
4890 f1 = n1;
4891#endif
4892 }
4893#ifdef FEAT_FLOAT
4894 if (var2.v_type == VAR_FLOAT)
4895 {
4896 f2 = var2.vval.v_float;
4897 n2 = 0;
4898 }
4899 else
4900#endif
4901 {
4902 n2 = get_tv_number_chk(&var2, &error);
4903 if (error)
4904 {
4905 clear_tv(rettv);
4906 clear_tv(&var2);
4907 return FAIL;
4908 }
4909#ifdef FEAT_FLOAT
4910 if (rettv->v_type == VAR_FLOAT)
4911 f2 = n2;
4912#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004913 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004914 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004915
4916#ifdef FEAT_FLOAT
4917 /* If there is a float on either side the result is a float. */
4918 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4919 {
4920 if (op == '+')
4921 f1 = f1 + f2;
4922 else
4923 f1 = f1 - f2;
4924 rettv->v_type = VAR_FLOAT;
4925 rettv->vval.v_float = f1;
4926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004928#endif
4929 {
4930 if (op == '+')
4931 n1 = n1 + n2;
4932 else
4933 n1 = n1 - n2;
4934 rettv->v_type = VAR_NUMBER;
4935 rettv->vval.v_number = n1;
4936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004938 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 }
4940 }
4941 return OK;
4942}
4943
4944/*
4945 * Handle fifth level expression:
4946 * * number multiplication
4947 * / number division
4948 * % number modulo
4949 *
4950 * "arg" must point to the first non-white of the expression.
4951 * "arg" is advanced to the next non-white after the recognized expression.
4952 *
4953 * Return OK or FAIL.
4954 */
4955 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004956eval6(
4957 char_u **arg,
4958 typval_T *rettv,
4959 int evaluate,
4960 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961{
Bram Moolenaar33570922005-01-25 22:26:29 +00004962 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004964 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004965#ifdef FEAT_FLOAT
4966 int use_float = FALSE;
4967 float_T f1 = 0, f2;
4968#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004969 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970
4971 /*
4972 * Get the first variable.
4973 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004974 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 return FAIL;
4976
4977 /*
4978 * Repeat computing, until no '*', '/' or '%' is following.
4979 */
4980 for (;;)
4981 {
4982 op = **arg;
4983 if (op != '*' && op != '/' && op != '%')
4984 break;
4985
4986 if (evaluate)
4987 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004988#ifdef FEAT_FLOAT
4989 if (rettv->v_type == VAR_FLOAT)
4990 {
4991 f1 = rettv->vval.v_float;
4992 use_float = TRUE;
4993 n1 = 0;
4994 }
4995 else
4996#endif
4997 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004998 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004999 if (error)
5000 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 }
5002 else
5003 n1 = 0;
5004
5005 /*
5006 * Get the second variable.
5007 */
5008 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005009 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 return FAIL;
5011
5012 if (evaluate)
5013 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005014#ifdef FEAT_FLOAT
5015 if (var2.v_type == VAR_FLOAT)
5016 {
5017 if (!use_float)
5018 {
5019 f1 = n1;
5020 use_float = TRUE;
5021 }
5022 f2 = var2.vval.v_float;
5023 n2 = 0;
5024 }
5025 else
5026#endif
5027 {
5028 n2 = get_tv_number_chk(&var2, &error);
5029 clear_tv(&var2);
5030 if (error)
5031 return FAIL;
5032#ifdef FEAT_FLOAT
5033 if (use_float)
5034 f2 = n2;
5035#endif
5036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037
5038 /*
5039 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005040 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005042#ifdef FEAT_FLOAT
5043 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005045 if (op == '*')
5046 f1 = f1 * f2;
5047 else if (op == '/')
5048 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005049# ifdef VMS
5050 /* VMS crashes on divide by zero, work around it */
5051 if (f2 == 0.0)
5052 {
5053 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005054 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005055 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005056 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005057 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005058 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005059 }
5060 else
5061 f1 = f1 / f2;
5062# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005063 /* We rely on the floating point library to handle divide
5064 * by zero to result in "inf" and not a crash. */
5065 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005066# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005069 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005070 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005071 return FAIL;
5072 }
5073 rettv->v_type = VAR_FLOAT;
5074 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 }
5076 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005079 if (op == '*')
5080 n1 = n1 * n2;
5081 else if (op == '/')
5082 {
5083 if (n2 == 0) /* give an error message? */
5084 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005085#ifdef FEAT_NUM64
5086 if (n1 == 0)
5087 n1 = -0x7fffffffffffffff - 1; /* similar to NaN */
5088 else if (n1 < 0)
5089 n1 = -0x7fffffffffffffff;
5090 else
5091 n1 = 0x7fffffffffffffff;
5092#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005093 if (n1 == 0)
5094 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5095 else if (n1 < 0)
5096 n1 = -0x7fffffffL;
5097 else
5098 n1 = 0x7fffffffL;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005099#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005100 }
5101 else
5102 n1 = n1 / n2;
5103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005105 {
5106 if (n2 == 0) /* give an error message? */
5107 n1 = 0;
5108 else
5109 n1 = n1 % n2;
5110 }
5111 rettv->v_type = VAR_NUMBER;
5112 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 }
5115 }
5116
5117 return OK;
5118}
5119
5120/*
5121 * Handle sixth level expression:
5122 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005123 * "string" string constant
5124 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 * &option-name option value
5126 * @r register contents
5127 * identifier variable value
5128 * function() function call
5129 * $VAR environment variable
5130 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005131 * [expr, expr] List
5132 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 *
5134 * Also handle:
5135 * ! in front logical NOT
5136 * - in front unary minus
5137 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005138 * trailing [] subscript in String or List
5139 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 *
5141 * "arg" must point to the first non-white of the expression.
5142 * "arg" is advanced to the next non-white after the recognized expression.
5143 *
5144 * Return OK or FAIL.
5145 */
5146 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005147eval7(
5148 char_u **arg,
5149 typval_T *rettv,
5150 int evaluate,
5151 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005153 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 int len;
5155 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 char_u *start_leader, *end_leader;
5157 int ret = OK;
5158 char_u *alias;
5159
5160 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005161 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005162 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005164 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165
5166 /*
5167 * Skip '!' and '-' characters. They are handled later.
5168 */
5169 start_leader = *arg;
5170 while (**arg == '!' || **arg == '-' || **arg == '+')
5171 *arg = skipwhite(*arg + 1);
5172 end_leader = *arg;
5173
5174 switch (**arg)
5175 {
5176 /*
5177 * Number constant.
5178 */
5179 case '0':
5180 case '1':
5181 case '2':
5182 case '3':
5183 case '4':
5184 case '5':
5185 case '6':
5186 case '7':
5187 case '8':
5188 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005189 {
5190#ifdef FEAT_FLOAT
5191 char_u *p = skipdigits(*arg + 1);
5192 int get_float = FALSE;
5193
5194 /* We accept a float when the format matches
5195 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005196 * strict to avoid backwards compatibility problems.
5197 * Don't look for a float after the "." operator, so that
5198 * ":let vers = 1.2.3" doesn't fail. */
5199 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005201 get_float = TRUE;
5202 p = skipdigits(p + 2);
5203 if (*p == 'e' || *p == 'E')
5204 {
5205 ++p;
5206 if (*p == '-' || *p == '+')
5207 ++p;
5208 if (!vim_isdigit(*p))
5209 get_float = FALSE;
5210 else
5211 p = skipdigits(p + 1);
5212 }
5213 if (ASCII_ISALPHA(*p) || *p == '.')
5214 get_float = FALSE;
5215 }
5216 if (get_float)
5217 {
5218 float_T f;
5219
5220 *arg += string2float(*arg, &f);
5221 if (evaluate)
5222 {
5223 rettv->v_type = VAR_FLOAT;
5224 rettv->vval.v_float = f;
5225 }
5226 }
5227 else
5228#endif
5229 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005230 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005231 *arg += len;
5232 if (evaluate)
5233 {
5234 rettv->v_type = VAR_NUMBER;
5235 rettv->vval.v_number = n;
5236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 }
5238 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240
5241 /*
5242 * String constant: "string".
5243 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005244 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 break;
5246
5247 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005248 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005250 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005251 break;
5252
5253 /*
5254 * List: [expr, expr]
5255 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005256 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 break;
5258
5259 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005260 * Dictionary: {key: val, key: val}
5261 */
5262 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5263 break;
5264
5265 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005266 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005268 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 break;
5270
5271 /*
5272 * Environment variable: $VAR.
5273 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005274 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 break;
5276
5277 /*
5278 * Register contents: @r.
5279 */
5280 case '@': ++*arg;
5281 if (evaluate)
5282 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005283 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005284 rettv->vval.v_string = get_reg_contents(**arg,
5285 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 }
5287 if (**arg != NUL)
5288 ++*arg;
5289 break;
5290
5291 /*
5292 * nested expression: (expression).
5293 */
5294 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005295 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 if (**arg == ')')
5297 ++*arg;
5298 else if (ret == OK)
5299 {
5300 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005301 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 ret = FAIL;
5303 }
5304 break;
5305
Bram Moolenaar8c711452005-01-14 21:53:12 +00005306 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 break;
5308 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005309
5310 if (ret == NOTDONE)
5311 {
5312 /*
5313 * Must be a variable or function name.
5314 * Can also be a curly-braces kind of name: {expr}.
5315 */
5316 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005317 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005318 if (alias != NULL)
5319 s = alias;
5320
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005321 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005322 ret = FAIL;
5323 else
5324 {
5325 if (**arg == '(') /* recursive! */
5326 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005327 partial_T *partial;
5328
Bram Moolenaar8c711452005-01-14 21:53:12 +00005329 /* If "s" is the name of a variable of type VAR_FUNC
5330 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005331 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005332
5333 /* Invoke the function. */
5334 ret = get_func_tv(s, len, rettv, arg,
5335 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005336 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005337
5338 /* If evaluate is FALSE rettv->v_type was not set in
5339 * get_func_tv, but it's needed in handle_subscript() to parse
5340 * what follows. So set it here. */
5341 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5342 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02005343 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01005344 rettv->v_type = VAR_FUNC;
5345 }
5346
Bram Moolenaar8c711452005-01-14 21:53:12 +00005347 /* Stop the expression evaluation when immediately
5348 * aborting on error, or when an interrupt occurred or
5349 * an exception was thrown but not caught. */
5350 if (aborting())
5351 {
5352 if (ret == OK)
5353 clear_tv(rettv);
5354 ret = FAIL;
5355 }
5356 }
5357 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005358 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005359 else
5360 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005361 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005362 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005363 }
5364
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 *arg = skipwhite(*arg);
5366
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005367 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5368 * expr(expr). */
5369 if (ret == OK)
5370 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371
5372 /*
5373 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5374 */
5375 if (ret == OK && evaluate && end_leader > start_leader)
5376 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005377 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005378 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005379#ifdef FEAT_FLOAT
5380 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005381
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005382 if (rettv->v_type == VAR_FLOAT)
5383 f = rettv->vval.v_float;
5384 else
5385#endif
5386 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005387 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005389 clear_tv(rettv);
5390 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005392 else
5393 {
5394 while (end_leader > start_leader)
5395 {
5396 --end_leader;
5397 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005398 {
5399#ifdef FEAT_FLOAT
5400 if (rettv->v_type == VAR_FLOAT)
5401 f = !f;
5402 else
5403#endif
5404 val = !val;
5405 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005406 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005407 {
5408#ifdef FEAT_FLOAT
5409 if (rettv->v_type == VAR_FLOAT)
5410 f = -f;
5411 else
5412#endif
5413 val = -val;
5414 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005415 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005416#ifdef FEAT_FLOAT
5417 if (rettv->v_type == VAR_FLOAT)
5418 {
5419 clear_tv(rettv);
5420 rettv->vval.v_float = f;
5421 }
5422 else
5423#endif
5424 {
5425 clear_tv(rettv);
5426 rettv->v_type = VAR_NUMBER;
5427 rettv->vval.v_number = val;
5428 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 }
5431
5432 return ret;
5433}
5434
5435/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005436 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5437 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005438 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5439 */
5440 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005441eval_index(
5442 char_u **arg,
5443 typval_T *rettv,
5444 int evaluate,
5445 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005446{
5447 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005448 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005449 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005450 long len = -1;
5451 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005452 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005453 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005454
Bram Moolenaara03f2332016-02-06 18:09:59 +01005455 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005456 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005457 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005458 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005459 if (verbose)
5460 EMSG(_("E695: Cannot index a Funcref"));
5461 return FAIL;
5462 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005463#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005464 if (verbose)
5465 EMSG(_(e_float_as_string));
5466 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005467#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005468 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005469 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005470 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005471 if (verbose)
5472 EMSG(_("E909: Cannot index a special variable"));
5473 return FAIL;
5474 case VAR_UNKNOWN:
5475 if (evaluate)
5476 return FAIL;
5477 /* FALLTHROUGH */
5478
5479 case VAR_STRING:
5480 case VAR_NUMBER:
5481 case VAR_LIST:
5482 case VAR_DICT:
5483 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005484 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005485
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005486 init_tv(&var1);
5487 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005488 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005489 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005490 /*
5491 * dict.name
5492 */
5493 key = *arg + 1;
5494 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5495 ;
5496 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005497 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005498 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005499 }
5500 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005501 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005502 /*
5503 * something[idx]
5504 *
5505 * Get the (first) variable from inside the [].
5506 */
5507 *arg = skipwhite(*arg + 1);
5508 if (**arg == ':')
5509 empty1 = TRUE;
5510 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5511 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005512 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5513 {
5514 /* not a number or string */
5515 clear_tv(&var1);
5516 return FAIL;
5517 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005518
5519 /*
5520 * Get the second variable from inside the [:].
5521 */
5522 if (**arg == ':')
5523 {
5524 range = TRUE;
5525 *arg = skipwhite(*arg + 1);
5526 if (**arg == ']')
5527 empty2 = TRUE;
5528 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5529 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005530 if (!empty1)
5531 clear_tv(&var1);
5532 return FAIL;
5533 }
5534 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5535 {
5536 /* not a number or string */
5537 if (!empty1)
5538 clear_tv(&var1);
5539 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005540 return FAIL;
5541 }
5542 }
5543
5544 /* Check for the ']'. */
5545 if (**arg != ']')
5546 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005547 if (verbose)
5548 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005549 clear_tv(&var1);
5550 if (range)
5551 clear_tv(&var2);
5552 return FAIL;
5553 }
5554 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005555 }
5556
5557 if (evaluate)
5558 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005559 n1 = 0;
5560 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005561 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005562 n1 = get_tv_number(&var1);
5563 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005564 }
5565 if (range)
5566 {
5567 if (empty2)
5568 n2 = -1;
5569 else
5570 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005571 n2 = get_tv_number(&var2);
5572 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005573 }
5574 }
5575
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005576 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005577 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005578 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005579 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005580 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005581 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005582 case VAR_SPECIAL:
5583 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005584 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005585 break; /* not evaluating, skipping over subscript */
5586
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005587 case VAR_NUMBER:
5588 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005589 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005590 len = (long)STRLEN(s);
5591 if (range)
5592 {
5593 /* The resulting variable is a substring. If the indexes
5594 * are out of range the result is empty. */
5595 if (n1 < 0)
5596 {
5597 n1 = len + n1;
5598 if (n1 < 0)
5599 n1 = 0;
5600 }
5601 if (n2 < 0)
5602 n2 = len + n2;
5603 else if (n2 >= len)
5604 n2 = len;
5605 if (n1 >= len || n2 < 0 || n1 > n2)
5606 s = NULL;
5607 else
5608 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5609 }
5610 else
5611 {
5612 /* The resulting variable is a string of a single
5613 * character. If the index is too big or negative the
5614 * result is empty. */
5615 if (n1 >= len || n1 < 0)
5616 s = NULL;
5617 else
5618 s = vim_strnsave(s + n1, 1);
5619 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005620 clear_tv(rettv);
5621 rettv->v_type = VAR_STRING;
5622 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005623 break;
5624
5625 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005626 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005627 if (n1 < 0)
5628 n1 = len + n1;
5629 if (!empty1 && (n1 < 0 || n1 >= len))
5630 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005631 /* For a range we allow invalid values and return an empty
5632 * list. A list index out of range is an error. */
5633 if (!range)
5634 {
5635 if (verbose)
5636 EMSGN(_(e_listidx), n1);
5637 return FAIL;
5638 }
5639 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005640 }
5641 if (range)
5642 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005643 list_T *l;
5644 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005645
5646 if (n2 < 0)
5647 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005648 else if (n2 >= len)
5649 n2 = len - 1;
5650 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005651 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005652 l = list_alloc();
5653 if (l == NULL)
5654 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005655 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005656 n1 <= n2; ++n1)
5657 {
5658 if (list_append_tv(l, &item->li_tv) == FAIL)
5659 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005660 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005661 return FAIL;
5662 }
5663 item = item->li_next;
5664 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005665 clear_tv(rettv);
5666 rettv->v_type = VAR_LIST;
5667 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005668 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005669 }
5670 else
5671 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005672 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005673 clear_tv(rettv);
5674 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005675 }
5676 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005677
5678 case VAR_DICT:
5679 if (range)
5680 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005681 if (verbose)
5682 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005683 if (len == -1)
5684 clear_tv(&var1);
5685 return FAIL;
5686 }
5687 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005688 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005689
5690 if (len == -1)
5691 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005692 key = get_tv_string_chk(&var1);
5693 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005694 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005695 clear_tv(&var1);
5696 return FAIL;
5697 }
5698 }
5699
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005700 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005701
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005702 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005703 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005704 if (len == -1)
5705 clear_tv(&var1);
5706 if (item == NULL)
5707 return FAIL;
5708
5709 copy_tv(&item->di_tv, &var1);
5710 clear_tv(rettv);
5711 *rettv = var1;
5712 }
5713 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005714 }
5715 }
5716
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005717 return OK;
5718}
5719
5720/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 * Get an option value.
5722 * "arg" points to the '&' or '+' before the option name.
5723 * "arg" is advanced to character after the option name.
5724 * Return OK or FAIL.
5725 */
5726 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005727get_option_tv(
5728 char_u **arg,
5729 typval_T *rettv, /* when NULL, only check if option exists */
5730 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731{
5732 char_u *option_end;
5733 long numval;
5734 char_u *stringval;
5735 int opt_type;
5736 int c;
5737 int working = (**arg == '+'); /* has("+option") */
5738 int ret = OK;
5739 int opt_flags;
5740
5741 /*
5742 * Isolate the option name and find its value.
5743 */
5744 option_end = find_option_end(arg, &opt_flags);
5745 if (option_end == NULL)
5746 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005747 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 EMSG2(_("E112: Option name missing: %s"), *arg);
5749 return FAIL;
5750 }
5751
5752 if (!evaluate)
5753 {
5754 *arg = option_end;
5755 return OK;
5756 }
5757
5758 c = *option_end;
5759 *option_end = NUL;
5760 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005761 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762
5763 if (opt_type == -3) /* invalid name */
5764 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005765 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 EMSG2(_("E113: Unknown option: %s"), *arg);
5767 ret = FAIL;
5768 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005769 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 {
5771 if (opt_type == -2) /* hidden string option */
5772 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005773 rettv->v_type = VAR_STRING;
5774 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 }
5776 else if (opt_type == -1) /* hidden number option */
5777 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005778 rettv->v_type = VAR_NUMBER;
5779 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 }
5781 else if (opt_type == 1) /* number option */
5782 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005783 rettv->v_type = VAR_NUMBER;
5784 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 }
5786 else /* string option */
5787 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005788 rettv->v_type = VAR_STRING;
5789 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 }
5791 }
5792 else if (working && (opt_type == -2 || opt_type == -1))
5793 ret = FAIL;
5794
5795 *option_end = c; /* put back for error messages */
5796 *arg = option_end;
5797
5798 return ret;
5799}
5800
5801/*
5802 * Allocate a variable for a string constant.
5803 * Return OK or FAIL.
5804 */
5805 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005806get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807{
5808 char_u *p;
5809 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 int extra = 0;
5811
5812 /*
5813 * Find the end of the string, skipping backslashed characters.
5814 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005815 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 {
5817 if (*p == '\\' && p[1] != NUL)
5818 {
5819 ++p;
5820 /* A "\<x>" form occupies at least 4 characters, and produces up
5821 * to 6 characters: reserve space for 2 extra */
5822 if (*p == '<')
5823 extra += 2;
5824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 }
5826
5827 if (*p != '"')
5828 {
5829 EMSG2(_("E114: Missing quote: %s"), *arg);
5830 return FAIL;
5831 }
5832
5833 /* If only parsing, set *arg and return here */
5834 if (!evaluate)
5835 {
5836 *arg = p + 1;
5837 return OK;
5838 }
5839
5840 /*
5841 * Copy the string into allocated memory, handling backslashed
5842 * characters.
5843 */
5844 name = alloc((unsigned)(p - *arg + extra));
5845 if (name == NULL)
5846 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005847 rettv->v_type = VAR_STRING;
5848 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849
Bram Moolenaar8c711452005-01-14 21:53:12 +00005850 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 {
5852 if (*p == '\\')
5853 {
5854 switch (*++p)
5855 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005856 case 'b': *name++ = BS; ++p; break;
5857 case 'e': *name++ = ESC; ++p; break;
5858 case 'f': *name++ = FF; ++p; break;
5859 case 'n': *name++ = NL; ++p; break;
5860 case 'r': *name++ = CAR; ++p; break;
5861 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862
5863 case 'X': /* hex: "\x1", "\x12" */
5864 case 'x':
5865 case 'u': /* Unicode: "\u0023" */
5866 case 'U':
5867 if (vim_isxdigit(p[1]))
5868 {
5869 int n, nr;
5870 int c = toupper(*p);
5871
5872 if (c == 'X')
5873 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005874 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005876 else
5877 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 nr = 0;
5879 while (--n >= 0 && vim_isxdigit(p[1]))
5880 {
5881 ++p;
5882 nr = (nr << 4) + hex2nr(*p);
5883 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005884 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885#ifdef FEAT_MBYTE
5886 /* For "\u" store the number according to
5887 * 'encoding'. */
5888 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005889 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 else
5891#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005892 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 break;
5895
5896 /* octal: "\1", "\12", "\123" */
5897 case '0':
5898 case '1':
5899 case '2':
5900 case '3':
5901 case '4':
5902 case '5':
5903 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005904 case '7': *name = *p++ - '0';
5905 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005907 *name = (*name << 3) + *p++ - '0';
5908 if (*p >= '0' && *p <= '7')
5909 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005911 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 break;
5913
5914 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005915 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 if (extra != 0)
5917 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005918 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 break;
5920 }
5921 /* FALLTHROUGH */
5922
Bram Moolenaar8c711452005-01-14 21:53:12 +00005923 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 break;
5925 }
5926 }
5927 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005928 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005931 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 *arg = p + 1;
5933
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 return OK;
5935}
5936
5937/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005938 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 * Return OK or FAIL.
5940 */
5941 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005942get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943{
5944 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005945 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005946 int reduce = 0;
5947
5948 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005949 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005950 */
5951 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5952 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005953 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005954 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005955 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005956 break;
5957 ++reduce;
5958 ++p;
5959 }
5960 }
5961
Bram Moolenaar8c711452005-01-14 21:53:12 +00005962 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005963 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005964 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005965 return FAIL;
5966 }
5967
Bram Moolenaar8c711452005-01-14 21:53:12 +00005968 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005969 if (!evaluate)
5970 {
5971 *arg = p + 1;
5972 return OK;
5973 }
5974
5975 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005976 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005977 */
5978 str = alloc((unsigned)((p - *arg) - reduce));
5979 if (str == NULL)
5980 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005981 rettv->v_type = VAR_STRING;
5982 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005983
Bram Moolenaar8c711452005-01-14 21:53:12 +00005984 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005985 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005986 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005987 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005988 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005989 break;
5990 ++p;
5991 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005992 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005993 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005994 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005995 *arg = p + 1;
5996
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005997 return OK;
5998}
5999
Bram Moolenaarddecc252016-04-06 22:59:37 +02006000 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006001partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02006002{
6003 int i;
6004
6005 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006006 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02006007 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006008 dict_unref(pt->pt_dict);
Bram Moolenaarddecc252016-04-06 22:59:37 +02006009 func_unref(pt->pt_name);
6010 vim_free(pt->pt_name);
6011 vim_free(pt);
6012}
6013
6014/*
6015 * Unreference a closure: decrement the reference count and free it when it
6016 * becomes zero.
6017 */
6018 void
6019partial_unref(partial_T *pt)
6020{
6021 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006022 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02006023}
6024
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006025/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006026 * Allocate a variable for a List and fill it from "*arg".
6027 * Return OK or FAIL.
6028 */
6029 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006030get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006031{
Bram Moolenaar33570922005-01-25 22:26:29 +00006032 list_T *l = NULL;
6033 typval_T tv;
6034 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006035
6036 if (evaluate)
6037 {
6038 l = list_alloc();
6039 if (l == NULL)
6040 return FAIL;
6041 }
6042
6043 *arg = skipwhite(*arg + 1);
6044 while (**arg != ']' && **arg != NUL)
6045 {
6046 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6047 goto failret;
6048 if (evaluate)
6049 {
6050 item = listitem_alloc();
6051 if (item != NULL)
6052 {
6053 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006054 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006055 list_append(l, item);
6056 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006057 else
6058 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006059 }
6060
6061 if (**arg == ']')
6062 break;
6063 if (**arg != ',')
6064 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006065 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006066 goto failret;
6067 }
6068 *arg = skipwhite(*arg + 1);
6069 }
6070
6071 if (**arg != ']')
6072 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006073 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006074failret:
6075 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006076 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006077 return FAIL;
6078 }
6079
6080 *arg = skipwhite(*arg + 1);
6081 if (evaluate)
6082 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006083 rettv->v_type = VAR_LIST;
6084 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006085 ++l->lv_refcount;
6086 }
6087
6088 return OK;
6089}
6090
6091/*
6092 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006093 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006094 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006095 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006096list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006097{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006098 list_T *l;
6099
6100 l = (list_T *)alloc_clear(sizeof(list_T));
6101 if (l != NULL)
6102 {
6103 /* Prepend the list to the list of lists for garbage collection. */
6104 if (first_list != NULL)
6105 first_list->lv_used_prev = l;
6106 l->lv_used_prev = NULL;
6107 l->lv_used_next = first_list;
6108 first_list = l;
6109 }
6110 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006111}
6112
6113/*
Bram Moolenaar517ffbe2016-04-20 14:59:29 +02006114 * Allocate an empty list for a return value, with reference count set.
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006115 * Returns OK or FAIL.
6116 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006117 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006118rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006119{
6120 list_T *l = list_alloc();
6121
6122 if (l == NULL)
6123 return FAIL;
6124
6125 rettv->vval.v_list = l;
6126 rettv->v_type = VAR_LIST;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02006127 rettv->v_lock = 0;
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006128 ++l->lv_refcount;
6129 return OK;
6130}
6131
6132/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006133 * Unreference a list: decrement the reference count and free it when it
6134 * becomes zero.
6135 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006136 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006137list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006138{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006139 if (l != NULL && --l->lv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006140 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006141}
6142
6143/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006144 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006145 * Ignores the reference count.
6146 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006147 static void
6148list_free_contents(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006149{
Bram Moolenaar33570922005-01-25 22:26:29 +00006150 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006151
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006152 for (item = l->lv_first; item != NULL; item = l->lv_first)
6153 {
6154 /* Remove the item before deleting it. */
6155 l->lv_first = item->li_next;
6156 clear_tv(&item->li_tv);
6157 vim_free(item);
6158 }
6159}
6160
6161 static void
6162list_free_list(list_T *l)
6163{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006164 /* Remove the list from the list of lists for garbage collection. */
6165 if (l->lv_used_prev == NULL)
6166 first_list = l->lv_used_next;
6167 else
6168 l->lv_used_prev->lv_used_next = l->lv_used_next;
6169 if (l->lv_used_next != NULL)
6170 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6171
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006172 vim_free(l);
6173}
6174
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006175 void
6176list_free(list_T *l)
6177{
6178 if (!in_free_unref_items)
6179 {
6180 list_free_contents(l);
6181 list_free_list(l);
6182 }
6183}
6184
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006185/*
6186 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006187 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006188 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006189 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006190listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006191{
Bram Moolenaar33570922005-01-25 22:26:29 +00006192 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006193}
6194
6195/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006196 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006197 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006198 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006199listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006200{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006201 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006202 vim_free(item);
6203}
6204
6205/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006206 * Remove a list item from a List and free it. Also clears the value.
6207 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006208 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006209listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006210{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006211 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006212 listitem_free(item);
6213}
6214
6215/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006216 * Get the number of items in a list.
6217 */
6218 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006219list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006220{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006221 if (l == NULL)
6222 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006223 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006224}
6225
6226/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006227 * Return TRUE when two lists have exactly the same values.
6228 */
6229 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006230list_equal(
6231 list_T *l1,
6232 list_T *l2,
6233 int ic, /* ignore case for strings */
6234 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006235{
Bram Moolenaar33570922005-01-25 22:26:29 +00006236 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006237
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006238 if (l1 == NULL || l2 == NULL)
6239 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006240 if (l1 == l2)
6241 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006242 if (list_len(l1) != list_len(l2))
6243 return FALSE;
6244
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006245 for (item1 = l1->lv_first, item2 = l2->lv_first;
6246 item1 != NULL && item2 != NULL;
6247 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006248 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006249 return FALSE;
6250 return item1 == NULL && item2 == NULL;
6251}
6252
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006253/*
6254 * Return the dictitem that an entry in a hashtable points to.
6255 */
6256 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006257dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006258{
6259 return HI2DI(hi);
6260}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006261
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006262/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006263 * Return TRUE when two dictionaries have exactly the same key/values.
6264 */
6265 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006266dict_equal(
6267 dict_T *d1,
6268 dict_T *d2,
6269 int ic, /* ignore case for strings */
6270 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006271{
Bram Moolenaar33570922005-01-25 22:26:29 +00006272 hashitem_T *hi;
6273 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006274 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006275
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +02006276 if (d1 == NULL && d2 == NULL)
6277 return TRUE;
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006278 if (d1 == NULL || d2 == NULL)
6279 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006280 if (d1 == d2)
6281 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006282 if (dict_len(d1) != dict_len(d2))
6283 return FALSE;
6284
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006285 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006286 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006287 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006288 if (!HASHITEM_EMPTY(hi))
6289 {
6290 item2 = dict_find(d2, hi->hi_key, -1);
6291 if (item2 == NULL)
6292 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006293 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006294 return FALSE;
6295 --todo;
6296 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006297 }
6298 return TRUE;
6299}
6300
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006301static int tv_equal_recurse_limit;
6302
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02006303 static int
6304func_equal(
6305 typval_T *tv1,
6306 typval_T *tv2,
6307 int ic) /* ignore case */
6308{
6309 char_u *s1, *s2;
6310 dict_T *d1, *d2;
6311 int a1, a2;
6312 int i;
6313
6314 /* empty and NULL function name considered the same */
6315 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
6316 : tv1->vval.v_partial->pt_name;
6317 if (s1 != NULL && *s1 == NUL)
6318 s1 = NULL;
6319 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
6320 : tv2->vval.v_partial->pt_name;
6321 if (s2 != NULL && *s2 == NUL)
6322 s2 = NULL;
6323 if (s1 == NULL || s2 == NULL)
6324 {
6325 if (s1 != s2)
6326 return FALSE;
6327 }
6328 else if (STRCMP(s1, s2) != 0)
6329 return FALSE;
6330
6331 /* empty dict and NULL dict is different */
6332 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
6333 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
6334 if (d1 == NULL || d2 == NULL)
6335 {
6336 if (d1 != d2)
6337 return FALSE;
6338 }
6339 else if (!dict_equal(d1, d2, ic, TRUE))
6340 return FALSE;
6341
6342 /* empty list and no list considered the same */
6343 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
6344 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
6345 if (a1 != a2)
6346 return FALSE;
6347 for (i = 0; i < a1; ++i)
6348 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
6349 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
6350 return FALSE;
6351
6352 return TRUE;
6353}
6354
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006355/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006356 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006357 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006358 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006359 */
6360 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006361tv_equal(
6362 typval_T *tv1,
6363 typval_T *tv2,
6364 int ic, /* ignore case */
6365 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006366{
6367 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006368 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006369 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006370 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006371
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006372 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006373 * recursiveness to a limit. We guess they are equal then.
6374 * A fixed limit has the problem of still taking an awful long time.
6375 * Reduce the limit every time running into it. That should work fine for
6376 * deeply linked structures that are not recursively linked and catch
6377 * recursiveness quickly. */
6378 if (!recursive)
6379 tv_equal_recurse_limit = 1000;
6380 if (recursive_cnt >= tv_equal_recurse_limit)
6381 {
6382 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006383 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006384 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006385
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02006386 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
6387 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02006388 if ((tv1->v_type == VAR_FUNC
6389 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
6390 && (tv2->v_type == VAR_FUNC
6391 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
6392 {
6393 ++recursive_cnt;
6394 r = func_equal(tv1, tv2, ic);
6395 --recursive_cnt;
6396 return r;
6397 }
6398
6399 if (tv1->v_type != tv2->v_type)
6400 return FALSE;
6401
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006402 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006403 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006404 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006405 ++recursive_cnt;
6406 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6407 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006408 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006409
6410 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006411 ++recursive_cnt;
6412 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6413 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006414 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006415
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006416 case VAR_NUMBER:
6417 return tv1->vval.v_number == tv2->vval.v_number;
6418
6419 case VAR_STRING:
6420 s1 = get_tv_string_buf(tv1, buf1);
6421 s2 = get_tv_string_buf(tv2, buf2);
6422 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006423
6424 case VAR_SPECIAL:
6425 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006426
6427 case VAR_FLOAT:
6428#ifdef FEAT_FLOAT
6429 return tv1->vval.v_float == tv2->vval.v_float;
6430#endif
6431 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006432#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006433 return tv1->vval.v_job == tv2->vval.v_job;
6434#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006435 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006436#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006437 return tv1->vval.v_channel == tv2->vval.v_channel;
6438#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006439 case VAR_FUNC:
6440 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006441 case VAR_UNKNOWN:
6442 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006443 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006444
Bram Moolenaara03f2332016-02-06 18:09:59 +01006445 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6446 * does not equal anything, not even itself. */
6447 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006448}
6449
6450/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006451 * Locate item with index "n" in list "l" and return it.
6452 * A negative index is counted from the end; -1 is the last item.
6453 * Returns NULL when "n" is out of range.
6454 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006455 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006456list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006457{
Bram Moolenaar33570922005-01-25 22:26:29 +00006458 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006459 long idx;
6460
6461 if (l == NULL)
6462 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006463
6464 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006465 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006466 n = l->lv_len + n;
6467
6468 /* Check for index out of range. */
6469 if (n < 0 || n >= l->lv_len)
6470 return NULL;
6471
6472 /* When there is a cached index may start search from there. */
6473 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006474 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006475 if (n < l->lv_idx / 2)
6476 {
6477 /* closest to the start of the list */
6478 item = l->lv_first;
6479 idx = 0;
6480 }
6481 else if (n > (l->lv_idx + l->lv_len) / 2)
6482 {
6483 /* closest to the end of the list */
6484 item = l->lv_last;
6485 idx = l->lv_len - 1;
6486 }
6487 else
6488 {
6489 /* closest to the cached index */
6490 item = l->lv_idx_item;
6491 idx = l->lv_idx;
6492 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006493 }
6494 else
6495 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006496 if (n < l->lv_len / 2)
6497 {
6498 /* closest to the start of the list */
6499 item = l->lv_first;
6500 idx = 0;
6501 }
6502 else
6503 {
6504 /* closest to the end of the list */
6505 item = l->lv_last;
6506 idx = l->lv_len - 1;
6507 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006508 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006509
6510 while (n > idx)
6511 {
6512 /* search forward */
6513 item = item->li_next;
6514 ++idx;
6515 }
6516 while (n < idx)
6517 {
6518 /* search backward */
6519 item = item->li_prev;
6520 --idx;
6521 }
6522
6523 /* cache the used index */
6524 l->lv_idx = idx;
6525 l->lv_idx_item = item;
6526
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006527 return item;
6528}
6529
6530/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006531 * Get list item "l[idx]" as a number.
6532 */
6533 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006534list_find_nr(
6535 list_T *l,
6536 long idx,
6537 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006538{
6539 listitem_T *li;
6540
6541 li = list_find(l, idx);
6542 if (li == NULL)
6543 {
6544 if (errorp != NULL)
6545 *errorp = TRUE;
6546 return -1L;
6547 }
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006548 return (long)get_tv_number_chk(&li->li_tv, errorp);
Bram Moolenaara5525202006-03-02 22:52:09 +00006549}
6550
6551/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006552 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6553 */
6554 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006555list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006556{
6557 listitem_T *li;
6558
6559 li = list_find(l, idx - 1);
6560 if (li == NULL)
6561 {
6562 EMSGN(_(e_listidx), idx);
6563 return NULL;
6564 }
6565 return get_tv_string(&li->li_tv);
6566}
6567
6568/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006569 * Locate "item" list "l" and return its index.
6570 * Returns -1 when "item" is not in the list.
6571 */
6572 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006573list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006574{
6575 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006576 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006577
6578 if (l == NULL)
6579 return -1;
6580 idx = 0;
6581 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6582 ++idx;
6583 if (li == NULL)
6584 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006585 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006586}
6587
6588/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006589 * Append item "item" to the end of list "l".
6590 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006591 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006592list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006593{
6594 if (l->lv_last == NULL)
6595 {
6596 /* empty list */
6597 l->lv_first = item;
6598 l->lv_last = item;
6599 item->li_prev = NULL;
6600 }
6601 else
6602 {
6603 l->lv_last->li_next = item;
6604 item->li_prev = l->lv_last;
6605 l->lv_last = item;
6606 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006607 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006608 item->li_next = NULL;
6609}
6610
6611/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006612 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006613 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006614 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006615 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006616list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006617{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006618 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006619
Bram Moolenaar05159a02005-02-26 23:04:13 +00006620 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006621 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006622 copy_tv(tv, &li->li_tv);
6623 list_append(l, li);
6624 return OK;
6625}
6626
6627/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006628 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006629 * Return FAIL when out of memory.
6630 */
6631 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006632list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006633{
6634 listitem_T *li = listitem_alloc();
6635
6636 if (li == NULL)
6637 return FAIL;
6638 li->li_tv.v_type = VAR_DICT;
6639 li->li_tv.v_lock = 0;
6640 li->li_tv.vval.v_dict = dict;
6641 list_append(list, li);
6642 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006643 return OK;
6644}
6645
6646/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006647 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006648 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006649 * Returns FAIL when out of memory.
6650 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006651 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006652list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006653{
6654 listitem_T *li = listitem_alloc();
6655
6656 if (li == NULL)
6657 return FAIL;
6658 list_append(l, li);
6659 li->li_tv.v_type = VAR_STRING;
6660 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006661 if (str == NULL)
6662 li->li_tv.vval.v_string = NULL;
6663 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006664 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006665 return FAIL;
6666 return OK;
6667}
6668
6669/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006670 * Append "n" to list "l".
6671 * Returns FAIL when out of memory.
6672 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006673 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006674list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006675{
6676 listitem_T *li;
6677
6678 li = listitem_alloc();
6679 if (li == NULL)
6680 return FAIL;
6681 li->li_tv.v_type = VAR_NUMBER;
6682 li->li_tv.v_lock = 0;
6683 li->li_tv.vval.v_number = n;
6684 list_append(l, li);
6685 return OK;
6686}
6687
6688/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006689 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006690 * If "item" is NULL append at the end.
6691 * Return FAIL when out of memory.
6692 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006693 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006694list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006695{
Bram Moolenaar33570922005-01-25 22:26:29 +00006696 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006697
6698 if (ni == NULL)
6699 return FAIL;
6700 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006701 list_insert(l, ni, item);
6702 return OK;
6703}
6704
6705 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006706list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006707{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006708 if (item == NULL)
6709 /* Append new item at end of list. */
6710 list_append(l, ni);
6711 else
6712 {
6713 /* Insert new item before existing item. */
6714 ni->li_prev = item->li_prev;
6715 ni->li_next = item;
6716 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006717 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006718 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006719 ++l->lv_idx;
6720 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006721 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006722 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006723 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006724 l->lv_idx_item = NULL;
6725 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006726 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006727 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006728 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006729}
6730
6731/*
6732 * Extend "l1" with "l2".
6733 * If "bef" is NULL append at the end, otherwise insert before this item.
6734 * Returns FAIL when out of memory.
6735 */
6736 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006737list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006738{
Bram Moolenaar33570922005-01-25 22:26:29 +00006739 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006740 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006741
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006742 /* We also quit the loop when we have inserted the original item count of
6743 * the list, avoid a hang when we extend a list with itself. */
6744 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006745 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6746 return FAIL;
6747 return OK;
6748}
6749
6750/*
6751 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6752 * Return FAIL when out of memory.
6753 */
6754 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006755list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006756{
Bram Moolenaar33570922005-01-25 22:26:29 +00006757 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006758
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006759 if (l1 == NULL || l2 == NULL)
6760 return FAIL;
6761
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006762 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006763 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006764 if (l == NULL)
6765 return FAIL;
6766 tv->v_type = VAR_LIST;
6767 tv->vval.v_list = l;
6768
6769 /* append all items from the second list */
6770 return list_extend(l, l2, NULL);
6771}
6772
6773/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006774 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006775 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006776 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006777 * Returns NULL when out of memory.
6778 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006779 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006780list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006781{
Bram Moolenaar33570922005-01-25 22:26:29 +00006782 list_T *copy;
6783 listitem_T *item;
6784 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006785
6786 if (orig == NULL)
6787 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006788
6789 copy = list_alloc();
6790 if (copy != NULL)
6791 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006792 if (copyID != 0)
6793 {
6794 /* Do this before adding the items, because one of the items may
6795 * refer back to this list. */
6796 orig->lv_copyID = copyID;
6797 orig->lv_copylist = copy;
6798 }
6799 for (item = orig->lv_first; item != NULL && !got_int;
6800 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006801 {
6802 ni = listitem_alloc();
6803 if (ni == NULL)
6804 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006805 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006806 {
6807 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6808 {
6809 vim_free(ni);
6810 break;
6811 }
6812 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006813 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006814 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006815 list_append(copy, ni);
6816 }
6817 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006818 if (item != NULL)
6819 {
6820 list_unref(copy);
6821 copy = NULL;
6822 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006823 }
6824
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006825 return copy;
6826}
6827
6828/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006829 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006830 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006831 * This used to be called list_remove, but that conflicts with a Sun header
6832 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006833 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006834 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006835vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006836{
Bram Moolenaar33570922005-01-25 22:26:29 +00006837 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006838
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006839 /* notify watchers */
6840 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006841 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006842 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006843 list_fix_watch(l, ip);
6844 if (ip == item2)
6845 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006846 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006847
6848 if (item2->li_next == NULL)
6849 l->lv_last = item->li_prev;
6850 else
6851 item2->li_next->li_prev = item->li_prev;
6852 if (item->li_prev == NULL)
6853 l->lv_first = item2->li_next;
6854 else
6855 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006856 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006857}
6858
6859/*
6860 * Return an allocated string with the string representation of a list.
6861 * May return NULL.
6862 */
6863 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006864list2string(typval_T *tv, int copyID, int restore_copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006865{
6866 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006867
6868 if (tv->vval.v_list == NULL)
6869 return NULL;
6870 ga_init2(&ga, (int)sizeof(char), 80);
6871 ga_append(&ga, '[');
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006872 if (list_join(&ga, tv->vval.v_list, (char_u *)", ",
6873 FALSE, restore_copyID, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006874 {
6875 vim_free(ga.ga_data);
6876 return NULL;
6877 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006878 ga_append(&ga, ']');
6879 ga_append(&ga, NUL);
6880 return (char_u *)ga.ga_data;
6881}
6882
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006883typedef struct join_S {
6884 char_u *s;
6885 char_u *tofree;
6886} join_T;
6887
6888 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006889list_join_inner(
6890 garray_T *gap, /* to store the result in */
6891 list_T *l,
6892 char_u *sep,
6893 int echo_style,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006894 int restore_copyID,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006895 int copyID,
6896 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006897{
6898 int i;
6899 join_T *p;
6900 int len;
6901 int sumlen = 0;
6902 int first = TRUE;
6903 char_u *tofree;
6904 char_u numbuf[NUMBUFLEN];
6905 listitem_T *item;
6906 char_u *s;
6907
6908 /* Stringify each item in the list. */
6909 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6910 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006911 s = echo_string_core(&item->li_tv, &tofree, numbuf, copyID,
6912 echo_style, restore_copyID, FALSE);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006913 if (s == NULL)
6914 return FAIL;
6915
6916 len = (int)STRLEN(s);
6917 sumlen += len;
6918
Bram Moolenaarcde88542015-08-11 19:14:00 +02006919 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006920 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6921 if (tofree != NULL || s != numbuf)
6922 {
6923 p->s = s;
6924 p->tofree = tofree;
6925 }
6926 else
6927 {
6928 p->s = vim_strnsave(s, len);
6929 p->tofree = p->s;
6930 }
6931
6932 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006933 if (did_echo_string_emsg) /* recursion error, bail out */
6934 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006935 }
6936
6937 /* Allocate result buffer with its total size, avoid re-allocation and
6938 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6939 if (join_gap->ga_len >= 2)
6940 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6941 if (ga_grow(gap, sumlen + 2) == FAIL)
6942 return FAIL;
6943
6944 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6945 {
6946 if (first)
6947 first = FALSE;
6948 else
6949 ga_concat(gap, sep);
6950 p = ((join_T *)join_gap->ga_data) + i;
6951
6952 if (p->s != NULL)
6953 ga_concat(gap, p->s);
6954 line_breakcheck();
6955 }
6956
6957 return OK;
6958}
6959
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006960/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006961 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006962 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006963 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006964 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006965 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006966list_join(
6967 garray_T *gap,
6968 list_T *l,
6969 char_u *sep,
6970 int echo_style,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006971 int restore_copyID,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006972 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006973{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006974 garray_T join_ga;
6975 int retval;
6976 join_T *p;
6977 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006978
Bram Moolenaard39a7512015-04-16 22:51:22 +02006979 if (l->lv_len < 1)
6980 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006981 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006982 retval = list_join_inner(gap, l, sep, echo_style, restore_copyID,
6983 copyID, &join_ga);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006984
6985 /* Dispose each item in join_ga. */
6986 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006987 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006988 p = (join_T *)join_ga.ga_data;
6989 for (i = 0; i < join_ga.ga_len; ++i)
6990 {
6991 vim_free(p->tofree);
6992 ++p;
6993 }
6994 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006995 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006996
6997 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006998}
6999
7000/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007001 * Return the next (unique) copy ID.
7002 * Used for serializing nested structures.
7003 */
7004 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007005get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007006{
7007 current_copyID += COPYID_INC;
7008 return current_copyID;
7009}
7010
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007011/* Used by get_func_tv() */
7012static garray_T funcargs = GA_EMPTY;
7013
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007014/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007015 * Garbage collection for lists and dictionaries.
7016 *
7017 * We use reference counts to be able to free most items right away when they
7018 * are no longer used. But for composite items it's possible that it becomes
7019 * unused while the reference count is > 0: When there is a recursive
7020 * reference. Example:
7021 * :let l = [1, 2, 3]
7022 * :let d = {9: l}
7023 * :let l[1] = d
7024 *
7025 * Since this is quite unusual we handle this with garbage collection: every
7026 * once in a while find out which lists and dicts are not referenced from any
7027 * variable.
7028 *
7029 * Here is a good reference text about garbage collection (refers to Python
7030 * but it applies to all reference-counting mechanisms):
7031 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00007032 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007033
7034/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007035 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02007036 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007037 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00007038 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007039 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007040garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007041{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007042 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007043 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007044 buf_T *buf;
7045 win_T *wp;
7046 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007047 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01007048 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007049 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007050#ifdef FEAT_WINDOWS
7051 tabpage_T *tp;
7052#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007053
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007054 if (!testing)
7055 {
7056 /* Only do this once. */
7057 want_garbage_collect = FALSE;
7058 may_garbage_collect = FALSE;
7059 garbage_collect_at_exit = FALSE;
7060 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00007061
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007062 /* We advance by two because we add one for items referenced through
7063 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007064 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007065
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007066 /*
7067 * 1. Go through all accessible variables and mark all lists and dicts
7068 * with copyID.
7069 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007070
7071 /* Don't free variables in the previous_funccal list unless they are only
7072 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00007073 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007074 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
7075 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007076 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
7077 NULL);
7078 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
7079 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007080 }
7081
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007082 /* script-local variables */
7083 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007084 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007085
7086 /* buffer-local variables */
7087 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007088 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
7089 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007090
7091 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007092 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007093 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
7094 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02007095#ifdef FEAT_AUTOCMD
7096 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007097 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
7098 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02007099#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007100
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007101#ifdef FEAT_WINDOWS
7102 /* tabpage-local variables */
7103 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007104 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
7105 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007106#endif
7107
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007108 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007109 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007110
7111 /* function-local variables */
7112 for (fc = current_funccal; fc != NULL; fc = fc->caller)
7113 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007114 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
7115 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007116 }
7117
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007118 /* function call arguments, if v:testing is set. */
7119 for (i = 0; i < funcargs.ga_len; ++i)
7120 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7121 copyID, NULL, NULL);
7122
Bram Moolenaard812df62008-11-09 12:46:09 +00007123 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007124 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00007125
Bram Moolenaar1dced572012-04-05 16:54:08 +02007126#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007127 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02007128#endif
7129
Bram Moolenaardb913952012-06-29 12:54:53 +02007130#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007131 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007132#endif
7133
7134#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007135 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007136#endif
7137
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007138#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02007139 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02007140 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007141#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02007142#ifdef FEAT_NETBEANS_INTG
7143 abort = abort || set_ref_in_nb_channel(copyID);
7144#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007145
Bram Moolenaare3188e22016-05-31 21:13:04 +02007146#ifdef FEAT_TIMERS
7147 abort = abort || set_ref_in_timer(copyID);
7148#endif
7149
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007150 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007151 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007152 /*
7153 * 2. Free lists and dictionaries that are not referenced.
7154 */
7155 did_free = free_unref_items(copyID);
7156
7157 /*
7158 * 3. Check if any funccal can be freed now.
7159 */
7160 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007161 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007162 if (can_free_funccal(*pfc, copyID))
7163 {
7164 fc = *pfc;
7165 *pfc = fc->caller;
7166 free_funccal(fc, TRUE);
7167 did_free = TRUE;
7168 did_free_funccal = TRUE;
7169 }
7170 else
7171 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007172 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007173 if (did_free_funccal)
7174 /* When a funccal was freed some more items might be garbage
7175 * collected, so run again. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007176 (void)garbage_collect(testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007177 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007178 else if (p_verbose > 0)
7179 {
7180 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
7181 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007182
7183 return did_free;
7184}
7185
7186/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007187 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007188 */
7189 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007190free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007191{
Bram Moolenaare71eea82015-02-03 17:10:06 +01007192 dict_T *dd, *dd_next;
7193 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007194 int did_free = FALSE;
7195
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007196 /* Let all "free" functions know that we are here. This means no
7197 * dictionaries, lists, channels or jobs are to be freed, because we will
7198 * do that here. */
7199 in_free_unref_items = TRUE;
7200
7201 /*
7202 * PASS 1: free the contents of the items. We don't free the items
7203 * themselves yet, so that it is possible to decrement refcount counters
7204 */
7205
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007206 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007207 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007208 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007209 for (dd = first_dict; dd != NULL; dd = dd->dv_used_next)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007210 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007211 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007212 /* Free the Dictionary and ordinary items it contains, but don't
7213 * recurse into Lists and Dictionaries, they will be in the list
7214 * of dicts or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007215 dict_free_contents(dd);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007216 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007217 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007218
7219 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007220 * Go through the list of lists and free items without the copyID.
7221 * But don't free a list that has a watcher (used in a for loop), these
7222 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007223 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007224 for (ll = first_list; ll != NULL; ll = ll->lv_used_next)
7225 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7226 && ll->lv_watch == NULL)
7227 {
7228 /* Free the List and ordinary items it contains, but don't recurse
7229 * into Lists and Dictionaries, they will be in the list of dicts
7230 * or list of lists. */
7231 list_free_contents(ll);
7232 did_free = TRUE;
7233 }
7234
7235#ifdef FEAT_JOB_CHANNEL
7236 /* Go through the list of jobs and free items without the copyID. This
7237 * must happen before doing channels, because jobs refer to channels, but
7238 * the reference from the channel to the job isn't tracked. */
7239 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
7240
7241 /* Go through the list of channels and free items without the copyID. */
7242 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
7243#endif
7244
7245 /*
7246 * PASS 2: free the items themselves.
7247 */
7248 for (dd = first_dict; dd != NULL; dd = dd_next)
7249 {
7250 dd_next = dd->dv_used_next;
7251 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
7252 dict_free_dict(dd);
7253 }
7254
7255 for (ll = first_list; ll != NULL; ll = ll_next)
Bram Moolenaare71eea82015-02-03 17:10:06 +01007256 {
7257 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007258 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7259 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007260 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007261 /* Free the List and ordinary items it contains, but don't recurse
7262 * into Lists and Dictionaries, they will be in the list of dicts
7263 * or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007264 list_free_list(ll);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007265 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007266 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007267
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007268#ifdef FEAT_JOB_CHANNEL
7269 /* Go through the list of jobs and free items without the copyID. This
7270 * must happen before doing channels, because jobs refer to channels, but
7271 * the reference from the channel to the job isn't tracked. */
7272 free_unused_jobs(copyID, COPYID_MASK);
7273
7274 /* Go through the list of channels and free items without the copyID. */
7275 free_unused_channels(copyID, COPYID_MASK);
7276#endif
7277
7278 in_free_unref_items = FALSE;
7279
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007280 return did_free;
7281}
7282
7283/*
7284 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007285 * "list_stack" is used to add lists to be marked. Can be NULL.
7286 *
7287 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007288 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007289 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007290set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007291{
7292 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007293 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007294 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007295 hashtab_T *cur_ht;
7296 ht_stack_T *ht_stack = NULL;
7297 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007298
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007299 cur_ht = ht;
7300 for (;;)
7301 {
7302 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007303 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007304 /* Mark each item in the hashtab. If the item contains a hashtab
7305 * it is added to ht_stack, if it contains a list it is added to
7306 * list_stack. */
7307 todo = (int)cur_ht->ht_used;
7308 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7309 if (!HASHITEM_EMPTY(hi))
7310 {
7311 --todo;
7312 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7313 &ht_stack, list_stack);
7314 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007315 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007316
7317 if (ht_stack == NULL)
7318 break;
7319
7320 /* take an item from the stack */
7321 cur_ht = ht_stack->ht;
7322 tempitem = ht_stack;
7323 ht_stack = ht_stack->prev;
7324 free(tempitem);
7325 }
7326
7327 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007328}
7329
7330/*
7331 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007332 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7333 *
7334 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007335 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007336 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007337set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007338{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007339 listitem_T *li;
7340 int abort = FALSE;
7341 list_T *cur_l;
7342 list_stack_T *list_stack = NULL;
7343 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007344
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007345 cur_l = l;
7346 for (;;)
7347 {
7348 if (!abort)
7349 /* Mark each item in the list. If the item contains a hashtab
7350 * it is added to ht_stack, if it contains a list it is added to
7351 * list_stack. */
7352 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7353 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7354 ht_stack, &list_stack);
7355 if (list_stack == NULL)
7356 break;
7357
7358 /* take an item from the stack */
7359 cur_l = list_stack->list;
7360 tempitem = list_stack;
7361 list_stack = list_stack->prev;
7362 free(tempitem);
7363 }
7364
7365 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007366}
7367
7368/*
7369 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007370 * "list_stack" is used to add lists to be marked. Can be NULL.
7371 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7372 *
7373 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007374 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007375 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007376set_ref_in_item(
7377 typval_T *tv,
7378 int copyID,
7379 ht_stack_T **ht_stack,
7380 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007381{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007382 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007383
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007384 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007385 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007386 dict_T *dd = tv->vval.v_dict;
7387
Bram Moolenaara03f2332016-02-06 18:09:59 +01007388 if (dd != NULL && dd->dv_copyID != copyID)
7389 {
7390 /* Didn't see this dict yet. */
7391 dd->dv_copyID = copyID;
7392 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007393 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007394 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7395 }
7396 else
7397 {
7398 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7399 if (newitem == NULL)
7400 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007401 else
7402 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007403 newitem->ht = &dd->dv_hashtab;
7404 newitem->prev = *ht_stack;
7405 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007406 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007407 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007408 }
7409 }
7410 else if (tv->v_type == VAR_LIST)
7411 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007412 list_T *ll = tv->vval.v_list;
7413
Bram Moolenaara03f2332016-02-06 18:09:59 +01007414 if (ll != NULL && ll->lv_copyID != copyID)
7415 {
7416 /* Didn't see this list yet. */
7417 ll->lv_copyID = copyID;
7418 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007419 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007420 abort = set_ref_in_list(ll, copyID, ht_stack);
7421 }
7422 else
7423 {
7424 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007425 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007426 if (newitem == NULL)
7427 abort = TRUE;
7428 else
7429 {
7430 newitem->list = ll;
7431 newitem->prev = *list_stack;
7432 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007433 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007434 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007435 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007436 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007437 else if (tv->v_type == VAR_PARTIAL)
7438 {
7439 partial_T *pt = tv->vval.v_partial;
7440 int i;
7441
7442 /* A partial does not have a copyID, because it cannot contain itself.
7443 */
7444 if (pt != NULL)
7445 {
7446 if (pt->pt_dict != NULL)
7447 {
7448 typval_T dtv;
7449
7450 dtv.v_type = VAR_DICT;
7451 dtv.vval.v_dict = pt->pt_dict;
7452 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7453 }
7454
7455 for (i = 0; i < pt->pt_argc; ++i)
7456 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
7457 ht_stack, list_stack);
7458 }
7459 }
7460#ifdef FEAT_JOB_CHANNEL
7461 else if (tv->v_type == VAR_JOB)
7462 {
7463 job_T *job = tv->vval.v_job;
7464 typval_T dtv;
7465
7466 if (job != NULL && job->jv_copyID != copyID)
7467 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007468 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007469 if (job->jv_channel != NULL)
7470 {
7471 dtv.v_type = VAR_CHANNEL;
7472 dtv.vval.v_channel = job->jv_channel;
7473 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7474 }
7475 if (job->jv_exit_partial != NULL)
7476 {
7477 dtv.v_type = VAR_PARTIAL;
7478 dtv.vval.v_partial = job->jv_exit_partial;
7479 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7480 }
7481 }
7482 }
7483 else if (tv->v_type == VAR_CHANNEL)
7484 {
7485 channel_T *ch =tv->vval.v_channel;
7486 int part;
7487 typval_T dtv;
7488 jsonq_T *jq;
7489 cbq_T *cq;
7490
7491 if (ch != NULL && ch->ch_copyID != copyID)
7492 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007493 ch->ch_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007494 for (part = PART_SOCK; part <= PART_IN; ++part)
7495 {
7496 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
7497 jq = jq->jq_next)
7498 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
7499 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
7500 cq = cq->cq_next)
7501 if (cq->cq_partial != NULL)
7502 {
7503 dtv.v_type = VAR_PARTIAL;
7504 dtv.vval.v_partial = cq->cq_partial;
7505 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7506 }
7507 if (ch->ch_part[part].ch_partial != NULL)
7508 {
7509 dtv.v_type = VAR_PARTIAL;
7510 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
7511 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7512 }
7513 }
7514 if (ch->ch_partial != NULL)
7515 {
7516 dtv.v_type = VAR_PARTIAL;
7517 dtv.vval.v_partial = ch->ch_partial;
7518 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7519 }
7520 if (ch->ch_close_partial != NULL)
7521 {
7522 dtv.v_type = VAR_PARTIAL;
7523 dtv.vval.v_partial = ch->ch_close_partial;
7524 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7525 }
7526 }
7527 }
7528#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007529 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007530}
7531
7532/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007533 * Allocate an empty header for a dictionary.
7534 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007535 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007536dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007537{
Bram Moolenaar33570922005-01-25 22:26:29 +00007538 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007539
Bram Moolenaar33570922005-01-25 22:26:29 +00007540 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007541 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007542 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007543 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007544 if (first_dict != NULL)
7545 first_dict->dv_used_prev = d;
7546 d->dv_used_next = first_dict;
7547 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007548 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007549
Bram Moolenaar33570922005-01-25 22:26:29 +00007550 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007551 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007552 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007553 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007554 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007555 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007556 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007557}
7558
7559/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007560 * Allocate an empty dict for a return value.
7561 * Returns OK or FAIL.
7562 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007563 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007564rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007565{
7566 dict_T *d = dict_alloc();
7567
7568 if (d == NULL)
7569 return FAIL;
7570
7571 rettv->vval.v_dict = d;
7572 rettv->v_type = VAR_DICT;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02007573 rettv->v_lock = 0;
Bram Moolenaara800b422010-06-27 01:15:55 +02007574 ++d->dv_refcount;
7575 return OK;
7576}
7577
7578
7579/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007580 * Unreference a Dictionary: decrement the reference count and free it when it
7581 * becomes zero.
7582 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007583 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007584dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007585{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007586 if (d != NULL && --d->dv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007587 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007588}
7589
7590/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007591 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007592 * Ignores the reference count.
7593 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007594 static void
7595dict_free_contents(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007596{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007597 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007598 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007599 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007600
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007601 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007602 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007603 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007604 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007605 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007606 if (!HASHITEM_EMPTY(hi))
7607 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007608 /* Remove the item before deleting it, just in case there is
7609 * something recursive causing trouble. */
7610 di = HI2DI(hi);
7611 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007612 clear_tv(&di->di_tv);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007613 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007614 --todo;
7615 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007616 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007617 hash_clear(&d->dv_hashtab);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007618}
7619
7620 static void
7621dict_free_dict(dict_T *d)
7622{
7623 /* Remove the dict from the list of dicts for garbage collection. */
7624 if (d->dv_used_prev == NULL)
7625 first_dict = d->dv_used_next;
7626 else
7627 d->dv_used_prev->dv_used_next = d->dv_used_next;
7628 if (d->dv_used_next != NULL)
7629 d->dv_used_next->dv_used_prev = d->dv_used_prev;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007630 vim_free(d);
7631}
7632
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007633 void
7634dict_free(dict_T *d)
7635{
7636 if (!in_free_unref_items)
7637 {
7638 dict_free_contents(d);
7639 dict_free_dict(d);
7640 }
7641}
7642
Bram Moolenaar8c711452005-01-14 21:53:12 +00007643/*
7644 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007645 * The "key" is copied to the new item.
7646 * Note that the value of the item "di_tv" still needs to be initialized!
7647 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007648 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007649 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007650dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007651{
Bram Moolenaar33570922005-01-25 22:26:29 +00007652 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007653
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007654 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007655 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007656 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007657 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007658 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007659 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007660 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007661}
7662
7663/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007664 * Make a copy of a Dictionary item.
7665 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007666 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007667dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007668{
Bram Moolenaar33570922005-01-25 22:26:29 +00007669 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007670
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007671 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7672 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007673 if (di != NULL)
7674 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007675 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007676 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007677 copy_tv(&org->di_tv, &di->di_tv);
7678 }
7679 return di;
7680}
7681
7682/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007683 * Remove item "item" from Dictionary "dict" and free it.
7684 */
7685 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007686dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007687{
Bram Moolenaar33570922005-01-25 22:26:29 +00007688 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007689
Bram Moolenaar33570922005-01-25 22:26:29 +00007690 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007691 if (HASHITEM_EMPTY(hi))
7692 EMSG2(_(e_intern2), "dictitem_remove()");
7693 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007694 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007695 dictitem_free(item);
7696}
7697
7698/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007699 * Free a dict item. Also clears the value.
7700 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007701 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007702dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007703{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007704 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007705 if (item->di_flags & DI_FLAGS_ALLOC)
7706 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007707}
7708
7709/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007710 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7711 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007712 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007713 * Returns NULL when out of memory.
7714 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007715 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007716dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007717{
Bram Moolenaar33570922005-01-25 22:26:29 +00007718 dict_T *copy;
7719 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007720 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007721 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007722
7723 if (orig == NULL)
7724 return NULL;
7725
7726 copy = dict_alloc();
7727 if (copy != NULL)
7728 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007729 if (copyID != 0)
7730 {
7731 orig->dv_copyID = copyID;
7732 orig->dv_copydict = copy;
7733 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007734 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007735 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007736 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007737 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007738 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007739 --todo;
7740
7741 di = dictitem_alloc(hi->hi_key);
7742 if (di == NULL)
7743 break;
7744 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007745 {
7746 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7747 copyID) == FAIL)
7748 {
7749 vim_free(di);
7750 break;
7751 }
7752 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007753 else
7754 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7755 if (dict_add(copy, di) == FAIL)
7756 {
7757 dictitem_free(di);
7758 break;
7759 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007760 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007761 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007762
Bram Moolenaare9a41262005-01-15 22:18:47 +00007763 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007764 if (todo > 0)
7765 {
7766 dict_unref(copy);
7767 copy = NULL;
7768 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007769 }
7770
7771 return copy;
7772}
7773
7774/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007775 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007776 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007777 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007778 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007779dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007780{
Bram Moolenaar33570922005-01-25 22:26:29 +00007781 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007782}
7783
Bram Moolenaar8c711452005-01-14 21:53:12 +00007784/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007785 * Add a number or string entry to dictionary "d".
7786 * When "str" is NULL use number "nr", otherwise use "str".
7787 * Returns FAIL when out of memory and when key already exists.
7788 */
7789 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007790dict_add_nr_str(
7791 dict_T *d,
7792 char *key,
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007793 varnumber_T nr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007794 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007795{
7796 dictitem_T *item;
7797
7798 item = dictitem_alloc((char_u *)key);
7799 if (item == NULL)
7800 return FAIL;
7801 item->di_tv.v_lock = 0;
7802 if (str == NULL)
7803 {
7804 item->di_tv.v_type = VAR_NUMBER;
7805 item->di_tv.vval.v_number = nr;
7806 }
7807 else
7808 {
7809 item->di_tv.v_type = VAR_STRING;
7810 item->di_tv.vval.v_string = vim_strsave(str);
7811 }
7812 if (dict_add(d, item) == FAIL)
7813 {
7814 dictitem_free(item);
7815 return FAIL;
7816 }
7817 return OK;
7818}
7819
7820/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007821 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007822 * Returns FAIL when out of memory and when key already exists.
7823 */
7824 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007825dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007826{
7827 dictitem_T *item;
7828
7829 item = dictitem_alloc((char_u *)key);
7830 if (item == NULL)
7831 return FAIL;
7832 item->di_tv.v_lock = 0;
7833 item->di_tv.v_type = VAR_LIST;
7834 item->di_tv.vval.v_list = list;
7835 if (dict_add(d, item) == FAIL)
7836 {
7837 dictitem_free(item);
7838 return FAIL;
7839 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007840 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007841 return OK;
7842}
7843
7844/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007845 * Get the number of items in a Dictionary.
7846 */
7847 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007848dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007849{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007850 if (d == NULL)
7851 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007852 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007853}
7854
7855/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007856 * Find item "key[len]" in Dictionary "d".
7857 * If "len" is negative use strlen(key).
7858 * Returns NULL when not found.
7859 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007860 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007861dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007862{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007863#define AKEYLEN 200
7864 char_u buf[AKEYLEN];
7865 char_u *akey;
7866 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007867 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007868
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +02007869 if (d == NULL)
7870 return NULL;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007871 if (len < 0)
7872 akey = key;
7873 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007874 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007875 tofree = akey = vim_strnsave(key, len);
7876 if (akey == NULL)
7877 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007878 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007879 else
7880 {
7881 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007882 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007883 akey = buf;
7884 }
7885
Bram Moolenaar33570922005-01-25 22:26:29 +00007886 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007887 vim_free(tofree);
7888 if (HASHITEM_EMPTY(hi))
7889 return NULL;
7890 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007891}
7892
7893/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007894 * Get a string item from a dictionary.
7895 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007896 * Returns NULL if the entry doesn't exist or out of memory.
7897 */
7898 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007899get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007900{
7901 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007902 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007903
7904 di = dict_find(d, key, -1);
7905 if (di == NULL)
7906 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007907 s = get_tv_string(&di->di_tv);
7908 if (save && s != NULL)
7909 s = vim_strsave(s);
7910 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007911}
7912
7913/*
7914 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007915 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007916 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007917 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01007918get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007919{
7920 dictitem_T *di;
7921
7922 di = dict_find(d, key, -1);
7923 if (di == NULL)
7924 return 0;
7925 return get_tv_number(&di->di_tv);
7926}
7927
7928/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007929 * Return an allocated string with the string representation of a Dictionary.
7930 * May return NULL.
7931 */
7932 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02007933dict2string(typval_T *tv, int copyID, int restore_copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007934{
7935 garray_T ga;
7936 int first = TRUE;
7937 char_u *tofree;
7938 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007939 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007940 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007941 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007942 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007943
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007944 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007945 return NULL;
7946 ga_init2(&ga, (int)sizeof(char), 80);
7947 ga_append(&ga, '{');
7948
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007949 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007950 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007951 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007952 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007953 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007954 --todo;
7955
7956 if (first)
7957 first = FALSE;
7958 else
7959 ga_concat(&ga, (char_u *)", ");
7960
7961 tofree = string_quote(hi->hi_key, FALSE);
7962 if (tofree != NULL)
7963 {
7964 ga_concat(&ga, tofree);
7965 vim_free(tofree);
7966 }
7967 ga_concat(&ga, (char_u *)": ");
Bram Moolenaar18dfb442016-05-31 22:31:23 +02007968 s = echo_string_core(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID,
7969 FALSE, restore_copyID, TRUE);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007970 if (s != NULL)
7971 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007972 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007973 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007974 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007975 line_breakcheck();
7976
Bram Moolenaar8c711452005-01-14 21:53:12 +00007977 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007978 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007979 if (todo > 0)
7980 {
7981 vim_free(ga.ga_data);
7982 return NULL;
7983 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007984
7985 ga_append(&ga, '}');
7986 ga_append(&ga, NUL);
7987 return (char_u *)ga.ga_data;
7988}
7989
7990/*
7991 * Allocate a variable for a Dictionary and fill it from "*arg".
7992 * Return OK or FAIL. Returns NOTDONE for {expr}.
7993 */
7994 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007995get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007996{
Bram Moolenaar33570922005-01-25 22:26:29 +00007997 dict_T *d = NULL;
7998 typval_T tvkey;
7999 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00008000 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00008001 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008002 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008003 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00008004
8005 /*
8006 * First check if it's not a curly-braces thing: {expr}.
8007 * Must do this without evaluating, otherwise a function may be called
8008 * twice. Unfortunately this means we need to call eval1() twice for the
8009 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008010 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008011 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008012 if (*start != '}')
8013 {
8014 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
8015 return FAIL;
8016 if (*start == '}')
8017 return NOTDONE;
8018 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008019
8020 if (evaluate)
8021 {
8022 d = dict_alloc();
8023 if (d == NULL)
8024 return FAIL;
8025 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008026 tvkey.v_type = VAR_UNKNOWN;
8027 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008028
8029 *arg = skipwhite(*arg + 1);
8030 while (**arg != '}' && **arg != NUL)
8031 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008032 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008033 goto failret;
8034 if (**arg != ':')
8035 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008036 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008037 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008038 goto failret;
8039 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00008040 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00008041 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00008042 key = get_tv_string_buf_chk(&tvkey, buf);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02008043 if (key == NULL)
Bram Moolenaar037cc642007-09-13 18:40:54 +00008044 {
8045 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
Bram Moolenaar037cc642007-09-13 18:40:54 +00008046 clear_tv(&tvkey);
8047 goto failret;
8048 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008049 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008050
8051 *arg = skipwhite(*arg + 1);
8052 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
8053 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00008054 if (evaluate)
8055 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008056 goto failret;
8057 }
8058 if (evaluate)
8059 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008060 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008061 if (item != NULL)
8062 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00008063 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008064 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008065 clear_tv(&tv);
8066 goto failret;
8067 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008068 item = dictitem_alloc(key);
8069 clear_tv(&tvkey);
8070 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00008071 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008072 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008073 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008074 if (dict_add(d, item) == FAIL)
8075 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008076 }
8077 }
8078
8079 if (**arg == '}')
8080 break;
8081 if (**arg != ',')
8082 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008083 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008084 goto failret;
8085 }
8086 *arg = skipwhite(*arg + 1);
8087 }
8088
8089 if (**arg != '}')
8090 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008091 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008092failret:
8093 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02008094 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008095 return FAIL;
8096 }
8097
8098 *arg = skipwhite(*arg + 1);
8099 if (evaluate)
8100 {
8101 rettv->v_type = VAR_DICT;
8102 rettv->vval.v_dict = d;
8103 ++d->dv_refcount;
8104 }
8105
8106 return OK;
8107}
8108
Bram Moolenaar17a13432016-01-24 14:22:10 +01008109 static char *
8110get_var_special_name(int nr)
8111{
8112 switch (nr)
8113 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01008114 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01008115 case VVAL_TRUE: return "v:true";
8116 case VVAL_NONE: return "v:none";
8117 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01008118 }
8119 EMSG2(_(e_intern2), "get_var_special_name()");
8120 return "42";
8121}
8122
Bram Moolenaar8c711452005-01-14 21:53:12 +00008123/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008124 * Return a string with the string representation of a variable.
8125 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008126 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008127 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008128 * When both "echo_style" and "dict_val" are FALSE, put quotes around stings as
8129 * "string()", otherwise does not put quotes around strings, as ":echo"
8130 * displays values.
8131 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
8132 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008133 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008134 */
8135 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008136echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01008137 typval_T *tv,
8138 char_u **tofree,
8139 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008140 int copyID,
8141 int echo_style,
8142 int restore_copyID,
8143 int dict_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008144{
Bram Moolenaare9a41262005-01-15 22:18:47 +00008145 static int recurse = 0;
8146 char_u *r = NULL;
8147
Bram Moolenaar33570922005-01-25 22:26:29 +00008148 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008149 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02008150 if (!did_echo_string_emsg)
8151 {
8152 /* Only give this message once for a recursive call to avoid
8153 * flooding the user with errors. And stop iterating over lists
8154 * and dicts. */
8155 did_echo_string_emsg = TRUE;
8156 EMSG(_("E724: variable nested too deep for displaying"));
8157 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008158 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02008159 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00008160 }
8161 ++recurse;
8162
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008163 switch (tv->v_type)
8164 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008165 case VAR_STRING:
8166 if (echo_style && !dict_val)
8167 {
8168 *tofree = NULL;
8169 r = get_tv_string_buf(tv, numbuf);
8170 }
8171 else
8172 {
8173 *tofree = string_quote(tv->vval.v_string, FALSE);
8174 r = *tofree;
8175 }
8176 break;
8177
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008178 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008179 if (echo_style)
8180 {
8181 *tofree = NULL;
8182 r = tv->vval.v_string;
8183 }
8184 else
8185 {
8186 *tofree = string_quote(tv->vval.v_string, TRUE);
8187 r = *tofree;
8188 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008189 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008190
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008191 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008192 {
8193 partial_T *pt = tv->vval.v_partial;
8194 char_u *fname = string_quote(pt == NULL ? NULL
8195 : pt->pt_name, FALSE);
8196 garray_T ga;
8197 int i;
8198 char_u *tf;
8199
8200 ga_init2(&ga, 1, 100);
8201 ga_concat(&ga, (char_u *)"function(");
8202 if (fname != NULL)
8203 {
8204 ga_concat(&ga, fname);
8205 vim_free(fname);
8206 }
8207 if (pt != NULL && pt->pt_argc > 0)
8208 {
8209 ga_concat(&ga, (char_u *)", [");
8210 for (i = 0; i < pt->pt_argc; ++i)
8211 {
8212 if (i > 0)
8213 ga_concat(&ga, (char_u *)", ");
8214 ga_concat(&ga,
8215 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
8216 vim_free(tf);
8217 }
8218 ga_concat(&ga, (char_u *)"]");
8219 }
8220 if (pt != NULL && pt->pt_dict != NULL)
8221 {
8222 typval_T dtv;
8223
8224 ga_concat(&ga, (char_u *)", ");
8225 dtv.v_type = VAR_DICT;
8226 dtv.vval.v_dict = pt->pt_dict;
8227 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
8228 vim_free(tf);
8229 }
8230 ga_concat(&ga, (char_u *)")");
8231
8232 *tofree = ga.ga_data;
8233 r = *tofree;
8234 break;
8235 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008236
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008237 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008238 if (tv->vval.v_list == NULL)
8239 {
8240 *tofree = NULL;
8241 r = NULL;
8242 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008243 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
8244 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008245 {
8246 *tofree = NULL;
8247 r = (char_u *)"[...]";
8248 }
8249 else
8250 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008251 int old_copyID = tv->vval.v_list->lv_copyID;
8252
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008253 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008254 *tofree = list2string(tv, copyID, restore_copyID);
8255 if (restore_copyID)
8256 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008257 r = *tofree;
8258 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008259 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008260
Bram Moolenaar8c711452005-01-14 21:53:12 +00008261 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008262 if (tv->vval.v_dict == NULL)
8263 {
8264 *tofree = NULL;
8265 r = NULL;
8266 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008267 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
8268 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008269 {
8270 *tofree = NULL;
8271 r = (char_u *)"{...}";
8272 }
8273 else
8274 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008275 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008276 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008277 *tofree = dict2string(tv, copyID, restore_copyID);
8278 if (restore_copyID)
8279 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008280 r = *tofree;
8281 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008282 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008283
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008284 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008285 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008286 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008287 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008288 *tofree = NULL;
8289 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008290 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008291
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008292 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008293#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008294 *tofree = NULL;
8295 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
8296 r = numbuf;
8297 break;
8298#endif
8299
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008300 case VAR_SPECIAL:
8301 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01008302 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008303 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008304 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008305
Bram Moolenaar8502c702014-06-17 12:51:16 +02008306 if (--recurse == 0)
8307 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008308 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008309}
8310
8311/*
8312 * Return a string with the string representation of a variable.
8313 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8314 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008315 * Does not put quotes around strings, as ":echo" displays values.
8316 * When "copyID" is not NULL replace recursive lists and dicts with "...".
8317 * May return NULL.
8318 */
8319 static char_u *
8320echo_string(
8321 typval_T *tv,
8322 char_u **tofree,
8323 char_u *numbuf,
8324 int copyID)
8325{
8326 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
8327}
8328
8329/*
8330 * Return a string with the string representation of a variable.
8331 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8332 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008333 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008334 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008335 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02008336 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008337tv2string(
8338 typval_T *tv,
8339 char_u **tofree,
8340 char_u *numbuf,
8341 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008342{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008343 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008344}
8345
8346/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008347 * Return string "str" in ' quotes, doubling ' characters.
8348 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008349 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008350 */
8351 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008352string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008353{
Bram Moolenaar33570922005-01-25 22:26:29 +00008354 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008355 char_u *p, *r, *s;
8356
Bram Moolenaar33570922005-01-25 22:26:29 +00008357 len = (function ? 13 : 3);
8358 if (str != NULL)
8359 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008360 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008361 for (p = str; *p != NUL; mb_ptr_adv(p))
8362 if (*p == '\'')
8363 ++len;
8364 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008365 s = r = alloc(len);
8366 if (r != NULL)
8367 {
8368 if (function)
8369 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008370 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008371 r += 10;
8372 }
8373 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008374 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008375 if (str != NULL)
8376 for (p = str; *p != NUL; )
8377 {
8378 if (*p == '\'')
8379 *r++ = '\'';
8380 MB_COPY_CHAR(p, r);
8381 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008382 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008383 if (function)
8384 *r++ = ')';
8385 *r++ = NUL;
8386 }
8387 return s;
8388}
8389
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008390#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008391/*
8392 * Convert the string "text" to a floating point number.
8393 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8394 * this always uses a decimal point.
8395 * Returns the length of the text that was consumed.
8396 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008397 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008398string2float(
8399 char_u *text,
8400 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008401{
8402 char *s = (char *)text;
8403 float_T f;
8404
8405 f = strtod(s, &s);
8406 *value = f;
8407 return (int)((char_u *)s - text);
8408}
8409#endif
8410
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008411/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 * Get the value of an environment variable.
8413 * "arg" is pointing to the '$'. It is advanced to after the name.
8414 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008415 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 */
8417 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008418get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419{
8420 char_u *string = NULL;
8421 int len;
8422 int cc;
8423 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008424 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425
8426 ++*arg;
8427 name = *arg;
8428 len = get_env_len(arg);
8429 if (evaluate)
8430 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008431 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008432 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008433
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008434 cc = name[len];
8435 name[len] = NUL;
8436 /* first try vim_getenv(), fast for normal environment vars */
8437 string = vim_getenv(name, &mustfree);
8438 if (string != NULL && *string != NUL)
8439 {
8440 if (!mustfree)
8441 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008443 else
8444 {
8445 if (mustfree)
8446 vim_free(string);
8447
8448 /* next try expanding things like $VIM and ${HOME} */
8449 string = expand_env_save(name - 1);
8450 if (string != NULL && *string == '$')
8451 {
8452 vim_free(string);
8453 string = NULL;
8454 }
8455 }
8456 name[len] = cc;
8457
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008458 rettv->v_type = VAR_STRING;
8459 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 }
8461
8462 return OK;
8463}
8464
8465/*
8466 * Array with names and number of arguments of all internal functions
8467 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8468 */
8469static struct fst
8470{
8471 char *f_name; /* function name */
8472 char f_min_argc; /* minimal number of arguments */
8473 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008474 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008475 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476} functions[] =
8477{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008478#ifdef FEAT_FLOAT
8479 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008480 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008481#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008482 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008483 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 {"append", 2, 2, f_append},
8485 {"argc", 0, 0, f_argc},
8486 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008487 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008488 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008489#ifdef FEAT_FLOAT
8490 {"asin", 1, 1, f_asin}, /* WJMc */
8491#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008492 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008493 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008494 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008495 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaarea6553b2016-03-27 15:13:38 +02008496 {"assert_match", 2, 3, f_assert_match},
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008497 {"assert_notequal", 2, 3, f_assert_notequal},
8498 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008499 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008500#ifdef FEAT_FLOAT
8501 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008502 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008503#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008505 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 {"bufexists", 1, 1, f_bufexists},
8507 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8508 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8509 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8510 {"buflisted", 1, 1, f_buflisted},
8511 {"bufloaded", 1, 1, f_bufloaded},
8512 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008513 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaarb3619a92016-06-04 17:58:52 +02008514 {"bufwinid", 1, 1, f_bufwinid},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 {"bufwinnr", 1, 1, f_bufwinnr},
8516 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008517 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008518 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008519 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008520#ifdef FEAT_FLOAT
8521 {"ceil", 1, 1, f_ceil},
8522#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008523#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008524 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008525 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8526 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008527 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008528 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar03602ec2016-03-20 20:57:45 +01008529 {"ch_info", 1, 1, f_ch_info},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008530 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008531 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008532 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008533 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008534 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008535 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8536 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008537 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008538 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008539#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008540 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008541 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008543 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008545#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008546 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008547 {"complete_add", 1, 1, f_complete_add},
8548 {"complete_check", 0, 0, f_complete_check},
8549#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008551 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008552#ifdef FEAT_FLOAT
8553 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008554 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008555#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008556 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008558 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008559 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008560 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008562 {"diff_filler", 1, 1, f_diff_filler},
8563 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008564 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008566 {"eval", 1, 1, f_eval},
Bram Moolenaar1e5e1232016-07-07 17:33:02 +02008567 {"evalcmd", 1, 1, f_evalcmd},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 {"eventhandler", 0, 0, f_eventhandler},
8569 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008570 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008572#ifdef FEAT_FLOAT
8573 {"exp", 1, 1, f_exp},
8574#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008575 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008576 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008577 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8579 {"filereadable", 1, 1, f_filereadable},
8580 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008581 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008582 {"finddir", 1, 3, f_finddir},
8583 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008584#ifdef FEAT_FLOAT
8585 {"float2nr", 1, 1, f_float2nr},
8586 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008587 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008588#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008589 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 {"fnamemodify", 2, 2, f_fnamemodify},
8591 {"foldclosed", 1, 1, f_foldclosed},
8592 {"foldclosedend", 1, 1, f_foldclosedend},
8593 {"foldlevel", 1, 1, f_foldlevel},
8594 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008595 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008597 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008598 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008599 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008600 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008601 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 {"getchar", 0, 1, f_getchar},
8603 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008604 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 {"getcmdline", 0, 0, f_getcmdline},
8606 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008607 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008608 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008609 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008610 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008611 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008612 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 {"getfsize", 1, 1, f_getfsize},
8614 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008615 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008616 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008617 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008618 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008619 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008620 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008621 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008622 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008624 {"gettabvar", 2, 3, f_gettabvar},
8625 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 {"getwinposx", 0, 0, f_getwinposx},
8627 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008628 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008629 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008630 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008631 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008633 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008634 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008635 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8637 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8638 {"histadd", 2, 2, f_histadd},
8639 {"histdel", 1, 2, f_histdel},
8640 {"histget", 1, 2, f_histget},
8641 {"histnr", 1, 1, f_histnr},
8642 {"hlID", 1, 1, f_hlID},
8643 {"hlexists", 1, 1, f_hlexists},
8644 {"hostname", 0, 0, f_hostname},
8645 {"iconv", 3, 3, f_iconv},
8646 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008647 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008648 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008650 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651 {"inputrestore", 0, 0, f_inputrestore},
8652 {"inputsave", 0, 0, f_inputsave},
8653 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008654 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008655 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008657 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008658#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8659 {"isnan", 1, 1, f_isnan},
8660#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008661 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008662#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008663 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008664 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008665 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008666 {"job_start", 1, 2, f_job_start},
8667 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008668 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008669#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008670 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008671 {"js_decode", 1, 1, f_js_decode},
8672 {"js_encode", 1, 1, f_js_encode},
8673 {"json_decode", 1, 1, f_json_decode},
8674 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008675 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008677 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 {"libcall", 3, 3, f_libcall},
8679 {"libcallnr", 3, 3, f_libcallnr},
8680 {"line", 1, 1, f_line},
8681 {"line2byte", 1, 1, f_line2byte},
8682 {"lispindent", 1, 1, f_lispindent},
8683 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008684#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008685 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008686 {"log10", 1, 1, f_log10},
8687#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008688#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008689 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008690#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008691 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008692 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008693 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008694 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008695 {"matchadd", 2, 5, f_matchadd},
8696 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008697 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008698 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008699 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008700 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008701 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar7fed5c12016-03-29 23:10:31 +02008702 {"matchstrpos", 2, 4, f_matchstrpos},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008703 {"max", 1, 1, f_max},
8704 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008705#ifdef vim_mkdir
8706 {"mkdir", 1, 3, f_mkdir},
8707#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008708 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008709#ifdef FEAT_MZSCHEME
8710 {"mzeval", 1, 1, f_mzeval},
8711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008713 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008714 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008715 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008716#ifdef FEAT_PERL
8717 {"perleval", 1, 1, f_perleval},
8718#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008719#ifdef FEAT_FLOAT
8720 {"pow", 2, 2, f_pow},
8721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008723 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008724 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008725#ifdef FEAT_PYTHON3
8726 {"py3eval", 1, 1, f_py3eval},
8727#endif
8728#ifdef FEAT_PYTHON
8729 {"pyeval", 1, 1, f_pyeval},
8730#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008731 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008732 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008733 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008734#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008735 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008736#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008737 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 {"remote_expr", 2, 3, f_remote_expr},
8739 {"remote_foreground", 1, 1, f_remote_foreground},
8740 {"remote_peek", 1, 2, f_remote_peek},
8741 {"remote_read", 1, 1, f_remote_read},
8742 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008743 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008745 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008747 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008748#ifdef FEAT_FLOAT
8749 {"round", 1, 1, f_round},
8750#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008751 {"screenattr", 2, 2, f_screenattr},
8752 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008753 {"screencol", 0, 0, f_screencol},
8754 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008755 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008756 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008757 {"searchpair", 3, 7, f_searchpair},
8758 {"searchpairpos", 3, 7, f_searchpairpos},
8759 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 {"server2client", 2, 2, f_server2client},
8761 {"serverlist", 0, 0, f_serverlist},
8762 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008763 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008765 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008767 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008768 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008769 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008770 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008772 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008773 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008775#ifdef FEAT_CRYPT
8776 {"sha256", 1, 1, f_sha256},
8777#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008778 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008779 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008781#ifdef FEAT_FLOAT
8782 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008783 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008784#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008785 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008786 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008787 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008788 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008789 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008790#ifdef FEAT_FLOAT
8791 {"sqrt", 1, 1, f_sqrt},
8792 {"str2float", 1, 1, f_str2float},
8793#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008794 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008795 {"strcharpart", 2, 3, f_strcharpart},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008796 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008797 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798#ifdef HAVE_STRFTIME
8799 {"strftime", 1, 2, f_strftime},
8800#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008801 {"strgetchar", 2, 2, f_strgetchar},
Bram Moolenaar33570922005-01-25 22:26:29 +00008802 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008803 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804 {"strlen", 1, 1, f_strlen},
8805 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008806 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008808 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008809 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 {"substitute", 4, 4, f_substitute},
8811 {"synID", 3, 3, f_synID},
8812 {"synIDattr", 2, 3, f_synIDattr},
8813 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008814 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008815 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008816 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008817 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008818 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008819 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008820 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008821 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008822 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008823#ifdef FEAT_FLOAT
8824 {"tan", 1, 1, f_tan},
8825 {"tanh", 1, 1, f_tanh},
8826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827 {"tempname", 0, 0, f_tempname},
Bram Moolenaar8e8df252016-05-25 21:23:21 +02008828 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
8829 {"test_disable_char_avail", 1, 1, f_test_disable_char_avail},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008830 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
8831#ifdef FEAT_JOB_CHANNEL
8832 {"test_null_channel", 0, 0, f_test_null_channel},
8833#endif
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02008834 {"test_null_dict", 0, 0, f_test_null_dict},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008835#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02008836 {"test_null_job", 0, 0, f_test_null_job},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008837#endif
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02008838 {"test_null_list", 0, 0, f_test_null_list},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008839 {"test_null_partial", 0, 0, f_test_null_partial},
8840 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02008841 {"test_settime", 1, 1, f_test_settime},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008842#ifdef FEAT_TIMERS
8843 {"timer_start", 2, 3, f_timer_start},
8844 {"timer_stop", 1, 1, f_timer_stop},
8845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 {"tolower", 1, 1, f_tolower},
8847 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008848 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008849#ifdef FEAT_FLOAT
8850 {"trunc", 1, 1, f_trunc},
8851#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008853 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008854 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008855 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008856 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 {"virtcol", 1, 1, f_virtcol},
8858 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008859 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008860 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008861 {"win_getid", 0, 2, f_win_getid},
8862 {"win_gotoid", 1, 1, f_win_gotoid},
8863 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8864 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 {"winbufnr", 1, 1, f_winbufnr},
8866 {"wincol", 0, 0, f_wincol},
8867 {"winheight", 1, 1, f_winheight},
8868 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008869 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008871 {"winrestview", 1, 1, f_winrestview},
8872 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008874 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008875 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008876 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877};
8878
8879#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8880
8881/*
8882 * Function given to ExpandGeneric() to obtain the list of internal
8883 * or user defined function names.
8884 */
8885 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008886get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887{
8888 static int intidx = -1;
8889 char_u *name;
8890
8891 if (idx == 0)
8892 intidx = -1;
8893 if (intidx < 0)
8894 {
8895 name = get_user_func_name(xp, idx);
8896 if (name != NULL)
8897 return name;
8898 }
8899 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8900 {
8901 STRCPY(IObuff, functions[intidx].f_name);
8902 STRCAT(IObuff, "(");
8903 if (functions[intidx].f_max_argc == 0)
8904 STRCAT(IObuff, ")");
8905 return IObuff;
8906 }
8907
8908 return NULL;
8909}
8910
8911/*
8912 * Function given to ExpandGeneric() to obtain the list of internal or
8913 * user defined variable or function names.
8914 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008916get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917{
8918 static int intidx = -1;
8919 char_u *name;
8920
8921 if (idx == 0)
8922 intidx = -1;
8923 if (intidx < 0)
8924 {
8925 name = get_function_name(xp, idx);
8926 if (name != NULL)
8927 return name;
8928 }
8929 return get_user_var_name(xp, ++intidx);
8930}
8931
8932#endif /* FEAT_CMDL_COMPL */
8933
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008934#if defined(EBCDIC) || defined(PROTO)
8935/*
8936 * Compare struct fst by function name.
8937 */
8938 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008939compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008940{
8941 struct fst *p1 = (struct fst *)s1;
8942 struct fst *p2 = (struct fst *)s2;
8943
8944 return STRCMP(p1->f_name, p2->f_name);
8945}
8946
8947/*
8948 * Sort the function table by function name.
8949 * The sorting of the table above is ASCII dependant.
8950 * On machines using EBCDIC we have to sort it.
8951 */
8952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008953sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008954{
8955 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8956
8957 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8958}
8959#endif
8960
8961
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962/*
8963 * Find internal function in table above.
8964 * Return index, or -1 if not found
8965 */
8966 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008967find_internal_func(
8968 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969{
8970 int first = 0;
8971 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8972 int cmp;
8973 int x;
8974
8975 /*
8976 * Find the function name in the table. Binary search.
8977 */
8978 while (first <= last)
8979 {
8980 x = first + ((unsigned)(last - first) >> 1);
8981 cmp = STRCMP(name, functions[x].f_name);
8982 if (cmp < 0)
8983 last = x - 1;
8984 else if (cmp > 0)
8985 first = x + 1;
8986 else
8987 return x;
8988 }
8989 return -1;
8990}
8991
8992/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008993 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8994 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01008995 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
8996 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008997 */
8998 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01008999deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009000{
Bram Moolenaar33570922005-01-25 22:26:29 +00009001 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009002 int cc;
9003
Bram Moolenaar65639032016-03-16 21:40:30 +01009004 if (partialp != NULL)
9005 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009006
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009007 cc = name[*lenp];
9008 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01009009 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009010 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00009011 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009012 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009013 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009014 {
9015 *lenp = 0;
9016 return (char_u *)""; /* just in case */
9017 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009018 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00009019 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009020 }
9021
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009022 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
9023 {
Bram Moolenaar65639032016-03-16 21:40:30 +01009024 partial_T *pt = v->di_tv.vval.v_partial;
9025
9026 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009027 {
9028 *lenp = 0;
9029 return (char_u *)""; /* just in case */
9030 }
Bram Moolenaar65639032016-03-16 21:40:30 +01009031 if (partialp != NULL)
9032 *partialp = pt;
9033 *lenp = (int)STRLEN(pt->pt_name);
9034 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009035 }
9036
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009037 return name;
9038}
9039
9040/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041 * Allocate a variable for the result of a function.
9042 * Return OK or FAIL.
9043 */
9044 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009045get_func_tv(
9046 char_u *name, /* name of the function */
9047 int len, /* length of "name" */
9048 typval_T *rettv,
9049 char_u **arg, /* argument, pointing to the '(' */
9050 linenr_T firstline, /* first line of range */
9051 linenr_T lastline, /* last line of range */
9052 int *doesrange, /* return: function handled range */
9053 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009054 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01009055 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056{
9057 char_u *argp;
9058 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009059 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060 int argcount = 0; /* number of arguments found */
9061
9062 /*
9063 * Get the arguments.
9064 */
9065 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009066 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067 {
9068 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
9069 if (*argp == ')' || *argp == ',' || *argp == NUL)
9070 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
9072 {
9073 ret = FAIL;
9074 break;
9075 }
9076 ++argcount;
9077 if (*argp != ',')
9078 break;
9079 }
9080 if (*argp == ')')
9081 ++argp;
9082 else
9083 ret = FAIL;
9084
9085 if (ret == OK)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02009086 {
9087 int i = 0;
9088
9089 if (get_vim_var_nr(VV_TESTING))
9090 {
Bram Moolenaar8e8df252016-05-25 21:23:21 +02009091 /* Prepare for calling test_garbagecollect_now(), need to know
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02009092 * what variables are used on the call stack. */
9093 if (funcargs.ga_itemsize == 0)
9094 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
9095 for (i = 0; i < argcount; ++i)
9096 if (ga_grow(&funcargs, 1) == OK)
9097 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
9098 &argvars[i];
9099 }
9100
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009101 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009102 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02009103
9104 funcargs.ga_len -= i;
9105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00009107 {
9108 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009109 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00009110 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009111 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00009112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113
9114 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009115 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116
9117 *arg = skipwhite(argp);
9118 return ret;
9119}
9120
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009121#define ERROR_UNKNOWN 0
9122#define ERROR_TOOMANY 1
9123#define ERROR_TOOFEW 2
9124#define ERROR_SCRIPT 3
9125#define ERROR_DICT 4
9126#define ERROR_NONE 5
9127#define ERROR_OTHER 6
9128#define FLEN_FIXED 40
9129
9130/*
9131 * In a script change <SID>name() and s:name() to K_SNR 123_name().
9132 * Change <SNR>123_name() to K_SNR 123_name().
9133 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
9134 * (slow).
9135 */
9136 static char_u *
9137fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
9138{
9139 int llen;
9140 char_u *fname;
9141 int i;
9142
9143 llen = eval_fname_script(name);
9144 if (llen > 0)
9145 {
9146 fname_buf[0] = K_SPECIAL;
9147 fname_buf[1] = KS_EXTRA;
9148 fname_buf[2] = (int)KE_SNR;
9149 i = 3;
9150 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
9151 {
9152 if (current_SID <= 0)
9153 *error = ERROR_SCRIPT;
9154 else
9155 {
9156 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
9157 i = (int)STRLEN(fname_buf);
9158 }
9159 }
9160 if (i + STRLEN(name + llen) < FLEN_FIXED)
9161 {
9162 STRCPY(fname_buf + i, name + llen);
9163 fname = fname_buf;
9164 }
9165 else
9166 {
9167 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
9168 if (fname == NULL)
9169 *error = ERROR_OTHER;
9170 else
9171 {
9172 *tofree = fname;
9173 mch_memmove(fname, fname_buf, (size_t)i);
9174 STRCPY(fname + i, name + llen);
9175 }
9176 }
9177 }
9178 else
9179 fname = name;
9180 return fname;
9181}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182
9183/*
9184 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02009185 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00009186 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01009188 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009189call_func(
9190 char_u *funcname, /* name of the function */
9191 int len, /* length of "name" */
9192 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009193 int argcount_in, /* number of "argvars" */
9194 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009195 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01009196 linenr_T firstline, /* first line of range */
9197 linenr_T lastline, /* last line of range */
9198 int *doesrange, /* return: function handled range */
9199 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009200 partial_T *partial, /* optional, can be NULL */
9201 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202{
9203 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009204 int error = ERROR_NONE;
9205 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009208 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009210 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009211 int argcount = argcount_in;
9212 typval_T *argvars = argvars_in;
9213 dict_T *selfdict = selfdict_in;
9214 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
9215 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009216
9217 /* Make a copy of the name, if it comes from a funcref variable it could
9218 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02009219 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009220 if (name == NULL)
9221 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009223 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224
9225 *doesrange = FALSE;
9226
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009227 if (partial != NULL)
9228 {
Bram Moolenaar1d429612016-05-24 15:44:17 +02009229 /* When the function has a partial with a dict and there is a dict
9230 * argument, use the dict argument. That is backwards compatible.
9231 * When the dict was bound explicitly use the one from the partial. */
9232 if (partial->pt_dict != NULL
9233 && (selfdict_in == NULL || !partial->pt_auto))
9234 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009235 if (error == ERROR_NONE && partial->pt_argc > 0)
9236 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009237 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
9238 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
9239 for (i = 0; i < argcount_in; ++i)
9240 argv[i + argv_clear] = argvars_in[i];
9241 argvars = argv;
9242 argcount = partial->pt_argc + argcount_in;
9243 }
9244 }
9245
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246
9247 /* execute the function if no errors detected and executing */
9248 if (evaluate && error == ERROR_NONE)
9249 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009250 char_u *rfname = fname;
9251
9252 /* Ignore "g:" before a function name. */
9253 if (fname[0] == 'g' && fname[1] == ':')
9254 rfname = fname + 2;
9255
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009256 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
9257 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258 error = ERROR_UNKNOWN;
9259
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009260 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261 {
9262 /*
9263 * User defined function.
9264 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009265 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009266
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009268 /* Trigger FuncUndefined event, may load the function. */
9269 if (fp == NULL
9270 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009271 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009272 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009274 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009275 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276 }
9277#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009278 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009279 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009280 {
9281 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009282 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009283 }
9284
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285 if (fp != NULL)
9286 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009287 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009288 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009289 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009291 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009293 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009294 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295 else
9296 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009297 int did_save_redo = FALSE;
9298
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299 /*
9300 * Call the user function.
9301 * Save and restore search patterns, script variables and
9302 * redo buffer.
9303 */
9304 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009305#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009306 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009307#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009308 {
9309 saveRedobuff();
9310 did_save_redo = TRUE;
9311 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009312 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009313 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009314 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009315 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
9316 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
9317 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00009318 /* Function was unreferenced while being used, free it
9319 * now. */
9320 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009321 if (did_save_redo)
9322 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323 restore_search_patterns();
9324 error = ERROR_NONE;
9325 }
9326 }
9327 }
9328 else
9329 {
9330 /*
9331 * Find the function name in the table, call its implementation.
9332 */
9333 i = find_internal_func(fname);
9334 if (i >= 0)
9335 {
9336 if (argcount < functions[i].f_min_argc)
9337 error = ERROR_TOOFEW;
9338 else if (argcount > functions[i].f_max_argc)
9339 error = ERROR_TOOMANY;
9340 else
9341 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009342 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009343 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344 error = ERROR_NONE;
9345 }
9346 }
9347 }
9348 /*
9349 * The function call (or "FuncUndefined" autocommand sequence) might
9350 * have been aborted by an error, an interrupt, or an explicitly thrown
9351 * exception that has not been caught so far. This situation can be
9352 * tested for by calling aborting(). For an error in an internal
9353 * function or for the "E132" error in call_user_func(), however, the
9354 * throw point at which the "force_abort" flag (temporarily reset by
9355 * emsg()) is normally updated has not been reached yet. We need to
9356 * update that flag first to make aborting() reliable.
9357 */
9358 update_force_abort();
9359 }
9360 if (error == ERROR_NONE)
9361 ret = OK;
9362
9363 /*
9364 * Report an error unless the argument evaluation or function call has been
9365 * cancelled due to an aborting error, an interrupt, or an exception.
9366 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00009367 if (!aborting())
9368 {
9369 switch (error)
9370 {
9371 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009372 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009373 break;
9374 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009375 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009376 break;
9377 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009378 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009379 name);
9380 break;
9381 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009382 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009383 name);
9384 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009385 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009386 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00009387 name);
9388 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009389 }
9390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009392 while (argv_clear > 0)
9393 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009394 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009395 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396
9397 return ret;
9398}
9399
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009400/*
9401 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009402 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009403 */
9404 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009405emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009406{
9407 char_u *p;
9408
9409 if (*name == K_SPECIAL)
9410 p = concat_str((char_u *)"<SNR>", name + 3);
9411 else
9412 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009413 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009414 if (p != name)
9415 vim_free(p);
9416}
9417
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009418/*
9419 * Return TRUE for a non-zero Number and a non-empty String.
9420 */
9421 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009422non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009423{
9424 return ((argvars[0].v_type == VAR_NUMBER
9425 && argvars[0].vval.v_number != 0)
Bram Moolenaare381d3d2016-07-07 14:50:41 +02009426 || (argvars[0].v_type == VAR_SPECIAL
9427 && argvars[0].vval.v_number == VVAL_TRUE)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009428 || (argvars[0].v_type == VAR_STRING
9429 && argvars[0].vval.v_string != NULL
9430 && *argvars[0].vval.v_string != NUL));
9431}
9432
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433/*********************************************
9434 * Implementation of the built-in functions
9435 */
9436
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009437#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009438static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009439
9440/*
9441 * Get the float value of "argvars[0]" into "f".
9442 * Returns FAIL when the argument is not a Number or Float.
9443 */
9444 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009445get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009446{
9447 if (argvars[0].v_type == VAR_FLOAT)
9448 {
9449 *f = argvars[0].vval.v_float;
9450 return OK;
9451 }
9452 if (argvars[0].v_type == VAR_NUMBER)
9453 {
9454 *f = (float_T)argvars[0].vval.v_number;
9455 return OK;
9456 }
9457 EMSG(_("E808: Number or Float required"));
9458 return FAIL;
9459}
9460
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009461/*
9462 * "abs(expr)" function
9463 */
9464 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009465f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009466{
9467 if (argvars[0].v_type == VAR_FLOAT)
9468 {
9469 rettv->v_type = VAR_FLOAT;
9470 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9471 }
9472 else
9473 {
9474 varnumber_T n;
9475 int error = FALSE;
9476
9477 n = get_tv_number_chk(&argvars[0], &error);
9478 if (error)
9479 rettv->vval.v_number = -1;
9480 else if (n > 0)
9481 rettv->vval.v_number = n;
9482 else
9483 rettv->vval.v_number = -n;
9484 }
9485}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009486
9487/*
9488 * "acos()" function
9489 */
9490 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009491f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009492{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009493 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009494
9495 rettv->v_type = VAR_FLOAT;
9496 if (get_float_arg(argvars, &f) == OK)
9497 rettv->vval.v_float = acos(f);
9498 else
9499 rettv->vval.v_float = 0.0;
9500}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009501#endif
9502
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009504 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505 */
9506 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009507f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508{
Bram Moolenaar33570922005-01-25 22:26:29 +00009509 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009511 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009512 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009514 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009515 && !tv_check_lock(l->lv_lock,
9516 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009517 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009518 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009519 }
9520 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009521 EMSG(_(e_listreq));
9522}
9523
9524/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009525 * "and(expr, expr)" function
9526 */
9527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009528f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009529{
9530 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9531 & get_tv_number_chk(&argvars[1], NULL);
9532}
9533
9534/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009535 * "append(lnum, string/list)" function
9536 */
9537 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009538f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009539{
9540 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009541 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009542 list_T *l = NULL;
9543 listitem_T *li = NULL;
9544 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009545 long added = 0;
9546
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009547 /* When coming here from Insert mode, sync undo, so that this can be
9548 * undone separately from what was previously inserted. */
9549 if (u_sync_once == 2)
9550 {
9551 u_sync_once = 1; /* notify that u_sync() was called */
9552 u_sync(TRUE);
9553 }
9554
Bram Moolenaar0d660222005-01-07 21:51:51 +00009555 lnum = get_tv_lnum(argvars);
9556 if (lnum >= 0
9557 && lnum <= curbuf->b_ml.ml_line_count
9558 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009559 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009560 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009561 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009562 l = argvars[1].vval.v_list;
9563 if (l == NULL)
9564 return;
9565 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009566 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009567 for (;;)
9568 {
9569 if (l == NULL)
9570 tv = &argvars[1]; /* append a string */
9571 else if (li == NULL)
9572 break; /* end of list */
9573 else
9574 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009575 line = get_tv_string_chk(tv);
9576 if (line == NULL) /* type error */
9577 {
9578 rettv->vval.v_number = 1; /* Failed */
9579 break;
9580 }
9581 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009582 ++added;
9583 if (l == NULL)
9584 break;
9585 li = li->li_next;
9586 }
9587
9588 appended_lines_mark(lnum, added);
9589 if (curwin->w_cursor.lnum > lnum)
9590 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009592 else
9593 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594}
9595
9596/*
9597 * "argc()" function
9598 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009600f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009602 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603}
9604
9605/*
9606 * "argidx()" function
9607 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009609f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009611 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612}
9613
9614/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009615 * "arglistid()" function
9616 */
9617 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009618f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009619{
9620 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009621
9622 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009623 wp = find_tabwin(&argvars[0], &argvars[1]);
9624 if (wp != NULL)
9625 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009626}
9627
9628/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629 * "argv(nr)" function
9630 */
9631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009632f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633{
9634 int idx;
9635
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009636 if (argvars[0].v_type != VAR_UNKNOWN)
9637 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02009638 idx = (int)get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009639 if (idx >= 0 && idx < ARGCOUNT)
9640 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9641 else
9642 rettv->vval.v_string = NULL;
9643 rettv->v_type = VAR_STRING;
9644 }
9645 else if (rettv_list_alloc(rettv) == OK)
9646 for (idx = 0; idx < ARGCOUNT; ++idx)
9647 list_append_string(rettv->vval.v_list,
9648 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649}
9650
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009651typedef enum
9652{
9653 ASSERT_EQUAL,
9654 ASSERT_NOTEQUAL,
9655 ASSERT_MATCH,
9656 ASSERT_NOTMATCH,
Bram Moolenaar3780bb92016-04-12 22:18:53 +02009657 ASSERT_OTHER
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009658} assert_type_T;
9659
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009660static void prepare_assert_error(garray_T*gap);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009661static 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 +01009662static void assert_error(garray_T *gap);
9663static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009664
9665/*
9666 * Prepare "gap" for an assert error and add the sourcing position.
9667 */
9668 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009669prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009670{
9671 char buf[NUMBUFLEN];
9672
9673 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009674 if (sourcing_name != NULL)
9675 {
9676 ga_concat(gap, sourcing_name);
9677 if (sourcing_lnum > 0)
9678 ga_concat(gap, (char_u *)" ");
9679 }
9680 if (sourcing_lnum > 0)
9681 {
9682 sprintf(buf, "line %ld", (long)sourcing_lnum);
9683 ga_concat(gap, (char_u *)buf);
9684 }
9685 if (sourcing_name != NULL || sourcing_lnum > 0)
9686 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009687}
9688
9689/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009690 * Append "str" to "gap", escaping unprintable characters.
9691 * Changes NL to \n, CR to \r, etc.
9692 */
9693 static void
9694ga_concat_esc(garray_T *gap, char_u *str)
9695{
9696 char_u *p;
9697 char_u buf[NUMBUFLEN];
9698
Bram Moolenaarf1551962016-03-15 12:55:58 +01009699 if (str == NULL)
9700 {
9701 ga_concat(gap, (char_u *)"NULL");
9702 return;
9703 }
9704
Bram Moolenaar23689172016-02-15 22:37:37 +01009705 for (p = str; *p != NUL; ++p)
9706 switch (*p)
9707 {
9708 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9709 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9710 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9711 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9712 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9713 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9714 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9715 default:
9716 if (*p < ' ')
9717 {
9718 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9719 ga_concat(gap, buf);
9720 }
9721 else
9722 ga_append(gap, *p);
9723 break;
9724 }
9725}
9726
9727/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009728 * Fill "gap" with information about an assert error.
9729 */
9730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009731fill_assert_error(
9732 garray_T *gap,
9733 typval_T *opt_msg_tv,
9734 char_u *exp_str,
9735 typval_T *exp_tv,
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009736 typval_T *got_tv,
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009737 assert_type_T atype)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009738{
9739 char_u numbuf[NUMBUFLEN];
9740 char_u *tofree;
9741
9742 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9743 {
9744 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9745 vim_free(tofree);
9746 }
9747 else
9748 {
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009749 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009750 ga_concat(gap, (char_u *)"Pattern ");
9751 else
9752 ga_concat(gap, (char_u *)"Expected ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009753 if (exp_str == NULL)
9754 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009755 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009756 vim_free(tofree);
9757 }
9758 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009759 ga_concat_esc(gap, exp_str);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009760 if (atype == ASSERT_MATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009761 ga_concat(gap, (char_u *)" does not match ");
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009762 else if (atype == ASSERT_NOTMATCH)
9763 ga_concat(gap, (char_u *)" does match ");
9764 else if (atype == ASSERT_NOTEQUAL)
9765 ga_concat(gap, (char_u *)" differs from ");
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009766 else
9767 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009768 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009769 vim_free(tofree);
9770 }
9771}
Bram Moolenaar43345542015-11-29 17:35:35 +01009772
9773/*
9774 * Add an assert error to v:errors.
9775 */
9776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009777assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009778{
9779 struct vimvar *vp = &vimvars[VV_ERRORS];
9780
9781 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9782 /* Make sure v:errors is a list. */
9783 set_vim_var_list(VV_ERRORS, list_alloc());
9784 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9785}
9786
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009787 static void
9788assert_equal_common(typval_T *argvars, assert_type_T atype)
9789{
9790 garray_T ga;
9791
9792 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9793 != (atype == ASSERT_EQUAL))
9794 {
9795 prepare_assert_error(&ga);
9796 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9797 atype);
9798 assert_error(&ga);
9799 ga_clear(&ga);
9800 }
9801}
9802
Bram Moolenaar43345542015-11-29 17:35:35 +01009803/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009804 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009805 */
9806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009807f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009808{
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009809 assert_equal_common(argvars, ASSERT_EQUAL);
9810}
Bram Moolenaar43345542015-11-29 17:35:35 +01009811
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009812/*
9813 * "assert_notequal(expected, actual[, msg])" function
9814 */
9815 static void
9816f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
9817{
9818 assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar43345542015-11-29 17:35:35 +01009819}
9820
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009821/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009822 * "assert_exception(string[, msg])" function
9823 */
9824 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009825f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009826{
9827 garray_T ga;
9828 char *error;
9829
9830 error = (char *)get_tv_string_chk(&argvars[0]);
9831 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9832 {
9833 prepare_assert_error(&ga);
9834 ga_concat(&ga, (char_u *)"v:exception is not set");
9835 assert_error(&ga);
9836 ga_clear(&ga);
9837 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009838 else if (error != NULL
9839 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009840 {
9841 prepare_assert_error(&ga);
9842 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009843 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009844 assert_error(&ga);
9845 ga_clear(&ga);
9846 }
9847}
9848
9849/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009850 * "assert_fails(cmd [, error])" function
9851 */
9852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009853f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009854{
9855 char_u *cmd = get_tv_string_chk(&argvars[0]);
9856 garray_T ga;
9857
9858 called_emsg = FALSE;
9859 suppress_errthrow = TRUE;
9860 emsg_silent = TRUE;
9861 do_cmdline_cmd(cmd);
9862 if (!called_emsg)
9863 {
9864 prepare_assert_error(&ga);
9865 ga_concat(&ga, (char_u *)"command did not fail: ");
9866 ga_concat(&ga, cmd);
9867 assert_error(&ga);
9868 ga_clear(&ga);
9869 }
9870 else if (argvars[1].v_type != VAR_UNKNOWN)
9871 {
9872 char_u buf[NUMBUFLEN];
9873 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9874
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009875 if (error == NULL
9876 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009877 {
9878 prepare_assert_error(&ga);
9879 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009880 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
Bram Moolenaara260b872016-01-15 20:48:22 +01009881 assert_error(&ga);
9882 ga_clear(&ga);
9883 }
9884 }
9885
9886 called_emsg = FALSE;
9887 suppress_errthrow = FALSE;
9888 emsg_silent = FALSE;
9889 emsg_on_display = FALSE;
9890 set_vim_var_string(VV_ERRMSG, NULL, 0);
9891}
9892
9893/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009894 * Common for assert_true() and assert_false().
9895 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009896 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009897assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009898{
9899 int error = FALSE;
9900 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009901
Bram Moolenaar37127922016-02-06 20:29:28 +01009902 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009903 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009904 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009905 if (argvars[0].v_type != VAR_NUMBER
9906 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9907 || error)
9908 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009909 prepare_assert_error(&ga);
9910 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009911 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009912 NULL, &argvars[0], ASSERT_OTHER);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009913 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009914 ga_clear(&ga);
9915 }
9916}
9917
9918/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009919 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009920 */
9921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009922f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009923{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009924 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009925}
9926
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009927 static void
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009928assert_match_common(typval_T *argvars, assert_type_T atype)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009929{
9930 garray_T ga;
9931 char_u buf1[NUMBUFLEN];
9932 char_u buf2[NUMBUFLEN];
9933 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9934 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9935
Bram Moolenaar72188e92016-03-28 22:48:29 +02009936 if (pat == NULL || text == NULL)
9937 EMSG(_(e_invarg));
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009938 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009939 {
9940 prepare_assert_error(&ga);
9941 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009942 atype);
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009943 assert_error(&ga);
9944 ga_clear(&ga);
9945 }
9946}
9947
9948/*
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009949 * "assert_match(pattern, actual[, msg])" function
9950 */
9951 static void
9952f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
9953{
9954 assert_match_common(argvars, ASSERT_MATCH);
9955}
9956
9957/*
9958 * "assert_notmatch(pattern, actual[, msg])" function
9959 */
9960 static void
9961f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
9962{
9963 assert_match_common(argvars, ASSERT_NOTMATCH);
9964}
9965
9966/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009967 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009968 */
9969 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009970f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009971{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009972 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009973}
9974
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009975#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009976/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009977 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009978 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009979 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009980f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009981{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009982 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009983
9984 rettv->v_type = VAR_FLOAT;
9985 if (get_float_arg(argvars, &f) == OK)
9986 rettv->vval.v_float = asin(f);
9987 else
9988 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009989}
9990
9991/*
9992 * "atan()" function
9993 */
9994 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009995f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009996{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009997 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009998
9999 rettv->v_type = VAR_FLOAT;
10000 if (get_float_arg(argvars, &f) == OK)
10001 rettv->vval.v_float = atan(f);
10002 else
10003 rettv->vval.v_float = 0.0;
10004}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010005
10006/*
10007 * "atan2()" function
10008 */
10009 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010010f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010011{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010012 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010013
10014 rettv->v_type = VAR_FLOAT;
10015 if (get_float_arg(argvars, &fx) == OK
10016 && get_float_arg(&argvars[1], &fy) == OK)
10017 rettv->vval.v_float = atan2(fx, fy);
10018 else
10019 rettv->vval.v_float = 0.0;
10020}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010021#endif
10022
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023/*
10024 * "browse(save, title, initdir, default)" function
10025 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010027f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028{
10029#ifdef FEAT_BROWSE
10030 int save;
10031 char_u *title;
10032 char_u *initdir;
10033 char_u *defname;
10034 char_u buf[NUMBUFLEN];
10035 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010036 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010038 save = (int)get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010039 title = get_tv_string_chk(&argvars[1]);
10040 initdir = get_tv_string_buf_chk(&argvars[2], buf);
10041 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010043 if (error || title == NULL || initdir == NULL || defname == NULL)
10044 rettv->vval.v_string = NULL;
10045 else
10046 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010047 do_browse(save ? BROWSE_SAVE : 0,
10048 title, defname, NULL, initdir, NULL, curbuf);
10049#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010050 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010051#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010052 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010053}
10054
10055/*
10056 * "browsedir(title, initdir)" function
10057 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010058 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010059f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010060{
10061#ifdef FEAT_BROWSE
10062 char_u *title;
10063 char_u *initdir;
10064 char_u buf[NUMBUFLEN];
10065
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010066 title = get_tv_string_chk(&argvars[0]);
10067 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010068
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010069 if (title == NULL || initdir == NULL)
10070 rettv->vval.v_string = NULL;
10071 else
10072 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010073 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010075 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010077 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078}
10079
Bram Moolenaar48e697e2016-01-23 22:17:30 +010010080static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010081
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082/*
10083 * Find a buffer by number or exact name.
10084 */
10085 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010086find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087{
10088 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010090 if (avar->v_type == VAR_NUMBER)
10091 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000010092 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010094 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +000010095 if (buf == NULL)
10096 {
10097 /* No full path name match, try a match with a URL or a "nofile"
10098 * buffer, these don't use the full path. */
10099 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10100 if (buf->b_fname != NULL
10101 && (path_with_url(buf->b_fname)
10102#ifdef FEAT_QUICKFIX
10103 || bt_nofile(buf)
10104#endif
10105 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010106 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +000010107 break;
10108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109 }
10110 return buf;
10111}
10112
10113/*
10114 * "bufexists(expr)" function
10115 */
10116 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010117f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010119 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120}
10121
10122/*
10123 * "buflisted(expr)" function
10124 */
10125 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010126f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127{
10128 buf_T *buf;
10129
10130 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010131 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132}
10133
10134/*
10135 * "bufloaded(expr)" function
10136 */
10137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010138f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139{
10140 buf_T *buf;
10141
10142 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010143 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144}
10145
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010146 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +010010147buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149 int save_magic;
10150 char_u *save_cpo;
10151 buf_T *buf;
10152
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
10154 save_magic = p_magic;
10155 p_magic = TRUE;
10156 save_cpo = p_cpo;
10157 p_cpo = (char_u *)"";
10158
10159 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010160 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161
10162 p_magic = save_magic;
10163 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +010010164 return buf;
10165}
10166
10167/*
10168 * Get buffer by number or pattern.
10169 */
10170 static buf_T *
10171get_buf_tv(typval_T *tv, int curtab_only)
10172{
10173 char_u *name = tv->vval.v_string;
10174 buf_T *buf;
10175
10176 if (tv->v_type == VAR_NUMBER)
10177 return buflist_findnr((int)tv->vval.v_number);
10178 if (tv->v_type != VAR_STRING)
10179 return NULL;
10180 if (name == NULL || *name == NUL)
10181 return curbuf;
10182 if (name[0] == '$' && name[1] == NUL)
10183 return lastbuf;
10184
10185 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186
10187 /* If not found, try expanding the name, like done for bufexists(). */
10188 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010189 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190
10191 return buf;
10192}
10193
10194/*
10195 * "bufname(expr)" function
10196 */
10197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010198f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199{
10200 buf_T *buf;
10201
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010202 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010203 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010204 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010205 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010207 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010209 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210 --emsg_off;
10211}
10212
10213/*
10214 * "bufnr(expr)" function
10215 */
10216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010217f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218{
10219 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010220 int error = FALSE;
10221 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010223 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010225 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010226 --emsg_off;
10227
10228 /* If the buffer isn't found and the second argument is not zero create a
10229 * new buffer. */
10230 if (buf == NULL
10231 && argvars[1].v_type != VAR_UNKNOWN
10232 && get_tv_number_chk(&argvars[1], &error) != 0
10233 && !error
10234 && (name = get_tv_string_chk(&argvars[0])) != NULL
10235 && !error)
10236 buf = buflist_new(name, NULL, (linenr_T)1, 0);
10237
Bram Moolenaar071d4272004-06-13 20:20:40 +000010238 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010239 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010241 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242}
10243
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244 static void
Bram Moolenaarb3619a92016-06-04 17:58:52 +020010245buf_win_common(typval_T *argvars, typval_T *rettv, int get_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246{
10247#ifdef FEAT_WINDOWS
10248 win_T *wp;
10249 int winnr = 0;
10250#endif
10251 buf_T *buf;
10252
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010253 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010255 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256#ifdef FEAT_WINDOWS
10257 for (wp = firstwin; wp; wp = wp->w_next)
10258 {
10259 ++winnr;
10260 if (wp->w_buffer == buf)
10261 break;
10262 }
Bram Moolenaarb3619a92016-06-04 17:58:52 +020010263 rettv->vval.v_number = (wp != NULL ? (get_nr ? winnr : wp->w_id) : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264#else
Bram Moolenaarb3619a92016-06-04 17:58:52 +020010265 rettv->vval.v_number = (curwin->w_buffer == buf
10266 ? (get_nr ? 1 : curwin->w_id) : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267#endif
10268 --emsg_off;
10269}
10270
10271/*
Bram Moolenaarb3619a92016-06-04 17:58:52 +020010272 * "bufwinid(nr)" function
10273 */
10274 static void
10275f_bufwinid(typval_T *argvars, typval_T *rettv)
10276{
10277 buf_win_common(argvars, rettv, FALSE);
10278}
10279
10280/*
10281 * "bufwinnr(nr)" function
10282 */
10283 static void
10284f_bufwinnr(typval_T *argvars, typval_T *rettv)
10285{
10286 buf_win_common(argvars, rettv, TRUE);
10287}
10288
10289/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290 * "byte2line(byte)" function
10291 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010293f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294{
10295#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010296 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297#else
10298 long boff = 0;
10299
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010300 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010301 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010302 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010303 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010304 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305 (linenr_T)0, &boff);
10306#endif
10307}
10308
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010310byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010311{
10312#ifdef FEAT_MBYTE
10313 char_u *t;
10314#endif
10315 char_u *str;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010316 varnumber_T idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010317
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010318 str = get_tv_string_chk(&argvars[0]);
10319 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010320 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010321 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010322 return;
10323
10324#ifdef FEAT_MBYTE
10325 t = str;
10326 for ( ; idx > 0; idx--)
10327 {
10328 if (*t == NUL) /* EOL reached */
10329 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010330 if (enc_utf8 && comp)
10331 t += utf_ptr2len(t);
10332 else
10333 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010334 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010335 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010336#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010337 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010338 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010339#endif
10340}
10341
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010342/*
10343 * "byteidx()" function
10344 */
10345 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010346f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010347{
10348 byteidx(argvars, rettv, FALSE);
10349}
10350
10351/*
10352 * "byteidxcomp()" function
10353 */
10354 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010355f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010356{
10357 byteidx(argvars, rettv, TRUE);
10358}
10359
Bram Moolenaardb913952012-06-29 12:54:53 +020010360 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010361func_call(
10362 char_u *name,
10363 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010364 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010365 dict_T *selfdict,
10366 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020010367{
10368 listitem_T *item;
10369 typval_T argv[MAX_FUNC_ARGS + 1];
10370 int argc = 0;
10371 int dummy;
10372 int r = 0;
10373
10374 for (item = args->vval.v_list->lv_first; item != NULL;
10375 item = item->li_next)
10376 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010377 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +020010378 {
10379 EMSG(_("E699: Too many arguments"));
10380 break;
10381 }
10382 /* Make a copy of each argument. This is needed to be able to set
10383 * v_lock to VAR_FIXED in the copy without changing the original list.
10384 */
10385 copy_tv(&item->li_tv, &argv[argc++]);
10386 }
10387
10388 if (item == NULL)
10389 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
10390 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010391 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +020010392
10393 /* Free the arguments. */
10394 while (argc > 0)
10395 clear_tv(&argv[--argc]);
10396
10397 return r;
10398}
10399
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010400/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010401 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010402 */
10403 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010404f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010405{
10406 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010407 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000010408 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010409
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010410 if (argvars[1].v_type != VAR_LIST)
10411 {
10412 EMSG(_(e_listreq));
10413 return;
10414 }
10415 if (argvars[1].vval.v_list == NULL)
10416 return;
10417
10418 if (argvars[0].v_type == VAR_FUNC)
10419 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010420 else if (argvars[0].v_type == VAR_PARTIAL)
10421 {
10422 partial = argvars[0].vval.v_partial;
10423 func = partial->pt_name;
10424 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010425 else
10426 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010427 if (*func == NUL)
10428 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010429
Bram Moolenaare9a41262005-01-15 22:18:47 +000010430 if (argvars[2].v_type != VAR_UNKNOWN)
10431 {
10432 if (argvars[2].v_type != VAR_DICT)
10433 {
10434 EMSG(_(e_dictreq));
10435 return;
10436 }
10437 selfdict = argvars[2].vval.v_dict;
10438 }
10439
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010440 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010441}
10442
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010443#ifdef FEAT_FLOAT
10444/*
10445 * "ceil({float})" function
10446 */
10447 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010448f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010449{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010450 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010451
10452 rettv->v_type = VAR_FLOAT;
10453 if (get_float_arg(argvars, &f) == OK)
10454 rettv->vval.v_float = ceil(f);
10455 else
10456 rettv->vval.v_float = 0.0;
10457}
10458#endif
10459
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010460#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010461/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010462 * "ch_close()" function
10463 */
10464 static void
10465f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10466{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010467 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010468
10469 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010470 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010471 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010472 channel_clear(channel);
10473 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010474}
10475
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010476/*
10477 * "ch_getbufnr()" function
10478 */
10479 static void
10480f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10481{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010482 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010483
10484 rettv->vval.v_number = -1;
10485 if (channel != NULL)
10486 {
10487 char_u *what = get_tv_string(&argvars[1]);
10488 int part;
10489
10490 if (STRCMP(what, "err") == 0)
10491 part = PART_ERR;
10492 else if (STRCMP(what, "out") == 0)
10493 part = PART_OUT;
10494 else if (STRCMP(what, "in") == 0)
10495 part = PART_IN;
10496 else
10497 part = PART_SOCK;
10498 if (channel->ch_part[part].ch_buffer != NULL)
10499 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10500 }
10501}
10502
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010503/*
10504 * "ch_getjob()" function
10505 */
10506 static void
10507f_ch_getjob(typval_T *argvars, typval_T *rettv)
10508{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010509 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010510
10511 if (channel != NULL)
10512 {
10513 rettv->v_type = VAR_JOB;
10514 rettv->vval.v_job = channel->ch_job;
10515 if (channel->ch_job != NULL)
10516 ++channel->ch_job->jv_refcount;
10517 }
10518}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010519
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010520/*
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010521 * "ch_info()" function
10522 */
10523 static void
10524f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
10525{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010526 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010527
10528 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
10529 channel_info(channel, rettv->vval.v_dict);
10530}
10531
10532/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010533 * "ch_log()" function
10534 */
10535 static void
10536f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10537{
10538 char_u *msg = get_tv_string(&argvars[0]);
10539 channel_T *channel = NULL;
10540
10541 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar437905c2016-04-26 19:01:05 +020010542 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010543
10544 ch_log(channel, (char *)msg);
10545}
10546
10547/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010548 * "ch_logfile()" function
10549 */
10550 static void
10551f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10552{
10553 char_u *fname;
10554 char_u *opt = (char_u *)"";
10555 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010556
10557 fname = get_tv_string(&argvars[0]);
10558 if (argvars[1].v_type == VAR_STRING)
10559 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010560 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010561}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010562
10563/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010564 * "ch_open()" function
10565 */
10566 static void
10567f_ch_open(typval_T *argvars, typval_T *rettv)
10568{
Bram Moolenaar77073442016-02-13 23:23:53 +010010569 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar38499922016-04-22 20:46:52 +020010570 if (check_restricted() || check_secure())
10571 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010572 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010573}
10574
10575/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010576 * "ch_read()" function
10577 */
10578 static void
10579f_ch_read(typval_T *argvars, typval_T *rettv)
10580{
10581 common_channel_read(argvars, rettv, FALSE);
10582}
10583
10584/*
10585 * "ch_readraw()" function
10586 */
10587 static void
10588f_ch_readraw(typval_T *argvars, typval_T *rettv)
10589{
10590 common_channel_read(argvars, rettv, TRUE);
10591}
10592
10593/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010594 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010595 */
10596 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010597f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10598{
10599 ch_expr_common(argvars, rettv, TRUE);
10600}
10601
10602/*
10603 * "ch_sendexpr()" function
10604 */
10605 static void
10606f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10607{
10608 ch_expr_common(argvars, rettv, FALSE);
10609}
10610
10611/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010612 * "ch_evalraw()" function
10613 */
10614 static void
10615f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10616{
10617 ch_raw_common(argvars, rettv, TRUE);
10618}
10619
10620/*
10621 * "ch_sendraw()" function
10622 */
10623 static void
10624f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10625{
10626 ch_raw_common(argvars, rettv, FALSE);
10627}
10628
10629/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010630 * "ch_setoptions()" function
10631 */
10632 static void
10633f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10634{
10635 channel_T *channel;
10636 jobopt_T opt;
10637
Bram Moolenaar437905c2016-04-26 19:01:05 +020010638 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010639 if (channel == NULL)
10640 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010641 clear_job_options(&opt);
10642 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020010643 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == OK)
10644 channel_set_options(channel, &opt);
10645 free_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010646}
10647
10648/*
10649 * "ch_status()" function
10650 */
10651 static void
10652f_ch_status(typval_T *argvars, typval_T *rettv)
10653{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010654 channel_T *channel;
10655
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010656 /* return an empty string by default */
10657 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010658 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010659
Bram Moolenaar437905c2016-04-26 19:01:05 +020010660 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010661 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010662}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010663#endif
10664
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010665/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010666 * "changenr()" function
10667 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010668 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010669f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010670{
10671 rettv->vval.v_number = curbuf->b_u_seq_cur;
10672}
10673
10674/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675 * "char2nr(string)" function
10676 */
10677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010678f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010679{
10680#ifdef FEAT_MBYTE
10681 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010682 {
10683 int utf8 = 0;
10684
10685 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010686 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaard35d7842013-01-23 17:17:10 +010010687
10688 if (utf8)
10689 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10690 else
10691 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693 else
10694#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010695 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696}
10697
10698/*
10699 * "cindent(lnum)" function
10700 */
10701 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010702f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010703{
10704#ifdef FEAT_CINDENT
10705 pos_T pos;
10706 linenr_T lnum;
10707
10708 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010709 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10711 {
10712 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010713 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714 curwin->w_cursor = pos;
10715 }
10716 else
10717#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010718 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010719}
10720
10721/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010722 * "clearmatches()" function
10723 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010724 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010725f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010726{
10727#ifdef FEAT_SEARCH_EXTRA
10728 clear_matches(curwin);
10729#endif
10730}
10731
10732/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010733 * "col(string)" function
10734 */
10735 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010736f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010737{
10738 colnr_T col = 0;
10739 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010740 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010741
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010742 fp = var2fpos(&argvars[0], FALSE, &fnum);
10743 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010744 {
10745 if (fp->col == MAXCOL)
10746 {
10747 /* '> can be MAXCOL, get the length of the line then */
10748 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010749 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010750 else
10751 col = MAXCOL;
10752 }
10753 else
10754 {
10755 col = fp->col + 1;
10756#ifdef FEAT_VIRTUALEDIT
10757 /* col(".") when the cursor is on the NUL at the end of the line
10758 * because of "coladd" can be seen as an extra column. */
10759 if (virtual_active() && fp == &curwin->w_cursor)
10760 {
10761 char_u *p = ml_get_cursor();
10762
10763 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10764 curwin->w_virtcol - curwin->w_cursor.coladd))
10765 {
10766# ifdef FEAT_MBYTE
10767 int l;
10768
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010769 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770 col += l;
10771# else
10772 if (*p != NUL && p[1] == NUL)
10773 ++col;
10774# endif
10775 }
10776 }
10777#endif
10778 }
10779 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010780 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010781}
10782
Bram Moolenaar572cb562005-08-05 21:35:02 +000010783#if defined(FEAT_INS_EXPAND)
10784/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010785 * "complete()" function
10786 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010787 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010788f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010789{
10790 int startcol;
10791
10792 if ((State & INSERT) == 0)
10793 {
10794 EMSG(_("E785: complete() can only be used in Insert mode"));
10795 return;
10796 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010797
10798 /* Check for undo allowed here, because if something was already inserted
10799 * the line was already saved for undo and this check isn't done. */
10800 if (!undo_allowed())
10801 return;
10802
Bram Moolenaarade00832006-03-10 21:46:58 +000010803 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10804 {
10805 EMSG(_(e_invarg));
10806 return;
10807 }
10808
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010809 startcol = (int)get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaarade00832006-03-10 21:46:58 +000010810 if (startcol <= 0)
10811 return;
10812
10813 set_completion(startcol - 1, argvars[1].vval.v_list);
10814}
10815
10816/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010817 * "complete_add()" function
10818 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010819 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010820f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010821{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010822 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010823}
10824
10825/*
10826 * "complete_check()" function
10827 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010829f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010830{
10831 int saved = RedrawingDisabled;
10832
10833 RedrawingDisabled = 0;
10834 ins_compl_check_keys(0);
10835 rettv->vval.v_number = compl_interrupted;
10836 RedrawingDisabled = saved;
10837}
10838#endif
10839
Bram Moolenaar071d4272004-06-13 20:20:40 +000010840/*
10841 * "confirm(message, buttons[, default [, type]])" function
10842 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010844f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010845{
10846#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10847 char_u *message;
10848 char_u *buttons = NULL;
10849 char_u buf[NUMBUFLEN];
10850 char_u buf2[NUMBUFLEN];
10851 int def = 1;
10852 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010853 char_u *typestr;
10854 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010856 message = get_tv_string_chk(&argvars[0]);
10857 if (message == NULL)
10858 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010859 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010860 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010861 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10862 if (buttons == NULL)
10863 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010864 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010865 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010866 def = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010867 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010869 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10870 if (typestr == NULL)
10871 error = TRUE;
10872 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010874 switch (TOUPPER_ASC(*typestr))
10875 {
10876 case 'E': type = VIM_ERROR; break;
10877 case 'Q': type = VIM_QUESTION; break;
10878 case 'I': type = VIM_INFO; break;
10879 case 'W': type = VIM_WARNING; break;
10880 case 'G': type = VIM_GENERIC; break;
10881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010882 }
10883 }
10884 }
10885 }
10886
10887 if (buttons == NULL || *buttons == NUL)
10888 buttons = (char_u *)_("&Ok");
10889
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010890 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010891 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010892 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893#endif
10894}
10895
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010896/*
10897 * "copy()" function
10898 */
10899 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010900f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010901{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010902 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010903}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010904
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010905#ifdef FEAT_FLOAT
10906/*
10907 * "cos()" function
10908 */
10909 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010910f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010911{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010912 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010913
10914 rettv->v_type = VAR_FLOAT;
10915 if (get_float_arg(argvars, &f) == OK)
10916 rettv->vval.v_float = cos(f);
10917 else
10918 rettv->vval.v_float = 0.0;
10919}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010920
10921/*
10922 * "cosh()" function
10923 */
10924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010925f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010926{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010927 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010928
10929 rettv->v_type = VAR_FLOAT;
10930 if (get_float_arg(argvars, &f) == OK)
10931 rettv->vval.v_float = cosh(f);
10932 else
10933 rettv->vval.v_float = 0.0;
10934}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010935#endif
10936
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010938 * "count()" function
10939 */
10940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010941f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010942{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010943 long n = 0;
10944 int ic = FALSE;
10945
Bram Moolenaare9a41262005-01-15 22:18:47 +000010946 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010947 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010948 listitem_T *li;
10949 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010950 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010951
Bram Moolenaare9a41262005-01-15 22:18:47 +000010952 if ((l = argvars[0].vval.v_list) != NULL)
10953 {
10954 li = l->lv_first;
10955 if (argvars[2].v_type != VAR_UNKNOWN)
10956 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010957 int error = FALSE;
10958
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010959 ic = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010960 if (argvars[3].v_type != VAR_UNKNOWN)
10961 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010962 idx = (long)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010963 if (!error)
10964 {
10965 li = list_find(l, idx);
10966 if (li == NULL)
10967 EMSGN(_(e_listidx), idx);
10968 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010969 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010970 if (error)
10971 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010972 }
10973
10974 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010975 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010976 ++n;
10977 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010978 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010979 else if (argvars[0].v_type == VAR_DICT)
10980 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010981 int todo;
10982 dict_T *d;
10983 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010984
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010985 if ((d = argvars[0].vval.v_dict) != NULL)
10986 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010987 int error = FALSE;
10988
Bram Moolenaare9a41262005-01-15 22:18:47 +000010989 if (argvars[2].v_type != VAR_UNKNOWN)
10990 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020010991 ic = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010992 if (argvars[3].v_type != VAR_UNKNOWN)
10993 EMSG(_(e_invarg));
10994 }
10995
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010996 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010997 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010998 {
10999 if (!HASHITEM_EMPTY(hi))
11000 {
11001 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010011002 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011003 ++n;
11004 }
11005 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011006 }
11007 }
11008 else
11009 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011010 rettv->vval.v_number = n;
11011}
11012
11013/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011014 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
11015 *
11016 * Checks the existence of a cscope connection.
11017 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011018 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011019f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020{
11021#ifdef FEAT_CSCOPE
11022 int num = 0;
11023 char_u *dbpath = NULL;
11024 char_u *prepend = NULL;
11025 char_u buf[NUMBUFLEN];
11026
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011027 if (argvars[0].v_type != VAR_UNKNOWN
11028 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011029 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011030 num = (int)get_tv_number(&argvars[0]);
11031 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011032 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011033 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011034 }
11035
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011036 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037#endif
11038}
11039
11040/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011041 * "cursor(lnum, col)" function, or
11042 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011044 * Moves the cursor to the specified line and column.
11045 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011047 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011048f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011049{
11050 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011051#ifdef FEAT_VIRTUALEDIT
11052 long coladd = 0;
11053#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011054 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011055
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011056 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011057 if (argvars[1].v_type == VAR_UNKNOWN)
11058 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011059 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020011060 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011061
Bram Moolenaar493c1782014-05-28 14:34:46 +020011062 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011063 {
11064 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000011065 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011066 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011067 line = pos.lnum;
11068 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011069#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011070 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000011071#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020011072 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011073 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020011074 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011075 set_curswant = FALSE;
11076 }
Bram Moolenaara5525202006-03-02 22:52:09 +000011077 }
11078 else
11079 {
11080 line = get_tv_lnum(argvars);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011081 col = (long)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaara5525202006-03-02 22:52:09 +000011082#ifdef FEAT_VIRTUALEDIT
11083 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011084 coladd = (long)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaara5525202006-03-02 22:52:09 +000011085#endif
11086 }
11087 if (line < 0 || col < 0
11088#ifdef FEAT_VIRTUALEDIT
11089 || coladd < 0
11090#endif
11091 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011092 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011093 if (line > 0)
11094 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011095 if (col > 0)
11096 curwin->w_cursor.col = col - 1;
11097#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000011098 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011099#endif
11100
11101 /* Make sure the cursor is in a valid position. */
11102 check_cursor();
11103#ifdef FEAT_MBYTE
11104 /* Correct cursor for multi-byte character. */
11105 if (has_mbyte)
11106 mb_adjust_cursor();
11107#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000011108
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011109 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011110 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111}
11112
11113/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011114 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115 */
11116 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011117f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011118{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011119 int noref = 0;
11120
11121 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011122 noref = (int)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011123 if (noref < 0 || noref > 1)
11124 EMSG(_(e_invarg));
11125 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000011126 {
11127 current_copyID += COPYID_INC;
11128 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
11129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130}
11131
11132/*
11133 * "delete()" function
11134 */
11135 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011136f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137{
Bram Moolenaarda440d22016-01-16 21:27:23 +010011138 char_u nbuf[NUMBUFLEN];
11139 char_u *name;
11140 char_u *flags;
11141
11142 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011143 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010011144 return;
11145
11146 name = get_tv_string(&argvars[0]);
11147 if (name == NULL || *name == NUL)
11148 {
11149 EMSG(_(e_invarg));
11150 return;
11151 }
11152
11153 if (argvars[1].v_type != VAR_UNKNOWN)
11154 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011155 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010011156 flags = (char_u *)"";
11157
11158 if (*flags == NUL)
11159 /* delete a file */
11160 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
11161 else if (STRCMP(flags, "d") == 0)
11162 /* delete an empty directory */
11163 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
11164 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010011165 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010011166 rettv->vval.v_number = delete_recursive(name);
11167 else
11168 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011169}
11170
11171/*
11172 * "did_filetype()" function
11173 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011175f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011176{
11177#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011178 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011179#endif
11180}
11181
11182/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000011183 * "diff_filler()" function
11184 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011186f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011187{
11188#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011189 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000011190#endif
11191}
11192
11193/*
11194 * "diff_hlID()" function
11195 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011197f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011198{
11199#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011200 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011201 static linenr_T prev_lnum = 0;
11202 static int changedtick = 0;
11203 static int fnum = 0;
11204 static int change_start = 0;
11205 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011206 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011207 int filler_lines;
11208 int col;
11209
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011210 if (lnum < 0) /* ignore type error in {lnum} arg */
11211 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011212 if (lnum != prev_lnum
11213 || changedtick != curbuf->b_changedtick
11214 || fnum != curbuf->b_fnum)
11215 {
11216 /* New line, buffer, change: need to get the values. */
11217 filler_lines = diff_check(curwin, lnum);
11218 if (filler_lines < 0)
11219 {
11220 if (filler_lines == -1)
11221 {
11222 change_start = MAXCOL;
11223 change_end = -1;
11224 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11225 hlID = HLF_ADD; /* added line */
11226 else
11227 hlID = HLF_CHD; /* changed line */
11228 }
11229 else
11230 hlID = HLF_ADD; /* added line */
11231 }
11232 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011233 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011234 prev_lnum = lnum;
11235 changedtick = curbuf->b_changedtick;
11236 fnum = curbuf->b_fnum;
11237 }
11238
11239 if (hlID == HLF_CHD || hlID == HLF_TXD)
11240 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011241 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011242 if (col >= change_start && col <= change_end)
11243 hlID = HLF_TXD; /* changed text */
11244 else
11245 hlID = HLF_CHD; /* changed line */
11246 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011247 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011248#endif
11249}
11250
11251/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011252 * "empty({expr})" function
11253 */
11254 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011255f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011256{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011257 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011258
11259 switch (argvars[0].v_type)
11260 {
11261 case VAR_STRING:
11262 case VAR_FUNC:
11263 n = argvars[0].vval.v_string == NULL
11264 || *argvars[0].vval.v_string == NUL;
11265 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011266 case VAR_PARTIAL:
11267 n = FALSE;
11268 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011269 case VAR_NUMBER:
11270 n = argvars[0].vval.v_number == 0;
11271 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011272 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011273#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011274 n = argvars[0].vval.v_float == 0.0;
11275 break;
11276#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011277 case VAR_LIST:
11278 n = argvars[0].vval.v_list == NULL
11279 || argvars[0].vval.v_list->lv_first == NULL;
11280 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011281 case VAR_DICT:
11282 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011283 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011284 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011285 case VAR_SPECIAL:
11286 n = argvars[0].vval.v_number != VVAL_TRUE;
11287 break;
11288
Bram Moolenaar835dc632016-02-07 14:27:38 +010011289 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011290#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011291 n = argvars[0].vval.v_job == NULL
11292 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11293 break;
11294#endif
11295 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011296#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011297 n = argvars[0].vval.v_channel == NULL
11298 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011299 break;
11300#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011301 case VAR_UNKNOWN:
11302 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11303 n = TRUE;
11304 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011305 }
11306
11307 rettv->vval.v_number = n;
11308}
11309
11310/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011311 * "escape({string}, {chars})" function
11312 */
11313 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011314f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011315{
11316 char_u buf[NUMBUFLEN];
11317
Bram Moolenaar758711c2005-02-02 23:11:38 +000011318 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11319 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011320 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011321}
11322
11323/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011324 * "eval()" function
11325 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011326 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011327f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011328{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011329 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011330
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011331 s = get_tv_string_chk(&argvars[0]);
11332 if (s != NULL)
11333 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011334
Bram Moolenaar615b9972015-01-14 17:15:05 +010011335 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011336 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11337 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011338 if (p != NULL && !aborting())
11339 EMSG2(_(e_invexpr2), p);
11340 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011341 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011342 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011343 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011344 else if (*s != NUL)
11345 EMSG(_(e_trailing));
11346}
11347
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +020011348static garray_T redir_evalcmd_ga;
11349
11350/*
11351 * Append "value[value_len]" to the evalcmd() output.
11352 */
11353 void
11354evalcmd_redir_str(char_u *value, int value_len)
11355{
11356 int len;
11357
11358 if (value_len == -1)
11359 len = (int)STRLEN(value); /* Append the entire string */
11360 else
11361 len = value_len; /* Append only "value_len" characters */
11362 if (ga_grow(&redir_evalcmd_ga, len) == OK)
11363 {
11364 mch_memmove((char *)redir_evalcmd_ga.ga_data
11365 + redir_evalcmd_ga.ga_len, value, len);
11366 redir_evalcmd_ga.ga_len += len;
11367 }
11368}
11369
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011370/*
Bram Moolenaar1e5e1232016-07-07 17:33:02 +020011371 * "evalcmd()" function
11372 */
11373 static void
11374f_evalcmd(typval_T *argvars, typval_T *rettv)
11375{
11376 char_u *s;
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +020011377 int save_msg_silent = msg_silent;
11378 int save_redir_evalcmd = redir_evalcmd;
11379 garray_T save_ga;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +020011380
11381 rettv->vval.v_string = NULL;
11382 rettv->v_type = VAR_STRING;
11383
11384 s = get_tv_string_chk(&argvars[0]);
11385 if (s != NULL)
11386 {
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +020011387 if (redir_evalcmd)
11388 save_ga = redir_evalcmd_ga;
11389 ga_init2(&redir_evalcmd_ga, (int)sizeof(char), 500);
11390 redir_evalcmd = TRUE;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +020011391
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +020011392 ++msg_silent;
11393 do_cmdline_cmd(s);
11394 rettv->vval.v_string = redir_evalcmd_ga.ga_data;
11395 msg_silent = save_msg_silent;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +020011396
Bram Moolenaarbc5d6dd2016-07-07 23:04:18 +020011397 redir_evalcmd = save_redir_evalcmd;
11398 if (redir_evalcmd)
11399 redir_evalcmd_ga = save_ga;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +020011400 }
Bram Moolenaar1e5e1232016-07-07 17:33:02 +020011401}
11402
11403/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011404 * "eventhandler()" function
11405 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011406 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011407f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011408{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011409 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011410}
11411
11412/*
11413 * "executable()" function
11414 */
11415 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011416f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011417{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011418 char_u *name = get_tv_string(&argvars[0]);
11419
11420 /* Check in $PATH and also check directly if there is a directory name. */
11421 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11422 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011423}
11424
11425/*
11426 * "exepath()" function
11427 */
11428 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011429f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011430{
11431 char_u *p = NULL;
11432
Bram Moolenaarb5971142015-03-21 17:32:19 +010011433 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011434 rettv->v_type = VAR_STRING;
11435 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011436}
11437
11438/*
11439 * "exists()" function
11440 */
11441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011442f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011443{
11444 char_u *p;
11445 char_u *name;
11446 int n = FALSE;
11447 int len = 0;
11448
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011449 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011450 if (*p == '$') /* environment variable */
11451 {
11452 /* first try "normal" environment variables (fast) */
11453 if (mch_getenv(p + 1) != NULL)
11454 n = TRUE;
11455 else
11456 {
11457 /* try expanding things like $VIM and ${HOME} */
11458 p = expand_env_save(p);
11459 if (p != NULL && *p != '$')
11460 n = TRUE;
11461 vim_free(p);
11462 }
11463 }
11464 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011465 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011466 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011467 if (*skipwhite(p) != NUL)
11468 n = FALSE; /* trailing garbage */
11469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011470 else if (*p == '*') /* internal or user defined function */
11471 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011472 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011473 }
11474 else if (*p == ':')
11475 {
11476 n = cmd_exists(p + 1);
11477 }
11478 else if (*p == '#')
11479 {
11480#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011481 if (p[1] == '#')
11482 n = autocmd_supported(p + 2);
11483 else
11484 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011485#endif
11486 }
11487 else /* internal variable */
11488 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011489 char_u *tofree;
11490 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011491
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011492 /* get_name_len() takes care of expanding curly braces */
11493 name = p;
11494 len = get_name_len(&p, &tofree, TRUE, FALSE);
11495 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011496 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011497 if (tofree != NULL)
11498 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011499 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011500 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011501 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011502 /* handle d.key, l[idx], f(expr) */
11503 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11504 if (n)
11505 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011506 }
11507 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011508 if (*p != NUL)
11509 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011510
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011511 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011512 }
11513
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011514 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011515}
11516
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011517#ifdef FEAT_FLOAT
11518/*
11519 * "exp()" function
11520 */
11521 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011522f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011523{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011524 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011525
11526 rettv->v_type = VAR_FLOAT;
11527 if (get_float_arg(argvars, &f) == OK)
11528 rettv->vval.v_float = exp(f);
11529 else
11530 rettv->vval.v_float = 0.0;
11531}
11532#endif
11533
Bram Moolenaar071d4272004-06-13 20:20:40 +000011534/*
11535 * "expand()" function
11536 */
11537 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011538f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011539{
11540 char_u *s;
11541 int len;
11542 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011543 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011544 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011545 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011546 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011547
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011548 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011549 if (argvars[1].v_type != VAR_UNKNOWN
11550 && argvars[2].v_type != VAR_UNKNOWN
11551 && get_tv_number_chk(&argvars[2], &error)
11552 && !error)
11553 {
11554 rettv->v_type = VAR_LIST;
11555 rettv->vval.v_list = NULL;
11556 }
11557
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011558 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011559 if (*s == '%' || *s == '#' || *s == '<')
11560 {
11561 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011562 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011563 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011564 if (rettv->v_type == VAR_LIST)
11565 {
11566 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11567 list_append_string(rettv->vval.v_list, result, -1);
11568 else
11569 vim_free(result);
11570 }
11571 else
11572 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011573 }
11574 else
11575 {
11576 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011577 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011578 if (argvars[1].v_type != VAR_UNKNOWN
11579 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011580 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011581 if (!error)
11582 {
11583 ExpandInit(&xpc);
11584 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011585 if (p_wic)
11586 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011587 if (rettv->v_type == VAR_STRING)
11588 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11589 options, WILD_ALL);
11590 else if (rettv_list_alloc(rettv) != FAIL)
11591 {
11592 int i;
11593
11594 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11595 for (i = 0; i < xpc.xp_numfiles; i++)
11596 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11597 ExpandCleanup(&xpc);
11598 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011599 }
11600 else
11601 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011602 }
11603}
11604
11605/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011606 * Go over all entries in "d2" and add them to "d1".
11607 * When "action" is "error" then a duplicate key is an error.
11608 * When "action" is "force" then a duplicate key is overwritten.
11609 * Otherwise duplicate keys are ignored ("action" is "keep").
11610 */
11611 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011612dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011613{
11614 dictitem_T *di1;
11615 hashitem_T *hi2;
11616 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011617 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011618
11619 todo = (int)d2->dv_hashtab.ht_used;
11620 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11621 {
11622 if (!HASHITEM_EMPTY(hi2))
11623 {
11624 --todo;
11625 di1 = dict_find(d1, hi2->hi_key, -1);
11626 if (d1->dv_scope != 0)
11627 {
11628 /* Disallow replacing a builtin function in l: and g:.
11629 * Check the key to be valid when adding to any
11630 * scope. */
11631 if (d1->dv_scope == VAR_DEF_SCOPE
11632 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11633 && var_check_func_name(hi2->hi_key,
11634 di1 == NULL))
11635 break;
11636 if (!valid_varname(hi2->hi_key))
11637 break;
11638 }
11639 if (di1 == NULL)
11640 {
11641 di1 = dictitem_copy(HI2DI(hi2));
11642 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11643 dictitem_free(di1);
11644 }
11645 else if (*action == 'e')
11646 {
11647 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11648 break;
11649 }
11650 else if (*action == 'f' && HI2DI(hi2) != di1)
11651 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011652 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11653 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011654 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011655 clear_tv(&di1->di_tv);
11656 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11657 }
11658 }
11659 }
11660}
11661
11662/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011663 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011664 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011665 */
11666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011667f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011668{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011669 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011670
Bram Moolenaare9a41262005-01-15 22:18:47 +000011671 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011672 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011673 list_T *l1, *l2;
11674 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011675 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011676 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011677
Bram Moolenaare9a41262005-01-15 22:18:47 +000011678 l1 = argvars[0].vval.v_list;
11679 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011680 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011681 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011682 {
11683 if (argvars[2].v_type != VAR_UNKNOWN)
11684 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011685 before = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011686 if (error)
11687 return; /* type error; errmsg already given */
11688
Bram Moolenaar758711c2005-02-02 23:11:38 +000011689 if (before == l1->lv_len)
11690 item = NULL;
11691 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011692 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011693 item = list_find(l1, before);
11694 if (item == NULL)
11695 {
11696 EMSGN(_(e_listidx), before);
11697 return;
11698 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011699 }
11700 }
11701 else
11702 item = NULL;
11703 list_extend(l1, l2, item);
11704
Bram Moolenaare9a41262005-01-15 22:18:47 +000011705 copy_tv(&argvars[0], rettv);
11706 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011707 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011708 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11709 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011710 dict_T *d1, *d2;
11711 char_u *action;
11712 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011713
11714 d1 = argvars[0].vval.v_dict;
11715 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011716 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011717 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011718 {
11719 /* Check the third argument. */
11720 if (argvars[2].v_type != VAR_UNKNOWN)
11721 {
11722 static char *(av[]) = {"keep", "force", "error"};
11723
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011724 action = get_tv_string_chk(&argvars[2]);
11725 if (action == NULL)
11726 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011727 for (i = 0; i < 3; ++i)
11728 if (STRCMP(action, av[i]) == 0)
11729 break;
11730 if (i == 3)
11731 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011732 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011733 return;
11734 }
11735 }
11736 else
11737 action = (char_u *)"force";
11738
Bram Moolenaara9922d62013-05-30 13:01:18 +020011739 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011740
Bram Moolenaare9a41262005-01-15 22:18:47 +000011741 copy_tv(&argvars[0], rettv);
11742 }
11743 }
11744 else
11745 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011746}
11747
11748/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011749 * "feedkeys()" function
11750 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011751 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011752f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011753{
11754 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011755 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011756 char_u *keys, *flags;
11757 char_u nbuf[NUMBUFLEN];
11758 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011759 int execute = FALSE;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011760 int dangerous = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011761 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011762
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011763 /* This is not allowed in the sandbox. If the commands would still be
11764 * executed in the sandbox it would be OK, but it probably happens later,
11765 * when "sandbox" is no longer set. */
11766 if (check_secure())
11767 return;
11768
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011769 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011770
11771 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011772 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011773 flags = get_tv_string_buf(&argvars[1], nbuf);
11774 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011775 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011776 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011777 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011778 case 'n': remap = FALSE; break;
11779 case 'm': remap = TRUE; break;
11780 case 't': typed = TRUE; break;
11781 case 'i': insert = TRUE; break;
11782 case 'x': execute = TRUE; break;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011783 case '!': dangerous = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011784 }
11785 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011786 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011787
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011788 if (*keys != NUL || execute)
11789 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011790 /* Need to escape K_SPECIAL and CSI before putting the string in the
11791 * typeahead buffer. */
11792 keys_esc = vim_strsave_escape_csi(keys);
11793 if (keys_esc != NULL)
11794 {
11795 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011796 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011797 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011798 if (vgetc_busy)
11799 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011800 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011801 {
11802 int save_msg_scroll = msg_scroll;
11803
11804 /* Avoid a 1 second delay when the keys start Insert mode. */
11805 msg_scroll = FALSE;
Bram Moolenaar9bd547a2016-04-01 21:00:48 +020011806
Bram Moolenaar245c4102016-04-20 17:37:41 +020011807 if (!dangerous)
11808 ++ex_normal_busy;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011809 exec_normal(TRUE);
Bram Moolenaar245c4102016-04-20 17:37:41 +020011810 if (!dangerous)
11811 --ex_normal_busy;
Bram Moolenaar9e496852016-03-11 19:31:47 +010011812 msg_scroll |= save_msg_scroll;
11813 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011814 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011815 }
11816}
11817
11818/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011819 * "filereadable()" function
11820 */
11821 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011822f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011823{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011824 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011825 char_u *p;
11826 int n;
11827
Bram Moolenaarc236c162008-07-13 17:41:49 +000011828#ifndef O_NONBLOCK
11829# define O_NONBLOCK 0
11830#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011831 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011832 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11833 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011834 {
11835 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011836 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011837 }
11838 else
11839 n = FALSE;
11840
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011841 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011842}
11843
11844/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011845 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011846 * rights to write into.
11847 */
11848 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011849f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011850{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011851 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011852}
11853
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011854 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011855findfilendir(
11856 typval_T *argvars UNUSED,
11857 typval_T *rettv,
11858 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011859{
11860#ifdef FEAT_SEARCHPATH
11861 char_u *fname;
11862 char_u *fresult = NULL;
11863 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11864 char_u *p;
11865 char_u pathbuf[NUMBUFLEN];
11866 int count = 1;
11867 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011868 int error = FALSE;
11869#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011870
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011871 rettv->vval.v_string = NULL;
11872 rettv->v_type = VAR_STRING;
11873
11874#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011875 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011876
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011877 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011878 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011879 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11880 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011881 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011882 else
11883 {
11884 if (*p != NUL)
11885 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011886
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011887 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011888 count = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011889 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011890 }
11891
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011892 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11893 error = TRUE;
11894
11895 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011896 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011897 do
11898 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011899 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011900 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011901 fresult = find_file_in_path_option(first ? fname : NULL,
11902 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011903 0, first, path,
11904 find_what,
11905 curbuf->b_ffname,
11906 find_what == FINDFILE_DIR
11907 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011908 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011909
11910 if (fresult != NULL && rettv->v_type == VAR_LIST)
11911 list_append_string(rettv->vval.v_list, fresult, -1);
11912
11913 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011914 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011915
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011916 if (rettv->v_type == VAR_STRING)
11917 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011918#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011919}
11920
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011921static void filter_map(typval_T *argvars, typval_T *rettv, int map);
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020011922static int filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011923
11924/*
11925 * Implementation of map() and filter().
11926 */
11927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011928filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011929{
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020011930 typval_T *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011931 listitem_T *li, *nli;
11932 list_T *l = NULL;
11933 dictitem_T *di;
11934 hashtab_T *ht;
11935 hashitem_T *hi;
11936 dict_T *d = NULL;
11937 typval_T save_val;
11938 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011939 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011940 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011941 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011942 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011943 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011944 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011945 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011946
Bram Moolenaare9a41262005-01-15 22:18:47 +000011947 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011948 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011949 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011950 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011951 return;
11952 }
11953 else if (argvars[0].v_type == VAR_DICT)
11954 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011955 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011956 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011957 return;
11958 }
11959 else
11960 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011961 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011962 return;
11963 }
11964
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020011965 expr = &argvars[1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011966 /* On type errors, the preceding call has already displayed an error
11967 * message. Avoid a misleading error message for an empty string that
11968 * was not passed as argument. */
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020011969 if (expr->v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011970 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011971 prepare_vimvar(VV_VAL, &save_val);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011972
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011973 /* We reset "did_emsg" to be able to detect whether an error
11974 * occurred during evaluation of the expression. */
11975 save_did_emsg = did_emsg;
11976 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011977
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011978 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011979 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011980 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011981 vimvars[VV_KEY].vv_type = VAR_STRING;
11982
11983 ht = &d->dv_hashtab;
11984 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011985 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011986 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011987 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011988 if (!HASHITEM_EMPTY(hi))
11989 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011990 int r;
11991
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011992 --todo;
11993 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011994 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011995 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11996 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011997 break;
11998 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011999 r = filter_map_one(&di->di_tv, expr, map, &rem);
12000 clear_tv(&vimvars[VV_KEY].vv_tv);
12001 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012002 break;
12003 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012004 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020012005 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
12006 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012007 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012008 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012009 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012010 }
12011 }
12012 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012013 }
12014 else
12015 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000012016 vimvars[VV_KEY].vv_type = VAR_NUMBER;
12017
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012018 for (li = l->lv_first; li != NULL; li = nli)
12019 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020012020 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012021 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012022 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020012023 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000012024 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012025 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012026 break;
12027 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012028 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020012029 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012030 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012031 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012032
Bram Moolenaar627b1d32009-11-17 11:20:35 +000012033 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012034 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012035
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012036 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012037 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012038
12039 copy_tv(&argvars[0], rettv);
12040}
12041
12042 static int
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012043filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012044{
Bram Moolenaar33570922005-01-25 22:26:29 +000012045 typval_T rettv;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012046 typval_T argv[3];
Bram Moolenaara06ec8f2016-07-08 20:11:07 +020012047 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000012048 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012049 int retval = FAIL;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012050 int dummy;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012051
Bram Moolenaar33570922005-01-25 22:26:29 +000012052 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012053 argv[0] = vimvars[VV_KEY].vv_tv;
12054 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012055 if (expr->v_type == VAR_FUNC)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012056 {
Bram Moolenaara06ec8f2016-07-08 20:11:07 +020012057 s = expr->vval.v_string;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012058 if (call_func(s, (int)STRLEN(s),
12059 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
12060 goto theend;
12061 }
12062 else if (expr->v_type == VAR_PARTIAL)
12063 {
12064 partial_T *partial = expr->vval.v_partial;
12065
12066 s = partial->pt_name;
12067 if (call_func(s, (int)STRLEN(s),
12068 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, partial, NULL)
12069 == FAIL)
12070 goto theend;
12071 }
12072 else
12073 {
Bram Moolenaara06ec8f2016-07-08 20:11:07 +020012074 s = get_tv_string_buf_chk(expr, buf);
12075 if (s == NULL)
12076 goto theend;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012077 s = skipwhite(s);
12078 if (eval1(&s, &rettv, TRUE) == FAIL)
12079 goto theend;
12080 if (*s != NUL) /* check for trailing chars after expr */
12081 {
12082 EMSG2(_(e_invexpr2), s);
12083 goto theend;
12084 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012085 }
12086 if (map)
12087 {
12088 /* map(): replace the list item value */
12089 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000012090 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012091 *tv = rettv;
12092 }
12093 else
12094 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012095 int error = FALSE;
12096
Bram Moolenaare9a41262005-01-15 22:18:47 +000012097 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012098 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012099 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012100 /* On type error, nothing has been removed; return FAIL to stop the
12101 * loop. The error message was given by get_tv_number_chk(). */
12102 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012103 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012104 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012105 retval = OK;
12106theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000012107 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012108 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012109}
12110
12111/*
12112 * "filter()" function
12113 */
12114 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012115f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012116{
12117 filter_map(argvars, rettv, FALSE);
12118}
12119
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012120/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012121 * "finddir({fname}[, {path}[, {count}]])" function
12122 */
12123 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012124f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012125{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012126 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012127}
12128
12129/*
12130 * "findfile({fname}[, {path}[, {count}]])" function
12131 */
12132 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012133f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012134{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012135 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012136}
12137
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012138#ifdef FEAT_FLOAT
12139/*
12140 * "float2nr({float})" function
12141 */
12142 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012143f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012144{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012145 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012146
12147 if (get_float_arg(argvars, &f) == OK)
12148 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020012149# ifdef FEAT_NUM64
12150 if (f < -0x7fffffffffffffff)
12151 rettv->vval.v_number = -0x7fffffffffffffff;
12152 else if (f > 0x7fffffffffffffff)
12153 rettv->vval.v_number = 0x7fffffffffffffff;
12154 else
12155 rettv->vval.v_number = (varnumber_T)f;
12156# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012157 if (f < -0x7fffffff)
12158 rettv->vval.v_number = -0x7fffffff;
12159 else if (f > 0x7fffffff)
12160 rettv->vval.v_number = 0x7fffffff;
12161 else
12162 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020012163# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012164 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012165}
12166
12167/*
12168 * "floor({float})" function
12169 */
12170 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012171f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012172{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012173 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012174
12175 rettv->v_type = VAR_FLOAT;
12176 if (get_float_arg(argvars, &f) == OK)
12177 rettv->vval.v_float = floor(f);
12178 else
12179 rettv->vval.v_float = 0.0;
12180}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012181
12182/*
12183 * "fmod()" function
12184 */
12185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012186f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012187{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012188 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012189
12190 rettv->v_type = VAR_FLOAT;
12191 if (get_float_arg(argvars, &fx) == OK
12192 && get_float_arg(&argvars[1], &fy) == OK)
12193 rettv->vval.v_float = fmod(fx, fy);
12194 else
12195 rettv->vval.v_float = 0.0;
12196}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012197#endif
12198
Bram Moolenaar0d660222005-01-07 21:51:51 +000012199/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012200 * "fnameescape({string})" function
12201 */
12202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012203f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012204{
12205 rettv->vval.v_string = vim_strsave_fnameescape(
12206 get_tv_string(&argvars[0]), FALSE);
12207 rettv->v_type = VAR_STRING;
12208}
12209
12210/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012211 * "fnamemodify({fname}, {mods})" function
12212 */
12213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012214f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012215{
12216 char_u *fname;
12217 char_u *mods;
12218 int usedlen = 0;
12219 int len;
12220 char_u *fbuf = NULL;
12221 char_u buf[NUMBUFLEN];
12222
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012223 fname = get_tv_string_chk(&argvars[0]);
12224 mods = get_tv_string_buf_chk(&argvars[1], buf);
12225 if (fname == NULL || mods == NULL)
12226 fname = NULL;
12227 else
12228 {
12229 len = (int)STRLEN(fname);
12230 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
12231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012232
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012233 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012234 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012235 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012236 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012237 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012238 vim_free(fbuf);
12239}
12240
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012241static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012242
12243/*
12244 * "foldclosed()" function
12245 */
12246 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012247foldclosed_both(
12248 typval_T *argvars UNUSED,
12249 typval_T *rettv,
12250 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012251{
12252#ifdef FEAT_FOLDING
12253 linenr_T lnum;
12254 linenr_T first, last;
12255
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012256 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012257 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12258 {
12259 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12260 {
12261 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012262 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012263 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012264 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012265 return;
12266 }
12267 }
12268#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012269 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012270}
12271
12272/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012273 * "foldclosed()" function
12274 */
12275 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012276f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012277{
12278 foldclosed_both(argvars, rettv, FALSE);
12279}
12280
12281/*
12282 * "foldclosedend()" function
12283 */
12284 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012285f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012286{
12287 foldclosed_both(argvars, rettv, TRUE);
12288}
12289
12290/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012291 * "foldlevel()" function
12292 */
12293 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012294f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012295{
12296#ifdef FEAT_FOLDING
12297 linenr_T lnum;
12298
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012299 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012300 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012301 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012302#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012303}
12304
12305/*
12306 * "foldtext()" function
12307 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012308 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012309f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012310{
12311#ifdef FEAT_FOLDING
12312 linenr_T lnum;
12313 char_u *s;
12314 char_u *r;
12315 int len;
12316 char *txt;
12317#endif
12318
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012319 rettv->v_type = VAR_STRING;
12320 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012321#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012322 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12323 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12324 <= curbuf->b_ml.ml_line_count
12325 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012326 {
12327 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012328 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12329 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012330 {
12331 if (!linewhite(lnum))
12332 break;
12333 ++lnum;
12334 }
12335
12336 /* Find interesting text in this line. */
12337 s = skipwhite(ml_get(lnum));
12338 /* skip C comment-start */
12339 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012340 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012341 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012342 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012343 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012344 {
12345 s = skipwhite(ml_get(lnum + 1));
12346 if (*s == '*')
12347 s = skipwhite(s + 1);
12348 }
12349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012350 txt = _("+-%s%3ld lines: ");
12351 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012352 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012353 + 20 /* for %3ld */
12354 + STRLEN(s))); /* concatenated */
12355 if (r != NULL)
12356 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012357 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12358 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12359 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012360 len = (int)STRLEN(r);
12361 STRCAT(r, s);
12362 /* remove 'foldmarker' and 'commentstring' */
12363 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012364 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012365 }
12366 }
12367#endif
12368}
12369
12370/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012371 * "foldtextresult(lnum)" function
12372 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012373 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012374f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012375{
12376#ifdef FEAT_FOLDING
12377 linenr_T lnum;
12378 char_u *text;
12379 char_u buf[51];
12380 foldinfo_T foldinfo;
12381 int fold_count;
12382#endif
12383
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012384 rettv->v_type = VAR_STRING;
12385 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012386#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012387 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012388 /* treat illegal types and illegal string values for {lnum} the same */
12389 if (lnum < 0)
12390 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012391 fold_count = foldedCount(curwin, lnum, &foldinfo);
12392 if (fold_count > 0)
12393 {
12394 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12395 &foldinfo, buf);
12396 if (text == buf)
12397 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012398 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012399 }
12400#endif
12401}
12402
12403/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012404 * "foreground()" function
12405 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012406 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012407f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012408{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012409#ifdef FEAT_GUI
12410 if (gui.in_use)
12411 gui_mch_set_foreground();
12412#else
12413# ifdef WIN32
12414 win32_set_foreground();
12415# endif
12416#endif
12417}
12418
12419/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012420 * "function()" function
12421 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012422 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012423f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012424{
12425 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012426 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012427 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012428 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012429
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012430 if (argvars[0].v_type == VAR_FUNC)
12431 {
12432 /* function(MyFunc, [arg], dict) */
12433 s = argvars[0].vval.v_string;
12434 }
12435 else if (argvars[0].v_type == VAR_PARTIAL
12436 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012437 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012438 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012439 arg_pt = argvars[0].vval.v_partial;
12440 s = arg_pt->pt_name;
12441 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012442 else
12443 {
12444 /* function('MyFunc', [arg], dict) */
12445 s = get_tv_string(&argvars[0]);
12446 use_string = TRUE;
12447 }
12448
12449 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012450 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012451 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012452 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
12453 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012454 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012455 else
12456 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012457 int dict_idx = 0;
12458 int arg_idx = 0;
12459 list_T *list = NULL;
12460
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012461 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012462 {
12463 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012464 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012465
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012466 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12467 * also be called from another script. Using trans_function_name()
12468 * would also work, but some plugins depend on the name being
12469 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012470 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012471 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
12472 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012473 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012474 STRCPY(name, sid_buf);
12475 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012476 }
12477 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012478 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012479 name = vim_strsave(s);
12480
12481 if (argvars[1].v_type != VAR_UNKNOWN)
12482 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012483 if (argvars[2].v_type != VAR_UNKNOWN)
12484 {
12485 /* function(name, [args], dict) */
12486 arg_idx = 1;
12487 dict_idx = 2;
12488 }
12489 else if (argvars[1].v_type == VAR_DICT)
12490 /* function(name, dict) */
12491 dict_idx = 1;
12492 else
12493 /* function(name, [args]) */
12494 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010012495 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012496 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012497 if (argvars[dict_idx].v_type != VAR_DICT)
12498 {
12499 EMSG(_("E922: expected a dict"));
12500 vim_free(name);
12501 return;
12502 }
12503 if (argvars[dict_idx].vval.v_dict == NULL)
12504 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012505 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012506 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012507 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012508 if (argvars[arg_idx].v_type != VAR_LIST)
12509 {
12510 EMSG(_("E923: Second argument of function() must be a list or a dict"));
12511 vim_free(name);
12512 return;
12513 }
12514 list = argvars[arg_idx].vval.v_list;
12515 if (list == NULL || list->lv_len == 0)
12516 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012517 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012518 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012519 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010012520 {
12521 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012522
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012523 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010012524 if (pt == NULL)
12525 vim_free(name);
12526 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012527 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012528 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012529 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012530 listitem_T *li;
12531 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012532 int arg_len = 0;
12533 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012534
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012535 if (arg_pt != NULL)
12536 arg_len = arg_pt->pt_argc;
12537 if (list != NULL)
12538 lv_len = list->lv_len;
12539 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012540 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012541 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012542 if (pt->pt_argv == NULL)
12543 {
12544 vim_free(pt);
12545 vim_free(name);
12546 return;
12547 }
12548 else
12549 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012550 for (i = 0; i < arg_len; i++)
12551 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
12552 if (lv_len > 0)
12553 for (li = list->lv_first; li != NULL;
12554 li = li->li_next)
12555 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012556 }
12557 }
12558
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012559 /* For "function(dict.func, [], dict)" and "func" is a partial
12560 * use "dict". That is backwards compatible. */
12561 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012562 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012563 /* The dict is bound explicitly, pt_auto is FALSE. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012564 pt->pt_dict = argvars[dict_idx].vval.v_dict;
12565 ++pt->pt_dict->dv_refcount;
12566 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012567 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012568 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012569 /* If the dict was bound automatically the result is also
12570 * bound automatically. */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012571 pt->pt_dict = arg_pt->pt_dict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020012572 pt->pt_auto = arg_pt->pt_auto;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012573 if (pt->pt_dict != NULL)
12574 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012575 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012576
12577 pt->pt_refcount = 1;
12578 pt->pt_name = name;
12579 func_ref(pt->pt_name);
12580 }
12581 rettv->v_type = VAR_PARTIAL;
12582 rettv->vval.v_partial = pt;
12583 }
12584 else
12585 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012586 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012587 rettv->v_type = VAR_FUNC;
12588 rettv->vval.v_string = name;
12589 func_ref(name);
12590 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012591 }
12592}
12593
12594/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012595 * "garbagecollect()" function
12596 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012597 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012598f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012599{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012600 /* This is postponed until we are back at the toplevel, because we may be
12601 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12602 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012603
12604 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12605 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012606}
12607
12608/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012609 * "get()" function
12610 */
12611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012612f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012613{
Bram Moolenaar33570922005-01-25 22:26:29 +000012614 listitem_T *li;
12615 list_T *l;
12616 dictitem_T *di;
12617 dict_T *d;
12618 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012619
Bram Moolenaare9a41262005-01-15 22:18:47 +000012620 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012621 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012622 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012623 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012624 int error = FALSE;
12625
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020012626 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012627 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012628 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012629 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012630 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012631 else if (argvars[0].v_type == VAR_DICT)
12632 {
12633 if ((d = argvars[0].vval.v_dict) != NULL)
12634 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012635 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012636 if (di != NULL)
12637 tv = &di->di_tv;
12638 }
12639 }
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012640 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012641 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012642 partial_T *pt;
12643 partial_T fref_pt;
12644
12645 if (argvars[0].v_type == VAR_PARTIAL)
12646 pt = argvars[0].vval.v_partial;
12647 else
12648 {
12649 vim_memset(&fref_pt, 0, sizeof(fref_pt));
12650 fref_pt.pt_name = argvars[0].vval.v_string;
12651 pt = &fref_pt;
12652 }
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012653
12654 if (pt != NULL)
12655 {
12656 char_u *what = get_tv_string(&argvars[1]);
12657
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012658 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012659 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012660 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012661 if (pt->pt_name == NULL)
12662 rettv->vval.v_string = NULL;
12663 else
12664 rettv->vval.v_string = vim_strsave(pt->pt_name);
12665 }
12666 else if (STRCMP(what, "dict") == 0)
12667 {
12668 rettv->v_type = VAR_DICT;
12669 rettv->vval.v_dict = pt->pt_dict;
12670 if (pt->pt_dict != NULL)
12671 ++pt->pt_dict->dv_refcount;
12672 }
12673 else if (STRCMP(what, "args") == 0)
12674 {
12675 rettv->v_type = VAR_LIST;
12676 if (rettv_list_alloc(rettv) == OK)
12677 {
12678 int i;
12679
12680 for (i = 0; i < pt->pt_argc; ++i)
12681 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
12682 }
12683 }
12684 else
12685 EMSG2(_(e_invarg2), what);
12686 return;
12687 }
12688 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012689 else
12690 EMSG2(_(e_listdictarg), "get()");
12691
12692 if (tv == NULL)
12693 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012694 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012695 copy_tv(&argvars[2], rettv);
12696 }
12697 else
12698 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012699}
12700
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012701static 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 +000012702
12703/*
12704 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012705 * Return a range (from start to end) of lines in rettv from the specified
12706 * buffer.
12707 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012708 */
12709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012710get_buffer_lines(
12711 buf_T *buf,
12712 linenr_T start,
12713 linenr_T end,
12714 int retlist,
12715 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012716{
12717 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012718
Bram Moolenaar959a1432013-12-14 12:17:38 +010012719 rettv->v_type = VAR_STRING;
12720 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012721 if (retlist && rettv_list_alloc(rettv) == FAIL)
12722 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012723
12724 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12725 return;
12726
12727 if (!retlist)
12728 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012729 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12730 p = ml_get_buf(buf, start, FALSE);
12731 else
12732 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012733 rettv->vval.v_string = vim_strsave(p);
12734 }
12735 else
12736 {
12737 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012738 return;
12739
12740 if (start < 1)
12741 start = 1;
12742 if (end > buf->b_ml.ml_line_count)
12743 end = buf->b_ml.ml_line_count;
12744 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012745 if (list_append_string(rettv->vval.v_list,
12746 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012747 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012748 }
12749}
12750
12751/*
12752 * "getbufline()" function
12753 */
12754 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012755f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012756{
12757 linenr_T lnum;
12758 linenr_T end;
12759 buf_T *buf;
12760
12761 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12762 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012763 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012764 --emsg_off;
12765
Bram Moolenaar661b1822005-07-28 22:36:45 +000012766 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012767 if (argvars[2].v_type == VAR_UNKNOWN)
12768 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012769 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012770 end = get_tv_lnum_buf(&argvars[2], buf);
12771
Bram Moolenaar342337a2005-07-21 21:11:17 +000012772 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012773}
12774
Bram Moolenaar0d660222005-01-07 21:51:51 +000012775/*
12776 * "getbufvar()" function
12777 */
12778 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012779f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012780{
12781 buf_T *buf;
12782 buf_T *save_curbuf;
12783 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012784 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012785 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012786
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012787 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12788 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012789 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012790 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012791
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012792 rettv->v_type = VAR_STRING;
12793 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012794
12795 if (buf != NULL && varname != NULL)
12796 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012797 /* set curbuf to be our buf, temporarily */
12798 save_curbuf = curbuf;
12799 curbuf = buf;
12800
Bram Moolenaar0d660222005-01-07 21:51:51 +000012801 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012802 {
12803 if (get_option_tv(&varname, rettv, TRUE) == OK)
12804 done = TRUE;
12805 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012806 else if (STRCMP(varname, "changedtick") == 0)
12807 {
12808 rettv->v_type = VAR_NUMBER;
12809 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012810 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012811 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012812 else
12813 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012814 /* Look up the variable. */
12815 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12816 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12817 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012818 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012819 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012820 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012821 done = TRUE;
12822 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012823 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012824
12825 /* restore previous notion of curbuf */
12826 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012827 }
12828
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012829 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12830 /* use the default value */
12831 copy_tv(&argvars[2], rettv);
12832
Bram Moolenaar0d660222005-01-07 21:51:51 +000012833 --emsg_off;
12834}
12835
12836/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012837 * "getchar()" function
12838 */
12839 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012840f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012841{
12842 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012843 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012844
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012845 /* Position the cursor. Needed after a message that ends in a space. */
12846 windgoto(msg_row, msg_col);
12847
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848 ++no_mapping;
12849 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012850 for (;;)
12851 {
12852 if (argvars[0].v_type == VAR_UNKNOWN)
12853 /* getchar(): blocking wait. */
12854 n = safe_vgetc();
12855 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12856 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012857 n = vpeekc_any();
12858 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012859 /* illegal argument or getchar(0) and no char avail: return zero */
12860 n = 0;
12861 else
12862 /* getchar(0) and char avail: return char */
12863 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012864
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012865 if (n == K_IGNORE)
12866 continue;
12867 break;
12868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012869 --no_mapping;
12870 --allow_keys;
12871
Bram Moolenaar219b8702006-11-01 14:32:36 +000012872 vimvars[VV_MOUSE_WIN].vv_nr = 0;
Bram Moolenaar511972d2016-06-04 18:09:59 +020012873 vimvars[VV_MOUSE_WINID].vv_nr = 0;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012874 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12875 vimvars[VV_MOUSE_COL].vv_nr = 0;
12876
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012877 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012878 if (IS_SPECIAL(n) || mod_mask != 0)
12879 {
12880 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12881 int i = 0;
12882
12883 /* Turn a special key into three bytes, plus modifier. */
12884 if (mod_mask != 0)
12885 {
12886 temp[i++] = K_SPECIAL;
12887 temp[i++] = KS_MODIFIER;
12888 temp[i++] = mod_mask;
12889 }
12890 if (IS_SPECIAL(n))
12891 {
12892 temp[i++] = K_SPECIAL;
12893 temp[i++] = K_SECOND(n);
12894 temp[i++] = K_THIRD(n);
12895 }
12896#ifdef FEAT_MBYTE
12897 else if (has_mbyte)
12898 i += (*mb_char2bytes)(n, temp + i);
12899#endif
12900 else
12901 temp[i++] = n;
12902 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012903 rettv->v_type = VAR_STRING;
12904 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012905
12906#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012907 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012908 {
12909 int row = mouse_row;
12910 int col = mouse_col;
12911 win_T *win;
12912 linenr_T lnum;
12913# ifdef FEAT_WINDOWS
12914 win_T *wp;
12915# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012916 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012917
12918 if (row >= 0 && col >= 0)
12919 {
12920 /* Find the window at the mouse coordinates and compute the
12921 * text position. */
12922 win = mouse_find_win(&row, &col);
12923 (void)mouse_comp_pos(win, &row, &col, &lnum);
12924# ifdef FEAT_WINDOWS
12925 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012926 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012927# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012928 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar511972d2016-06-04 18:09:59 +020012929 vimvars[VV_MOUSE_WINID].vv_nr = win->w_id;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012930 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12931 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12932 }
12933 }
12934#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012935 }
12936}
12937
12938/*
12939 * "getcharmod()" function
12940 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012941 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012942f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012943{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012944 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012945}
12946
12947/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012948 * "getcharsearch()" function
12949 */
12950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012951f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012952{
12953 if (rettv_dict_alloc(rettv) != FAIL)
12954 {
12955 dict_T *dict = rettv->vval.v_dict;
12956
12957 dict_add_nr_str(dict, "char", 0L, last_csearch());
12958 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12959 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12960 }
12961}
12962
12963/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012964 * "getcmdline()" function
12965 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012966 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012967f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012968{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012969 rettv->v_type = VAR_STRING;
12970 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012971}
12972
12973/*
12974 * "getcmdpos()" function
12975 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012976 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012977f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012978{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012979 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012980}
12981
12982/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012983 * "getcmdtype()" function
12984 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012985 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012986f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012987{
12988 rettv->v_type = VAR_STRING;
12989 rettv->vval.v_string = alloc(2);
12990 if (rettv->vval.v_string != NULL)
12991 {
12992 rettv->vval.v_string[0] = get_cmdline_type();
12993 rettv->vval.v_string[1] = NUL;
12994 }
12995}
12996
12997/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012998 * "getcmdwintype()" function
12999 */
13000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013001f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020013002{
13003 rettv->v_type = VAR_STRING;
13004 rettv->vval.v_string = NULL;
13005#ifdef FEAT_CMDWIN
13006 rettv->vval.v_string = alloc(2);
13007 if (rettv->vval.v_string != NULL)
13008 {
13009 rettv->vval.v_string[0] = cmdwin_type;
13010 rettv->vval.v_string[1] = NUL;
13011 }
13012#endif
13013}
13014
13015/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013016 * "getcwd()" function
13017 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013018 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013019f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013020{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013021 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020013022 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013023
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013024 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020013025 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010013026
13027 wp = find_tabwin(&argvars[0], &argvars[1]);
13028 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013029 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010013030 if (wp->w_localdir != NULL)
13031 rettv->vval.v_string = vim_strsave(wp->w_localdir);
13032 else if(globaldir != NULL)
13033 rettv->vval.v_string = vim_strsave(globaldir);
13034 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020013035 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010013036 cwd = alloc(MAXPATHL);
13037 if (cwd != NULL)
13038 {
13039 if (mch_dirname(cwd, MAXPATHL) != FAIL)
13040 rettv->vval.v_string = vim_strsave(cwd);
13041 vim_free(cwd);
13042 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020013043 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010013044#ifdef BACKSLASH_IN_FILENAME
13045 if (rettv->vval.v_string != NULL)
13046 slash_adjust(rettv->vval.v_string);
13047#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013048 }
13049}
13050
13051/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013052 * "getfontname()" function
13053 */
13054 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013055f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013056{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013057 rettv->v_type = VAR_STRING;
13058 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013059#ifdef FEAT_GUI
13060 if (gui.in_use)
13061 {
13062 GuiFont font;
13063 char_u *name = NULL;
13064
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013065 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013066 {
13067 /* Get the "Normal" font. Either the name saved by
13068 * hl_set_font_name() or from the font ID. */
13069 font = gui.norm_font;
13070 name = hl_get_font_name();
13071 }
13072 else
13073 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013074 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013075 if (STRCMP(name, "*") == 0) /* don't use font dialog */
13076 return;
13077 font = gui_mch_get_font(name, FALSE);
13078 if (font == NOFONT)
13079 return; /* Invalid font name, return empty string. */
13080 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013081 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013082 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013083 gui_mch_free_font(font);
13084 }
13085#endif
13086}
13087
13088/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013089 * "getfperm({fname})" function
13090 */
13091 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013092f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013093{
13094 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020013095 stat_T st;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013096 char_u *perm = NULL;
13097 char_u flags[] = "rwx";
13098 int i;
13099
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013100 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013101
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013102 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013103 if (mch_stat((char *)fname, &st) >= 0)
13104 {
13105 perm = vim_strsave((char_u *)"---------");
13106 if (perm != NULL)
13107 {
13108 for (i = 0; i < 9; i++)
13109 {
13110 if (st.st_mode & (1 << (8 - i)))
13111 perm[i] = flags[i % 3];
13112 }
13113 }
13114 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013115 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013116}
13117
13118/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013119 * "getfsize({fname})" function
13120 */
13121 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013122f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013123{
13124 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020013125 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013126
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013127 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013128
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013129 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013130
13131 if (mch_stat((char *)fname, &st) >= 0)
13132 {
13133 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013134 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013135 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000013136 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013137 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000013138
13139 /* non-perfect check for overflow */
Bram Moolenaar8767f522016-07-01 17:17:39 +020013140 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
Bram Moolenaard827ada2007-06-19 15:19:55 +000013141 rettv->vval.v_number = -2;
13142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013143 }
13144 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013145 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013146}
13147
13148/*
13149 * "getftime({fname})" function
13150 */
13151 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013152f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013153{
13154 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020013155 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013156
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013157 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013158
13159 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013160 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013161 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013162 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013163}
13164
13165/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013166 * "getftype({fname})" function
13167 */
13168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013169f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013170{
13171 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020013172 stat_T st;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013173 char_u *type = NULL;
13174 char *t;
13175
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013176 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013177
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013178 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013179 if (mch_lstat((char *)fname, &st) >= 0)
13180 {
13181#ifdef S_ISREG
13182 if (S_ISREG(st.st_mode))
13183 t = "file";
13184 else if (S_ISDIR(st.st_mode))
13185 t = "dir";
13186# ifdef S_ISLNK
13187 else if (S_ISLNK(st.st_mode))
13188 t = "link";
13189# endif
13190# ifdef S_ISBLK
13191 else if (S_ISBLK(st.st_mode))
13192 t = "bdev";
13193# endif
13194# ifdef S_ISCHR
13195 else if (S_ISCHR(st.st_mode))
13196 t = "cdev";
13197# endif
13198# ifdef S_ISFIFO
13199 else if (S_ISFIFO(st.st_mode))
13200 t = "fifo";
13201# endif
13202# ifdef S_ISSOCK
13203 else if (S_ISSOCK(st.st_mode))
13204 t = "fifo";
13205# endif
13206 else
13207 t = "other";
13208#else
13209# ifdef S_IFMT
13210 switch (st.st_mode & S_IFMT)
13211 {
13212 case S_IFREG: t = "file"; break;
13213 case S_IFDIR: t = "dir"; break;
13214# ifdef S_IFLNK
13215 case S_IFLNK: t = "link"; break;
13216# endif
13217# ifdef S_IFBLK
13218 case S_IFBLK: t = "bdev"; break;
13219# endif
13220# ifdef S_IFCHR
13221 case S_IFCHR: t = "cdev"; break;
13222# endif
13223# ifdef S_IFIFO
13224 case S_IFIFO: t = "fifo"; break;
13225# endif
13226# ifdef S_IFSOCK
13227 case S_IFSOCK: t = "socket"; break;
13228# endif
13229 default: t = "other";
13230 }
13231# else
13232 if (mch_isdir(fname))
13233 t = "dir";
13234 else
13235 t = "file";
13236# endif
13237#endif
13238 type = vim_strsave((char_u *)t);
13239 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013240 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013241}
13242
13243/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013244 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000013245 */
13246 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013247f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013248{
13249 linenr_T lnum;
13250 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013251 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013252
13253 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013254 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000013255 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013256 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013257 retlist = FALSE;
13258 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013259 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000013260 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013261 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000013262 retlist = TRUE;
13263 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013264
Bram Moolenaar342337a2005-07-21 21:11:17 +000013265 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013266}
13267
13268/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013269 * "getmatches()" function
13270 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013272f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013273{
13274#ifdef FEAT_SEARCH_EXTRA
13275 dict_T *dict;
13276 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013277 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013278
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013279 if (rettv_list_alloc(rettv) == OK)
13280 {
13281 while (cur != NULL)
13282 {
13283 dict = dict_alloc();
13284 if (dict == NULL)
13285 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013286 if (cur->match.regprog == NULL)
13287 {
13288 /* match added with matchaddpos() */
13289 for (i = 0; i < MAXPOSMATCH; ++i)
13290 {
13291 llpos_T *llpos;
13292 char buf[6];
13293 list_T *l;
13294
13295 llpos = &cur->pos.pos[i];
13296 if (llpos->lnum == 0)
13297 break;
13298 l = list_alloc();
13299 if (l == NULL)
13300 break;
13301 list_append_number(l, (varnumber_T)llpos->lnum);
13302 if (llpos->col > 0)
13303 {
13304 list_append_number(l, (varnumber_T)llpos->col);
13305 list_append_number(l, (varnumber_T)llpos->len);
13306 }
13307 sprintf(buf, "pos%d", i + 1);
13308 dict_add_list(dict, buf, l);
13309 }
13310 }
13311 else
13312 {
13313 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
13314 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013315 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013316 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
13317 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar42356152016-03-31 22:27:40 +020013318# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +020013319 if (cur->conceal_char)
13320 {
13321 char_u buf[MB_MAXBYTES + 1];
13322
13323 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
13324 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
13325 }
13326# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013327 list_append_dict(rettv->vval.v_list, dict);
13328 cur = cur->next;
13329 }
13330 }
13331#endif
13332}
13333
13334/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000013335 * "getpid()" function
13336 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000013337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013338f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013339{
13340 rettv->vval.v_number = mch_get_pid();
13341}
13342
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013343 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013344getpos_both(
13345 typval_T *argvars,
13346 typval_T *rettv,
13347 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013348{
Bram Moolenaara5525202006-03-02 22:52:09 +000013349 pos_T *fp;
13350 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013351 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013352
13353 if (rettv_list_alloc(rettv) == OK)
13354 {
13355 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013356 if (getcurpos)
13357 fp = &curwin->w_cursor;
13358 else
13359 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013360 if (fnum != -1)
13361 list_append_number(l, (varnumber_T)fnum);
13362 else
13363 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013364 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13365 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013366 list_append_number(l, (fp != NULL)
13367 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013368 : (varnumber_T)0);
13369 list_append_number(l,
13370#ifdef FEAT_VIRTUALEDIT
13371 (fp != NULL) ? (varnumber_T)fp->coladd :
13372#endif
13373 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013374 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013375 {
13376 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013377 list_append_number(l, curwin->w_curswant == MAXCOL ?
13378 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013379 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013380 }
13381 else
13382 rettv->vval.v_number = FALSE;
13383}
13384
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020013385
13386/*
13387 * "getcurpos()" function
13388 */
13389 static void
13390f_getcurpos(typval_T *argvars, typval_T *rettv)
13391{
13392 getpos_both(argvars, rettv, TRUE);
13393}
13394
13395/*
13396 * "getpos(string)" function
13397 */
13398 static void
13399f_getpos(typval_T *argvars, typval_T *rettv)
13400{
13401 getpos_both(argvars, rettv, FALSE);
13402}
13403
Bram Moolenaara5525202006-03-02 22:52:09 +000013404/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013405 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013406 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013407 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013408f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013409{
13410#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013411 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013412#endif
13413
Bram Moolenaar2641f772005-03-25 21:58:17 +000013414#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013415 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013416 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013417 wp = NULL;
13418 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13419 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013420 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013421 if (wp == NULL)
13422 return;
13423 }
13424
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013425 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013426 }
13427#endif
13428}
13429
13430/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013431 * "getreg()" function
13432 */
13433 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013434f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013435{
13436 char_u *strregname;
13437 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013438 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013439 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013440 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013441
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013442 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013443 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013444 strregname = get_tv_string_chk(&argvars[0]);
13445 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013446 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013447 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013448 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013449 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013450 return_list = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013451 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013453 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013454 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013455
13456 if (error)
13457 return;
13458
Bram Moolenaar071d4272004-06-13 20:20:40 +000013459 regname = (strregname == NULL ? '"' : *strregname);
13460 if (regname == 0)
13461 regname = '"';
13462
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013463 if (return_list)
13464 {
13465 rettv->v_type = VAR_LIST;
13466 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13467 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013468 if (rettv->vval.v_list == NULL)
Bram Moolenaar8ed43912016-04-21 08:56:12 +020013469 (void)rettv_list_alloc(rettv);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013470 else
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013471 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013472 }
13473 else
13474 {
13475 rettv->v_type = VAR_STRING;
13476 rettv->vval.v_string = get_reg_contents(regname,
13477 arg2 ? GREG_EXPR_SRC : 0);
13478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013479}
13480
13481/*
13482 * "getregtype()" function
13483 */
13484 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013485f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013486{
13487 char_u *strregname;
13488 int regname;
13489 char_u buf[NUMBUFLEN + 2];
13490 long reglen = 0;
13491
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013492 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013493 {
13494 strregname = get_tv_string_chk(&argvars[0]);
13495 if (strregname == NULL) /* type error; errmsg already given */
13496 {
13497 rettv->v_type = VAR_STRING;
13498 rettv->vval.v_string = NULL;
13499 return;
13500 }
13501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013502 else
13503 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013504 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013505
13506 regname = (strregname == NULL ? '"' : *strregname);
13507 if (regname == 0)
13508 regname = '"';
13509
13510 buf[0] = NUL;
13511 buf[1] = NUL;
13512 switch (get_reg_type(regname, &reglen))
13513 {
13514 case MLINE: buf[0] = 'V'; break;
13515 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013516 case MBLOCK:
13517 buf[0] = Ctrl_V;
13518 sprintf((char *)buf + 1, "%ld", reglen + 1);
13519 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013520 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013521 rettv->v_type = VAR_STRING;
13522 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013523}
13524
13525/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013526 * "gettabvar()" function
13527 */
13528 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013529f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013530{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013531 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013532 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013533 dictitem_T *v;
13534 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013535 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013536
13537 rettv->v_type = VAR_STRING;
13538 rettv->vval.v_string = NULL;
13539
13540 varname = get_tv_string_chk(&argvars[1]);
13541 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13542 if (tp != NULL && varname != NULL)
13543 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013544 /* Set tp to be our tabpage, temporarily. Also set the window to the
13545 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013546 if (switch_win(&oldcurwin, &oldtabpage,
13547 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013548 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013549 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013550 /* look up the variable */
13551 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13552 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13553 if (v != NULL)
13554 {
13555 copy_tv(&v->di_tv, rettv);
13556 done = TRUE;
13557 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013558 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013559
13560 /* restore previous notion of curwin */
13561 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013562 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013563
13564 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013565 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013566}
13567
13568/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013569 * "gettabwinvar()" function
13570 */
13571 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013572f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013573{
13574 getwinvar(argvars, rettv, 1);
13575}
13576
13577/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013578 * "getwinposx()" function
13579 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013580 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013581f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013582{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013583 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013584#ifdef FEAT_GUI
13585 if (gui.in_use)
13586 {
13587 int x, y;
13588
13589 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013590 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013591 }
13592#endif
13593}
13594
13595/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010013596 * "win_findbuf()" function
13597 */
13598 static void
13599f_win_findbuf(typval_T *argvars, typval_T *rettv)
13600{
13601 if (rettv_list_alloc(rettv) != FAIL)
13602 win_findbuf(argvars, rettv->vval.v_list);
13603}
13604
13605/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010013606 * "win_getid()" function
13607 */
13608 static void
13609f_win_getid(typval_T *argvars, typval_T *rettv)
13610{
13611 rettv->vval.v_number = win_getid(argvars);
13612}
13613
13614/*
13615 * "win_gotoid()" function
13616 */
13617 static void
13618f_win_gotoid(typval_T *argvars, typval_T *rettv)
13619{
13620 rettv->vval.v_number = win_gotoid(argvars);
13621}
13622
13623/*
13624 * "win_id2tabwin()" function
13625 */
13626 static void
13627f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13628{
13629 if (rettv_list_alloc(rettv) != FAIL)
13630 win_id2tabwin(argvars, rettv->vval.v_list);
13631}
13632
13633/*
13634 * "win_id2win()" function
13635 */
13636 static void
13637f_win_id2win(typval_T *argvars, typval_T *rettv)
13638{
13639 rettv->vval.v_number = win_id2win(argvars);
13640}
13641
13642/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013643 * "getwinposy()" function
13644 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013645 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013646f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013647{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013648 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013649#ifdef FEAT_GUI
13650 if (gui.in_use)
13651 {
13652 int x, y;
13653
13654 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013655 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013656 }
13657#endif
13658}
13659
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013660/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013661 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013662 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013663 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013664find_win_by_nr(
13665 typval_T *vp,
13666 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013667{
13668#ifdef FEAT_WINDOWS
13669 win_T *wp;
13670#endif
13671 int nr;
13672
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013673 nr = (int)get_tv_number_chk(vp, NULL);
Bram Moolenaara40058a2005-07-11 22:42:07 +000013674
13675#ifdef FEAT_WINDOWS
13676 if (nr < 0)
13677 return NULL;
13678 if (nr == 0)
13679 return curwin;
13680
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013681 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13682 wp != NULL; wp = wp->w_next)
Bram Moolenaar888ccac2016-06-04 18:49:36 +020013683 if (nr >= LOWEST_WIN_ID)
13684 {
13685 if (wp->w_id == nr)
13686 return wp;
13687 }
13688 else if (--nr <= 0)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013689 break;
Bram Moolenaar888ccac2016-06-04 18:49:36 +020013690 if (nr >= LOWEST_WIN_ID)
13691 return NULL;
Bram Moolenaara40058a2005-07-11 22:42:07 +000013692 return wp;
13693#else
Bram Moolenaar888ccac2016-06-04 18:49:36 +020013694 if (nr == 0 || nr == 1 || nr == curwin->w_id)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013695 return curwin;
13696 return NULL;
13697#endif
13698}
13699
Bram Moolenaar071d4272004-06-13 20:20:40 +000013700/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013701 * Find window specified by "wvp" in tabpage "tvp".
13702 */
13703 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013704find_tabwin(
13705 typval_T *wvp, /* VAR_UNKNOWN for current window */
13706 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013707{
13708 win_T *wp = NULL;
13709 tabpage_T *tp = NULL;
13710 long n;
13711
13712 if (wvp->v_type != VAR_UNKNOWN)
13713 {
13714 if (tvp->v_type != VAR_UNKNOWN)
13715 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013716 n = (long)get_tv_number(tvp);
Bram Moolenaarc9703302016-01-17 21:49:33 +010013717 if (n >= 0)
13718 tp = find_tabpage(n);
13719 }
13720 else
13721 tp = curtab;
13722
13723 if (tp != NULL)
13724 wp = find_win_by_nr(wvp, tp);
13725 }
13726 else
13727 wp = curwin;
13728
13729 return wp;
13730}
13731
13732/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013733 * "getwinvar()" function
13734 */
13735 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013736f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013737{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013738 getwinvar(argvars, rettv, 0);
13739}
13740
13741/*
13742 * getwinvar() and gettabwinvar()
13743 */
13744 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013745getwinvar(
13746 typval_T *argvars,
13747 typval_T *rettv,
13748 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013749{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013750 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013751 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013752 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013753 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013754 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013755#ifdef FEAT_WINDOWS
13756 win_T *oldcurwin;
13757 tabpage_T *oldtabpage;
13758 int need_switch_win;
13759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013760
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013761#ifdef FEAT_WINDOWS
13762 if (off == 1)
13763 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13764 else
13765 tp = curtab;
13766#endif
13767 win = find_win_by_nr(&argvars[off], tp);
13768 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013769 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013770
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013771 rettv->v_type = VAR_STRING;
13772 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013773
13774 if (win != NULL && varname != NULL)
13775 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013776#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013777 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013778 * otherwise the window is not valid. Only do this when needed,
13779 * autocommands get blocked. */
13780 need_switch_win = !(tp == curtab && win == curwin);
13781 if (!need_switch_win
13782 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13783#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013784 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013785 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013786 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013787 if (get_option_tv(&varname, rettv, 1) == OK)
13788 done = TRUE;
13789 }
13790 else
13791 {
13792 /* Look up the variable. */
13793 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13794 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13795 varname, FALSE);
13796 if (v != NULL)
13797 {
13798 copy_tv(&v->di_tv, rettv);
13799 done = TRUE;
13800 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013802 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013803
Bram Moolenaarba117c22015-09-29 16:53:22 +020013804#ifdef FEAT_WINDOWS
13805 if (need_switch_win)
13806 /* restore previous notion of curwin */
13807 restore_win(oldcurwin, oldtabpage, TRUE);
13808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013809 }
13810
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013811 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13812 /* use the default return value */
13813 copy_tv(&argvars[off + 2], rettv);
13814
Bram Moolenaar071d4272004-06-13 20:20:40 +000013815 --emsg_off;
13816}
13817
13818/*
13819 * "glob()" function
13820 */
13821 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013822f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013823{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013824 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013825 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013826 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013827
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013828 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013829 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013830 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013831 if (argvars[1].v_type != VAR_UNKNOWN)
13832 {
13833 if (get_tv_number_chk(&argvars[1], &error))
13834 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013835 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013836 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013837 if (get_tv_number_chk(&argvars[2], &error))
13838 {
13839 rettv->v_type = VAR_LIST;
13840 rettv->vval.v_list = NULL;
13841 }
13842 if (argvars[3].v_type != VAR_UNKNOWN
13843 && get_tv_number_chk(&argvars[3], &error))
13844 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013845 }
13846 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013847 if (!error)
13848 {
13849 ExpandInit(&xpc);
13850 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013851 if (p_wic)
13852 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013853 if (rettv->v_type == VAR_STRING)
13854 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013855 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013856 else if (rettv_list_alloc(rettv) != FAIL)
13857 {
13858 int i;
13859
13860 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13861 NULL, options, WILD_ALL_KEEP);
13862 for (i = 0; i < xpc.xp_numfiles; i++)
13863 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13864
13865 ExpandCleanup(&xpc);
13866 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013867 }
13868 else
13869 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013870}
13871
13872/*
13873 * "globpath()" function
13874 */
13875 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013876f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013877{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013878 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013879 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013880 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013881 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013882 garray_T ga;
13883 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013884
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013885 /* When the optional second argument is non-zero, don't remove matches
13886 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013887 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013888 if (argvars[2].v_type != VAR_UNKNOWN)
13889 {
13890 if (get_tv_number_chk(&argvars[2], &error))
13891 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013892 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013893 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013894 if (get_tv_number_chk(&argvars[3], &error))
13895 {
13896 rettv->v_type = VAR_LIST;
13897 rettv->vval.v_list = NULL;
13898 }
13899 if (argvars[4].v_type != VAR_UNKNOWN
13900 && get_tv_number_chk(&argvars[4], &error))
13901 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013902 }
13903 }
13904 if (file != NULL && !error)
13905 {
13906 ga_init2(&ga, (int)sizeof(char_u *), 10);
13907 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13908 if (rettv->v_type == VAR_STRING)
13909 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13910 else if (rettv_list_alloc(rettv) != FAIL)
13911 for (i = 0; i < ga.ga_len; ++i)
13912 list_append_string(rettv->vval.v_list,
13913 ((char_u **)(ga.ga_data))[i], -1);
13914 ga_clear_strings(&ga);
13915 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013916 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013917 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013918}
13919
13920/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013921 * "glob2regpat()" function
13922 */
13923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013924f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013925{
13926 char_u *pat = get_tv_string_chk(&argvars[0]);
13927
13928 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013929 rettv->vval.v_string = (pat == NULL)
13930 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013931}
13932
13933/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013934 * "has()" function
13935 */
13936 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013937f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013938{
13939 int i;
13940 char_u *name;
13941 int n = FALSE;
13942 static char *(has_list[]) =
13943 {
13944#ifdef AMIGA
13945 "amiga",
13946# ifdef FEAT_ARP
13947 "arp",
13948# endif
13949#endif
13950#ifdef __BEOS__
13951 "beos",
13952#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013953#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013954 "mac",
13955#endif
13956#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013957 "macunix", /* built with 'darwin' enabled */
13958#endif
13959#if defined(__APPLE__) && __APPLE__ == 1
13960 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013961#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013962#ifdef __QNX__
13963 "qnx",
13964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013965#ifdef UNIX
13966 "unix",
13967#endif
13968#ifdef VMS
13969 "vms",
13970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013971#ifdef WIN32
13972 "win32",
13973#endif
13974#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13975 "win32unix",
13976#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013977#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013978 "win64",
13979#endif
13980#ifdef EBCDIC
13981 "ebcdic",
13982#endif
13983#ifndef CASE_INSENSITIVE_FILENAME
13984 "fname_case",
13985#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013986#ifdef HAVE_ACL
13987 "acl",
13988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013989#ifdef FEAT_ARABIC
13990 "arabic",
13991#endif
13992#ifdef FEAT_AUTOCMD
13993 "autocmd",
13994#endif
13995#ifdef FEAT_BEVAL
13996 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013997# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13998 "balloon_multiline",
13999# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014000#endif
14001#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
14002 "builtin_terms",
14003# ifdef ALL_BUILTIN_TCAPS
14004 "all_builtin_terms",
14005# endif
14006#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020014007#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
14008 || defined(FEAT_GUI_W32) \
14009 || defined(FEAT_GUI_MOTIF))
14010 "browsefilter",
14011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014012#ifdef FEAT_BYTEOFF
14013 "byte_offset",
14014#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014015#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010014016 "channel",
14017#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014018#ifdef FEAT_CINDENT
14019 "cindent",
14020#endif
14021#ifdef FEAT_CLIENTSERVER
14022 "clientserver",
14023#endif
14024#ifdef FEAT_CLIPBOARD
14025 "clipboard",
14026#endif
14027#ifdef FEAT_CMDL_COMPL
14028 "cmdline_compl",
14029#endif
14030#ifdef FEAT_CMDHIST
14031 "cmdline_hist",
14032#endif
14033#ifdef FEAT_COMMENTS
14034 "comments",
14035#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020014036#ifdef FEAT_CONCEAL
14037 "conceal",
14038#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014039#ifdef FEAT_CRYPT
14040 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010014041 "crypt-blowfish",
14042 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014043#endif
14044#ifdef FEAT_CSCOPE
14045 "cscope",
14046#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020014047#ifdef FEAT_CURSORBIND
14048 "cursorbind",
14049#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014050#ifdef CURSOR_SHAPE
14051 "cursorshape",
14052#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014053#ifdef DEBUG
14054 "debug",
14055#endif
14056#ifdef FEAT_CON_DIALOG
14057 "dialog_con",
14058#endif
14059#ifdef FEAT_GUI_DIALOG
14060 "dialog_gui",
14061#endif
14062#ifdef FEAT_DIFF
14063 "diff",
14064#endif
14065#ifdef FEAT_DIGRAPHS
14066 "digraphs",
14067#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020014068#ifdef FEAT_DIRECTX
14069 "directx",
14070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014071#ifdef FEAT_DND
14072 "dnd",
14073#endif
14074#ifdef FEAT_EMACS_TAGS
14075 "emacs_tags",
14076#endif
14077 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010014078 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014079#ifdef FEAT_SEARCH_EXTRA
14080 "extra_search",
14081#endif
14082#ifdef FEAT_FKMAP
14083 "farsi",
14084#endif
14085#ifdef FEAT_SEARCHPATH
14086 "file_in_path",
14087#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020014088#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014089 "filterpipe",
14090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014091#ifdef FEAT_FIND_ID
14092 "find_in_path",
14093#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014094#ifdef FEAT_FLOAT
14095 "float",
14096#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014097#ifdef FEAT_FOLDING
14098 "folding",
14099#endif
14100#ifdef FEAT_FOOTER
14101 "footer",
14102#endif
14103#if !defined(USE_SYSTEM) && defined(UNIX)
14104 "fork",
14105#endif
14106#ifdef FEAT_GETTEXT
14107 "gettext",
14108#endif
14109#ifdef FEAT_GUI
14110 "gui",
14111#endif
14112#ifdef FEAT_GUI_ATHENA
14113# ifdef FEAT_GUI_NEXTAW
14114 "gui_neXtaw",
14115# else
14116 "gui_athena",
14117# endif
14118#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014119#ifdef FEAT_GUI_GTK
14120 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010014121# ifdef USE_GTK3
14122 "gui_gtk3",
14123# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000014124 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010014125# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014126#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000014127#ifdef FEAT_GUI_GNOME
14128 "gui_gnome",
14129#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014130#ifdef FEAT_GUI_MAC
14131 "gui_mac",
14132#endif
14133#ifdef FEAT_GUI_MOTIF
14134 "gui_motif",
14135#endif
14136#ifdef FEAT_GUI_PHOTON
14137 "gui_photon",
14138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014139#ifdef FEAT_GUI_W32
14140 "gui_win32",
14141#endif
14142#ifdef FEAT_HANGULIN
14143 "hangul_input",
14144#endif
14145#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
14146 "iconv",
14147#endif
14148#ifdef FEAT_INS_EXPAND
14149 "insert_expand",
14150#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014151#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010014152 "job",
14153#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014154#ifdef FEAT_JUMPLIST
14155 "jumplist",
14156#endif
14157#ifdef FEAT_KEYMAP
14158 "keymap",
14159#endif
14160#ifdef FEAT_LANGMAP
14161 "langmap",
14162#endif
14163#ifdef FEAT_LIBCALL
14164 "libcall",
14165#endif
14166#ifdef FEAT_LINEBREAK
14167 "linebreak",
14168#endif
14169#ifdef FEAT_LISP
14170 "lispindent",
14171#endif
14172#ifdef FEAT_LISTCMDS
14173 "listcmds",
14174#endif
14175#ifdef FEAT_LOCALMAP
14176 "localmap",
14177#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014178#ifdef FEAT_LUA
14179# ifndef DYNAMIC_LUA
14180 "lua",
14181# endif
14182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014183#ifdef FEAT_MENU
14184 "menu",
14185#endif
14186#ifdef FEAT_SESSION
14187 "mksession",
14188#endif
14189#ifdef FEAT_MODIFY_FNAME
14190 "modify_fname",
14191#endif
14192#ifdef FEAT_MOUSE
14193 "mouse",
14194#endif
14195#ifdef FEAT_MOUSESHAPE
14196 "mouseshape",
14197#endif
14198#if defined(UNIX) || defined(VMS)
14199# ifdef FEAT_MOUSE_DEC
14200 "mouse_dec",
14201# endif
14202# ifdef FEAT_MOUSE_GPM
14203 "mouse_gpm",
14204# endif
14205# ifdef FEAT_MOUSE_JSB
14206 "mouse_jsbterm",
14207# endif
14208# ifdef FEAT_MOUSE_NET
14209 "mouse_netterm",
14210# endif
14211# ifdef FEAT_MOUSE_PTERM
14212 "mouse_pterm",
14213# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020014214# ifdef FEAT_MOUSE_SGR
14215 "mouse_sgr",
14216# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014217# ifdef FEAT_SYSMOUSE
14218 "mouse_sysmouse",
14219# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020014220# ifdef FEAT_MOUSE_URXVT
14221 "mouse_urxvt",
14222# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014223# ifdef FEAT_MOUSE_XTERM
14224 "mouse_xterm",
14225# endif
14226#endif
14227#ifdef FEAT_MBYTE
14228 "multi_byte",
14229#endif
14230#ifdef FEAT_MBYTE_IME
14231 "multi_byte_ime",
14232#endif
14233#ifdef FEAT_MULTI_LANG
14234 "multi_lang",
14235#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014236#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000014237#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014238 "mzscheme",
14239#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014240#endif
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014241#ifdef FEAT_NUM64
14242 "num64",
14243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014244#ifdef FEAT_OLE
14245 "ole",
14246#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010014247 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014248#ifdef FEAT_PATH_EXTRA
14249 "path_extra",
14250#endif
14251#ifdef FEAT_PERL
14252#ifndef DYNAMIC_PERL
14253 "perl",
14254#endif
14255#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020014256#ifdef FEAT_PERSISTENT_UNDO
14257 "persistent_undo",
14258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014259#ifdef FEAT_PYTHON
14260#ifndef DYNAMIC_PYTHON
14261 "python",
14262#endif
14263#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014264#ifdef FEAT_PYTHON3
14265#ifndef DYNAMIC_PYTHON3
14266 "python3",
14267#endif
14268#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014269#ifdef FEAT_POSTSCRIPT
14270 "postscript",
14271#endif
14272#ifdef FEAT_PRINTER
14273 "printer",
14274#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000014275#ifdef FEAT_PROFILE
14276 "profile",
14277#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014278#ifdef FEAT_RELTIME
14279 "reltime",
14280#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014281#ifdef FEAT_QUICKFIX
14282 "quickfix",
14283#endif
14284#ifdef FEAT_RIGHTLEFT
14285 "rightleft",
14286#endif
14287#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
14288 "ruby",
14289#endif
14290#ifdef FEAT_SCROLLBIND
14291 "scrollbind",
14292#endif
14293#ifdef FEAT_CMDL_INFO
14294 "showcmd",
14295 "cmdline_info",
14296#endif
14297#ifdef FEAT_SIGNS
14298 "signs",
14299#endif
14300#ifdef FEAT_SMARTINDENT
14301 "smartindent",
14302#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000014303#ifdef STARTUPTIME
14304 "startuptime",
14305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014306#ifdef FEAT_STL_OPT
14307 "statusline",
14308#endif
14309#ifdef FEAT_SUN_WORKSHOP
14310 "sun_workshop",
14311#endif
14312#ifdef FEAT_NETBEANS_INTG
14313 "netbeans_intg",
14314#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014315#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000014316 "spell",
14317#endif
14318#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000014319 "syntax",
14320#endif
14321#if defined(USE_SYSTEM) || !defined(UNIX)
14322 "system",
14323#endif
14324#ifdef FEAT_TAG_BINS
14325 "tag_binary",
14326#endif
14327#ifdef FEAT_TAG_OLDSTATIC
14328 "tag_old_static",
14329#endif
14330#ifdef FEAT_TAG_ANYWHITE
14331 "tag_any_white",
14332#endif
14333#ifdef FEAT_TCL
14334# ifndef DYNAMIC_TCL
14335 "tcl",
14336# endif
14337#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020014338#ifdef FEAT_TERMGUICOLORS
14339 "termguicolors",
14340#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014341#ifdef TERMINFO
14342 "terminfo",
14343#endif
14344#ifdef FEAT_TERMRESPONSE
14345 "termresponse",
14346#endif
14347#ifdef FEAT_TEXTOBJ
14348 "textobjects",
14349#endif
14350#ifdef HAVE_TGETENT
14351 "tgetent",
14352#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010014353#ifdef FEAT_TIMERS
14354 "timers",
14355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014356#ifdef FEAT_TITLE
14357 "title",
14358#endif
14359#ifdef FEAT_TOOLBAR
14360 "toolbar",
14361#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014362#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14363 "unnamedplus",
14364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014365#ifdef FEAT_USR_CMDS
14366 "user-commands", /* was accidentally included in 5.4 */
14367 "user_commands",
14368#endif
14369#ifdef FEAT_VIMINFO
14370 "viminfo",
14371#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010014372#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000014373 "vertsplit",
14374#endif
14375#ifdef FEAT_VIRTUALEDIT
14376 "virtualedit",
14377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014378 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014379#ifdef FEAT_VISUALEXTRA
14380 "visualextra",
14381#endif
14382#ifdef FEAT_VREPLACE
14383 "vreplace",
14384#endif
14385#ifdef FEAT_WILDIGN
14386 "wildignore",
14387#endif
14388#ifdef FEAT_WILDMENU
14389 "wildmenu",
14390#endif
14391#ifdef FEAT_WINDOWS
14392 "windows",
14393#endif
14394#ifdef FEAT_WAK
14395 "winaltkeys",
14396#endif
14397#ifdef FEAT_WRITEBACKUP
14398 "writebackup",
14399#endif
14400#ifdef FEAT_XIM
14401 "xim",
14402#endif
14403#ifdef FEAT_XFONTSET
14404 "xfontset",
14405#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014406#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014407 "xpm",
14408 "xpm_w32", /* for backward compatibility */
14409#else
14410# if defined(HAVE_XPM)
14411 "xpm",
14412# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014413#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014414#ifdef USE_XSMP
14415 "xsmp",
14416#endif
14417#ifdef USE_XSMP_INTERACT
14418 "xsmp_interact",
14419#endif
14420#ifdef FEAT_XCLIPBOARD
14421 "xterm_clipboard",
14422#endif
14423#ifdef FEAT_XTERM_SAVE
14424 "xterm_save",
14425#endif
14426#if defined(UNIX) && defined(FEAT_X11)
14427 "X11",
14428#endif
14429 NULL
14430 };
14431
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014432 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014433 for (i = 0; has_list[i] != NULL; ++i)
14434 if (STRICMP(name, has_list[i]) == 0)
14435 {
14436 n = TRUE;
14437 break;
14438 }
14439
14440 if (n == FALSE)
14441 {
14442 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014443 {
14444 if (name[5] == '-'
Bram Moolenaar819821c2016-03-26 21:24:14 +010014445 && STRLEN(name) >= 11
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014446 && vim_isdigit(name[6])
14447 && vim_isdigit(name[8])
14448 && vim_isdigit(name[10]))
14449 {
14450 int major = atoi((char *)name + 6);
14451 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014452
14453 /* Expect "patch-9.9.01234". */
14454 n = (major < VIM_VERSION_MAJOR
14455 || (major == VIM_VERSION_MAJOR
14456 && (minor < VIM_VERSION_MINOR
14457 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014458 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014459 }
14460 else
14461 n = has_patch(atoi((char *)name + 5));
14462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014463 else if (STRICMP(name, "vim_starting") == 0)
14464 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014465#ifdef FEAT_MBYTE
14466 else if (STRICMP(name, "multi_byte_encoding") == 0)
14467 n = has_mbyte;
14468#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014469#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14470 else if (STRICMP(name, "balloon_multiline") == 0)
14471 n = multiline_balloon_available();
14472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014473#ifdef DYNAMIC_TCL
14474 else if (STRICMP(name, "tcl") == 0)
14475 n = tcl_enabled(FALSE);
14476#endif
14477#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14478 else if (STRICMP(name, "iconv") == 0)
14479 n = iconv_enabled(FALSE);
14480#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014481#ifdef DYNAMIC_LUA
14482 else if (STRICMP(name, "lua") == 0)
14483 n = lua_enabled(FALSE);
14484#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014485#ifdef DYNAMIC_MZSCHEME
14486 else if (STRICMP(name, "mzscheme") == 0)
14487 n = mzscheme_enabled(FALSE);
14488#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014489#ifdef DYNAMIC_RUBY
14490 else if (STRICMP(name, "ruby") == 0)
14491 n = ruby_enabled(FALSE);
14492#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014493#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014494#ifdef DYNAMIC_PYTHON
14495 else if (STRICMP(name, "python") == 0)
14496 n = python_enabled(FALSE);
14497#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014498#endif
14499#ifdef FEAT_PYTHON3
14500#ifdef DYNAMIC_PYTHON3
14501 else if (STRICMP(name, "python3") == 0)
14502 n = python3_enabled(FALSE);
14503#endif
14504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014505#ifdef DYNAMIC_PERL
14506 else if (STRICMP(name, "perl") == 0)
14507 n = perl_enabled(FALSE);
14508#endif
14509#ifdef FEAT_GUI
14510 else if (STRICMP(name, "gui_running") == 0)
14511 n = (gui.in_use || gui.starting);
14512# ifdef FEAT_GUI_W32
14513 else if (STRICMP(name, "gui_win32s") == 0)
14514 n = gui_is_win32s();
14515# endif
14516# ifdef FEAT_BROWSE
14517 else if (STRICMP(name, "browse") == 0)
14518 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14519# endif
14520#endif
14521#ifdef FEAT_SYN_HL
14522 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014523 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014524#endif
14525#if defined(WIN3264)
14526 else if (STRICMP(name, "win95") == 0)
14527 n = mch_windows95();
14528#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014529#ifdef FEAT_NETBEANS_INTG
14530 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014531 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014533 }
14534
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014535 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014536}
14537
14538/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014539 * "has_key()" function
14540 */
14541 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014542f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014543{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014544 if (argvars[0].v_type != VAR_DICT)
14545 {
14546 EMSG(_(e_dictreq));
14547 return;
14548 }
14549 if (argvars[0].vval.v_dict == NULL)
14550 return;
14551
14552 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014553 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014554}
14555
14556/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014557 * "haslocaldir()" function
14558 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014559 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014560f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014561{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014562 win_T *wp = NULL;
14563
14564 wp = find_tabwin(&argvars[0], &argvars[1]);
14565 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014566}
14567
14568/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014569 * "hasmapto()" function
14570 */
14571 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014572f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014573{
14574 char_u *name;
14575 char_u *mode;
14576 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014577 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014578
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014579 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014580 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014581 mode = (char_u *)"nvo";
14582 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014583 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014584 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014585 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014586 abbr = (int)get_tv_number(&argvars[2]);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014588
Bram Moolenaar2c932302006-03-18 21:42:09 +000014589 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014590 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014591 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014592 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014593}
14594
14595/*
14596 * "histadd()" function
14597 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014598 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014599f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014600{
14601#ifdef FEAT_CMDHIST
14602 int histype;
14603 char_u *str;
14604 char_u buf[NUMBUFLEN];
14605#endif
14606
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014607 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014608 if (check_restricted() || check_secure())
14609 return;
14610#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014611 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14612 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014613 if (histype >= 0)
14614 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014615 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014616 if (*str != NUL)
14617 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014618 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014619 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014620 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014621 return;
14622 }
14623 }
14624#endif
14625}
14626
14627/*
14628 * "histdel()" function
14629 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014630 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014631f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014632{
14633#ifdef FEAT_CMDHIST
14634 int n;
14635 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014636 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014637
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014638 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14639 if (str == NULL)
14640 n = 0;
14641 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014642 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014643 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014644 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014645 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014646 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014647 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014648 else
14649 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014650 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014651 get_tv_string_buf(&argvars[1], buf));
14652 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014653#endif
14654}
14655
14656/*
14657 * "histget()" function
14658 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014659 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014660f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014661{
14662#ifdef FEAT_CMDHIST
14663 int type;
14664 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014665 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014666
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014667 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14668 if (str == NULL)
14669 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014670 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014671 {
14672 type = get_histtype(str);
14673 if (argvars[1].v_type == VAR_UNKNOWN)
14674 idx = get_history_idx(type);
14675 else
14676 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14677 /* -1 on type error */
14678 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014680#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014681 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014682#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014683 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014684}
14685
14686/*
14687 * "histnr()" function
14688 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014689 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014690f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014691{
14692 int i;
14693
14694#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014695 char_u *history = get_tv_string_chk(&argvars[0]);
14696
14697 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014698 if (i >= HIST_CMD && i < HIST_COUNT)
14699 i = get_history_idx(i);
14700 else
14701#endif
14702 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014703 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014704}
14705
14706/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014707 * "highlightID(name)" function
14708 */
14709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014710f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014711{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014712 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014713}
14714
14715/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014716 * "highlight_exists()" function
14717 */
14718 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014719f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014720{
14721 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14722}
14723
14724/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014725 * "hostname()" function
14726 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014727 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014728f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014729{
14730 char_u hostname[256];
14731
14732 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014733 rettv->v_type = VAR_STRING;
14734 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014735}
14736
14737/*
14738 * iconv() function
14739 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014740 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014741f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014742{
14743#ifdef FEAT_MBYTE
14744 char_u buf1[NUMBUFLEN];
14745 char_u buf2[NUMBUFLEN];
14746 char_u *from, *to, *str;
14747 vimconv_T vimconv;
14748#endif
14749
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014750 rettv->v_type = VAR_STRING;
14751 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014752
14753#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014754 str = get_tv_string(&argvars[0]);
14755 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14756 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014757 vimconv.vc_type = CONV_NONE;
14758 convert_setup(&vimconv, from, to);
14759
14760 /* If the encodings are equal, no conversion needed. */
14761 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014762 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014763 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014764 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014765
14766 convert_setup(&vimconv, NULL, NULL);
14767 vim_free(from);
14768 vim_free(to);
14769#endif
14770}
14771
14772/*
14773 * "indent()" function
14774 */
14775 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014776f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014777{
14778 linenr_T lnum;
14779
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014780 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014781 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014782 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014783 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014784 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014785}
14786
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014787/*
14788 * "index()" function
14789 */
14790 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014791f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014792{
Bram Moolenaar33570922005-01-25 22:26:29 +000014793 list_T *l;
14794 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014795 long idx = 0;
14796 int ic = FALSE;
14797
14798 rettv->vval.v_number = -1;
14799 if (argvars[0].v_type != VAR_LIST)
14800 {
14801 EMSG(_(e_listreq));
14802 return;
14803 }
14804 l = argvars[0].vval.v_list;
14805 if (l != NULL)
14806 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014807 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014808 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014809 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014810 int error = FALSE;
14811
Bram Moolenaar758711c2005-02-02 23:11:38 +000014812 /* Start at specified item. Use the cached index that list_find()
14813 * sets, so that a negative number also works. */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014814 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014815 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014816 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014817 ic = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014818 if (error)
14819 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014820 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014821
Bram Moolenaar758711c2005-02-02 23:11:38 +000014822 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014823 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014824 {
14825 rettv->vval.v_number = idx;
14826 break;
14827 }
14828 }
14829}
14830
Bram Moolenaar071d4272004-06-13 20:20:40 +000014831static int inputsecret_flag = 0;
14832
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014833static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014834
Bram Moolenaar071d4272004-06-13 20:20:40 +000014835/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014836 * This function is used by f_input() and f_inputdialog() functions. The third
14837 * argument to f_input() specifies the type of completion to use at the
14838 * prompt. The third argument to f_inputdialog() specifies the value to return
14839 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014840 */
14841 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014842get_user_input(
14843 typval_T *argvars,
14844 typval_T *rettv,
14845 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014846{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014847 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014848 char_u *p = NULL;
14849 int c;
14850 char_u buf[NUMBUFLEN];
14851 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014852 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014853 int xp_type = EXPAND_NOTHING;
14854 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014855
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014856 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014857 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014858
14859#ifdef NO_CONSOLE_INPUT
14860 /* While starting up, there is no place to enter text. */
14861 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014862 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014863#endif
14864
14865 cmd_silent = FALSE; /* Want to see the prompt. */
14866 if (prompt != NULL)
14867 {
14868 /* Only the part of the message after the last NL is considered as
14869 * prompt for the command line */
14870 p = vim_strrchr(prompt, '\n');
14871 if (p == NULL)
14872 p = prompt;
14873 else
14874 {
14875 ++p;
14876 c = *p;
14877 *p = NUL;
14878 msg_start();
14879 msg_clr_eos();
14880 msg_puts_attr(prompt, echo_attr);
14881 msg_didout = FALSE;
14882 msg_starthere();
14883 *p = c;
14884 }
14885 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014886
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014887 if (argvars[1].v_type != VAR_UNKNOWN)
14888 {
14889 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14890 if (defstr != NULL)
14891 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014892
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014893 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014894 {
14895 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014896 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014897 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014898
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014899 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014900 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014901
Bram Moolenaar4463f292005-09-25 22:20:24 +000014902 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14903 if (xp_name == NULL)
14904 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014905
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014906 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014907
Bram Moolenaar4463f292005-09-25 22:20:24 +000014908 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14909 &xp_arg) == FAIL)
14910 return;
14911 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014912 }
14913
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014914 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014915 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014916 int save_ex_normal_busy = ex_normal_busy;
14917 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014918 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014919 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14920 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014921 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014922 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014923 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014924 && argvars[1].v_type != VAR_UNKNOWN
14925 && argvars[2].v_type != VAR_UNKNOWN)
14926 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14927 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014928
14929 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014930
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014931 /* since the user typed this, no need to wait for return */
14932 need_wait_return = FALSE;
14933 msg_didout = FALSE;
14934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014935 cmd_silent = cmd_silent_save;
14936}
14937
14938/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014939 * "input()" function
14940 * Also handles inputsecret() when inputsecret is set.
14941 */
14942 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014943f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014944{
14945 get_user_input(argvars, rettv, FALSE);
14946}
14947
14948/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014949 * "inputdialog()" function
14950 */
14951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014952f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014953{
14954#if defined(FEAT_GUI_TEXTDIALOG)
14955 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14956 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14957 {
14958 char_u *message;
14959 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014960 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014961
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014962 message = get_tv_string_chk(&argvars[0]);
14963 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014964 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014965 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014966 else
14967 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014968 if (message != NULL && defstr != NULL
14969 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014970 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014971 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014972 else
14973 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014974 if (message != NULL && defstr != NULL
14975 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014976 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014977 rettv->vval.v_string = vim_strsave(
14978 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014979 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014980 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014981 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014982 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014983 }
14984 else
14985#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014986 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014987}
14988
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014989/*
14990 * "inputlist()" function
14991 */
14992 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014993f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014994{
14995 listitem_T *li;
14996 int selected;
14997 int mouse_used;
14998
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014999#ifdef NO_CONSOLE_INPUT
15000 /* While starting up, there is no place to enter text. */
15001 if (no_console_input())
15002 return;
15003#endif
15004 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
15005 {
15006 EMSG2(_(e_listarg), "inputlist()");
15007 return;
15008 }
15009
15010 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000015011 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000015012 lines_left = Rows; /* avoid more prompt */
15013 msg_scroll = TRUE;
15014 msg_clr_eos();
15015
15016 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
15017 {
15018 msg_puts(get_tv_string(&li->li_tv));
15019 msg_putchar('\n');
15020 }
15021
15022 /* Ask for choice. */
15023 selected = prompt_for_number(&mouse_used);
15024 if (mouse_used)
15025 selected -= lines_left;
15026
15027 rettv->vval.v_number = selected;
15028}
15029
15030
Bram Moolenaar071d4272004-06-13 20:20:40 +000015031static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
15032
15033/*
15034 * "inputrestore()" function
15035 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015036 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015037f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015038{
15039 if (ga_userinput.ga_len > 0)
15040 {
15041 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015042 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
15043 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015044 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015045 }
15046 else if (p_verbose > 1)
15047 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000015048 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015049 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015050 }
15051}
15052
15053/*
15054 * "inputsave()" function
15055 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015057f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015058{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015059 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015060 if (ga_grow(&ga_userinput, 1) == OK)
15061 {
15062 save_typeahead((tasave_T *)(ga_userinput.ga_data)
15063 + ga_userinput.ga_len);
15064 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015065 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015066 }
15067 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015068 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015069}
15070
15071/*
15072 * "inputsecret()" function
15073 */
15074 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015075f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015076{
15077 ++cmdline_star;
15078 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015079 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015080 --cmdline_star;
15081 --inputsecret_flag;
15082}
15083
15084/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015085 * "insert()" function
15086 */
15087 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015088f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015089{
15090 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015091 listitem_T *item;
15092 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015093 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015094
15095 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015096 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015097 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015098 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015099 {
15100 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015101 before = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015102 if (error)
15103 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015104
Bram Moolenaar758711c2005-02-02 23:11:38 +000015105 if (before == l->lv_len)
15106 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015107 else
15108 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015109 item = list_find(l, before);
15110 if (item == NULL)
15111 {
15112 EMSGN(_(e_listidx), before);
15113 l = NULL;
15114 }
15115 }
15116 if (l != NULL)
15117 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015118 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015119 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015120 }
15121 }
15122}
15123
15124/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015125 * "invert(expr)" function
15126 */
15127 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015128f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015129{
15130 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
15131}
15132
15133/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015134 * "isdirectory()" function
15135 */
15136 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015137f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015138{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015139 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015140}
15141
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015142/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015143 * "islocked()" function
15144 */
15145 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015146f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015147{
15148 lval_T lv;
15149 char_u *end;
15150 dictitem_T *di;
15151
15152 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010015153 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
15154 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015155 if (end != NULL && lv.ll_name != NULL)
15156 {
15157 if (*end != NUL)
15158 EMSG(_(e_trailing));
15159 else
15160 {
15161 if (lv.ll_tv == NULL)
15162 {
15163 if (check_changedtick(lv.ll_name))
15164 rettv->vval.v_number = 1; /* always locked */
15165 else
15166 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010015167 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015168 if (di != NULL)
15169 {
15170 /* Consider a variable locked when:
15171 * 1. the variable itself is locked
15172 * 2. the value of the variable is locked.
15173 * 3. the List or Dict value is locked.
15174 */
15175 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
15176 || tv_islocked(&di->di_tv));
15177 }
15178 }
15179 }
15180 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015181 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015182 else if (lv.ll_newkey != NULL)
15183 EMSG2(_(e_dictkey), lv.ll_newkey);
15184 else if (lv.ll_list != NULL)
15185 /* List item. */
15186 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
15187 else
15188 /* Dictionary item. */
15189 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
15190 }
15191 }
15192
15193 clear_lval(&lv);
15194}
15195
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010015196#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
15197/*
15198 * "isnan()" function
15199 */
15200 static void
15201f_isnan(typval_T *argvars, typval_T *rettv)
15202{
15203 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
15204 && isnan(argvars[0].vval.v_float);
15205}
15206#endif
15207
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015208static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015209
15210/*
15211 * Turn a dict into a list:
15212 * "what" == 0: list of keys
15213 * "what" == 1: list of values
15214 * "what" == 2: list of items
15215 */
15216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015217dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015218{
Bram Moolenaar33570922005-01-25 22:26:29 +000015219 list_T *l2;
15220 dictitem_T *di;
15221 hashitem_T *hi;
15222 listitem_T *li;
15223 listitem_T *li2;
15224 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015225 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015226
Bram Moolenaar8c711452005-01-14 21:53:12 +000015227 if (argvars[0].v_type != VAR_DICT)
15228 {
15229 EMSG(_(e_dictreq));
15230 return;
15231 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015232 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015233 return;
15234
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015235 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015236 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015237
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015238 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015239 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015240 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015241 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015242 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015243 --todo;
15244 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015245
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015246 li = listitem_alloc();
15247 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015248 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015249 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015250
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015251 if (what == 0)
15252 {
15253 /* keys() */
15254 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015255 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015256 li->li_tv.vval.v_string = vim_strsave(di->di_key);
15257 }
15258 else if (what == 1)
15259 {
15260 /* values() */
15261 copy_tv(&di->di_tv, &li->li_tv);
15262 }
15263 else
15264 {
15265 /* items() */
15266 l2 = list_alloc();
15267 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015268 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015269 li->li_tv.vval.v_list = l2;
15270 if (l2 == NULL)
15271 break;
15272 ++l2->lv_refcount;
15273
15274 li2 = listitem_alloc();
15275 if (li2 == NULL)
15276 break;
15277 list_append(l2, li2);
15278 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015279 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015280 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
15281
15282 li2 = listitem_alloc();
15283 if (li2 == NULL)
15284 break;
15285 list_append(l2, li2);
15286 copy_tv(&di->di_tv, &li2->li_tv);
15287 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015288 }
15289 }
15290}
15291
15292/*
15293 * "items(dict)" function
15294 */
15295 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015296f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015297{
15298 dict_list(argvars, rettv, 2);
15299}
15300
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010015301#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015302/*
15303 * Get the job from the argument.
15304 * Returns NULL if the job is invalid.
15305 */
15306 static job_T *
15307get_job_arg(typval_T *tv)
15308{
15309 job_T *job;
15310
15311 if (tv->v_type != VAR_JOB)
15312 {
15313 EMSG2(_(e_invarg2), get_tv_string(tv));
15314 return NULL;
15315 }
15316 job = tv->vval.v_job;
15317
15318 if (job == NULL)
15319 EMSG(_("E916: not a valid job"));
15320 return job;
15321}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015322
Bram Moolenaar835dc632016-02-07 14:27:38 +010015323/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015324 * "job_getchannel()" function
15325 */
15326 static void
15327f_job_getchannel(typval_T *argvars, typval_T *rettv)
15328{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015329 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015330
Bram Moolenaar65edff82016-02-21 16:40:11 +010015331 if (job != NULL)
15332 {
Bram Moolenaar77073442016-02-13 23:23:53 +010015333 rettv->v_type = VAR_CHANNEL;
15334 rettv->vval.v_channel = job->jv_channel;
15335 if (job->jv_channel != NULL)
15336 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015337 }
15338}
15339
15340/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010015341 * "job_info()" function
15342 */
15343 static void
15344f_job_info(typval_T *argvars, typval_T *rettv)
15345{
15346 job_T *job = get_job_arg(&argvars[0]);
15347
15348 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
15349 job_info(job, rettv->vval.v_dict);
15350}
15351
15352/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015353 * "job_setoptions()" function
15354 */
15355 static void
15356f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15357{
15358 job_T *job = get_job_arg(&argvars[0]);
15359 jobopt_T opt;
15360
15361 if (job == NULL)
15362 return;
15363 clear_job_options(&opt);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020015364 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
15365 job_set_options(job, &opt);
15366 free_job_options(&opt);
Bram Moolenaar65edff82016-02-21 16:40:11 +010015367}
15368
15369/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015370 * "job_start()" function
15371 */
15372 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010015373f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015374{
Bram Moolenaar835dc632016-02-07 14:27:38 +010015375 rettv->v_type = VAR_JOB;
Bram Moolenaar38499922016-04-22 20:46:52 +020015376 if (check_restricted() || check_secure())
15377 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015378 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015379}
15380
15381/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015382 * "job_status()" function
15383 */
15384 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015385f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015386{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015387 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015388
Bram Moolenaar65edff82016-02-21 16:40:11 +010015389 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015390 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015391 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010015392 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010015393 }
15394}
15395
15396/*
15397 * "job_stop()" function
15398 */
15399 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015400f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015401{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015402 job_T *job = get_job_arg(&argvars[0]);
15403
15404 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015405 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015406}
15407#endif
15408
Bram Moolenaar071d4272004-06-13 20:20:40 +000015409/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015410 * "join()" function
15411 */
15412 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015413f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015414{
15415 garray_T ga;
15416 char_u *sep;
15417
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015418 if (argvars[0].v_type != VAR_LIST)
15419 {
15420 EMSG(_(e_listreq));
15421 return;
15422 }
15423 if (argvars[0].vval.v_list == NULL)
15424 return;
15425 if (argvars[1].v_type == VAR_UNKNOWN)
15426 sep = (char_u *)" ";
15427 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015428 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015429
15430 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015431
15432 if (sep != NULL)
15433 {
15434 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar18dfb442016-05-31 22:31:23 +020015435 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015436 ga_append(&ga, NUL);
15437 rettv->vval.v_string = (char_u *)ga.ga_data;
15438 }
15439 else
15440 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015441}
15442
15443/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015444 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015445 */
15446 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015447f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015448{
15449 js_read_T reader;
15450
15451 reader.js_buf = get_tv_string(&argvars[0]);
15452 reader.js_fill = NULL;
15453 reader.js_used = 0;
15454 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15455 EMSG(_(e_invarg));
15456}
15457
15458/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015459 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015460 */
15461 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015462f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015463{
15464 rettv->v_type = VAR_STRING;
15465 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15466}
15467
15468/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015469 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015470 */
15471 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015472f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015473{
15474 js_read_T reader;
15475
15476 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015477 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015478 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015479 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015480 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015481}
15482
15483/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015484 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015485 */
15486 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015487f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015488{
15489 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015490 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015491}
15492
15493/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015494 * "keys()" function
15495 */
15496 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015497f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015498{
15499 dict_list(argvars, rettv, 0);
15500}
15501
15502/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015503 * "last_buffer_nr()" function.
15504 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015505 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015506f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015507{
15508 int n = 0;
15509 buf_T *buf;
15510
15511 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15512 if (n < buf->b_fnum)
15513 n = buf->b_fnum;
15514
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015515 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015516}
15517
15518/*
15519 * "len()" function
15520 */
15521 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015522f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015523{
15524 switch (argvars[0].v_type)
15525 {
15526 case VAR_STRING:
15527 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015528 rettv->vval.v_number = (varnumber_T)STRLEN(
15529 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015530 break;
15531 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015532 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015533 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015534 case VAR_DICT:
15535 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15536 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015537 case VAR_UNKNOWN:
15538 case VAR_SPECIAL:
15539 case VAR_FLOAT:
15540 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010015541 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015542 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015543 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015544 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015545 break;
15546 }
15547}
15548
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015549static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015550
15551 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015552libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015553{
15554#ifdef FEAT_LIBCALL
15555 char_u *string_in;
15556 char_u **string_result;
15557 int nr_result;
15558#endif
15559
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015560 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015561 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015562 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015563
15564 if (check_restricted() || check_secure())
15565 return;
15566
15567#ifdef FEAT_LIBCALL
15568 /* The first two args must be strings, otherwise its meaningless */
15569 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15570 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015571 string_in = NULL;
15572 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015573 string_in = argvars[2].vval.v_string;
15574 if (type == VAR_NUMBER)
15575 string_result = NULL;
15576 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015577 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015578 if (mch_libcall(argvars[0].vval.v_string,
15579 argvars[1].vval.v_string,
15580 string_in,
15581 argvars[2].vval.v_number,
15582 string_result,
15583 &nr_result) == OK
15584 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015585 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015586 }
15587#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015588}
15589
15590/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015591 * "libcall()" function
15592 */
15593 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015594f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015595{
15596 libcall_common(argvars, rettv, VAR_STRING);
15597}
15598
15599/*
15600 * "libcallnr()" function
15601 */
15602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015603f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015604{
15605 libcall_common(argvars, rettv, VAR_NUMBER);
15606}
15607
15608/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015609 * "line(string)" function
15610 */
15611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015612f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015613{
15614 linenr_T lnum = 0;
15615 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015616 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015617
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015618 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015619 if (fp != NULL)
15620 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015621 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015622}
15623
15624/*
15625 * "line2byte(lnum)" function
15626 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015627 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015628f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015629{
15630#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015631 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015632#else
15633 linenr_T lnum;
15634
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015635 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015636 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015637 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015638 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015639 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15640 if (rettv->vval.v_number >= 0)
15641 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015642#endif
15643}
15644
15645/*
15646 * "lispindent(lnum)" function
15647 */
15648 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015649f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015650{
15651#ifdef FEAT_LISP
15652 pos_T pos;
15653 linenr_T lnum;
15654
15655 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015656 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015657 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15658 {
15659 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015660 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015661 curwin->w_cursor = pos;
15662 }
15663 else
15664#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015665 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015666}
15667
15668/*
15669 * "localtime()" function
15670 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015672f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015673{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015674 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015675}
15676
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015677static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015678
15679 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015680get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015681{
15682 char_u *keys;
15683 char_u *which;
15684 char_u buf[NUMBUFLEN];
15685 char_u *keys_buf = NULL;
15686 char_u *rhs;
15687 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015688 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015689 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015690 mapblock_T *mp;
15691 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015692
15693 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015694 rettv->v_type = VAR_STRING;
15695 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015696
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015697 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015698 if (*keys == NUL)
15699 return;
15700
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015701 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015702 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015703 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015704 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015705 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015706 abbr = (int)get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015707 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015708 get_dict = (int)get_tv_number(&argvars[3]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015709 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015711 else
15712 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015713 if (which == NULL)
15714 return;
15715
Bram Moolenaar071d4272004-06-13 20:20:40 +000015716 mode = get_map_mode(&which, 0);
15717
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015718 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015719 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015720 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015721
15722 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015723 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015724 /* Return a string. */
15725 if (rhs != NULL)
15726 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015727
Bram Moolenaarbd743252010-10-20 21:23:33 +020015728 }
15729 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15730 {
15731 /* Return a dictionary. */
15732 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15733 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15734 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015735
Bram Moolenaarbd743252010-10-20 21:23:33 +020015736 dict_add_nr_str(dict, "lhs", 0L, lhs);
15737 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15738 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15739 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15740 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15741 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15742 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015743 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015744 dict_add_nr_str(dict, "mode", 0L, mapmode);
15745
15746 vim_free(lhs);
15747 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015748 }
15749}
15750
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015751#ifdef FEAT_FLOAT
15752/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015753 * "log()" function
15754 */
15755 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015756f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015757{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015758 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015759
15760 rettv->v_type = VAR_FLOAT;
15761 if (get_float_arg(argvars, &f) == OK)
15762 rettv->vval.v_float = log(f);
15763 else
15764 rettv->vval.v_float = 0.0;
15765}
15766
15767/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015768 * "log10()" function
15769 */
15770 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015771f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015772{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015773 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015774
15775 rettv->v_type = VAR_FLOAT;
15776 if (get_float_arg(argvars, &f) == OK)
15777 rettv->vval.v_float = log10(f);
15778 else
15779 rettv->vval.v_float = 0.0;
15780}
15781#endif
15782
Bram Moolenaar1dced572012-04-05 16:54:08 +020015783#ifdef FEAT_LUA
15784/*
15785 * "luaeval()" function
15786 */
15787 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015788f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015789{
15790 char_u *str;
15791 char_u buf[NUMBUFLEN];
15792
15793 str = get_tv_string_buf(&argvars[0], buf);
15794 do_luaeval(str, argvars + 1, rettv);
15795}
15796#endif
15797
Bram Moolenaar071d4272004-06-13 20:20:40 +000015798/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015799 * "map()" function
15800 */
15801 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015802f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015803{
15804 filter_map(argvars, rettv, TRUE);
15805}
15806
15807/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015808 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015809 */
15810 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015811f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015812{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015813 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015814}
15815
15816/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015817 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015818 */
15819 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015820f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015821{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015822 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015823}
15824
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015825static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015826
15827 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015828find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015829{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015830 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015831 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015832 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015833 char_u *pat;
15834 regmatch_T regmatch;
15835 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015836 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015837 char_u *save_cpo;
15838 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015839 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015840 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015841 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015842 list_T *l = NULL;
15843 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015844 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015845 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015846
15847 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15848 save_cpo = p_cpo;
15849 p_cpo = (char_u *)"";
15850
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015851 rettv->vval.v_number = -1;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015852 if (type == 3 || type == 4)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015853 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015854 /* type 3: return empty list when there are no matches.
15855 * type 4: return ["", -1, -1, -1] */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015856 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015857 goto theend;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015858 if (type == 4
15859 && (list_append_string(rettv->vval.v_list,
15860 (char_u *)"", 0) == FAIL
15861 || list_append_number(rettv->vval.v_list,
15862 (varnumber_T)-1) == FAIL
15863 || list_append_number(rettv->vval.v_list,
15864 (varnumber_T)-1) == FAIL
15865 || list_append_number(rettv->vval.v_list,
15866 (varnumber_T)-1) == FAIL))
15867 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020015868 list_free(rettv->vval.v_list);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015869 rettv->vval.v_list = NULL;
15870 goto theend;
15871 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015872 }
15873 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015874 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015875 rettv->v_type = VAR_STRING;
15876 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015878
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015879 if (argvars[0].v_type == VAR_LIST)
15880 {
15881 if ((l = argvars[0].vval.v_list) == NULL)
15882 goto theend;
15883 li = l->lv_first;
15884 }
15885 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015886 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015887 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015888 len = (long)STRLEN(str);
15889 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015890
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015891 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15892 if (pat == NULL)
15893 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015894
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015895 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015896 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015897 int error = FALSE;
15898
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015899 start = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015900 if (error)
15901 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015902 if (l != NULL)
15903 {
15904 li = list_find(l, start);
15905 if (li == NULL)
15906 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015907 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015908 }
15909 else
15910 {
15911 if (start < 0)
15912 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015913 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015914 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015915 /* When "count" argument is there ignore matches before "start",
15916 * otherwise skip part of the string. Differs when pattern is "^"
15917 * or "\<". */
15918 if (argvars[3].v_type != VAR_UNKNOWN)
15919 startcol = start;
15920 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015921 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015922 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015923 len -= start;
15924 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015925 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015926
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015927 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015928 nth = (long)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015929 if (error)
15930 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015931 }
15932
15933 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15934 if (regmatch.regprog != NULL)
15935 {
15936 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015937
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015938 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015939 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015940 if (l != NULL)
15941 {
15942 if (li == NULL)
15943 {
15944 match = FALSE;
15945 break;
15946 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015947 vim_free(tofree);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015948 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015949 if (str == NULL)
15950 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015951 }
15952
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015953 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015954
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015955 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015956 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015957 if (l == NULL && !match)
15958 break;
15959
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015960 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015961 if (l != NULL)
15962 {
15963 li = li->li_next;
15964 ++idx;
15965 }
15966 else
15967 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015968#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015969 startcol = (colnr_T)(regmatch.startp[0]
15970 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015971#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015972 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015973#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015974 if (startcol > (colnr_T)len
15975 || str + startcol <= regmatch.startp[0])
15976 {
15977 match = FALSE;
15978 break;
15979 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015980 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015981 }
15982
15983 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015984 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015985 if (type == 4)
15986 {
15987 listitem_T *li1 = rettv->vval.v_list->lv_first;
15988 listitem_T *li2 = li1->li_next;
15989 listitem_T *li3 = li2->li_next;
15990 listitem_T *li4 = li3->li_next;
15991
Bram Moolenaar3c809342016-06-01 22:34:48 +020015992 vim_free(li1->li_tv.vval.v_string);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015993 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
15994 (int)(regmatch.endp[0] - regmatch.startp[0]));
15995 li3->li_tv.vval.v_number =
15996 (varnumber_T)(regmatch.startp[0] - expr);
15997 li4->li_tv.vval.v_number =
15998 (varnumber_T)(regmatch.endp[0] - expr);
15999 if (l != NULL)
16000 li2->li_tv.vval.v_number = (varnumber_T)idx;
16001 }
16002 else if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016003 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016004 int i;
16005
16006 /* return list with matched string and submatches */
16007 for (i = 0; i < NSUBEXP; ++i)
16008 {
16009 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000016010 {
16011 if (list_append_string(rettv->vval.v_list,
16012 (char_u *)"", 0) == FAIL)
16013 break;
16014 }
16015 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000016016 regmatch.startp[i],
16017 (int)(regmatch.endp[i] - regmatch.startp[i]))
16018 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016019 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016020 }
16021 }
16022 else if (type == 2)
16023 {
16024 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016025 if (l != NULL)
16026 copy_tv(&li->li_tv, rettv);
16027 else
16028 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000016029 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016030 }
16031 else if (l != NULL)
16032 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016033 else
16034 {
16035 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016036 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000016037 (varnumber_T)(regmatch.startp[0] - str);
16038 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016039 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000016040 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016041 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016042 }
16043 }
Bram Moolenaar473de612013-06-08 18:19:48 +020016044 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016045 }
16046
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016047 if (type == 4 && l == NULL)
16048 /* matchstrpos() without a list: drop the second item. */
16049 listitem_remove(rettv->vval.v_list,
16050 rettv->vval.v_list->lv_first->li_next);
16051
Bram Moolenaar071d4272004-06-13 20:20:40 +000016052theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016053 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016054 p_cpo = save_cpo;
16055}
16056
16057/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016058 * "match()" function
16059 */
16060 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016061f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016062{
16063 find_some_match(argvars, rettv, 1);
16064}
16065
16066/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016067 * "matchadd()" function
16068 */
16069 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016070f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016071{
16072#ifdef FEAT_SEARCH_EXTRA
16073 char_u buf[NUMBUFLEN];
16074 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
16075 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
16076 int prio = 10; /* default priority */
16077 int id = -1;
16078 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020016079 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016080
16081 rettv->vval.v_number = -1;
16082
16083 if (grp == NULL || pat == NULL)
16084 return;
16085 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016086 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016087 prio = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016088 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020016089 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016090 id = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020016091 if (argvars[4].v_type != VAR_UNKNOWN)
16092 {
16093 if (argvars[4].v_type != VAR_DICT)
16094 {
16095 EMSG(_(e_dictreq));
16096 return;
16097 }
16098 if (dict_find(argvars[4].vval.v_dict,
16099 (char_u *)"conceal", -1) != NULL)
16100 conceal_char = get_dict_string(argvars[4].vval.v_dict,
16101 (char_u *)"conceal", FALSE);
16102 }
16103 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016104 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016105 if (error == TRUE)
16106 return;
16107 if (id >= 1 && id <= 3)
16108 {
16109 EMSGN("E798: ID is reserved for \":match\": %ld", id);
16110 return;
16111 }
16112
Bram Moolenaar6561d522015-07-21 15:48:27 +020016113 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
16114 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020016115#endif
16116}
16117
16118/*
16119 * "matchaddpos()" function
16120 */
16121 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016122f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020016123{
16124#ifdef FEAT_SEARCH_EXTRA
16125 char_u buf[NUMBUFLEN];
16126 char_u *group;
16127 int prio = 10;
16128 int id = -1;
16129 int error = FALSE;
16130 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020016131 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020016132
16133 rettv->vval.v_number = -1;
16134
16135 group = get_tv_string_buf_chk(&argvars[0], buf);
16136 if (group == NULL)
16137 return;
16138
16139 if (argvars[1].v_type != VAR_LIST)
16140 {
16141 EMSG2(_(e_listarg), "matchaddpos()");
16142 return;
16143 }
16144 l = argvars[1].vval.v_list;
16145 if (l == NULL)
16146 return;
16147
16148 if (argvars[2].v_type != VAR_UNKNOWN)
16149 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016150 prio = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaarb3414592014-06-17 17:48:32 +020016151 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020016152 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016153 id = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020016154 if (argvars[4].v_type != VAR_UNKNOWN)
16155 {
16156 if (argvars[4].v_type != VAR_DICT)
16157 {
16158 EMSG(_(e_dictreq));
16159 return;
16160 }
16161 if (dict_find(argvars[4].vval.v_dict,
16162 (char_u *)"conceal", -1) != NULL)
16163 conceal_char = get_dict_string(argvars[4].vval.v_dict,
16164 (char_u *)"conceal", FALSE);
16165 }
16166 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020016167 }
16168 if (error == TRUE)
16169 return;
16170
16171 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
16172 if (id == 1 || id == 2)
16173 {
16174 EMSGN("E798: ID is reserved for \":match\": %ld", id);
16175 return;
16176 }
16177
Bram Moolenaar6561d522015-07-21 15:48:27 +020016178 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
16179 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016180#endif
16181}
16182
16183/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016184 * "matcharg()" function
16185 */
16186 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016187f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016188{
16189 if (rettv_list_alloc(rettv) == OK)
16190 {
16191#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016192 int id = (int)get_tv_number(&argvars[0]);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016193 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016194
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016195 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016196 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016197 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
16198 {
16199 list_append_string(rettv->vval.v_list,
16200 syn_id2name(m->hlg_id), -1);
16201 list_append_string(rettv->vval.v_list, m->pattern, -1);
16202 }
16203 else
16204 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010016205 list_append_string(rettv->vval.v_list, NULL, -1);
16206 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016207 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016208 }
16209#endif
16210 }
16211}
16212
16213/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016214 * "matchdelete()" function
16215 */
16216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016217f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016218{
16219#ifdef FEAT_SEARCH_EXTRA
16220 rettv->vval.v_number = match_delete(curwin,
16221 (int)get_tv_number(&argvars[0]), TRUE);
16222#endif
16223}
16224
16225/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016226 * "matchend()" function
16227 */
16228 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016229f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016230{
16231 find_some_match(argvars, rettv, 0);
16232}
16233
16234/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016235 * "matchlist()" function
16236 */
16237 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016238f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016239{
16240 find_some_match(argvars, rettv, 3);
16241}
16242
16243/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016244 * "matchstr()" function
16245 */
16246 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016247f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016248{
16249 find_some_match(argvars, rettv, 2);
16250}
16251
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016252/*
16253 * "matchstrpos()" function
16254 */
16255 static void
16256f_matchstrpos(typval_T *argvars, typval_T *rettv)
16257{
16258 find_some_match(argvars, rettv, 4);
16259}
16260
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016261static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016262
16263 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016264max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016265{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016266 varnumber_T n = 0;
16267 varnumber_T i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016268 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016269
16270 if (argvars[0].v_type == VAR_LIST)
16271 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016272 list_T *l;
16273 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016274
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016275 l = argvars[0].vval.v_list;
16276 if (l != NULL)
16277 {
16278 li = l->lv_first;
16279 if (li != NULL)
16280 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016281 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000016282 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016283 {
16284 li = li->li_next;
16285 if (li == NULL)
16286 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016287 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016288 if (domax ? i > n : i < n)
16289 n = i;
16290 }
16291 }
16292 }
16293 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016294 else if (argvars[0].v_type == VAR_DICT)
16295 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016296 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016297 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016298 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016299 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016300
16301 d = argvars[0].vval.v_dict;
16302 if (d != NULL)
16303 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016304 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016305 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016306 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016307 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016308 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016309 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016310 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016311 if (first)
16312 {
16313 n = i;
16314 first = FALSE;
16315 }
16316 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016317 n = i;
16318 }
16319 }
16320 }
16321 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016322 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016323 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016324 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016325}
16326
16327/*
16328 * "max()" function
16329 */
16330 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016331f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016332{
16333 max_min(argvars, rettv, TRUE);
16334}
16335
16336/*
16337 * "min()" function
16338 */
16339 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016340f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016341{
16342 max_min(argvars, rettv, FALSE);
16343}
16344
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016345static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016346
16347/*
16348 * Create the directory in which "dir" is located, and higher levels when
16349 * needed.
16350 */
16351 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016352mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016353{
16354 char_u *p;
16355 char_u *updir;
16356 int r = FAIL;
16357
16358 /* Get end of directory name in "dir".
16359 * We're done when it's "/" or "c:/". */
16360 p = gettail_sep(dir);
16361 if (p <= get_past_head(dir))
16362 return OK;
16363
16364 /* If the directory exists we're done. Otherwise: create it.*/
16365 updir = vim_strnsave(dir, (int)(p - dir));
16366 if (updir == NULL)
16367 return FAIL;
16368 if (mch_isdir(updir))
16369 r = OK;
16370 else if (mkdir_recurse(updir, prot) == OK)
16371 r = vim_mkdir_emsg(updir, prot);
16372 vim_free(updir);
16373 return r;
16374}
16375
16376#ifdef vim_mkdir
16377/*
16378 * "mkdir()" function
16379 */
16380 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016381f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016382{
16383 char_u *dir;
16384 char_u buf[NUMBUFLEN];
16385 int prot = 0755;
16386
16387 rettv->vval.v_number = FAIL;
16388 if (check_restricted() || check_secure())
16389 return;
16390
16391 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016392 if (*dir == NUL)
16393 rettv->vval.v_number = FAIL;
16394 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016395 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016396 if (*gettail(dir) == NUL)
16397 /* remove trailing slashes */
16398 *gettail_sep(dir) = NUL;
16399
16400 if (argvars[1].v_type != VAR_UNKNOWN)
16401 {
16402 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016403 prot = (int)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016404 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16405 mkdir_recurse(dir, prot);
16406 }
16407 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016408 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016409}
16410#endif
16411
Bram Moolenaar0d660222005-01-07 21:51:51 +000016412/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016413 * "mode()" function
16414 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016415 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016416f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016417{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016418 char_u buf[3];
16419
16420 buf[1] = NUL;
16421 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016422
Bram Moolenaare381d3d2016-07-07 14:50:41 +020016423 if (time_for_testing == 93784)
16424 {
16425 /* Testing the two-character code. */
16426 buf[0] = 'x';
16427 buf[1] = '!';
16428 }
16429 else if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016430 {
16431 if (VIsual_select)
16432 buf[0] = VIsual_mode + 's' - 'v';
16433 else
16434 buf[0] = VIsual_mode;
16435 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016436 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016437 || State == CONFIRM)
16438 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016439 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016440 if (State == ASKMORE)
16441 buf[1] = 'm';
16442 else if (State == CONFIRM)
16443 buf[1] = '?';
16444 }
16445 else if (State == EXTERNCMD)
16446 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016447 else if (State & INSERT)
16448 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016449#ifdef FEAT_VREPLACE
16450 if (State & VREPLACE_FLAG)
16451 {
16452 buf[0] = 'R';
16453 buf[1] = 'v';
16454 }
16455 else
16456#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016457 if (State & REPLACE_FLAG)
16458 buf[0] = 'R';
16459 else
16460 buf[0] = 'i';
16461 }
16462 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016463 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016464 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016465 if (exmode_active)
16466 buf[1] = 'v';
16467 }
16468 else if (exmode_active)
16469 {
16470 buf[0] = 'c';
16471 buf[1] = 'e';
16472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016473 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016474 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016475 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016476 if (finish_op)
16477 buf[1] = 'o';
16478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016479
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016480 /* Clear out the minor mode when the argument is not a non-zero number or
16481 * non-empty string. */
16482 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016483 buf[1] = NUL;
16484
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016485 rettv->vval.v_string = vim_strsave(buf);
16486 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016487}
16488
Bram Moolenaar429fa852013-04-15 12:27:36 +020016489#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016490/*
16491 * "mzeval()" function
16492 */
16493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016494f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016495{
16496 char_u *str;
16497 char_u buf[NUMBUFLEN];
16498
16499 str = get_tv_string_buf(&argvars[0], buf);
16500 do_mzeval(str, rettv);
16501}
Bram Moolenaar75676462013-01-30 14:55:42 +010016502
16503 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016504mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016505{
16506 typval_T argvars[3];
16507
16508 argvars[0].v_type = VAR_STRING;
16509 argvars[0].vval.v_string = name;
16510 copy_tv(args, &argvars[1]);
16511 argvars[2].v_type = VAR_UNKNOWN;
16512 f_call(argvars, rettv);
16513 clear_tv(&argvars[1]);
16514}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016515#endif
16516
Bram Moolenaar071d4272004-06-13 20:20:40 +000016517/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016518 * "nextnonblank()" function
16519 */
16520 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016521f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016522{
16523 linenr_T lnum;
16524
16525 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16526 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016527 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016528 {
16529 lnum = 0;
16530 break;
16531 }
16532 if (*skipwhite(ml_get(lnum)) != NUL)
16533 break;
16534 }
16535 rettv->vval.v_number = lnum;
16536}
16537
16538/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016539 * "nr2char()" function
16540 */
16541 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016542f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016543{
16544 char_u buf[NUMBUFLEN];
16545
16546#ifdef FEAT_MBYTE
16547 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016548 {
16549 int utf8 = 0;
16550
16551 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016552 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaard35d7842013-01-23 17:17:10 +010016553 if (utf8)
16554 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16555 else
16556 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016558 else
16559#endif
16560 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016561 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016562 buf[1] = NUL;
16563 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016564 rettv->v_type = VAR_STRING;
16565 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016566}
16567
16568/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016569 * "or(expr, expr)" function
16570 */
16571 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016572f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016573{
16574 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16575 | get_tv_number_chk(&argvars[1], NULL);
16576}
16577
16578/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016579 * "pathshorten()" function
16580 */
16581 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016582f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016583{
16584 char_u *p;
16585
16586 rettv->v_type = VAR_STRING;
16587 p = get_tv_string_chk(&argvars[0]);
16588 if (p == NULL)
16589 rettv->vval.v_string = NULL;
16590 else
16591 {
16592 p = vim_strsave(p);
16593 rettv->vval.v_string = p;
16594 if (p != NULL)
16595 shorten_dir(p);
16596 }
16597}
16598
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016599#ifdef FEAT_PERL
16600/*
16601 * "perleval()" function
16602 */
16603 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016604f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016605{
16606 char_u *str;
16607 char_u buf[NUMBUFLEN];
16608
16609 str = get_tv_string_buf(&argvars[0], buf);
16610 do_perleval(str, rettv);
16611}
16612#endif
16613
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016614#ifdef FEAT_FLOAT
16615/*
16616 * "pow()" function
16617 */
16618 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016619f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016620{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016621 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016622
16623 rettv->v_type = VAR_FLOAT;
16624 if (get_float_arg(argvars, &fx) == OK
16625 && get_float_arg(&argvars[1], &fy) == OK)
16626 rettv->vval.v_float = pow(fx, fy);
16627 else
16628 rettv->vval.v_float = 0.0;
16629}
16630#endif
16631
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016632/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016633 * "prevnonblank()" function
16634 */
16635 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016636f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016637{
16638 linenr_T lnum;
16639
16640 lnum = get_tv_lnum(argvars);
16641 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16642 lnum = 0;
16643 else
16644 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16645 --lnum;
16646 rettv->vval.v_number = lnum;
16647}
16648
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016649/* This dummy va_list is here because:
16650 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16651 * - locally in the function results in a "used before set" warning
16652 * - using va_start() to initialize it gives "function with fixed args" error */
16653static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016654
Bram Moolenaar8c711452005-01-14 21:53:12 +000016655/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016656 * "printf()" function
16657 */
16658 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016659f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016660{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016661 char_u buf[NUMBUFLEN];
16662 int len;
16663 char_u *s;
16664 int saved_did_emsg = did_emsg;
16665 char *fmt;
16666
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016667 rettv->v_type = VAR_STRING;
16668 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016669
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016670 /* Get the required length, allocate the buffer and do it for real. */
16671 did_emsg = FALSE;
16672 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16673 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16674 if (!did_emsg)
16675 {
16676 s = alloc(len + 1);
16677 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016678 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016679 rettv->vval.v_string = s;
16680 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016681 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016682 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016683 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016684}
16685
16686/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016687 * "pumvisible()" function
16688 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016689 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016690f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016691{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016692#ifdef FEAT_INS_EXPAND
16693 if (pum_visible())
16694 rettv->vval.v_number = 1;
16695#endif
16696}
16697
Bram Moolenaardb913952012-06-29 12:54:53 +020016698#ifdef FEAT_PYTHON3
16699/*
16700 * "py3eval()" function
16701 */
16702 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016703f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016704{
16705 char_u *str;
16706 char_u buf[NUMBUFLEN];
16707
16708 str = get_tv_string_buf(&argvars[0], buf);
16709 do_py3eval(str, rettv);
16710}
16711#endif
16712
16713#ifdef FEAT_PYTHON
16714/*
16715 * "pyeval()" function
16716 */
16717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016718f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016719{
16720 char_u *str;
16721 char_u buf[NUMBUFLEN];
16722
16723 str = get_tv_string_buf(&argvars[0], buf);
16724 do_pyeval(str, rettv);
16725}
16726#endif
16727
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016728/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016729 * "range()" function
16730 */
16731 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016732f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016733{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016734 varnumber_T start;
16735 varnumber_T end;
16736 varnumber_T stride = 1;
16737 varnumber_T i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016738 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016739
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016740 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016741 if (argvars[1].v_type == VAR_UNKNOWN)
16742 {
16743 end = start - 1;
16744 start = 0;
16745 }
16746 else
16747 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016748 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016749 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016750 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016751 }
16752
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016753 if (error)
16754 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016755 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016756 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016757 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016758 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016759 else
16760 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016761 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016762 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016763 if (list_append_number(rettv->vval.v_list,
16764 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016765 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016766 }
16767}
16768
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016769/*
16770 * "readfile()" function
16771 */
16772 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016773f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016774{
16775 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016776 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016777 char_u *fname;
16778 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016779 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16780 int io_size = sizeof(buf);
16781 int readlen; /* size of last fread() */
16782 char_u *prev = NULL; /* previously read bytes, if any */
16783 long prevlen = 0; /* length of data in prev */
16784 long prevsize = 0; /* size of prev buffer */
16785 long maxline = MAXLNUM;
16786 long cnt = 0;
16787 char_u *p; /* position in buf */
16788 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016789
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016790 if (argvars[1].v_type != VAR_UNKNOWN)
16791 {
16792 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16793 binary = TRUE;
16794 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016795 maxline = (long)get_tv_number(&argvars[2]);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016796 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016797
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016798 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016799 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016800
16801 /* Always open the file in binary mode, library functions have a mind of
16802 * their own about CR-LF conversion. */
16803 fname = get_tv_string(&argvars[0]);
16804 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16805 {
16806 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16807 return;
16808 }
16809
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016810 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016811 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016812 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016813
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016814 /* This for loop processes what was read, but is also entered at end
16815 * of file so that either:
16816 * - an incomplete line gets written
16817 * - a "binary" file gets an empty line at the end if it ends in a
16818 * newline. */
16819 for (p = buf, start = buf;
16820 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16821 ++p)
16822 {
16823 if (*p == '\n' || readlen <= 0)
16824 {
16825 listitem_T *li;
16826 char_u *s = NULL;
16827 long_u len = p - start;
16828
16829 /* Finished a line. Remove CRs before NL. */
16830 if (readlen > 0 && !binary)
16831 {
16832 while (len > 0 && start[len - 1] == '\r')
16833 --len;
16834 /* removal may cross back to the "prev" string */
16835 if (len == 0)
16836 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16837 --prevlen;
16838 }
16839 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016840 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016841 else
16842 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016843 /* Change "prev" buffer to be the right size. This way
16844 * the bytes are only copied once, and very long lines are
16845 * allocated only once. */
16846 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016847 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016848 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016849 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016850 prev = NULL; /* the list will own the string */
16851 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016852 }
16853 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016854 if (s == NULL)
16855 {
16856 do_outofmem_msg((long_u) prevlen + len + 1);
16857 failed = TRUE;
16858 break;
16859 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016860
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016861 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016862 {
16863 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016864 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016865 break;
16866 }
16867 li->li_tv.v_type = VAR_STRING;
16868 li->li_tv.v_lock = 0;
16869 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016870 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016871
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016872 start = p + 1; /* step over newline */
16873 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016874 break;
16875 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016876 else if (*p == NUL)
16877 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016878#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016879 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16880 * when finding the BF and check the previous two bytes. */
16881 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016882 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016883 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16884 * + 1, these may be in the "prev" string. */
16885 char_u back1 = p >= buf + 1 ? p[-1]
16886 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16887 char_u back2 = p >= buf + 2 ? p[-2]
16888 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16889 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016890
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016891 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016892 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016893 char_u *dest = p - 2;
16894
16895 /* Usually a BOM is at the beginning of a file, and so at
16896 * the beginning of a line; then we can just step over it.
16897 */
16898 if (start == dest)
16899 start = p + 1;
16900 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016901 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016902 /* have to shuffle buf to close gap */
16903 int adjust_prevlen = 0;
16904
16905 if (dest < buf)
16906 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016907 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016908 dest = buf;
16909 }
16910 if (readlen > p - buf + 1)
16911 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16912 readlen -= 3 - adjust_prevlen;
16913 prevlen -= adjust_prevlen;
16914 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016915 }
16916 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016917 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016918#endif
16919 } /* for */
16920
16921 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16922 break;
16923 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016924 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016925 /* There's part of a line in buf, store it in "prev". */
16926 if (p - start + prevlen >= prevsize)
16927 {
16928 /* need bigger "prev" buffer */
16929 char_u *newprev;
16930
16931 /* A common use case is ordinary text files and "prev" gets a
16932 * fragment of a line, so the first allocation is made
16933 * small, to avoid repeatedly 'allocing' large and
16934 * 'reallocing' small. */
16935 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016936 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016937 else
16938 {
16939 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016940 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016941 prevsize = grow50pc > growmin ? grow50pc : growmin;
16942 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016943 newprev = prev == NULL ? alloc(prevsize)
16944 : vim_realloc(prev, prevsize);
16945 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016946 {
16947 do_outofmem_msg((long_u)prevsize);
16948 failed = TRUE;
16949 break;
16950 }
16951 prev = newprev;
16952 }
16953 /* Add the line part to end of "prev". */
16954 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016955 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016956 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016957 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016958
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016959 /*
16960 * For a negative line count use only the lines at the end of the file,
16961 * free the rest.
16962 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016963 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016964 while (cnt > -maxline)
16965 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016966 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016967 --cnt;
16968 }
16969
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016970 if (failed)
16971 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020016972 list_free(rettv->vval.v_list);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016973 /* readfile doc says an empty list is returned on error */
16974 rettv->vval.v_list = list_alloc();
16975 }
16976
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016977 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016978 fclose(fd);
16979}
16980
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016981#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016982static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016983
16984/*
16985 * Convert a List to proftime_T.
16986 * Return FAIL when there is something wrong.
16987 */
16988 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016989list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016990{
16991 long n1, n2;
16992 int error = FALSE;
16993
16994 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16995 || arg->vval.v_list->lv_len != 2)
16996 return FAIL;
16997 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16998 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16999# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000017000 tm->HighPart = n1;
17001 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017002# else
17003 tm->tv_sec = n1;
17004 tm->tv_usec = n2;
17005# endif
17006 return error ? FAIL : OK;
17007}
17008#endif /* FEAT_RELTIME */
17009
17010/*
17011 * "reltime()" function
17012 */
17013 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017014f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017015{
17016#ifdef FEAT_RELTIME
17017 proftime_T res;
17018 proftime_T start;
17019
17020 if (argvars[0].v_type == VAR_UNKNOWN)
17021 {
17022 /* No arguments: get current time. */
17023 profile_start(&res);
17024 }
17025 else if (argvars[1].v_type == VAR_UNKNOWN)
17026 {
17027 if (list2proftime(&argvars[0], &res) == FAIL)
17028 return;
17029 profile_end(&res);
17030 }
17031 else
17032 {
17033 /* Two arguments: compute the difference. */
17034 if (list2proftime(&argvars[0], &start) == FAIL
17035 || list2proftime(&argvars[1], &res) == FAIL)
17036 return;
17037 profile_sub(&res, &start);
17038 }
17039
17040 if (rettv_list_alloc(rettv) == OK)
17041 {
17042 long n1, n2;
17043
17044# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000017045 n1 = res.HighPart;
17046 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017047# else
17048 n1 = res.tv_sec;
17049 n2 = res.tv_usec;
17050# endif
17051 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
17052 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
17053 }
17054#endif
17055}
17056
Bram Moolenaar79c2c882016-02-07 21:19:28 +010017057#ifdef FEAT_FLOAT
17058/*
17059 * "reltimefloat()" function
17060 */
17061 static void
17062f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
17063{
17064# ifdef FEAT_RELTIME
17065 proftime_T tm;
17066# endif
17067
17068 rettv->v_type = VAR_FLOAT;
17069 rettv->vval.v_float = 0;
17070# ifdef FEAT_RELTIME
17071 if (list2proftime(&argvars[0], &tm) == OK)
17072 rettv->vval.v_float = profile_float(&tm);
17073# endif
17074}
17075#endif
17076
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017077/*
17078 * "reltimestr()" function
17079 */
17080 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017081f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017082{
17083#ifdef FEAT_RELTIME
17084 proftime_T tm;
17085#endif
17086
17087 rettv->v_type = VAR_STRING;
17088 rettv->vval.v_string = NULL;
17089#ifdef FEAT_RELTIME
17090 if (list2proftime(&argvars[0], &tm) == OK)
17091 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
17092#endif
17093}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017094
Bram Moolenaar0d660222005-01-07 21:51:51 +000017095#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017096static void make_connection(void);
17097static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017098
17099 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017100make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017101{
17102 if (X_DISPLAY == NULL
17103# ifdef FEAT_GUI
17104 && !gui.in_use
17105# endif
17106 )
17107 {
17108 x_force_connect = TRUE;
17109 setup_term_clip();
17110 x_force_connect = FALSE;
17111 }
17112}
17113
17114 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017115check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017116{
17117 make_connection();
17118 if (X_DISPLAY == NULL)
17119 {
17120 EMSG(_("E240: No connection to Vim server"));
17121 return FAIL;
17122 }
17123 return OK;
17124}
17125#endif
17126
17127#ifdef FEAT_CLIENTSERVER
Bram Moolenaar0d660222005-01-07 21:51:51 +000017128 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017129remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017130{
17131 char_u *server_name;
17132 char_u *keys;
17133 char_u *r = NULL;
17134 char_u buf[NUMBUFLEN];
17135# ifdef WIN32
17136 HWND w;
17137# else
17138 Window w;
17139# endif
17140
17141 if (check_restricted() || check_secure())
17142 return;
17143
17144# ifdef FEAT_X11
17145 if (check_connection() == FAIL)
17146 return;
17147# endif
17148
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017149 server_name = get_tv_string_chk(&argvars[0]);
17150 if (server_name == NULL)
17151 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017152 keys = get_tv_string_buf(&argvars[1], buf);
17153# ifdef WIN32
17154 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
17155# else
17156 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
17157 < 0)
17158# endif
17159 {
17160 if (r != NULL)
17161 EMSG(r); /* sending worked but evaluation failed */
17162 else
17163 EMSG2(_("E241: Unable to send to %s"), server_name);
17164 return;
17165 }
17166
17167 rettv->vval.v_string = r;
17168
17169 if (argvars[2].v_type != VAR_UNKNOWN)
17170 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017171 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000017172 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017173 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017174
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017175 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000017176 v.di_tv.v_type = VAR_STRING;
17177 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017178 idvar = get_tv_string_chk(&argvars[2]);
17179 if (idvar != NULL)
17180 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017181 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017182 }
17183}
17184#endif
17185
17186/*
17187 * "remote_expr()" function
17188 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017190f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017191{
17192 rettv->v_type = VAR_STRING;
17193 rettv->vval.v_string = NULL;
17194#ifdef FEAT_CLIENTSERVER
17195 remote_common(argvars, rettv, TRUE);
17196#endif
17197}
17198
17199/*
17200 * "remote_foreground()" function
17201 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017203f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017204{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017205#ifdef FEAT_CLIENTSERVER
17206# ifdef WIN32
17207 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017208 {
17209 char_u *server_name = get_tv_string_chk(&argvars[0]);
17210
17211 if (server_name != NULL)
17212 serverForeground(server_name);
17213 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017214# else
17215 /* Send a foreground() expression to the server. */
17216 argvars[1].v_type = VAR_STRING;
17217 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
17218 argvars[2].v_type = VAR_UNKNOWN;
17219 remote_common(argvars, rettv, TRUE);
17220 vim_free(argvars[1].vval.v_string);
17221# endif
17222#endif
17223}
17224
Bram Moolenaar0d660222005-01-07 21:51:51 +000017225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017226f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017227{
17228#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000017229 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017230 char_u *s = NULL;
17231# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017232 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017233# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017234 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017235
17236 if (check_restricted() || check_secure())
17237 {
17238 rettv->vval.v_number = -1;
17239 return;
17240 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017241 serverid = get_tv_string_chk(&argvars[0]);
17242 if (serverid == NULL)
17243 {
17244 rettv->vval.v_number = -1;
17245 return; /* type error; errmsg already given */
17246 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017247# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017248 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017249 if (n == 0)
17250 rettv->vval.v_number = -1;
17251 else
17252 {
17253 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
17254 rettv->vval.v_number = (s != NULL);
17255 }
17256# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017257 if (check_connection() == FAIL)
17258 return;
17259
17260 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017261 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017262# endif
17263
17264 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
17265 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017266 char_u *retvar;
17267
Bram Moolenaar33570922005-01-25 22:26:29 +000017268 v.di_tv.v_type = VAR_STRING;
17269 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017270 retvar = get_tv_string_chk(&argvars[1]);
17271 if (retvar != NULL)
17272 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017273 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017274 }
17275#else
17276 rettv->vval.v_number = -1;
17277#endif
17278}
17279
Bram Moolenaar0d660222005-01-07 21:51:51 +000017280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017281f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017282{
17283 char_u *r = NULL;
17284
17285#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017286 char_u *serverid = get_tv_string_chk(&argvars[0]);
17287
17288 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000017289 {
17290# ifdef WIN32
17291 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017292 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017293
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017294 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017295 if (n != 0)
17296 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
17297 if (r == NULL)
17298# else
17299 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017300 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017301# endif
17302 EMSG(_("E277: Unable to read a server reply"));
17303 }
17304#endif
17305 rettv->v_type = VAR_STRING;
17306 rettv->vval.v_string = r;
17307}
17308
17309/*
17310 * "remote_send()" function
17311 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017312 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017313f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017314{
17315 rettv->v_type = VAR_STRING;
17316 rettv->vval.v_string = NULL;
17317#ifdef FEAT_CLIENTSERVER
17318 remote_common(argvars, rettv, FALSE);
17319#endif
17320}
17321
17322/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017323 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017324 */
17325 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017326f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017327{
Bram Moolenaar33570922005-01-25 22:26:29 +000017328 list_T *l;
17329 listitem_T *item, *item2;
17330 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017331 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017332 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017333 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017334 dict_T *d;
17335 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017336 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017337
Bram Moolenaar8c711452005-01-14 21:53:12 +000017338 if (argvars[0].v_type == VAR_DICT)
17339 {
17340 if (argvars[2].v_type != VAR_UNKNOWN)
17341 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017342 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017343 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017344 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017345 key = get_tv_string_chk(&argvars[1]);
17346 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017347 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017348 di = dict_find(d, key, -1);
17349 if (di == NULL)
17350 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017351 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17352 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017353 {
17354 *rettv = di->di_tv;
17355 init_tv(&di->di_tv);
17356 dictitem_remove(d, di);
17357 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017358 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017359 }
17360 }
17361 else if (argvars[0].v_type != VAR_LIST)
17362 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017363 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017364 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017365 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017366 int error = FALSE;
17367
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017368 idx = (long)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017369 if (error)
17370 ; /* type error: do nothing, errmsg already given */
17371 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017372 EMSGN(_(e_listidx), idx);
17373 else
17374 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017375 if (argvars[2].v_type == VAR_UNKNOWN)
17376 {
17377 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017378 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017379 *rettv = item->li_tv;
17380 vim_free(item);
17381 }
17382 else
17383 {
17384 /* Remove range of items, return list with values. */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017385 end = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017386 if (error)
17387 ; /* type error: do nothing */
17388 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017389 EMSGN(_(e_listidx), end);
17390 else
17391 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017392 int cnt = 0;
17393
17394 for (li = item; li != NULL; li = li->li_next)
17395 {
17396 ++cnt;
17397 if (li == item2)
17398 break;
17399 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017400 if (li == NULL) /* didn't find "item2" after "item" */
17401 EMSG(_(e_invrange));
17402 else
17403 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017404 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017405 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017406 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017407 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017408 l->lv_first = item;
17409 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017410 item->li_prev = NULL;
17411 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017412 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017413 }
17414 }
17415 }
17416 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017417 }
17418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017419}
17420
17421/*
17422 * "rename({from}, {to})" function
17423 */
17424 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017425f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017426{
17427 char_u buf[NUMBUFLEN];
17428
17429 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017430 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017431 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017432 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17433 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017434}
17435
17436/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017437 * "repeat()" function
17438 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017440f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017441{
17442 char_u *p;
17443 int n;
17444 int slen;
17445 int len;
17446 char_u *r;
17447 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017448
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017449 n = (int)get_tv_number(&argvars[1]);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017450 if (argvars[0].v_type == VAR_LIST)
17451 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017452 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017453 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017454 if (list_extend(rettv->vval.v_list,
17455 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017456 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017457 }
17458 else
17459 {
17460 p = get_tv_string(&argvars[0]);
17461 rettv->v_type = VAR_STRING;
17462 rettv->vval.v_string = NULL;
17463
17464 slen = (int)STRLEN(p);
17465 len = slen * n;
17466 if (len <= 0)
17467 return;
17468
17469 r = alloc(len + 1);
17470 if (r != NULL)
17471 {
17472 for (i = 0; i < n; i++)
17473 mch_memmove(r + i * slen, p, (size_t)slen);
17474 r[len] = NUL;
17475 }
17476
17477 rettv->vval.v_string = r;
17478 }
17479}
17480
17481/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017482 * "resolve()" function
17483 */
17484 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017485f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017486{
17487 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017488#ifdef HAVE_READLINK
17489 char_u *buf = NULL;
17490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017491
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017492 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017493#ifdef FEAT_SHORTCUT
17494 {
17495 char_u *v = NULL;
17496
17497 v = mch_resolve_shortcut(p);
17498 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017499 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017500 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017501 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017502 }
17503#else
17504# ifdef HAVE_READLINK
17505 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017506 char_u *cpy;
17507 int len;
17508 char_u *remain = NULL;
17509 char_u *q;
17510 int is_relative_to_current = FALSE;
17511 int has_trailing_pathsep = FALSE;
17512 int limit = 100;
17513
17514 p = vim_strsave(p);
17515
17516 if (p[0] == '.' && (vim_ispathsep(p[1])
17517 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17518 is_relative_to_current = TRUE;
17519
17520 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017521 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017522 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017523 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017524 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017526
17527 q = getnextcomp(p);
17528 if (*q != NUL)
17529 {
17530 /* Separate the first path component in "p", and keep the
17531 * remainder (beginning with the path separator). */
17532 remain = vim_strsave(q - 1);
17533 q[-1] = NUL;
17534 }
17535
Bram Moolenaard9462e32011-04-11 21:35:11 +020017536 buf = alloc(MAXPATHL + 1);
17537 if (buf == NULL)
17538 goto fail;
17539
Bram Moolenaar071d4272004-06-13 20:20:40 +000017540 for (;;)
17541 {
17542 for (;;)
17543 {
17544 len = readlink((char *)p, (char *)buf, MAXPATHL);
17545 if (len <= 0)
17546 break;
17547 buf[len] = NUL;
17548
17549 if (limit-- == 0)
17550 {
17551 vim_free(p);
17552 vim_free(remain);
17553 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017554 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017555 goto fail;
17556 }
17557
17558 /* Ensure that the result will have a trailing path separator
17559 * if the argument has one. */
17560 if (remain == NULL && has_trailing_pathsep)
17561 add_pathsep(buf);
17562
17563 /* Separate the first path component in the link value and
17564 * concatenate the remainders. */
17565 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17566 if (*q != NUL)
17567 {
17568 if (remain == NULL)
17569 remain = vim_strsave(q - 1);
17570 else
17571 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017572 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017573 if (cpy != NULL)
17574 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017575 vim_free(remain);
17576 remain = cpy;
17577 }
17578 }
17579 q[-1] = NUL;
17580 }
17581
17582 q = gettail(p);
17583 if (q > p && *q == NUL)
17584 {
17585 /* Ignore trailing path separator. */
17586 q[-1] = NUL;
17587 q = gettail(p);
17588 }
17589 if (q > p && !mch_isFullName(buf))
17590 {
17591 /* symlink is relative to directory of argument */
17592 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17593 if (cpy != NULL)
17594 {
17595 STRCPY(cpy, p);
17596 STRCPY(gettail(cpy), buf);
17597 vim_free(p);
17598 p = cpy;
17599 }
17600 }
17601 else
17602 {
17603 vim_free(p);
17604 p = vim_strsave(buf);
17605 }
17606 }
17607
17608 if (remain == NULL)
17609 break;
17610
17611 /* Append the first path component of "remain" to "p". */
17612 q = getnextcomp(remain + 1);
17613 len = q - remain - (*q != NUL);
17614 cpy = vim_strnsave(p, STRLEN(p) + len);
17615 if (cpy != NULL)
17616 {
17617 STRNCAT(cpy, remain, len);
17618 vim_free(p);
17619 p = cpy;
17620 }
17621 /* Shorten "remain". */
17622 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017623 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017624 else
17625 {
17626 vim_free(remain);
17627 remain = NULL;
17628 }
17629 }
17630
17631 /* If the result is a relative path name, make it explicitly relative to
17632 * the current directory if and only if the argument had this form. */
17633 if (!vim_ispathsep(*p))
17634 {
17635 if (is_relative_to_current
17636 && *p != NUL
17637 && !(p[0] == '.'
17638 && (p[1] == NUL
17639 || vim_ispathsep(p[1])
17640 || (p[1] == '.'
17641 && (p[2] == NUL
17642 || vim_ispathsep(p[2]))))))
17643 {
17644 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017645 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017646 if (cpy != NULL)
17647 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017648 vim_free(p);
17649 p = cpy;
17650 }
17651 }
17652 else if (!is_relative_to_current)
17653 {
17654 /* Strip leading "./". */
17655 q = p;
17656 while (q[0] == '.' && vim_ispathsep(q[1]))
17657 q += 2;
17658 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017659 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017660 }
17661 }
17662
17663 /* Ensure that the result will have no trailing path separator
17664 * if the argument had none. But keep "/" or "//". */
17665 if (!has_trailing_pathsep)
17666 {
17667 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017668 if (after_pathsep(p, q))
17669 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017670 }
17671
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017672 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017673 }
17674# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017675 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017676# endif
17677#endif
17678
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017679 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017680
17681#ifdef HAVE_READLINK
17682fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017683 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017684#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017685 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017686}
17687
17688/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017689 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017690 */
17691 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017692f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017693{
Bram Moolenaar33570922005-01-25 22:26:29 +000017694 list_T *l;
17695 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017696
Bram Moolenaar0d660222005-01-07 21:51:51 +000017697 if (argvars[0].v_type != VAR_LIST)
17698 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017699 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017700 && !tv_check_lock(l->lv_lock,
17701 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017702 {
17703 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017704 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017705 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017706 while (li != NULL)
17707 {
17708 ni = li->li_prev;
17709 list_append(l, li);
17710 li = ni;
17711 }
17712 rettv->vval.v_list = l;
17713 rettv->v_type = VAR_LIST;
17714 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017715 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017717}
17718
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017719#define SP_NOMOVE 0x01 /* don't move cursor */
17720#define SP_REPEAT 0x02 /* repeat to find outer pair */
17721#define SP_RETCOUNT 0x04 /* return matchcount */
17722#define SP_SETPCMARK 0x08 /* set previous context mark */
17723#define SP_START 0x10 /* accept match at start position */
17724#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17725#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017726#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017727
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017728static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017729
17730/*
17731 * Get flags for a search function.
17732 * Possibly sets "p_ws".
17733 * Returns BACKWARD, FORWARD or zero (for an error).
17734 */
17735 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017736get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017737{
17738 int dir = FORWARD;
17739 char_u *flags;
17740 char_u nbuf[NUMBUFLEN];
17741 int mask;
17742
17743 if (varp->v_type != VAR_UNKNOWN)
17744 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017745 flags = get_tv_string_buf_chk(varp, nbuf);
17746 if (flags == NULL)
17747 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017748 while (*flags != NUL)
17749 {
17750 switch (*flags)
17751 {
17752 case 'b': dir = BACKWARD; break;
17753 case 'w': p_ws = TRUE; break;
17754 case 'W': p_ws = FALSE; break;
17755 default: mask = 0;
17756 if (flagsp != NULL)
17757 switch (*flags)
17758 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017759 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017760 case 'e': mask = SP_END; break;
17761 case 'm': mask = SP_RETCOUNT; break;
17762 case 'n': mask = SP_NOMOVE; break;
17763 case 'p': mask = SP_SUBPAT; break;
17764 case 'r': mask = SP_REPEAT; break;
17765 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017766 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017767 }
17768 if (mask == 0)
17769 {
17770 EMSG2(_(e_invarg2), flags);
17771 dir = 0;
17772 }
17773 else
17774 *flagsp |= mask;
17775 }
17776 if (dir == 0)
17777 break;
17778 ++flags;
17779 }
17780 }
17781 return dir;
17782}
17783
Bram Moolenaar071d4272004-06-13 20:20:40 +000017784/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017785 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017786 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017787 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017788search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017789{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017790 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017791 char_u *pat;
17792 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017793 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017794 int save_p_ws = p_ws;
17795 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017796 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017797 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017798 proftime_T tm;
17799#ifdef FEAT_RELTIME
17800 long time_limit = 0;
17801#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017802 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017803 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017804
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017805 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017806 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017807 if (dir == 0)
17808 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017809 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017810 if (flags & SP_START)
17811 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017812 if (flags & SP_END)
17813 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017814 if (flags & SP_COLUMN)
17815 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017816
Bram Moolenaar76929292008-01-06 19:07:36 +000017817 /* Optional arguments: line number to stop searching and timeout. */
17818 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017819 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017820 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017821 if (lnum_stop < 0)
17822 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017823#ifdef FEAT_RELTIME
17824 if (argvars[3].v_type != VAR_UNKNOWN)
17825 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017826 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
Bram Moolenaar76929292008-01-06 19:07:36 +000017827 if (time_limit < 0)
17828 goto theend;
17829 }
17830#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017831 }
17832
Bram Moolenaar76929292008-01-06 19:07:36 +000017833#ifdef FEAT_RELTIME
17834 /* Set the time limit, if there is one. */
17835 profile_setlimit(time_limit, &tm);
17836#endif
17837
Bram Moolenaar231334e2005-07-25 20:46:57 +000017838 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017839 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017840 * Check to make sure only those flags are set.
17841 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17842 * flags cannot be set. Check for that condition also.
17843 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017844 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017845 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017846 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017847 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017848 goto theend;
17849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017850
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017851 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017852 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017853 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017854 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017855 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017856 if (flags & SP_SUBPAT)
17857 retval = subpatnum;
17858 else
17859 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017860 if (flags & SP_SETPCMARK)
17861 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017862 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017863 if (match_pos != NULL)
17864 {
17865 /* Store the match cursor position */
17866 match_pos->lnum = pos.lnum;
17867 match_pos->col = pos.col + 1;
17868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017869 /* "/$" will put the cursor after the end of the line, may need to
17870 * correct that here */
17871 check_cursor();
17872 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017873
17874 /* If 'n' flag is used: restore cursor position. */
17875 if (flags & SP_NOMOVE)
17876 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017877 else
17878 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017879theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017880 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017881
17882 return retval;
17883}
17884
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017885#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017886
17887/*
17888 * round() is not in C90, use ceil() or floor() instead.
17889 */
17890 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017891vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017892{
17893 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17894}
17895
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017896/*
17897 * "round({float})" function
17898 */
17899 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017900f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017901{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017902 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017903
17904 rettv->v_type = VAR_FLOAT;
17905 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017906 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017907 else
17908 rettv->vval.v_float = 0.0;
17909}
17910#endif
17911
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017912/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017913 * "screenattr()" function
17914 */
17915 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017916f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017917{
17918 int row;
17919 int col;
17920 int c;
17921
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017922 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
17923 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar9a773482013-06-11 18:40:13 +020017924 if (row < 0 || row >= screen_Rows
17925 || col < 0 || col >= screen_Columns)
17926 c = -1;
17927 else
17928 c = ScreenAttrs[LineOffset[row] + col];
17929 rettv->vval.v_number = c;
17930}
17931
17932/*
17933 * "screenchar()" function
17934 */
17935 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017936f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017937{
17938 int row;
17939 int col;
17940 int off;
17941 int c;
17942
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017943 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
17944 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar9a773482013-06-11 18:40:13 +020017945 if (row < 0 || row >= screen_Rows
17946 || col < 0 || col >= screen_Columns)
17947 c = -1;
17948 else
17949 {
17950 off = LineOffset[row] + col;
17951#ifdef FEAT_MBYTE
17952 if (enc_utf8 && ScreenLinesUC[off] != 0)
17953 c = ScreenLinesUC[off];
17954 else
17955#endif
17956 c = ScreenLines[off];
17957 }
17958 rettv->vval.v_number = c;
17959}
17960
17961/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017962 * "screencol()" function
17963 *
17964 * First column is 1 to be consistent with virtcol().
17965 */
17966 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017967f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017968{
17969 rettv->vval.v_number = screen_screencol() + 1;
17970}
17971
17972/*
17973 * "screenrow()" function
17974 */
17975 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017976f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017977{
17978 rettv->vval.v_number = screen_screenrow() + 1;
17979}
17980
17981/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017982 * "search()" function
17983 */
17984 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017985f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017986{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017987 int flags = 0;
17988
17989 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017990}
17991
Bram Moolenaar071d4272004-06-13 20:20:40 +000017992/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017993 * "searchdecl()" function
17994 */
17995 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017996f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017997{
17998 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017999 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018000 int error = FALSE;
18001 char_u *name;
18002
18003 rettv->vval.v_number = 1; /* default: FAIL */
18004
18005 name = get_tv_string_chk(&argvars[0]);
18006 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000018007 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018008 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000018009 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018010 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000018011 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018012 if (!error && name != NULL)
18013 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000018014 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000018015}
18016
18017/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018018 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000018019 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018020 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010018021searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018022{
18023 char_u *spat, *mpat, *epat;
18024 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018025 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018026 int dir;
18027 int flags = 0;
18028 char_u nbuf1[NUMBUFLEN];
18029 char_u nbuf2[NUMBUFLEN];
18030 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018031 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018032 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000018033 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018034
Bram Moolenaar071d4272004-06-13 20:20:40 +000018035 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018036 spat = get_tv_string_chk(&argvars[0]);
18037 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
18038 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
18039 if (spat == NULL || mpat == NULL || epat == NULL)
18040 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018041
Bram Moolenaar071d4272004-06-13 20:20:40 +000018042 /* Handle the optional fourth argument: flags */
18043 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018044 if (dir == 0)
18045 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018046
18047 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000018048 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
18049 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018050 if ((flags & (SP_END | SP_SUBPAT)) != 0
18051 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000018052 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018053 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000018054 goto theend;
18055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018056
Bram Moolenaar92de73d2008-01-22 10:59:38 +000018057 /* Using 'r' implies 'W', otherwise it doesn't work. */
18058 if (flags & SP_REPEAT)
18059 p_ws = FALSE;
18060
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018061 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018062 if (argvars[3].v_type == VAR_UNKNOWN
18063 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018064 skip = (char_u *)"";
18065 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018066 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018067 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018068 if (argvars[5].v_type != VAR_UNKNOWN)
18069 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018070 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018071 if (lnum_stop < 0)
18072 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000018073#ifdef FEAT_RELTIME
18074 if (argvars[6].v_type != VAR_UNKNOWN)
18075 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018076 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
Bram Moolenaar76929292008-01-06 19:07:36 +000018077 if (time_limit < 0)
18078 goto theend;
18079 }
18080#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018081 }
18082 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018083 if (skip == NULL)
18084 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018085
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018086 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000018087 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018088
18089theend:
18090 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018091
18092 return retval;
18093}
18094
18095/*
18096 * "searchpair()" function
18097 */
18098 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018099f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018100{
18101 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
18102}
18103
18104/*
18105 * "searchpairpos()" function
18106 */
18107 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018108f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018109{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018110 pos_T match_pos;
18111 int lnum = 0;
18112 int col = 0;
18113
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018114 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018115 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018116
18117 if (searchpair_cmn(argvars, &match_pos) > 0)
18118 {
18119 lnum = match_pos.lnum;
18120 col = match_pos.col;
18121 }
18122
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018123 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18124 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018125}
18126
18127/*
18128 * Search for a start/middle/end thing.
18129 * Used by searchpair(), see its documentation for the details.
18130 * Returns 0 or -1 for no match,
18131 */
18132 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010018133do_searchpair(
18134 char_u *spat, /* start pattern */
18135 char_u *mpat, /* middle pattern */
18136 char_u *epat, /* end pattern */
18137 int dir, /* BACKWARD or FORWARD */
18138 char_u *skip, /* skip expression */
18139 int flags, /* SP_SETPCMARK and other SP_ values */
18140 pos_T *match_pos,
18141 linenr_T lnum_stop, /* stop at this line if not zero */
18142 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018143{
18144 char_u *save_cpo;
18145 char_u *pat, *pat2 = NULL, *pat3 = NULL;
18146 long retval = 0;
18147 pos_T pos;
18148 pos_T firstpos;
18149 pos_T foundpos;
18150 pos_T save_cursor;
18151 pos_T save_pos;
18152 int n;
18153 int r;
18154 int nest = 1;
18155 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018156 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000018157 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018158
18159 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18160 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018161 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018162
Bram Moolenaar76929292008-01-06 19:07:36 +000018163#ifdef FEAT_RELTIME
18164 /* Set the time limit, if there is one. */
18165 profile_setlimit(time_limit, &tm);
18166#endif
18167
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018168 /* Make two search patterns: start/end (pat2, for in nested pairs) and
18169 * start/middle/end (pat3, for the top pair). */
18170 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
18171 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
18172 if (pat2 == NULL || pat3 == NULL)
18173 goto theend;
18174 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
18175 if (*mpat == NUL)
18176 STRCPY(pat3, pat2);
18177 else
18178 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
18179 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018180 if (flags & SP_START)
18181 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018182
Bram Moolenaar071d4272004-06-13 20:20:40 +000018183 save_cursor = curwin->w_cursor;
18184 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018185 clearpos(&firstpos);
18186 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018187 pat = pat3;
18188 for (;;)
18189 {
18190 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000018191 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018192 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
18193 /* didn't find it or found the first match again: FAIL */
18194 break;
18195
18196 if (firstpos.lnum == 0)
18197 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000018198 if (equalpos(pos, foundpos))
18199 {
18200 /* Found the same position again. Can happen with a pattern that
18201 * has "\zs" at the end and searching backwards. Advance one
18202 * character and try again. */
18203 if (dir == BACKWARD)
18204 decl(&pos);
18205 else
18206 incl(&pos);
18207 }
18208 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018209
Bram Moolenaar92de73d2008-01-22 10:59:38 +000018210 /* clear the start flag to avoid getting stuck here */
18211 options &= ~SEARCH_START;
18212
Bram Moolenaar071d4272004-06-13 20:20:40 +000018213 /* If the skip pattern matches, ignore this match. */
18214 if (*skip != NUL)
18215 {
18216 save_pos = curwin->w_cursor;
18217 curwin->w_cursor = pos;
18218 r = eval_to_bool(skip, &err, NULL, FALSE);
18219 curwin->w_cursor = save_pos;
18220 if (err)
18221 {
18222 /* Evaluating {skip} caused an error, break here. */
18223 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018224 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018225 break;
18226 }
18227 if (r)
18228 continue;
18229 }
18230
18231 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
18232 {
18233 /* Found end when searching backwards or start when searching
18234 * forward: nested pair. */
18235 ++nest;
18236 pat = pat2; /* nested, don't search for middle */
18237 }
18238 else
18239 {
18240 /* Found end when searching forward or start when searching
18241 * backward: end of (nested) pair; or found middle in outer pair. */
18242 if (--nest == 1)
18243 pat = pat3; /* outer level, search for middle */
18244 }
18245
18246 if (nest == 0)
18247 {
18248 /* Found the match: return matchcount or line number. */
18249 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018250 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018251 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018252 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000018253 if (flags & SP_SETPCMARK)
18254 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018255 curwin->w_cursor = pos;
18256 if (!(flags & SP_REPEAT))
18257 break;
18258 nest = 1; /* search for next unmatched */
18259 }
18260 }
18261
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018262 if (match_pos != NULL)
18263 {
18264 /* Store the match cursor position */
18265 match_pos->lnum = curwin->w_cursor.lnum;
18266 match_pos->col = curwin->w_cursor.col + 1;
18267 }
18268
Bram Moolenaar071d4272004-06-13 20:20:40 +000018269 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018270 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018271 curwin->w_cursor = save_cursor;
18272
18273theend:
18274 vim_free(pat2);
18275 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018276 if (p_cpo == empty_option)
18277 p_cpo = save_cpo;
18278 else
18279 /* Darn, evaluating the {skip} expression changed the value. */
18280 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018281
18282 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018283}
18284
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018285/*
18286 * "searchpos()" function
18287 */
18288 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018289f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018290{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018291 pos_T match_pos;
18292 int lnum = 0;
18293 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018294 int n;
18295 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018296
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018297 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018298 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018299
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018300 n = search_cmn(argvars, &match_pos, &flags);
18301 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018302 {
18303 lnum = match_pos.lnum;
18304 col = match_pos.col;
18305 }
18306
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018307 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18308 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018309 if (flags & SP_SUBPAT)
18310 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018311}
18312
Bram Moolenaar0d660222005-01-07 21:51:51 +000018313 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018314f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018315{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018316#ifdef FEAT_CLIENTSERVER
18317 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018318 char_u *server = get_tv_string_chk(&argvars[0]);
18319 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018320
Bram Moolenaar0d660222005-01-07 21:51:51 +000018321 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018322 if (server == NULL || reply == NULL)
18323 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018324 if (check_restricted() || check_secure())
18325 return;
18326# ifdef FEAT_X11
18327 if (check_connection() == FAIL)
18328 return;
18329# endif
18330
18331 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018332 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018333 EMSG(_("E258: Unable to send to client"));
18334 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018335 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018336 rettv->vval.v_number = 0;
18337#else
18338 rettv->vval.v_number = -1;
18339#endif
18340}
18341
Bram Moolenaar0d660222005-01-07 21:51:51 +000018342 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018343f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018344{
18345 char_u *r = NULL;
18346
18347#ifdef FEAT_CLIENTSERVER
18348# ifdef WIN32
18349 r = serverGetVimNames();
18350# else
18351 make_connection();
18352 if (X_DISPLAY != NULL)
18353 r = serverGetVimNames(X_DISPLAY);
18354# endif
18355#endif
18356 rettv->v_type = VAR_STRING;
18357 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018358}
18359
18360/*
18361 * "setbufvar()" function
18362 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018363 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018364f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018365{
18366 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018367 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018368 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018369 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018370 char_u nbuf[NUMBUFLEN];
18371
18372 if (check_restricted() || check_secure())
18373 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018374 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18375 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018376 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018377 varp = &argvars[2];
18378
18379 if (buf != NULL && varname != NULL && varp != NULL)
18380 {
18381 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018382 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018383
18384 if (*varname == '&')
18385 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018386 long numval;
18387 char_u *strval;
18388 int error = FALSE;
18389
Bram Moolenaar071d4272004-06-13 20:20:40 +000018390 ++varname;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018391 numval = (long)get_tv_number_chk(varp, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018392 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018393 if (!error && strval != NULL)
18394 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018395 }
18396 else
18397 {
18398 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18399 if (bufvarname != NULL)
18400 {
18401 STRCPY(bufvarname, "b:");
18402 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018403 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018404 vim_free(bufvarname);
18405 }
18406 }
18407
18408 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018409 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018411}
18412
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018413 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018414f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018415{
18416 dict_T *d;
18417 dictitem_T *di;
18418 char_u *csearch;
18419
18420 if (argvars[0].v_type != VAR_DICT)
18421 {
18422 EMSG(_(e_dictreq));
18423 return;
18424 }
18425
18426 if ((d = argvars[0].vval.v_dict) != NULL)
18427 {
18428 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18429 if (csearch != NULL)
18430 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018431#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018432 if (enc_utf8)
18433 {
18434 int pcc[MAX_MCO];
18435 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018436
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018437 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18438 }
18439 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018440#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018441 set_last_csearch(PTR2CHAR(csearch),
18442 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018443 }
18444
18445 di = dict_find(d, (char_u *)"forward", -1);
18446 if (di != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018447 set_csearch_direction((int)get_tv_number(&di->di_tv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018448 ? FORWARD : BACKWARD);
18449
18450 di = dict_find(d, (char_u *)"until", -1);
18451 if (di != NULL)
18452 set_csearch_until(!!get_tv_number(&di->di_tv));
18453 }
18454}
18455
Bram Moolenaar071d4272004-06-13 20:20:40 +000018456/*
18457 * "setcmdpos()" function
18458 */
18459 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018460f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018461{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018462 int pos = (int)get_tv_number(&argvars[0]) - 1;
18463
18464 if (pos >= 0)
18465 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018466}
18467
18468/*
Bram Moolenaar80492532016-03-08 17:08:53 +010018469 * "setfperm({fname}, {mode})" function
18470 */
18471 static void
18472f_setfperm(typval_T *argvars, typval_T *rettv)
18473{
18474 char_u *fname;
18475 char_u modebuf[NUMBUFLEN];
18476 char_u *mode_str;
18477 int i;
18478 int mask;
18479 int mode = 0;
18480
18481 rettv->vval.v_number = 0;
18482 fname = get_tv_string_chk(&argvars[0]);
18483 if (fname == NULL)
18484 return;
18485 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
18486 if (mode_str == NULL)
18487 return;
18488 if (STRLEN(mode_str) != 9)
18489 {
18490 EMSG2(_(e_invarg2), mode_str);
18491 return;
18492 }
18493
18494 mask = 1;
18495 for (i = 8; i >= 0; --i)
18496 {
18497 if (mode_str[i] != '-')
18498 mode |= mask;
18499 mask = mask << 1;
18500 }
18501 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
18502}
18503
18504/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018505 * "setline()" function
18506 */
18507 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018508f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018509{
18510 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018511 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018512 list_T *l = NULL;
18513 listitem_T *li = NULL;
18514 long added = 0;
18515 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018516
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018517 lnum = get_tv_lnum(&argvars[0]);
18518 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018519 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018520 l = argvars[1].vval.v_list;
18521 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018522 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018523 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018524 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018525
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018526 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018527 for (;;)
18528 {
18529 if (l != NULL)
18530 {
18531 /* list argument, get next string */
18532 if (li == NULL)
18533 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018534 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018535 li = li->li_next;
18536 }
18537
18538 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018539 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018540 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018541
18542 /* When coming here from Insert mode, sync undo, so that this can be
18543 * undone separately from what was previously inserted. */
18544 if (u_sync_once == 2)
18545 {
18546 u_sync_once = 1; /* notify that u_sync() was called */
18547 u_sync(TRUE);
18548 }
18549
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018550 if (lnum <= curbuf->b_ml.ml_line_count)
18551 {
18552 /* existing line, replace it */
18553 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18554 {
18555 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018556 if (lnum == curwin->w_cursor.lnum)
18557 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018558 rettv->vval.v_number = 0; /* OK */
18559 }
18560 }
18561 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18562 {
18563 /* lnum is one past the last line, append the line */
18564 ++added;
18565 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18566 rettv->vval.v_number = 0; /* OK */
18567 }
18568
18569 if (l == NULL) /* only one string argument */
18570 break;
18571 ++lnum;
18572 }
18573
18574 if (added > 0)
18575 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018576}
18577
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018578static 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 +000018579
Bram Moolenaar071d4272004-06-13 20:20:40 +000018580/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018581 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018582 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018583 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018584set_qf_ll_list(
18585 win_T *wp UNUSED,
18586 typval_T *list_arg UNUSED,
18587 typval_T *action_arg UNUSED,
18588 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018589{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018590#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018591 static char *e_invact = N_("E927: Invalid action: '%s'");
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018592 char_u *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018593 int action = 0;
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018594#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018595
Bram Moolenaar2641f772005-03-25 21:58:17 +000018596 rettv->vval.v_number = -1;
18597
18598#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018599 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018600 EMSG(_(e_listreq));
18601 else
18602 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018603 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018604
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018605 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018606 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018607 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018608 if (act == NULL)
18609 return; /* type error; errmsg already given */
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018610 if ((*act == 'a' || *act == 'r' || *act == ' ') && act[1] == NUL)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018611 action = *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018612 else
18613 EMSG2(_(e_invact), act);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018614 }
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018615 else if (action_arg->v_type == VAR_UNKNOWN)
18616 action = ' ';
18617 else
18618 EMSG(_(e_stringreq));
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018619
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018620 if (l != NULL && action && set_errorlist(wp, l, action,
Bram Moolenaar81484f42012-12-05 15:16:47 +010018621 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018622 rettv->vval.v_number = 0;
18623 }
18624#endif
18625}
18626
18627/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018628 * "setloclist()" function
18629 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018630 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018631f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018632{
18633 win_T *win;
18634
18635 rettv->vval.v_number = -1;
18636
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018637 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018638 if (win != NULL)
18639 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18640}
18641
18642/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018643 * "setmatches()" function
18644 */
18645 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018646f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018647{
18648#ifdef FEAT_SEARCH_EXTRA
18649 list_T *l;
18650 listitem_T *li;
18651 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018652 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018653
18654 rettv->vval.v_number = -1;
18655 if (argvars[0].v_type != VAR_LIST)
18656 {
18657 EMSG(_(e_listreq));
18658 return;
18659 }
18660 if ((l = argvars[0].vval.v_list) != NULL)
18661 {
18662
18663 /* To some extent make sure that we are dealing with a list from
18664 * "getmatches()". */
18665 li = l->lv_first;
18666 while (li != NULL)
18667 {
18668 if (li->li_tv.v_type != VAR_DICT
18669 || (d = li->li_tv.vval.v_dict) == NULL)
18670 {
18671 EMSG(_(e_invarg));
18672 return;
18673 }
18674 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018675 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18676 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018677 && dict_find(d, (char_u *)"priority", -1) != NULL
18678 && dict_find(d, (char_u *)"id", -1) != NULL))
18679 {
18680 EMSG(_(e_invarg));
18681 return;
18682 }
18683 li = li->li_next;
18684 }
18685
18686 clear_matches(curwin);
18687 li = l->lv_first;
18688 while (li != NULL)
18689 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018690 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018691 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018692 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018693 char_u *group;
18694 int priority;
18695 int id;
18696 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018697
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018698 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018699 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18700 {
18701 if (s == NULL)
18702 {
18703 s = list_alloc();
18704 if (s == NULL)
18705 return;
18706 }
18707
18708 /* match from matchaddpos() */
18709 for (i = 1; i < 9; i++)
18710 {
18711 sprintf((char *)buf, (char *)"pos%d", i);
18712 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18713 {
18714 if (di->di_tv.v_type != VAR_LIST)
18715 return;
18716
18717 list_append_tv(s, &di->di_tv);
18718 s->lv_refcount++;
18719 }
18720 else
18721 break;
18722 }
18723 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018724
18725 group = get_dict_string(d, (char_u *)"group", FALSE);
18726 priority = (int)get_dict_number(d, (char_u *)"priority");
18727 id = (int)get_dict_number(d, (char_u *)"id");
18728 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18729 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18730 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018731 if (i == 0)
18732 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018733 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018734 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018735 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018736 }
18737 else
18738 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018739 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018740 list_unref(s);
18741 s = NULL;
18742 }
18743
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018744 li = li->li_next;
18745 }
18746 rettv->vval.v_number = 0;
18747 }
18748#endif
18749}
18750
18751/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018752 * "setpos()" function
18753 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018754 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018755f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018756{
18757 pos_T pos;
18758 int fnum;
18759 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018760 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018761
Bram Moolenaar08250432008-02-13 11:42:46 +000018762 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018763 name = get_tv_string_chk(argvars);
18764 if (name != NULL)
18765 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018766 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018767 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018768 if (--pos.col < 0)
18769 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018770 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018771 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018772 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018773 if (fnum == curbuf->b_fnum)
18774 {
18775 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018776 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018777 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018778 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018779 curwin->w_set_curswant = FALSE;
18780 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018781 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018782 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018783 }
18784 else
18785 EMSG(_(e_invarg));
18786 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018787 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18788 {
18789 /* set mark */
18790 if (setmark_pos(name[1], &pos, fnum) == OK)
18791 rettv->vval.v_number = 0;
18792 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018793 else
18794 EMSG(_(e_invarg));
18795 }
18796 }
18797}
18798
18799/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018800 * "setqflist()" function
18801 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018803f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018804{
18805 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18806}
18807
18808/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018809 * "setreg()" function
18810 */
18811 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018812f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018813{
18814 int regname;
18815 char_u *strregname;
18816 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018817 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018818 int append;
18819 char_u yank_type;
18820 long block_len;
18821
18822 block_len = -1;
18823 yank_type = MAUTO;
18824 append = FALSE;
18825
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018826 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018827 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018828
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018829 if (strregname == NULL)
18830 return; /* type error; errmsg already given */
18831 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018832 if (regname == 0 || regname == '@')
18833 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018834
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018835 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018836 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018837 stropt = get_tv_string_chk(&argvars[2]);
18838 if (stropt == NULL)
18839 return; /* type error */
18840 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018841 switch (*stropt)
18842 {
18843 case 'a': case 'A': /* append */
18844 append = TRUE;
18845 break;
18846 case 'v': case 'c': /* character-wise selection */
18847 yank_type = MCHAR;
18848 break;
18849 case 'V': case 'l': /* line-wise selection */
18850 yank_type = MLINE;
18851 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018852 case 'b': case Ctrl_V: /* block-wise selection */
18853 yank_type = MBLOCK;
18854 if (VIM_ISDIGIT(stropt[1]))
18855 {
18856 ++stropt;
18857 block_len = getdigits(&stropt) - 1;
18858 --stropt;
18859 }
18860 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018861 }
18862 }
18863
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018864 if (argvars[1].v_type == VAR_LIST)
18865 {
18866 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018867 char_u **allocval;
18868 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018869 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018870 char_u **curallocval;
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018871 list_T *ll = argvars[1].vval.v_list;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018872 listitem_T *li;
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018873 int len;
18874
18875 /* If the list is NULL handle like an empty list. */
18876 len = ll == NULL ? 0 : ll->lv_len;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018877
Bram Moolenaar7d647822014-04-05 21:28:56 +020018878 /* First half: use for pointers to result lines; second half: use for
18879 * pointers to allocated copies. */
18880 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018881 if (lstval == NULL)
18882 return;
18883 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018884 allocval = lstval + len + 2;
18885 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018886
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018887 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018888 li = li->li_next)
18889 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018890 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018891 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018892 goto free_lstval;
18893 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018894 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018895 /* Need to make a copy, next get_tv_string_buf_chk() will
18896 * overwrite the string. */
18897 strval = vim_strsave(buf);
18898 if (strval == NULL)
18899 goto free_lstval;
18900 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018901 }
18902 *curval++ = strval;
18903 }
18904 *curval++ = NULL;
18905
18906 write_reg_contents_lst(regname, lstval, -1,
18907 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018908free_lstval:
18909 while (curallocval > allocval)
18910 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018911 vim_free(lstval);
18912 }
18913 else
18914 {
18915 strval = get_tv_string_chk(&argvars[1]);
18916 if (strval == NULL)
18917 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018918 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018919 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018920 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018921 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018922}
18923
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018924/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018925 * "settabvar()" function
18926 */
18927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018928f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018929{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018930#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018931 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018932 tabpage_T *tp;
18933#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018934 char_u *varname, *tabvarname;
18935 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018936
18937 rettv->vval.v_number = 0;
18938
18939 if (check_restricted() || check_secure())
18940 return;
18941
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018942#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018943 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018944#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018945 varname = get_tv_string_chk(&argvars[1]);
18946 varp = &argvars[2];
18947
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018948 if (varname != NULL && varp != NULL
18949#ifdef FEAT_WINDOWS
18950 && tp != NULL
18951#endif
18952 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018953 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018954#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018955 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018956 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018957#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018958
18959 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18960 if (tabvarname != NULL)
18961 {
18962 STRCPY(tabvarname, "t:");
18963 STRCPY(tabvarname + 2, varname);
18964 set_var(tabvarname, varp, TRUE);
18965 vim_free(tabvarname);
18966 }
18967
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018968#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018969 /* Restore current tabpage */
18970 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018971 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018972#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018973 }
18974}
18975
18976/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018977 * "settabwinvar()" function
18978 */
18979 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018980f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018981{
18982 setwinvar(argvars, rettv, 1);
18983}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018984
18985/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018986 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018987 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018988 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018989f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018990{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018991 setwinvar(argvars, rettv, 0);
18992}
18993
18994/*
18995 * "setwinvar()" and "settabwinvar()" functions
18996 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018997
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018998 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018999setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019000{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019001 win_T *win;
19002#ifdef FEAT_WINDOWS
19003 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019004 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020019005 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019006#endif
19007 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019008 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019009 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020019010 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019011
19012 if (check_restricted() || check_secure())
19013 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019014
19015#ifdef FEAT_WINDOWS
19016 if (off == 1)
19017 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
19018 else
19019 tp = curtab;
19020#endif
19021 win = find_win_by_nr(&argvars[off], tp);
19022 varname = get_tv_string_chk(&argvars[off + 1]);
19023 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019024
19025 if (win != NULL && varname != NULL && varp != NULL)
19026 {
19027#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020019028 need_switch_win = !(tp == curtab && win == curwin);
19029 if (!need_switch_win
19030 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019032 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020019033 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019034 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020019035 long numval;
19036 char_u *strval;
19037 int error = FALSE;
19038
19039 ++varname;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019040 numval = (long)get_tv_number_chk(varp, &error);
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020019041 strval = get_tv_string_buf_chk(varp, nbuf);
19042 if (!error && strval != NULL)
19043 set_option_value(varname, numval, strval, OPT_LOCAL);
19044 }
19045 else
19046 {
19047 winvarname = alloc((unsigned)STRLEN(varname) + 3);
19048 if (winvarname != NULL)
19049 {
19050 STRCPY(winvarname, "w:");
19051 STRCPY(winvarname + 2, varname);
19052 set_var(winvarname, varp, TRUE);
19053 vim_free(winvarname);
19054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019055 }
19056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019057#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020019058 if (need_switch_win)
19059 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019060#endif
19061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019062}
19063
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010019064#ifdef FEAT_CRYPT
19065/*
19066 * "sha256({string})" function
19067 */
19068 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019069f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010019070{
19071 char_u *p;
19072
19073 p = get_tv_string(&argvars[0]);
19074 rettv->vval.v_string = vim_strsave(
19075 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
19076 rettv->v_type = VAR_STRING;
19077}
19078#endif /* FEAT_CRYPT */
19079
Bram Moolenaar071d4272004-06-13 20:20:40 +000019080/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000019081 * "shellescape({string})" function
19082 */
19083 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019084f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000019085{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019086 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010019087 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000019088 rettv->v_type = VAR_STRING;
19089}
19090
19091/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020019092 * shiftwidth() function
19093 */
19094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019095f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020019096{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010019097 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020019098}
19099
19100/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019101 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019102 */
19103 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019104f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019105{
Bram Moolenaar0d660222005-01-07 21:51:51 +000019106 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019107
Bram Moolenaar0d660222005-01-07 21:51:51 +000019108 p = get_tv_string(&argvars[0]);
19109 rettv->vval.v_string = vim_strsave(p);
19110 simplify_filename(rettv->vval.v_string); /* simplify in place */
19111 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019112}
19113
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019114#ifdef FEAT_FLOAT
19115/*
19116 * "sin()" function
19117 */
19118 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019119f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019120{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019121 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019122
19123 rettv->v_type = VAR_FLOAT;
19124 if (get_float_arg(argvars, &f) == OK)
19125 rettv->vval.v_float = sin(f);
19126 else
19127 rettv->vval.v_float = 0.0;
19128}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019129
19130/*
19131 * "sinh()" function
19132 */
19133 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019134f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019135{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019136 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019137
19138 rettv->v_type = VAR_FLOAT;
19139 if (get_float_arg(argvars, &f) == OK)
19140 rettv->vval.v_float = sinh(f);
19141 else
19142 rettv->vval.v_float = 0.0;
19143}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019144#endif
19145
Bram Moolenaar0d660222005-01-07 21:51:51 +000019146static int
19147#ifdef __BORLANDC__
19148 _RTLENTRYF
19149#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019150 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019151static int
19152#ifdef __BORLANDC__
19153 _RTLENTRYF
19154#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019155 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019156
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019157/* struct used in the array that's given to qsort() */
19158typedef struct
19159{
19160 listitem_T *item;
19161 int idx;
19162} sortItem_T;
19163
Bram Moolenaar0b962472016-02-22 22:51:33 +010019164/* struct storing information about current sort */
19165typedef struct
19166{
19167 int item_compare_ic;
19168 int item_compare_numeric;
19169 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019170#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019171 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019172#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019173 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019174 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019175 dict_T *item_compare_selfdict;
19176 int item_compare_func_err;
19177 int item_compare_keep_zero;
19178} sortinfo_T;
19179static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019180static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019181#define ITEM_COMPARE_FAIL 999
19182
Bram Moolenaar071d4272004-06-13 20:20:40 +000019183/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019184 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019185 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019186 static int
19187#ifdef __BORLANDC__
19188_RTLENTRYF
19189#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019190item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019191{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019192 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019193 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019194 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019195 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019196 int res;
19197 char_u numbuf1[NUMBUFLEN];
19198 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019199
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019200 si1 = (sortItem_T *)s1;
19201 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019202 tv1 = &si1->item->li_tv;
19203 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019204
Bram Moolenaar0b962472016-02-22 22:51:33 +010019205 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019206 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019207 varnumber_T v1 = get_tv_number(tv1);
19208 varnumber_T v2 = get_tv_number(tv2);
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019209
19210 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19211 }
19212
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019213#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019214 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019215 {
19216 float_T v1 = get_tv_float(tv1);
19217 float_T v2 = get_tv_float(tv2);
19218
19219 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19220 }
19221#endif
19222
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019223 /* tv2string() puts quotes around a string and allocates memory. Don't do
19224 * that for string variables. Use a single quote when comparing with a
19225 * non-string to do what the docs promise. */
19226 if (tv1->v_type == VAR_STRING)
19227 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019228 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019229 p1 = (char_u *)"'";
19230 else
19231 p1 = tv1->vval.v_string;
19232 }
19233 else
19234 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
19235 if (tv2->v_type == VAR_STRING)
19236 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019237 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019238 p2 = (char_u *)"'";
19239 else
19240 p2 = tv2->vval.v_string;
19241 }
19242 else
19243 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019244 if (p1 == NULL)
19245 p1 = (char_u *)"";
19246 if (p2 == NULL)
19247 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010019248 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019249 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019250 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019251 res = STRICMP(p1, p2);
19252 else
19253 res = STRCMP(p1, p2);
19254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019255 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020019256 {
19257 double n1, n2;
19258 n1 = strtod((char *)p1, (char **)&p1);
19259 n2 = strtod((char *)p2, (char **)&p2);
19260 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
19261 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019262
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019263 /* When the result would be zero, compare the item indexes. Makes the
19264 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019265 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019266 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019267
Bram Moolenaar0d660222005-01-07 21:51:51 +000019268 vim_free(tofree1);
19269 vim_free(tofree2);
19270 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019271}
19272
19273 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000019274#ifdef __BORLANDC__
19275_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000019276#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019277item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019278{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019279 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019280 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000019281 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019282 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000019283 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019284 char_u *func_name;
19285 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019286
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019287 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019288 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019289 return 0;
19290
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019291 si1 = (sortItem_T *)s1;
19292 si2 = (sortItem_T *)s2;
19293
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019294 if (partial == NULL)
19295 func_name = sortinfo->item_compare_func;
19296 else
19297 func_name = partial->pt_name;
19298
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019299 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019300 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019301 copy_tv(&si1->item->li_tv, &argv[0]);
19302 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019303
19304 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019305 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020019306 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019307 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019308 clear_tv(&argv[0]);
19309 clear_tv(&argv[1]);
19310
19311 if (res == FAIL)
19312 res = ITEM_COMPARE_FAIL;
19313 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019314 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar0b962472016-02-22 22:51:33 +010019315 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000019316 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019317 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019318
19319 /* When the result would be zero, compare the pointers themselves. Makes
19320 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019321 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019322 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019323
Bram Moolenaar0d660222005-01-07 21:51:51 +000019324 return res;
19325}
19326
19327/*
19328 * "sort({list})" function
19329 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019330 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019331do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019332{
Bram Moolenaar33570922005-01-25 22:26:29 +000019333 list_T *l;
19334 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019335 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019336 sortinfo_T *old_sortinfo;
19337 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019338 long len;
19339 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019340
Bram Moolenaar0b962472016-02-22 22:51:33 +010019341 /* Pointer to current info struct used in compare function. Save and
19342 * restore the current one for nested calls. */
19343 old_sortinfo = sortinfo;
19344 sortinfo = &info;
19345
Bram Moolenaar0d660222005-01-07 21:51:51 +000019346 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019347 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019348 else
19349 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019350 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019351 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019352 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19353 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019354 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019355 rettv->vval.v_list = l;
19356 rettv->v_type = VAR_LIST;
19357 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019358
Bram Moolenaar0d660222005-01-07 21:51:51 +000019359 len = list_len(l);
19360 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019361 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019362
Bram Moolenaar0b962472016-02-22 22:51:33 +010019363 info.item_compare_ic = FALSE;
19364 info.item_compare_numeric = FALSE;
19365 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019366#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019367 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019368#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019369 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019370 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019371 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019372 if (argvars[1].v_type != VAR_UNKNOWN)
19373 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019374 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019375 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019376 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019377 else if (argvars[1].v_type == VAR_PARTIAL)
19378 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019379 else
19380 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019381 int error = FALSE;
19382
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019383 i = (long)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019384 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019385 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019386 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019387 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010019388 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019389 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010019390 else if (i != 0)
19391 {
19392 EMSG(_(e_invarg));
19393 goto theend;
19394 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019395 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019396 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010019397 if (*info.item_compare_func == NUL)
19398 {
19399 /* empty string means default sort */
19400 info.item_compare_func = NULL;
19401 }
19402 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019403 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019404 info.item_compare_func = NULL;
19405 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019406 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019407 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019408 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019409 info.item_compare_func = NULL;
19410 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019411 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019412#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019413 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019414 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019415 info.item_compare_func = NULL;
19416 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019417 }
19418#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019419 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019420 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019421 info.item_compare_func = NULL;
19422 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019423 }
19424 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019425 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019426
19427 if (argvars[2].v_type != VAR_UNKNOWN)
19428 {
19429 /* optional third argument: {dict} */
19430 if (argvars[2].v_type != VAR_DICT)
19431 {
19432 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019433 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019434 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019435 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019436 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019438
Bram Moolenaar0d660222005-01-07 21:51:51 +000019439 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019440 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019441 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019442 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019443
Bram Moolenaar327aa022014-03-25 18:24:23 +010019444 i = 0;
19445 if (sort)
19446 {
19447 /* sort(): ptrs will be the list to sort */
19448 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019449 {
19450 ptrs[i].item = li;
19451 ptrs[i].idx = i;
19452 ++i;
19453 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019454
Bram Moolenaar0b962472016-02-22 22:51:33 +010019455 info.item_compare_func_err = FALSE;
19456 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019457 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019458 if ((info.item_compare_func != NULL
19459 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019460 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019461 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019462 EMSG(_("E702: Sort compare function failed"));
19463 else
19464 {
19465 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019466 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019467 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019468 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010019469 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019470
Bram Moolenaar0b962472016-02-22 22:51:33 +010019471 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019472 {
19473 /* Clear the List and append the items in sorted order. */
19474 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19475 l->lv_len = 0;
19476 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019477 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019478 }
19479 }
19480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019481 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019482 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019483 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019484
19485 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019486 info.item_compare_func_err = FALSE;
19487 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019488 item_compare_func_ptr = info.item_compare_func != NULL
19489 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019490 ? item_compare2 : item_compare;
19491
19492 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19493 li = li->li_next)
19494 {
19495 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19496 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019497 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019498 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019499 {
19500 EMSG(_("E882: Uniq compare function failed"));
19501 break;
19502 }
19503 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019504
Bram Moolenaar0b962472016-02-22 22:51:33 +010019505 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019506 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019507 while (--i >= 0)
19508 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019509 li = ptrs[i].item->li_next;
19510 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019511 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019512 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019513 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019514 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019515 list_fix_watch(l, li);
19516 listitem_free(li);
19517 l->lv_len--;
19518 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019519 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019520 }
19521
19522 vim_free(ptrs);
19523 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019524theend:
19525 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019526}
19527
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019528/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019529 * "sort({list})" function
19530 */
19531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019532f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019533{
19534 do_sort_uniq(argvars, rettv, TRUE);
19535}
19536
19537/*
19538 * "uniq({list})" function
19539 */
19540 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019541f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019542{
19543 do_sort_uniq(argvars, rettv, FALSE);
19544}
19545
19546/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019547 * "soundfold({word})" function
19548 */
19549 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019550f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019551{
19552 char_u *s;
19553
19554 rettv->v_type = VAR_STRING;
19555 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019556#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019557 rettv->vval.v_string = eval_soundfold(s);
19558#else
19559 rettv->vval.v_string = vim_strsave(s);
19560#endif
19561}
19562
19563/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019564 * "spellbadword()" function
19565 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019566 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019567f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019568{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019569 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019570 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019571 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019572
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019573 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019574 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019575
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019576#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019577 if (argvars[0].v_type == VAR_UNKNOWN)
19578 {
19579 /* Find the start and length of the badly spelled word. */
19580 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19581 if (len != 0)
19582 word = ml_get_cursor();
19583 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019584 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019585 {
19586 char_u *str = get_tv_string_chk(&argvars[0]);
19587 int capcol = -1;
19588
19589 if (str != NULL)
19590 {
19591 /* Check the argument for spelling. */
19592 while (*str != NUL)
19593 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019594 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019595 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019596 {
19597 word = str;
19598 break;
19599 }
19600 str += len;
19601 }
19602 }
19603 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019604#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019605
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019606 list_append_string(rettv->vval.v_list, word, len);
19607 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019608 attr == HLF_SPB ? "bad" :
19609 attr == HLF_SPR ? "rare" :
19610 attr == HLF_SPL ? "local" :
19611 attr == HLF_SPC ? "caps" :
19612 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019613}
19614
19615/*
19616 * "spellsuggest()" function
19617 */
19618 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019619f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019620{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019621#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019622 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019623 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019624 int maxcount;
19625 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019626 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019627 listitem_T *li;
19628 int need_capital = FALSE;
19629#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019630
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019631 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019632 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019633
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019634#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019635 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019636 {
19637 str = get_tv_string(&argvars[0]);
19638 if (argvars[1].v_type != VAR_UNKNOWN)
19639 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019640 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019641 if (maxcount <= 0)
19642 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019643 if (argvars[2].v_type != VAR_UNKNOWN)
19644 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019645 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019646 if (typeerr)
19647 return;
19648 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019649 }
19650 else
19651 maxcount = 25;
19652
Bram Moolenaar4770d092006-01-12 23:22:24 +000019653 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019654
19655 for (i = 0; i < ga.ga_len; ++i)
19656 {
19657 str = ((char_u **)ga.ga_data)[i];
19658
19659 li = listitem_alloc();
19660 if (li == NULL)
19661 vim_free(str);
19662 else
19663 {
19664 li->li_tv.v_type = VAR_STRING;
19665 li->li_tv.v_lock = 0;
19666 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019667 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019668 }
19669 }
19670 ga_clear(&ga);
19671 }
19672#endif
19673}
19674
Bram Moolenaar0d660222005-01-07 21:51:51 +000019675 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019676f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019677{
19678 char_u *str;
19679 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019680 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019681 regmatch_T regmatch;
19682 char_u patbuf[NUMBUFLEN];
19683 char_u *save_cpo;
19684 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019685 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019686 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019687 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019688
19689 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19690 save_cpo = p_cpo;
19691 p_cpo = (char_u *)"";
19692
19693 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019694 if (argvars[1].v_type != VAR_UNKNOWN)
19695 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019696 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19697 if (pat == NULL)
19698 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019699 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019700 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019701 }
19702 if (pat == NULL || *pat == NUL)
19703 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019704
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019705 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019706 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019707 if (typeerr)
19708 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019709
Bram Moolenaar0d660222005-01-07 21:51:51 +000019710 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19711 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019712 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019713 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019714 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019715 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019716 if (*str == NUL)
19717 match = FALSE; /* empty item at the end */
19718 else
19719 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019720 if (match)
19721 end = regmatch.startp[0];
19722 else
19723 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019724 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19725 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019726 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019727 if (list_append_string(rettv->vval.v_list, str,
19728 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019729 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019730 }
19731 if (!match)
19732 break;
19733 /* Advance to just after the match. */
19734 if (regmatch.endp[0] > str)
19735 col = 0;
19736 else
19737 {
19738 /* Don't get stuck at the same match. */
19739#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019740 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019741#else
19742 col = 1;
19743#endif
19744 }
19745 str = regmatch.endp[0];
19746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019747
Bram Moolenaar473de612013-06-08 18:19:48 +020019748 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019750
Bram Moolenaar0d660222005-01-07 21:51:51 +000019751 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019752}
19753
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019754#ifdef FEAT_FLOAT
19755/*
19756 * "sqrt()" function
19757 */
19758 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019759f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019760{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019761 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019762
19763 rettv->v_type = VAR_FLOAT;
19764 if (get_float_arg(argvars, &f) == OK)
19765 rettv->vval.v_float = sqrt(f);
19766 else
19767 rettv->vval.v_float = 0.0;
19768}
19769
19770/*
19771 * "str2float()" function
19772 */
19773 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019774f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019775{
19776 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19777
19778 if (*p == '+')
19779 p = skipwhite(p + 1);
19780 (void)string2float(p, &rettv->vval.v_float);
19781 rettv->v_type = VAR_FLOAT;
19782}
19783#endif
19784
Bram Moolenaar2c932302006-03-18 21:42:09 +000019785/*
19786 * "str2nr()" function
19787 */
19788 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019789f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019790{
19791 int base = 10;
19792 char_u *p;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019793 varnumber_T n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019794 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019795
19796 if (argvars[1].v_type != VAR_UNKNOWN)
19797 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019798 base = (int)get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019799 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019800 {
19801 EMSG(_(e_invarg));
19802 return;
19803 }
19804 }
19805
19806 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019807 if (*p == '+')
19808 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019809 switch (base)
19810 {
19811 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19812 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19813 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19814 default: what = 0;
19815 }
19816 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019817 rettv->vval.v_number = n;
19818}
19819
Bram Moolenaar071d4272004-06-13 20:20:40 +000019820#ifdef HAVE_STRFTIME
19821/*
19822 * "strftime({format}[, {time}])" function
19823 */
19824 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019825f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019826{
19827 char_u result_buf[256];
19828 struct tm *curtime;
19829 time_t seconds;
19830 char_u *p;
19831
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019832 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019833
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019834 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019835 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019836 seconds = time(NULL);
19837 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019838 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019839 curtime = localtime(&seconds);
19840 /* MSVC returns NULL for an invalid value of seconds. */
19841 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019842 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019843 else
19844 {
19845# ifdef FEAT_MBYTE
19846 vimconv_T conv;
19847 char_u *enc;
19848
19849 conv.vc_type = CONV_NONE;
19850 enc = enc_locale();
19851 convert_setup(&conv, p_enc, enc);
19852 if (conv.vc_type != CONV_NONE)
19853 p = string_convert(&conv, p, NULL);
19854# endif
19855 if (p != NULL)
19856 (void)strftime((char *)result_buf, sizeof(result_buf),
19857 (char *)p, curtime);
19858 else
19859 result_buf[0] = NUL;
19860
19861# ifdef FEAT_MBYTE
19862 if (conv.vc_type != CONV_NONE)
19863 vim_free(p);
19864 convert_setup(&conv, enc, p_enc);
19865 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019866 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019867 else
19868# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019869 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019870
19871# ifdef FEAT_MBYTE
19872 /* Release conversion descriptors */
19873 convert_setup(&conv, NULL, NULL);
19874 vim_free(enc);
19875# endif
19876 }
19877}
19878#endif
19879
19880/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019881 * "strgetchar()" function
19882 */
19883 static void
19884f_strgetchar(typval_T *argvars, typval_T *rettv)
19885{
19886 char_u *str;
19887 int len;
19888 int error = FALSE;
19889 int charidx;
19890
19891 rettv->vval.v_number = -1;
19892 str = get_tv_string_chk(&argvars[0]);
19893 if (str == NULL)
19894 return;
19895 len = (int)STRLEN(str);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019896 charidx = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019897 if (error)
19898 return;
19899#ifdef FEAT_MBYTE
19900 {
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019901 int byteidx = 0;
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019902
19903 while (charidx >= 0 && byteidx < len)
19904 {
19905 if (charidx == 0)
19906 {
19907 rettv->vval.v_number = mb_ptr2char(str + byteidx);
19908 break;
19909 }
19910 --charidx;
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019911 byteidx += mb_cptr2len(str + byteidx);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019912 }
19913 }
19914#else
19915 if (charidx < len)
19916 rettv->vval.v_number = str[charidx];
19917#endif
19918}
19919
19920/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019921 * "stridx()" function
19922 */
19923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019924f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019925{
19926 char_u buf[NUMBUFLEN];
19927 char_u *needle;
19928 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019929 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019930 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019931 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019932
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019933 needle = get_tv_string_chk(&argvars[1]);
19934 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019935 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019936 if (needle == NULL || haystack == NULL)
19937 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019938
Bram Moolenaar33570922005-01-25 22:26:29 +000019939 if (argvars[2].v_type != VAR_UNKNOWN)
19940 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019941 int error = FALSE;
19942
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019943 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019944 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019945 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019946 if (start_idx >= 0)
19947 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019948 }
19949
19950 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19951 if (pos != NULL)
19952 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019953}
19954
19955/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019956 * "string()" function
19957 */
19958 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019959f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019960{
19961 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019962 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019963
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019964 rettv->v_type = VAR_STRING;
Bram Moolenaar24c77a12016-03-24 21:23:06 +010019965 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
19966 get_copyID());
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019967 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019968 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019969 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019970}
19971
19972/*
19973 * "strlen()" function
19974 */
19975 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019976f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019977{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019978 rettv->vval.v_number = (varnumber_T)(STRLEN(
19979 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019980}
19981
19982/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019983 * "strchars()" function
19984 */
19985 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019986f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019987{
19988 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019989 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019990#ifdef FEAT_MBYTE
19991 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019992 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019993#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019994
19995 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019996 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019997 if (skipcc < 0 || skipcc > 1)
19998 EMSG(_(e_invarg));
19999 else
20000 {
20001#ifdef FEAT_MBYTE
20002 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
20003 while (*s != NUL)
20004 {
20005 func_mb_ptr2char_adv(&s);
20006 ++len;
20007 }
20008 rettv->vval.v_number = len;
20009#else
20010 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
20011#endif
20012 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020020013}
20014
20015/*
Bram Moolenaardc536092010-07-18 15:45:49 +020020016 * "strdisplaywidth()" function
20017 */
20018 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020019f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020020020{
20021 char_u *s = get_tv_string(&argvars[0]);
20022 int col = 0;
20023
20024 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020025 col = (int)get_tv_number(&argvars[1]);
Bram Moolenaardc536092010-07-18 15:45:49 +020020026
Bram Moolenaar8a09b982010-07-22 22:20:57 +020020027 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020020028}
20029
20030/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020020031 * "strwidth()" function
20032 */
20033 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020034f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020020035{
20036 char_u *s = get_tv_string(&argvars[0]);
20037
20038 rettv->vval.v_number = (varnumber_T)(
20039#ifdef FEAT_MBYTE
20040 mb_string2cells(s, -1)
20041#else
20042 STRLEN(s)
20043#endif
20044 );
20045}
20046
20047/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020048 * "strcharpart()" function
20049 */
20050 static void
20051f_strcharpart(typval_T *argvars, typval_T *rettv)
20052{
20053#ifdef FEAT_MBYTE
20054 char_u *p;
20055 int nchar;
20056 int nbyte = 0;
20057 int charlen;
20058 int len = 0;
20059 int slen;
20060 int error = FALSE;
20061
20062 p = get_tv_string(&argvars[0]);
20063 slen = (int)STRLEN(p);
20064
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020065 nchar = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020066 if (!error)
20067 {
20068 if (nchar > 0)
20069 while (nchar > 0 && nbyte < slen)
20070 {
Bram Moolenaarfca66002016-04-23 15:30:09 +020020071 nbyte += mb_cptr2len(p + nbyte);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020072 --nchar;
20073 }
20074 else
20075 nbyte = nchar;
20076 if (argvars[2].v_type != VAR_UNKNOWN)
20077 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020078 charlen = (int)get_tv_number(&argvars[2]);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020079 while (charlen > 0 && nbyte + len < slen)
20080 {
Bram Moolenaar73dfe912016-04-23 13:54:48 +020020081 int off = nbyte + len;
20082
20083 if (off < 0)
20084 len += 1;
20085 else
Bram Moolenaarfca66002016-04-23 15:30:09 +020020086 len += mb_cptr2len(p + off);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020087 --charlen;
20088 }
20089 }
20090 else
20091 len = slen - nbyte; /* default: all bytes that are available. */
20092 }
20093
20094 /*
20095 * Only return the overlap between the specified part and the actual
20096 * string.
20097 */
20098 if (nbyte < 0)
20099 {
20100 len += nbyte;
20101 nbyte = 0;
20102 }
20103 else if (nbyte > slen)
20104 nbyte = slen;
20105 if (len < 0)
20106 len = 0;
20107 else if (nbyte + len > slen)
20108 len = slen - nbyte;
20109
20110 rettv->v_type = VAR_STRING;
20111 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
20112#else
20113 f_strpart(argvars, rettv);
20114#endif
20115}
20116
20117/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020118 * "strpart()" function
20119 */
20120 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020121f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020122{
20123 char_u *p;
20124 int n;
20125 int len;
20126 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020127 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020128
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020129 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020130 slen = (int)STRLEN(p);
20131
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020132 n = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020133 if (error)
20134 len = 0;
20135 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020136 len = (int)get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020137 else
20138 len = slen - n; /* default len: all bytes that are available. */
20139
20140 /*
20141 * Only return the overlap between the specified part and the actual
20142 * string.
20143 */
20144 if (n < 0)
20145 {
20146 len += n;
20147 n = 0;
20148 }
20149 else if (n > slen)
20150 n = slen;
20151 if (len < 0)
20152 len = 0;
20153 else if (n + len > slen)
20154 len = slen - n;
20155
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020156 rettv->v_type = VAR_STRING;
20157 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020158}
20159
20160/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000020161 * "strridx()" function
20162 */
20163 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020164f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020165{
20166 char_u buf[NUMBUFLEN];
20167 char_u *needle;
20168 char_u *haystack;
20169 char_u *rest;
20170 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020171 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000020172
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020173 needle = get_tv_string_chk(&argvars[1]);
20174 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020175
20176 rettv->vval.v_number = -1;
20177 if (needle == NULL || haystack == NULL)
20178 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020179
20180 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020181 if (argvars[2].v_type != VAR_UNKNOWN)
20182 {
20183 /* Third argument: upper limit for index */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020184 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020185 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020186 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020187 }
20188 else
20189 end_idx = haystack_len;
20190
Bram Moolenaar0d660222005-01-07 21:51:51 +000020191 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020192 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000020193 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020194 lastmatch = haystack + end_idx;
20195 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020196 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000020197 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000020198 for (rest = haystack; *rest != '\0'; ++rest)
20199 {
20200 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000020201 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020202 break;
20203 lastmatch = rest;
20204 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000020205 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020206
20207 if (lastmatch == NULL)
20208 rettv->vval.v_number = -1;
20209 else
20210 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
20211}
20212
20213/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020214 * "strtrans()" function
20215 */
20216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020217f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020218{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020219 rettv->v_type = VAR_STRING;
20220 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020221}
20222
20223/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000020224 * "submatch()" function
20225 */
20226 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020227f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020228{
Bram Moolenaar41571762014-04-02 19:00:58 +020020229 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020020230 int no;
20231 int retList = 0;
20232
20233 no = (int)get_tv_number_chk(&argvars[0], &error);
20234 if (error)
20235 return;
20236 error = FALSE;
20237 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020238 retList = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar41571762014-04-02 19:00:58 +020020239 if (error)
20240 return;
20241
20242 if (retList == 0)
20243 {
20244 rettv->v_type = VAR_STRING;
20245 rettv->vval.v_string = reg_submatch(no);
20246 }
20247 else
20248 {
20249 rettv->v_type = VAR_LIST;
20250 rettv->vval.v_list = reg_submatch_list(no);
20251 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020252}
20253
20254/*
20255 * "substitute()" function
20256 */
20257 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020258f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020259{
20260 char_u patbuf[NUMBUFLEN];
20261 char_u subbuf[NUMBUFLEN];
20262 char_u flagsbuf[NUMBUFLEN];
20263
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020264 char_u *str = get_tv_string_chk(&argvars[0]);
20265 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
20266 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
20267 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
20268
Bram Moolenaar0d660222005-01-07 21:51:51 +000020269 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020270 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
20271 rettv->vval.v_string = NULL;
20272 else
20273 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000020274}
20275
20276/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020277 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000020278 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020279 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020280f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020281{
20282 int id = 0;
20283#ifdef FEAT_SYN_HL
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020284 linenr_T lnum;
20285 colnr_T col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020286 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020287 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020288
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020289 lnum = get_tv_lnum(argvars); /* -1 on type error */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020290 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20291 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020292
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020293 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020294 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020295 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020296#endif
20297
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020298 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020299}
20300
20301/*
20302 * "synIDattr(id, what [, mode])" function
20303 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020304 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020305f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020306{
20307 char_u *p = NULL;
20308#ifdef FEAT_SYN_HL
20309 int id;
20310 char_u *what;
20311 char_u *mode;
20312 char_u modebuf[NUMBUFLEN];
20313 int modec;
20314
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020315 id = (int)get_tv_number(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020316 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020317 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020318 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020319 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020320 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020020321 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000020322 modec = 0; /* replace invalid with current */
20323 }
20324 else
20325 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +020020326#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaarda5b3dc2016-04-23 15:19:02 +020020327 if (USE_24BIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020328 modec = 'g';
20329 else
20330#endif
20331 if (t_colors > 1)
20332 modec = 'c';
20333 else
20334 modec = 't';
20335 }
20336
20337
20338 switch (TOLOWER_ASC(what[0]))
20339 {
20340 case 'b':
20341 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
20342 p = highlight_color(id, what, modec);
20343 else /* bold */
20344 p = highlight_has_attr(id, HL_BOLD, modec);
20345 break;
20346
Bram Moolenaar12682fd2010-03-10 13:43:49 +010020347 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020348 p = highlight_color(id, what, modec);
20349 break;
20350
20351 case 'i':
20352 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
20353 p = highlight_has_attr(id, HL_INVERSE, modec);
20354 else /* italic */
20355 p = highlight_has_attr(id, HL_ITALIC, modec);
20356 break;
20357
20358 case 'n': /* name */
20359 p = get_highlight_name(NULL, id - 1);
20360 break;
20361
20362 case 'r': /* reverse */
20363 p = highlight_has_attr(id, HL_INVERSE, modec);
20364 break;
20365
Bram Moolenaar6f507d62008-11-28 10:16:05 +000020366 case 's':
20367 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
20368 p = highlight_color(id, what, modec);
20369 else /* standout */
20370 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020371 break;
20372
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000020373 case 'u':
20374 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
20375 /* underline */
20376 p = highlight_has_attr(id, HL_UNDERLINE, modec);
20377 else
20378 /* undercurl */
20379 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020380 break;
20381 }
20382
20383 if (p != NULL)
20384 p = vim_strsave(p);
20385#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020386 rettv->v_type = VAR_STRING;
20387 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020388}
20389
20390/*
20391 * "synIDtrans(id)" function
20392 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020393 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020394f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020395{
20396 int id;
20397
20398#ifdef FEAT_SYN_HL
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020399 id = (int)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020400
20401 if (id > 0)
20402 id = syn_get_final_id(id);
20403 else
20404#endif
20405 id = 0;
20406
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020407 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020408}
20409
20410/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020411 * "synconcealed(lnum, col)" function
20412 */
20413 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020414f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020415{
20416#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020417 linenr_T lnum;
20418 colnr_T col;
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020419 int syntax_flags = 0;
20420 int cchar;
20421 int matchid = 0;
20422 char_u str[NUMBUFLEN];
20423#endif
20424
20425 rettv->v_type = VAR_LIST;
20426 rettv->vval.v_list = NULL;
20427
20428#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20429 lnum = get_tv_lnum(argvars); /* -1 on type error */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020430 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020431
20432 vim_memset(str, NUL, sizeof(str));
20433
20434 if (rettv_list_alloc(rettv) != FAIL)
20435 {
20436 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20437 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20438 && curwin->w_p_cole > 0)
20439 {
20440 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20441 syntax_flags = get_syntax_info(&matchid);
20442
20443 /* get the conceal character */
20444 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20445 {
20446 cchar = syn_get_sub_char();
20447 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20448 cchar = lcs_conceal;
20449 if (cchar != NUL)
20450 {
20451# ifdef FEAT_MBYTE
20452 if (has_mbyte)
20453 (*mb_char2bytes)(cchar, str);
20454 else
20455# endif
20456 str[0] = cchar;
20457 }
20458 }
20459 }
20460
20461 list_append_number(rettv->vval.v_list,
20462 (syntax_flags & HL_CONCEAL) != 0);
20463 /* -1 to auto-determine strlen */
20464 list_append_string(rettv->vval.v_list, str, -1);
20465 list_append_number(rettv->vval.v_list, matchid);
20466 }
20467#endif
20468}
20469
20470/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020471 * "synstack(lnum, col)" function
20472 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020473 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020474f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020475{
20476#ifdef FEAT_SYN_HL
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020477 linenr_T lnum;
20478 colnr_T col;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020479 int i;
20480 int id;
20481#endif
20482
20483 rettv->v_type = VAR_LIST;
20484 rettv->vval.v_list = NULL;
20485
20486#ifdef FEAT_SYN_HL
20487 lnum = get_tv_lnum(argvars); /* -1 on type error */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020488 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020489
20490 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020491 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020492 && rettv_list_alloc(rettv) != FAIL)
20493 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020494 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020495 for (i = 0; ; ++i)
20496 {
20497 id = syn_get_stack_item(i);
20498 if (id < 0)
20499 break;
20500 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20501 break;
20502 }
20503 }
20504#endif
20505}
20506
Bram Moolenaar071d4272004-06-13 20:20:40 +000020507 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020508get_cmd_output_as_rettv(
20509 typval_T *argvars,
20510 typval_T *rettv,
20511 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020512{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020513 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020514 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020515 char_u *infile = NULL;
20516 char_u buf[NUMBUFLEN];
20517 int err = FALSE;
20518 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020519 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020520 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020521
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020522 rettv->v_type = VAR_STRING;
20523 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020524 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020525 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020526
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020527 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020528 {
20529 /*
20530 * Write the string to a temp file, to be used for input of the shell
20531 * command.
20532 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020533 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020534 {
20535 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020536 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020537 }
20538
20539 fd = mch_fopen((char *)infile, WRITEBIN);
20540 if (fd == NULL)
20541 {
20542 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020543 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020544 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020545 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020546 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020547 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20548 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020549 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020550 else
20551 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020552 size_t len;
20553
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020554 p = get_tv_string_buf_chk(&argvars[1], buf);
20555 if (p == NULL)
20556 {
20557 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020558 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020559 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020560 len = STRLEN(p);
20561 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020562 err = TRUE;
20563 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020564 if (fclose(fd) != 0)
20565 err = TRUE;
20566 if (err)
20567 {
20568 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020569 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020570 }
20571 }
20572
Bram Moolenaar52a72462014-08-29 15:53:52 +020020573 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20574 * echoes typeahead, that messes up the display. */
20575 if (!msg_silent)
20576 flags += SHELL_COOKED;
20577
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020578 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020579 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020580 int len;
20581 listitem_T *li;
20582 char_u *s = NULL;
20583 char_u *start;
20584 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020585 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020586
Bram Moolenaar52a72462014-08-29 15:53:52 +020020587 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020588 if (res == NULL)
20589 goto errret;
20590
20591 list = list_alloc();
20592 if (list == NULL)
20593 goto errret;
20594
20595 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020596 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020597 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020598 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020599 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020600 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020601
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020602 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020603 if (s == NULL)
20604 goto errret;
20605
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020606 for (p = s; start < end; ++p, ++start)
20607 *p = *start == NUL ? NL : *start;
20608 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020609
20610 li = listitem_alloc();
20611 if (li == NULL)
20612 {
20613 vim_free(s);
20614 goto errret;
20615 }
20616 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020617 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020618 li->li_tv.vval.v_string = s;
20619 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020620 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020621
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020622 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020623 rettv->v_type = VAR_LIST;
20624 rettv->vval.v_list = list;
20625 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020626 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020627 else
20628 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020629 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020630#ifdef USE_CR
20631 /* translate <CR> into <NL> */
20632 if (res != NULL)
20633 {
20634 char_u *s;
20635
20636 for (s = res; *s; ++s)
20637 {
20638 if (*s == CAR)
20639 *s = NL;
20640 }
20641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020642#else
20643# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020644 /* translate <CR><NL> into <NL> */
20645 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020646 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020647 char_u *s, *d;
20648
20649 d = res;
20650 for (s = res; *s; ++s)
20651 {
20652 if (s[0] == CAR && s[1] == NL)
20653 ++s;
20654 *d++ = *s;
20655 }
20656 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020658# endif
20659#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020660 rettv->vval.v_string = res;
20661 res = NULL;
20662 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020663
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020664errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020665 if (infile != NULL)
20666 {
20667 mch_remove(infile);
20668 vim_free(infile);
20669 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020670 if (res != NULL)
20671 vim_free(res);
20672 if (list != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020020673 list_free(list);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020674}
20675
20676/*
20677 * "system()" function
20678 */
20679 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020680f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020681{
20682 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20683}
20684
20685/*
20686 * "systemlist()" function
20687 */
20688 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020689f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020690{
20691 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020692}
20693
20694/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020695 * "tabpagebuflist()" function
20696 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020698f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020699{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020700#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020701 tabpage_T *tp;
20702 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020703
20704 if (argvars[0].v_type == VAR_UNKNOWN)
20705 wp = firstwin;
20706 else
20707 {
20708 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20709 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020710 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020711 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020712 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020713 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020714 for (; wp != NULL; wp = wp->w_next)
20715 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020716 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020717 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020718 }
20719#endif
20720}
20721
20722
20723/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020724 * "tabpagenr()" function
20725 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020726 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020727f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020728{
20729 int nr = 1;
20730#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020731 char_u *arg;
20732
20733 if (argvars[0].v_type != VAR_UNKNOWN)
20734 {
20735 arg = get_tv_string_chk(&argvars[0]);
20736 nr = 0;
20737 if (arg != NULL)
20738 {
20739 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020740 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020741 else
20742 EMSG2(_(e_invexpr2), arg);
20743 }
20744 }
20745 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020746 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020747#endif
20748 rettv->vval.v_number = nr;
20749}
20750
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020751
20752#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020753static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020754
20755/*
20756 * Common code for tabpagewinnr() and winnr().
20757 */
20758 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020759get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020760{
20761 win_T *twin;
20762 int nr = 1;
20763 win_T *wp;
20764 char_u *arg;
20765
20766 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20767 if (argvar->v_type != VAR_UNKNOWN)
20768 {
20769 arg = get_tv_string_chk(argvar);
20770 if (arg == NULL)
20771 nr = 0; /* type error; errmsg already given */
20772 else if (STRCMP(arg, "$") == 0)
20773 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20774 else if (STRCMP(arg, "#") == 0)
20775 {
20776 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20777 if (twin == NULL)
20778 nr = 0;
20779 }
20780 else
20781 {
20782 EMSG2(_(e_invexpr2), arg);
20783 nr = 0;
20784 }
20785 }
20786
20787 if (nr > 0)
20788 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20789 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020790 {
20791 if (wp == NULL)
20792 {
20793 /* didn't find it in this tabpage */
20794 nr = 0;
20795 break;
20796 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020797 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020798 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020799 return nr;
20800}
20801#endif
20802
20803/*
20804 * "tabpagewinnr()" function
20805 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020807f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020808{
20809 int nr = 1;
20810#ifdef FEAT_WINDOWS
20811 tabpage_T *tp;
20812
20813 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20814 if (tp == NULL)
20815 nr = 0;
20816 else
20817 nr = get_winnr(tp, &argvars[1]);
20818#endif
20819 rettv->vval.v_number = nr;
20820}
20821
20822
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020823/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020824 * "tagfiles()" function
20825 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020826 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020827f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020828{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020829 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020830 tagname_T tn;
20831 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020832
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020833 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020834 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020835 fname = alloc(MAXPATHL);
20836 if (fname == NULL)
20837 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020838
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020839 for (first = TRUE; ; first = FALSE)
20840 if (get_tagfname(&tn, first, fname) == FAIL
20841 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020842 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020843 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020844 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020845}
20846
20847/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020848 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020849 */
20850 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020851f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020852{
20853 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020854
20855 tag_pattern = get_tv_string(&argvars[0]);
20856
20857 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020858 if (*tag_pattern == NUL)
20859 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020860
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020861 if (rettv_list_alloc(rettv) == OK)
20862 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020863}
20864
20865/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020866 * "tempname()" function
20867 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020868 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020869f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020870{
20871 static int x = 'A';
20872
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020873 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020874 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020875
20876 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20877 * names. Skip 'I' and 'O', they are used for shell redirection. */
20878 do
20879 {
20880 if (x == 'Z')
20881 x = '0';
20882 else if (x == '9')
20883 x = 'A';
20884 else
20885 {
20886#ifdef EBCDIC
20887 if (x == 'I')
20888 x = 'J';
20889 else if (x == 'R')
20890 x = 'S';
20891 else
20892#endif
20893 ++x;
20894 }
20895 } while (x == 'I' || x == 'O');
20896}
20897
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020898#ifdef FEAT_FLOAT
20899/*
20900 * "tan()" function
20901 */
20902 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020903f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020904{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020905 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020906
20907 rettv->v_type = VAR_FLOAT;
20908 if (get_float_arg(argvars, &f) == OK)
20909 rettv->vval.v_float = tan(f);
20910 else
20911 rettv->vval.v_float = 0.0;
20912}
20913
20914/*
20915 * "tanh()" function
20916 */
20917 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020918f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020919{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020920 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020921
20922 rettv->v_type = VAR_FLOAT;
20923 if (get_float_arg(argvars, &f) == OK)
20924 rettv->vval.v_float = tanh(f);
20925 else
20926 rettv->vval.v_float = 0.0;
20927}
20928#endif
20929
Bram Moolenaar574860b2016-05-24 17:33:34 +020020930/*
Bram Moolenaar8e8df252016-05-25 21:23:21 +020020931 * "test_alloc_fail(id, countdown, repeat)" function
20932 */
20933 static void
20934f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
20935{
20936 if (argvars[0].v_type != VAR_NUMBER
20937 || argvars[0].vval.v_number <= 0
20938 || argvars[1].v_type != VAR_NUMBER
20939 || argvars[1].vval.v_number < 0
20940 || argvars[2].v_type != VAR_NUMBER)
20941 EMSG(_(e_invarg));
20942 else
20943 {
20944 alloc_fail_id = argvars[0].vval.v_number;
20945 if (alloc_fail_id >= aid_last)
20946 EMSG(_(e_invarg));
20947 alloc_fail_countdown = argvars[1].vval.v_number;
20948 alloc_fail_repeat = argvars[2].vval.v_number;
20949 did_outofmem_msg = FALSE;
20950 }
20951}
20952
20953/*
20954 * "test_disable_char_avail({expr})" function
20955 */
20956 static void
20957f_test_disable_char_avail(typval_T *argvars, typval_T *rettv UNUSED)
20958{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020959 disable_char_avail_for_testing = (int)get_tv_number(&argvars[0]);
Bram Moolenaar8e8df252016-05-25 21:23:21 +020020960}
20961
20962/*
Bram Moolenaar574860b2016-05-24 17:33:34 +020020963 * "test_garbagecollect_now()" function
20964 */
20965 static void
20966f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20967{
20968 /* This is dangerous, any Lists and Dicts used internally may be freed
20969 * while still in use. */
20970 garbage_collect(TRUE);
20971}
20972
20973#ifdef FEAT_JOB_CHANNEL
20974 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020020975f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020020976{
20977 rettv->v_type = VAR_CHANNEL;
20978 rettv->vval.v_channel = NULL;
20979}
20980#endif
20981
20982 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020020983f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020020984{
20985 rettv->v_type = VAR_DICT;
20986 rettv->vval.v_dict = NULL;
20987}
20988
20989#ifdef FEAT_JOB_CHANNEL
20990 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020020991f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020020992{
20993 rettv->v_type = VAR_JOB;
20994 rettv->vval.v_job = NULL;
20995}
20996#endif
20997
20998 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020020999f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020021000{
21001 rettv->v_type = VAR_LIST;
21002 rettv->vval.v_list = NULL;
21003}
21004
21005 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021006f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020021007{
21008 rettv->v_type = VAR_PARTIAL;
21009 rettv->vval.v_partial = NULL;
21010}
21011
21012 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021013f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020021014{
21015 rettv->v_type = VAR_STRING;
21016 rettv->vval.v_string = NULL;
21017}
21018
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020021019 static void
21020f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
21021{
21022 time_for_testing = (time_t)get_tv_number(&argvars[0]);
21023}
21024
Bram Moolenaar975b5272016-03-15 23:10:59 +010021025#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
21026/*
21027 * Get a callback from "arg". It can be a Funcref or a function name.
21028 * When "arg" is zero return an empty string.
21029 * Return NULL for an invalid argument.
21030 */
21031 char_u *
21032get_callback(typval_T *arg, partial_T **pp)
21033{
21034 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
21035 {
21036 *pp = arg->vval.v_partial;
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010021037 ++(*pp)->pt_refcount;
Bram Moolenaar975b5272016-03-15 23:10:59 +010021038 return (*pp)->pt_name;
21039 }
21040 *pp = NULL;
21041 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
21042 return arg->vval.v_string;
21043 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
21044 return (char_u *)"";
21045 EMSG(_("E921: Invalid callback argument"));
21046 return NULL;
21047}
21048#endif
21049
21050#ifdef FEAT_TIMERS
21051/*
21052 * "timer_start(time, callback [, options])" function
21053 */
21054 static void
21055f_timer_start(typval_T *argvars, typval_T *rettv)
21056{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021057 long msec = (long)get_tv_number(&argvars[0]);
Bram Moolenaar975b5272016-03-15 23:10:59 +010021058 timer_T *timer;
21059 int repeat = 0;
21060 char_u *callback;
21061 dict_T *dict;
21062
Bram Moolenaar38499922016-04-22 20:46:52 +020021063 if (check_secure())
21064 return;
Bram Moolenaar975b5272016-03-15 23:10:59 +010021065 if (argvars[2].v_type != VAR_UNKNOWN)
21066 {
21067 if (argvars[2].v_type != VAR_DICT
21068 || (dict = argvars[2].vval.v_dict) == NULL)
21069 {
21070 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
21071 return;
21072 }
21073 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
21074 repeat = get_dict_number(dict, (char_u *)"repeat");
21075 }
21076
21077 timer = create_timer(msec, repeat);
21078 callback = get_callback(&argvars[1], &timer->tr_partial);
21079 if (callback == NULL)
21080 {
21081 stop_timer(timer);
21082 rettv->vval.v_number = -1;
21083 }
21084 else
21085 {
21086 timer->tr_callback = vim_strsave(callback);
21087 rettv->vval.v_number = timer->tr_id;
21088 }
21089}
21090
21091/*
21092 * "timer_stop(timer)" function
21093 */
21094 static void
21095f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
21096{
Bram Moolenaare40d75f2016-05-15 18:00:19 +020021097 timer_T *timer;
Bram Moolenaar975b5272016-03-15 23:10:59 +010021098
Bram Moolenaare40d75f2016-05-15 18:00:19 +020021099 if (argvars[0].v_type != VAR_NUMBER)
21100 {
21101 EMSG(_(e_number_exp));
21102 return;
21103 }
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021104 timer = find_timer((int)get_tv_number(&argvars[0]));
Bram Moolenaar975b5272016-03-15 23:10:59 +010021105 if (timer != NULL)
21106 stop_timer(timer);
21107}
21108#endif
21109
Bram Moolenaard52d9742005-08-21 22:20:28 +000021110/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021111 * "tolower(string)" function
21112 */
21113 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021114f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021115{
21116 char_u *p;
21117
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021118 p = vim_strsave(get_tv_string(&argvars[0]));
21119 rettv->v_type = VAR_STRING;
21120 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021121
21122 if (p != NULL)
21123 while (*p != NUL)
21124 {
21125#ifdef FEAT_MBYTE
21126 int l;
21127
21128 if (enc_utf8)
21129 {
21130 int c, lc;
21131
21132 c = utf_ptr2char(p);
21133 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021134 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021135 /* TODO: reallocate string when byte count changes. */
21136 if (utf_char2len(lc) == l)
21137 utf_char2bytes(lc, p);
21138 p += l;
21139 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021140 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021141 p += l; /* skip multi-byte character */
21142 else
21143#endif
21144 {
21145 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
21146 ++p;
21147 }
21148 }
21149}
21150
21151/*
21152 * "toupper(string)" function
21153 */
21154 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021155f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021156{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021157 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021158 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021159}
21160
21161/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000021162 * "tr(string, fromstr, tostr)" function
21163 */
21164 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021165f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021166{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021167 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021168 char_u *fromstr;
21169 char_u *tostr;
21170 char_u *p;
21171#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000021172 int inlen;
21173 int fromlen;
21174 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021175 int idx;
21176 char_u *cpstr;
21177 int cplen;
21178 int first = TRUE;
21179#endif
21180 char_u buf[NUMBUFLEN];
21181 char_u buf2[NUMBUFLEN];
21182 garray_T ga;
21183
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021184 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021185 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
21186 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021187
21188 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021189 rettv->v_type = VAR_STRING;
21190 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021191 if (fromstr == NULL || tostr == NULL)
21192 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000021193 ga_init2(&ga, (int)sizeof(char), 80);
21194
21195#ifdef FEAT_MBYTE
21196 if (!has_mbyte)
21197#endif
21198 /* not multi-byte: fromstr and tostr must be the same length */
21199 if (STRLEN(fromstr) != STRLEN(tostr))
21200 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000021201#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000021202error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000021203#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000021204 EMSG2(_(e_invarg2), fromstr);
21205 ga_clear(&ga);
21206 return;
21207 }
21208
21209 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021210 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021211 {
21212#ifdef FEAT_MBYTE
21213 if (has_mbyte)
21214 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021215 inlen = (*mb_ptr2len)(in_str);
21216 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021217 cplen = inlen;
21218 idx = 0;
21219 for (p = fromstr; *p != NUL; p += fromlen)
21220 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021221 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021222 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021223 {
21224 for (p = tostr; *p != NUL; p += tolen)
21225 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021226 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021227 if (idx-- == 0)
21228 {
21229 cplen = tolen;
21230 cpstr = p;
21231 break;
21232 }
21233 }
21234 if (*p == NUL) /* tostr is shorter than fromstr */
21235 goto error;
21236 break;
21237 }
21238 ++idx;
21239 }
21240
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021241 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021242 {
21243 /* Check that fromstr and tostr have the same number of
21244 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021245 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000021246 first = FALSE;
21247 for (p = tostr; *p != NUL; p += tolen)
21248 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021249 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021250 --idx;
21251 }
21252 if (idx != 0)
21253 goto error;
21254 }
21255
Bram Moolenaarcde88542015-08-11 19:14:00 +020021256 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000021257 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021258 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021259
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021260 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021261 }
21262 else
21263#endif
21264 {
21265 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021266 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021267 if (p != NULL)
21268 ga_append(&ga, tostr[p - fromstr]);
21269 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021270 ga_append(&ga, *in_str);
21271 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021272 }
21273 }
21274
Bram Moolenaar61b974b2006-12-05 09:32:29 +000021275 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020021276 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000021277 ga_append(&ga, NUL);
21278
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021279 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021280}
21281
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021282#ifdef FEAT_FLOAT
21283/*
21284 * "trunc({float})" function
21285 */
21286 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021287f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021288{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010021289 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021290
21291 rettv->v_type = VAR_FLOAT;
21292 if (get_float_arg(argvars, &f) == OK)
21293 /* trunc() is not in C90, use floor() or ceil() instead. */
21294 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
21295 else
21296 rettv->vval.v_float = 0.0;
21297}
21298#endif
21299
Bram Moolenaar8299df92004-07-10 09:47:34 +000021300/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021301 * "type(expr)" function
21302 */
21303 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021304f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021305{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010021306 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021307
21308 switch (argvars[0].v_type)
21309 {
21310 case VAR_NUMBER: n = 0; break;
21311 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010021312 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021313 case VAR_FUNC: n = 2; break;
21314 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021315 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021316 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010021317 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010021318 if (argvars[0].vval.v_number == VVAL_FALSE
21319 || argvars[0].vval.v_number == VVAL_TRUE)
21320 n = 6;
21321 else
21322 n = 7;
21323 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021324 case VAR_JOB: n = 8; break;
21325 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021326 case VAR_UNKNOWN:
21327 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
21328 n = -1;
21329 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021330 }
21331 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021332}
21333
21334/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021335 * "undofile(name)" function
21336 */
21337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021338f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021339{
21340 rettv->v_type = VAR_STRING;
21341#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021342 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021343 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021344
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021345 if (*fname == NUL)
21346 {
21347 /* If there is no file name there will be no undo file. */
21348 rettv->vval.v_string = NULL;
21349 }
21350 else
21351 {
21352 char_u *ffname = FullName_save(fname, FALSE);
21353
21354 if (ffname != NULL)
21355 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
21356 vim_free(ffname);
21357 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021358 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021359#else
21360 rettv->vval.v_string = NULL;
21361#endif
21362}
21363
21364/*
Bram Moolenaara800b422010-06-27 01:15:55 +020021365 * "undotree()" function
21366 */
21367 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021368f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020021369{
21370 if (rettv_dict_alloc(rettv) == OK)
21371 {
21372 dict_T *dict = rettv->vval.v_dict;
21373 list_T *list;
21374
Bram Moolenaar730cde92010-06-27 05:18:54 +020021375 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021376 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021377 dict_add_nr_str(dict, "save_last",
21378 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021379 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
21380 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021381 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021382
21383 list = list_alloc();
21384 if (list != NULL)
21385 {
21386 u_eval_tree(curbuf->b_u_oldhead, list);
21387 dict_add_list(dict, "entries", list);
21388 }
21389 }
21390}
21391
21392/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000021393 * "values(dict)" function
21394 */
21395 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021396f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000021397{
21398 dict_list(argvars, rettv, 1);
21399}
21400
21401/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021402 * "virtcol(string)" function
21403 */
21404 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021405f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021406{
21407 colnr_T vcol = 0;
21408 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021409 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021410
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021411 fp = var2fpos(&argvars[0], FALSE, &fnum);
21412 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
21413 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021414 {
21415 getvvcol(curwin, fp, NULL, NULL, &vcol);
21416 ++vcol;
21417 }
21418
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021419 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021420}
21421
21422/*
21423 * "visualmode()" function
21424 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021425 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010021426f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021427{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021428 char_u str[2];
21429
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021430 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021431 str[0] = curbuf->b_visual_mode_eval;
21432 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021433 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021434
21435 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000021436 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021437 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021438}
21439
21440/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021441 * "wildmenumode()" function
21442 */
21443 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021444f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021445{
21446#ifdef FEAT_WILDMENU
21447 if (wild_menu_showing)
21448 rettv->vval.v_number = 1;
21449#endif
21450}
21451
21452/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021453 * "winbufnr(nr)" function
21454 */
21455 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021456f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021457{
21458 win_T *wp;
21459
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021460 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021461 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021462 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021463 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021464 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021465}
21466
21467/*
21468 * "wincol()" function
21469 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021470 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021471f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021472{
21473 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021474 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021475}
21476
21477/*
21478 * "winheight(nr)" function
21479 */
21480 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021481f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021482{
21483 win_T *wp;
21484
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021485 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021486 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021487 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021488 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021489 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021490}
21491
21492/*
21493 * "winline()" function
21494 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021495 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021496f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021497{
21498 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021499 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021500}
21501
21502/*
21503 * "winnr()" function
21504 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021505 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021506f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021507{
21508 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021509
Bram Moolenaar071d4272004-06-13 20:20:40 +000021510#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021511 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021512#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021513 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021514}
21515
21516/*
21517 * "winrestcmd()" function
21518 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021519 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021520f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021521{
21522#ifdef FEAT_WINDOWS
21523 win_T *wp;
21524 int winnr = 1;
21525 garray_T ga;
21526 char_u buf[50];
21527
21528 ga_init2(&ga, (int)sizeof(char), 70);
21529 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21530 {
21531 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21532 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021533 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21534 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021535 ++winnr;
21536 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021537 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021538
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021539 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021540#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021541 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021542#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021543 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021544}
21545
21546/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021547 * "winrestview()" function
21548 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021549 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021550f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021551{
21552 dict_T *dict;
21553
21554 if (argvars[0].v_type != VAR_DICT
21555 || (dict = argvars[0].vval.v_dict) == NULL)
21556 EMSG(_(e_invarg));
21557 else
21558 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021559 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021560 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
Bram Moolenaar82c25852014-05-28 16:47:16 +020021561 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021562 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021563#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021564 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021565 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021566#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021567 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21568 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021569 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar82c25852014-05-28 16:47:16 +020021570 curwin->w_set_curswant = FALSE;
21571 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021572
Bram Moolenaar82c25852014-05-28 16:47:16 +020021573 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021574 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021575#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021576 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021577 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021578#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021579 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021580 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
Bram Moolenaar82c25852014-05-28 16:47:16 +020021581 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021582 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021583
21584 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021585 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021586# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020021587 win_new_width(curwin, W_WIDTH(curwin));
21588# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021589 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021590
Bram Moolenaarb851a962014-10-31 15:45:52 +010021591 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021592 curwin->w_topline = 1;
21593 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21594 curwin->w_topline = curbuf->b_ml.ml_line_count;
21595#ifdef FEAT_DIFF
21596 check_topfill(curwin, TRUE);
21597#endif
21598 }
21599}
21600
21601/*
21602 * "winsaveview()" function
21603 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021604 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021605f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021606{
21607 dict_T *dict;
21608
Bram Moolenaara800b422010-06-27 01:15:55 +020021609 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021610 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021611 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021612
21613 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21614 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21615#ifdef FEAT_VIRTUALEDIT
21616 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21617#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021618 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021619 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21620
21621 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21622#ifdef FEAT_DIFF
21623 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21624#endif
21625 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21626 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21627}
21628
21629/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021630 * "winwidth(nr)" function
21631 */
21632 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021633f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021634{
21635 win_T *wp;
21636
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021637 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021638 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021639 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021640 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021641#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021642 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021643#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021644 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021645#endif
21646}
21647
Bram Moolenaar071d4272004-06-13 20:20:40 +000021648/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021649 * "wordcount()" function
21650 */
21651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021652f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021653{
21654 if (rettv_dict_alloc(rettv) == FAIL)
21655 return;
21656 cursor_pos_info(rettv->vval.v_dict);
21657}
21658
21659/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021660 * Write list of strings to file
21661 */
21662 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021663write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021664{
21665 listitem_T *li;
21666 int c;
21667 int ret = OK;
21668 char_u *s;
21669
21670 for (li = list->lv_first; li != NULL; li = li->li_next)
21671 {
21672 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21673 {
21674 if (*s == '\n')
21675 c = putc(NUL, fd);
21676 else
21677 c = putc(*s, fd);
21678 if (c == EOF)
21679 {
21680 ret = FAIL;
21681 break;
21682 }
21683 }
21684 if (!binary || li->li_next != NULL)
21685 if (putc('\n', fd) == EOF)
21686 {
21687 ret = FAIL;
21688 break;
21689 }
21690 if (ret == FAIL)
21691 {
21692 EMSG(_(e_write));
21693 break;
21694 }
21695 }
21696 return ret;
21697}
21698
21699/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021700 * "writefile()" function
21701 */
21702 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021703f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021704{
21705 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021706 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021707 char_u *fname;
21708 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021709 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021710
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021711 if (check_restricted() || check_secure())
21712 return;
21713
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021714 if (argvars[0].v_type != VAR_LIST)
21715 {
21716 EMSG2(_(e_listarg), "writefile()");
21717 return;
21718 }
21719 if (argvars[0].vval.v_list == NULL)
21720 return;
21721
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021722 if (argvars[2].v_type != VAR_UNKNOWN)
21723 {
21724 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21725 binary = TRUE;
21726 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21727 append = TRUE;
21728 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021729
21730 /* Always open the file in binary mode, library functions have a mind of
21731 * their own about CR-LF conversion. */
21732 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021733 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21734 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021735 {
21736 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21737 ret = -1;
21738 }
21739 else
21740 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021741 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21742 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021743 fclose(fd);
21744 }
21745
21746 rettv->vval.v_number = ret;
21747}
21748
21749/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021750 * "xor(expr, expr)" function
21751 */
21752 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021753f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021754{
21755 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21756 ^ get_tv_number_chk(&argvars[1], NULL);
21757}
21758
21759
21760/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021761 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021762 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021763 */
21764 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021765var2fpos(
21766 typval_T *varp,
21767 int dollar_lnum, /* TRUE when $ is last line */
21768 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021769{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021770 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021771 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021772 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021773
Bram Moolenaara5525202006-03-02 22:52:09 +000021774 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021775 if (varp->v_type == VAR_LIST)
21776 {
21777 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021778 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021779 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021780 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021781
21782 l = varp->vval.v_list;
21783 if (l == NULL)
21784 return NULL;
21785
21786 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021787 pos.lnum = list_find_nr(l, 0L, &error);
21788 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021789 return NULL; /* invalid line number */
21790
21791 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021792 pos.col = list_find_nr(l, 1L, &error);
21793 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021794 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021795 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021796
21797 /* We accept "$" for the column number: last column. */
21798 li = list_find(l, 1L);
21799 if (li != NULL && li->li_tv.v_type == VAR_STRING
21800 && li->li_tv.vval.v_string != NULL
21801 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21802 pos.col = len + 1;
21803
Bram Moolenaara5525202006-03-02 22:52:09 +000021804 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021805 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021806 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021807 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021808
Bram Moolenaara5525202006-03-02 22:52:09 +000021809#ifdef FEAT_VIRTUALEDIT
21810 /* Get the virtual offset. Defaults to zero. */
21811 pos.coladd = list_find_nr(l, 2L, &error);
21812 if (error)
21813 pos.coladd = 0;
21814#endif
21815
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021816 return &pos;
21817 }
21818
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021819 name = get_tv_string_chk(varp);
21820 if (name == NULL)
21821 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021822 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021823 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021824 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21825 {
21826 if (VIsual_active)
21827 return &VIsual;
21828 return &curwin->w_cursor;
21829 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021830 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021831 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021832 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021833 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21834 return NULL;
21835 return pp;
21836 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021837
21838#ifdef FEAT_VIRTUALEDIT
21839 pos.coladd = 0;
21840#endif
21841
Bram Moolenaar477933c2007-07-17 14:32:23 +000021842 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021843 {
21844 pos.col = 0;
21845 if (name[1] == '0') /* "w0": first visible line */
21846 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021847 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021848 pos.lnum = curwin->w_topline;
21849 return &pos;
21850 }
21851 else if (name[1] == '$') /* "w$": last visible line */
21852 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021853 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021854 pos.lnum = curwin->w_botline - 1;
21855 return &pos;
21856 }
21857 }
21858 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021859 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021860 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021861 {
21862 pos.lnum = curbuf->b_ml.ml_line_count;
21863 pos.col = 0;
21864 }
21865 else
21866 {
21867 pos.lnum = curwin->w_cursor.lnum;
21868 pos.col = (colnr_T)STRLEN(ml_get_curline());
21869 }
21870 return &pos;
21871 }
21872 return NULL;
21873}
21874
21875/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021876 * Convert list in "arg" into a position and optional file number.
21877 * When "fnump" is NULL there is no file number, only 3 items.
21878 * Note that the column is passed on as-is, the caller may want to decrement
21879 * it to use 1 for the first column.
21880 * Return FAIL when conversion is not possible, doesn't check the position for
21881 * validity.
21882 */
21883 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021884list2fpos(
21885 typval_T *arg,
21886 pos_T *posp,
21887 int *fnump,
21888 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021889{
21890 list_T *l = arg->vval.v_list;
21891 long i = 0;
21892 long n;
21893
Bram Moolenaar493c1782014-05-28 14:34:46 +020021894 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21895 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021896 if (arg->v_type != VAR_LIST
21897 || l == NULL
21898 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021899 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021900 return FAIL;
21901
21902 if (fnump != NULL)
21903 {
21904 n = list_find_nr(l, i++, NULL); /* fnum */
21905 if (n < 0)
21906 return FAIL;
21907 if (n == 0)
21908 n = curbuf->b_fnum; /* current buffer */
21909 *fnump = n;
21910 }
21911
21912 n = list_find_nr(l, i++, NULL); /* lnum */
21913 if (n < 0)
21914 return FAIL;
21915 posp->lnum = n;
21916
21917 n = list_find_nr(l, i++, NULL); /* col */
21918 if (n < 0)
21919 return FAIL;
21920 posp->col = n;
21921
21922#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021923 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021924 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021925 posp->coladd = 0;
21926 else
21927 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021928#endif
21929
Bram Moolenaar493c1782014-05-28 14:34:46 +020021930 if (curswantp != NULL)
21931 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21932
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021933 return OK;
21934}
21935
21936/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021937 * Get the length of an environment variable name.
21938 * Advance "arg" to the first character after the name.
21939 * Return 0 for error.
21940 */
21941 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021942get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021943{
21944 char_u *p;
21945 int len;
21946
21947 for (p = *arg; vim_isIDc(*p); ++p)
21948 ;
21949 if (p == *arg) /* no name found */
21950 return 0;
21951
21952 len = (int)(p - *arg);
21953 *arg = p;
21954 return len;
21955}
21956
21957/*
21958 * Get the length of the name of a function or internal variable.
21959 * "arg" is advanced to the first non-white character after the name.
21960 * Return 0 if something is wrong.
21961 */
21962 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021963get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021964{
21965 char_u *p;
21966 int len;
21967
21968 /* Find the end of the name. */
21969 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021970 {
21971 if (*p == ':')
21972 {
21973 /* "s:" is start of "s:var", but "n:" is not and can be used in
21974 * slice "[n:]". Also "xx:" is not a namespace. */
21975 len = (int)(p - *arg);
21976 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21977 || len > 1)
21978 break;
21979 }
21980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021981 if (p == *arg) /* no name found */
21982 return 0;
21983
21984 len = (int)(p - *arg);
21985 *arg = skipwhite(p);
21986
21987 return len;
21988}
21989
21990/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021991 * Get the length of the name of a variable or function.
21992 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021993 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021994 * Return -1 if curly braces expansion failed.
21995 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021996 * If the name contains 'magic' {}'s, expand them and return the
21997 * expanded name in an allocated string via 'alias' - caller must free.
21998 */
21999 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022000get_name_len(
22001 char_u **arg,
22002 char_u **alias,
22003 int evaluate,
22004 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022005{
22006 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022007 char_u *p;
22008 char_u *expr_start;
22009 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022010
22011 *alias = NULL; /* default to no alias */
22012
22013 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
22014 && (*arg)[2] == (int)KE_SNR)
22015 {
22016 /* hard coded <SNR>, already translated */
22017 *arg += 3;
22018 return get_id_len(arg) + 3;
22019 }
22020 len = eval_fname_script(*arg);
22021 if (len > 0)
22022 {
22023 /* literal "<SID>", "s:" or "<SNR>" */
22024 *arg += len;
22025 }
22026
Bram Moolenaar071d4272004-06-13 20:20:40 +000022027 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022028 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022029 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022030 p = find_name_end(*arg, &expr_start, &expr_end,
22031 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022032 if (expr_start != NULL)
22033 {
22034 char_u *temp_string;
22035
22036 if (!evaluate)
22037 {
22038 len += (int)(p - *arg);
22039 *arg = skipwhite(p);
22040 return len;
22041 }
22042
22043 /*
22044 * Include any <SID> etc in the expanded string:
22045 * Thus the -len here.
22046 */
22047 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
22048 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022049 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022050 *alias = temp_string;
22051 *arg = skipwhite(p);
22052 return (int)STRLEN(temp_string);
22053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022054
22055 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022056 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022057 EMSG2(_(e_invexpr2), *arg);
22058
22059 return len;
22060}
22061
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022062/*
22063 * Find the end of a variable or function name, taking care of magic braces.
22064 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
22065 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022066 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022067 * Return a pointer to just after the name. Equal to "arg" if there is no
22068 * valid name.
22069 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022070 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022071find_name_end(
22072 char_u *arg,
22073 char_u **expr_start,
22074 char_u **expr_end,
22075 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022076{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022077 int mb_nest = 0;
22078 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022079 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022080 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022081
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022082 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022083 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022084 *expr_start = NULL;
22085 *expr_end = NULL;
22086 }
22087
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022088 /* Quick check for valid starting character. */
22089 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
22090 return arg;
22091
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022092 for (p = arg; *p != NUL
22093 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022094 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022095 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022096 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000022097 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022098 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000022099 if (*p == '\'')
22100 {
22101 /* skip over 'string' to avoid counting [ and ] inside it. */
22102 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
22103 ;
22104 if (*p == NUL)
22105 break;
22106 }
22107 else if (*p == '"')
22108 {
22109 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
22110 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
22111 if (*p == '\\' && p[1] != NUL)
22112 ++p;
22113 if (*p == NUL)
22114 break;
22115 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022116 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
22117 {
22118 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010022119 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022120 len = (int)(p - arg);
22121 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010022122 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022123 break;
22124 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000022125
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022126 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022127 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022128 if (*p == '[')
22129 ++br_nest;
22130 else if (*p == ']')
22131 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022132 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000022133
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022134 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022135 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022136 if (*p == '{')
22137 {
22138 mb_nest++;
22139 if (expr_start != NULL && *expr_start == NULL)
22140 *expr_start = p;
22141 }
22142 else if (*p == '}')
22143 {
22144 mb_nest--;
22145 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
22146 *expr_end = p;
22147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022149 }
22150
22151 return p;
22152}
22153
22154/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022155 * Expands out the 'magic' {}'s in a variable/function name.
22156 * Note that this can call itself recursively, to deal with
22157 * constructs like foo{bar}{baz}{bam}
22158 * The four pointer arguments point to "foo{expre}ss{ion}bar"
22159 * "in_start" ^
22160 * "expr_start" ^
22161 * "expr_end" ^
22162 * "in_end" ^
22163 *
22164 * Returns a new allocated string, which the caller must free.
22165 * Returns NULL for failure.
22166 */
22167 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022168make_expanded_name(
22169 char_u *in_start,
22170 char_u *expr_start,
22171 char_u *expr_end,
22172 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022173{
22174 char_u c1;
22175 char_u *retval = NULL;
22176 char_u *temp_result;
22177 char_u *nextcmd = NULL;
22178
22179 if (expr_end == NULL || in_end == NULL)
22180 return NULL;
22181 *expr_start = NUL;
22182 *expr_end = NUL;
22183 c1 = *in_end;
22184 *in_end = NUL;
22185
Bram Moolenaar362e1a32006-03-06 23:29:24 +000022186 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022187 if (temp_result != NULL && nextcmd == NULL)
22188 {
22189 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
22190 + (in_end - expr_end) + 1));
22191 if (retval != NULL)
22192 {
22193 STRCPY(retval, in_start);
22194 STRCAT(retval, temp_result);
22195 STRCAT(retval, expr_end + 1);
22196 }
22197 }
22198 vim_free(temp_result);
22199
22200 *in_end = c1; /* put char back for error messages */
22201 *expr_start = '{';
22202 *expr_end = '}';
22203
22204 if (retval != NULL)
22205 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022206 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022207 if (expr_start != NULL)
22208 {
22209 /* Further expansion! */
22210 temp_result = make_expanded_name(retval, expr_start,
22211 expr_end, temp_result);
22212 vim_free(retval);
22213 retval = temp_result;
22214 }
22215 }
22216
22217 return retval;
22218}
22219
22220/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022221 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022222 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022223 */
22224 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022225eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022226{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022227 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
22228}
22229
22230/*
22231 * Return TRUE if character "c" can be used as the first character in a
22232 * variable or function name (excluding '{' and '}').
22233 */
22234 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022235eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022236{
22237 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000022238}
22239
22240/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022241 * Set number v: variable to "val".
22242 */
22243 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022244set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022245{
Bram Moolenaare9a41262005-01-15 22:18:47 +000022246 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022247}
22248
22249/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022250 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022251 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022252 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022253get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022254{
Bram Moolenaare9a41262005-01-15 22:18:47 +000022255 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022256}
22257
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022258/*
22259 * Get string v: variable value. Uses a static buffer, can only be used once.
22260 */
22261 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022262get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022263{
22264 return get_tv_string(&vimvars[idx].vv_tv);
22265}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022266
Bram Moolenaar071d4272004-06-13 20:20:40 +000022267/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022268 * Get List v: variable value. Caller must take care of reference count when
22269 * needed.
22270 */
22271 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022272get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000022273{
22274 return vimvars[idx].vv_list;
22275}
22276
22277/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022278 * Set v:char to character "c".
22279 */
22280 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022281set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022282{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020022283 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022284
22285#ifdef FEAT_MBYTE
22286 if (has_mbyte)
22287 buf[(*mb_char2bytes)(c, buf)] = NUL;
22288 else
22289#endif
22290 {
22291 buf[0] = c;
22292 buf[1] = NUL;
22293 }
22294 set_vim_var_string(VV_CHAR, buf, -1);
22295}
22296
22297/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022298 * Set v:count to "count" and v:count1 to "count1".
22299 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022300 */
22301 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022302set_vcount(
22303 long count,
22304 long count1,
22305 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022306{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022307 if (set_prevcount)
22308 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022309 vimvars[VV_COUNT].vv_nr = count;
22310 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022311}
22312
22313/*
22314 * Set string v: variable to a copy of "val".
22315 */
22316 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022317set_vim_var_string(
22318 int idx,
22319 char_u *val,
22320 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022321{
Bram Moolenaara542c682016-01-31 16:28:04 +010022322 clear_tv(&vimvars[idx].vv_di.di_tv);
22323 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022324 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022325 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022326 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022327 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022328 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022329 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022330}
22331
22332/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022333 * Set List v: variable to "val".
22334 */
22335 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022336set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000022337{
Bram Moolenaara542c682016-01-31 16:28:04 +010022338 clear_tv(&vimvars[idx].vv_di.di_tv);
22339 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000022340 vimvars[idx].vv_list = val;
22341 if (val != NULL)
22342 ++val->lv_refcount;
22343}
22344
22345/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020022346 * Set Dictionary v: variable to "val".
22347 */
22348 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022349set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020022350{
22351 int todo;
22352 hashitem_T *hi;
22353
Bram Moolenaara542c682016-01-31 16:28:04 +010022354 clear_tv(&vimvars[idx].vv_di.di_tv);
22355 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020022356 vimvars[idx].vv_dict = val;
22357 if (val != NULL)
22358 {
22359 ++val->dv_refcount;
22360
22361 /* Set readonly */
22362 todo = (int)val->dv_hashtab.ht_used;
22363 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
22364 {
22365 if (HASHITEM_EMPTY(hi))
22366 continue;
22367 --todo;
22368 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22369 }
22370 }
22371}
22372
22373/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022374 * Set v:register if needed.
22375 */
22376 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022377set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022378{
22379 char_u regname;
22380
22381 if (c == 0 || c == ' ')
22382 regname = '"';
22383 else
22384 regname = c;
22385 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000022386 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022387 set_vim_var_string(VV_REG, &regname, 1);
22388}
22389
22390/*
22391 * Get or set v:exception. If "oldval" == NULL, return the current value.
22392 * Otherwise, restore the value to "oldval" and return NULL.
22393 * Must always be called in pairs to save and restore v:exception! Does not
22394 * take care of memory allocations.
22395 */
22396 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022397v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022398{
22399 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022400 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022401
Bram Moolenaare9a41262005-01-15 22:18:47 +000022402 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022403 return NULL;
22404}
22405
22406/*
22407 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
22408 * Otherwise, restore the value to "oldval" and return NULL.
22409 * Must always be called in pairs to save and restore v:throwpoint! Does not
22410 * take care of memory allocations.
22411 */
22412 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022413v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022414{
22415 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022416 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022417
Bram Moolenaare9a41262005-01-15 22:18:47 +000022418 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022419 return NULL;
22420}
22421
22422#if defined(FEAT_AUTOCMD) || defined(PROTO)
22423/*
22424 * Set v:cmdarg.
22425 * If "eap" != NULL, use "eap" to generate the value and return the old value.
22426 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
22427 * Must always be called in pairs!
22428 */
22429 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022430set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022431{
22432 char_u *oldval;
22433 char_u *newval;
22434 unsigned len;
22435
Bram Moolenaare9a41262005-01-15 22:18:47 +000022436 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022437 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022438 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022439 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000022440 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022441 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022442 }
22443
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022444 if (eap->force_bin == FORCE_BIN)
22445 len = 6;
22446 else if (eap->force_bin == FORCE_NOBIN)
22447 len = 8;
22448 else
22449 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022450
22451 if (eap->read_edit)
22452 len += 7;
22453
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022454 if (eap->force_ff != 0)
22455 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
22456# ifdef FEAT_MBYTE
22457 if (eap->force_enc != 0)
22458 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022459 if (eap->bad_char != 0)
22460 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022461# endif
22462
22463 newval = alloc(len + 1);
22464 if (newval == NULL)
22465 return NULL;
22466
22467 if (eap->force_bin == FORCE_BIN)
22468 sprintf((char *)newval, " ++bin");
22469 else if (eap->force_bin == FORCE_NOBIN)
22470 sprintf((char *)newval, " ++nobin");
22471 else
22472 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022473
22474 if (eap->read_edit)
22475 STRCAT(newval, " ++edit");
22476
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022477 if (eap->force_ff != 0)
22478 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
22479 eap->cmd + eap->force_ff);
22480# ifdef FEAT_MBYTE
22481 if (eap->force_enc != 0)
22482 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
22483 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022484 if (eap->bad_char == BAD_KEEP)
22485 STRCPY(newval + STRLEN(newval), " ++bad=keep");
22486 else if (eap->bad_char == BAD_DROP)
22487 STRCPY(newval + STRLEN(newval), " ++bad=drop");
22488 else if (eap->bad_char != 0)
22489 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022490# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022491 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022492 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022493}
22494#endif
22495
22496/*
22497 * Get the value of internal variable "name".
22498 * Return OK or FAIL.
22499 */
22500 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022501get_var_tv(
22502 char_u *name,
22503 int len, /* length of "name" */
22504 typval_T *rettv, /* NULL when only checking existence */
22505 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
22506 int verbose, /* may give error message */
22507 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022508{
22509 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000022510 typval_T *tv = NULL;
22511 typval_T atv;
22512 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022513 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022514
22515 /* truncate the name, so that we can use strcmp() */
22516 cc = name[len];
22517 name[len] = NUL;
22518
22519 /*
22520 * Check for "b:changedtick".
22521 */
22522 if (STRCMP(name, "b:changedtick") == 0)
22523 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022524 atv.v_type = VAR_NUMBER;
22525 atv.vval.v_number = curbuf->b_changedtick;
22526 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022527 }
22528
22529 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022530 * Check for user-defined variables.
22531 */
22532 else
22533 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022534 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022535 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022536 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022537 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022538 if (dip != NULL)
22539 *dip = v;
22540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022541 }
22542
Bram Moolenaare9a41262005-01-15 22:18:47 +000022543 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022544 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022545 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022546 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022547 ret = FAIL;
22548 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022549 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022550 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022551
22552 name[len] = cc;
22553
22554 return ret;
22555}
22556
22557/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022558 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22559 * Also handle function call with Funcref variable: func(expr)
22560 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22561 */
22562 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022563handle_subscript(
22564 char_u **arg,
22565 typval_T *rettv,
22566 int evaluate, /* do more than finding the end */
22567 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022568{
22569 int ret = OK;
22570 dict_T *selfdict = NULL;
22571 char_u *s;
22572 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022573 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022574
22575 while (ret == OK
22576 && (**arg == '['
22577 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022578 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
22579 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022580 && !vim_iswhite(*(*arg - 1)))
22581 {
22582 if (**arg == '(')
22583 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010022584 partial_T *pt = NULL;
22585
Bram Moolenaard9fba312005-06-26 22:34:35 +000022586 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022587 if (evaluate)
22588 {
22589 functv = *rettv;
22590 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022591
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022592 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022593 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022594 {
22595 pt = functv.vval.v_partial;
22596 s = pt->pt_name;
22597 }
22598 else
22599 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022600 }
22601 else
22602 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022603 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022604 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022605 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000022606
22607 /* Clear the funcref afterwards, so that deleting it while
22608 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022609 if (evaluate)
22610 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022611
22612 /* Stop the expression evaluation when immediately aborting on
22613 * error, or when an interrupt occurred or an exception was thrown
22614 * but not caught. */
22615 if (aborting())
22616 {
22617 if (ret == OK)
22618 clear_tv(rettv);
22619 ret = FAIL;
22620 }
22621 dict_unref(selfdict);
22622 selfdict = NULL;
22623 }
22624 else /* **arg == '[' || **arg == '.' */
22625 {
22626 dict_unref(selfdict);
22627 if (rettv->v_type == VAR_DICT)
22628 {
22629 selfdict = rettv->vval.v_dict;
22630 if (selfdict != NULL)
22631 ++selfdict->dv_refcount;
22632 }
22633 else
22634 selfdict = NULL;
22635 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22636 {
22637 clear_tv(rettv);
22638 ret = FAIL;
22639 }
22640 }
22641 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022642
Bram Moolenaar1d429612016-05-24 15:44:17 +020022643 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
22644 * Don't do this when "Func" is already a partial that was bound
22645 * explicitly (pt_auto is FALSE). */
22646 if (selfdict != NULL
22647 && (rettv->v_type == VAR_FUNC
22648 || (rettv->v_type == VAR_PARTIAL
22649 && (rettv->vval.v_partial->pt_auto
22650 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022651 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022652 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
22653 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022654 char_u *tofree = NULL;
22655 ufunc_T *fp;
22656 char_u fname_buf[FLEN_FIXED + 1];
22657 int error;
22658
22659 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022660 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022661 fp = find_func(fname);
22662 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022663
Bram Moolenaar65639032016-03-16 21:40:30 +010022664 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022665 {
Bram Moolenaar65639032016-03-16 21:40:30 +010022666 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
22667
22668 if (pt != NULL)
22669 {
22670 pt->pt_refcount = 1;
22671 pt->pt_dict = selfdict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020022672 pt->pt_auto = TRUE;
Bram Moolenaar65639032016-03-16 21:40:30 +010022673 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022674 if (rettv->v_type == VAR_FUNC)
22675 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022676 /* Just a function: Take over the function name and use
22677 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022678 pt->pt_name = rettv->vval.v_string;
22679 }
22680 else
22681 {
22682 partial_T *ret_pt = rettv->vval.v_partial;
22683 int i;
22684
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022685 /* Partial: copy the function name, use selfdict and copy
22686 * args. Can't take over name or args, the partial might
22687 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022688 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022689 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022690 if (ret_pt->pt_argc > 0)
22691 {
22692 pt->pt_argv = (typval_T *)alloc(
22693 sizeof(typval_T) * ret_pt->pt_argc);
22694 if (pt->pt_argv == NULL)
22695 /* out of memory: drop the arguments */
22696 pt->pt_argc = 0;
22697 else
22698 {
22699 pt->pt_argc = ret_pt->pt_argc;
22700 for (i = 0; i < pt->pt_argc; i++)
22701 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
22702 }
22703 }
22704 partial_unref(ret_pt);
22705 }
Bram Moolenaar65639032016-03-16 21:40:30 +010022706 rettv->v_type = VAR_PARTIAL;
22707 rettv->vval.v_partial = pt;
22708 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022709 }
22710 }
22711
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022712 dict_unref(selfdict);
22713 return ret;
22714}
22715
22716/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022717 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022718 * value).
22719 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022720 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022721alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022722{
Bram Moolenaar33570922005-01-25 22:26:29 +000022723 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022724}
22725
22726/*
22727 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022728 * The string "s" must have been allocated, it is consumed.
22729 * Return NULL for out of memory, the variable otherwise.
22730 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022731 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022732alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022733{
Bram Moolenaar33570922005-01-25 22:26:29 +000022734 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022735
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022736 rettv = alloc_tv();
22737 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022738 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022739 rettv->v_type = VAR_STRING;
22740 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022741 }
22742 else
22743 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022744 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022745}
22746
22747/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022748 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022749 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022750 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022751free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022752{
22753 if (varp != NULL)
22754 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022755 switch (varp->v_type)
22756 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022757 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022758 func_unref(varp->vval.v_string);
22759 /*FALLTHROUGH*/
22760 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022761 vim_free(varp->vval.v_string);
22762 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022763 case VAR_PARTIAL:
22764 partial_unref(varp->vval.v_partial);
22765 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022766 case VAR_LIST:
22767 list_unref(varp->vval.v_list);
22768 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022769 case VAR_DICT:
22770 dict_unref(varp->vval.v_dict);
22771 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022772 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022773#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022774 job_unref(varp->vval.v_job);
22775 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022776#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022777 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022778#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022779 channel_unref(varp->vval.v_channel);
22780 break;
22781#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022782 case VAR_NUMBER:
22783 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022784 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022785 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022786 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022788 vim_free(varp);
22789 }
22790}
22791
22792/*
22793 * Free the memory for a variable value and set the value to NULL or 0.
22794 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022795 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022796clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022797{
22798 if (varp != NULL)
22799 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022800 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022801 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022802 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022803 func_unref(varp->vval.v_string);
22804 /*FALLTHROUGH*/
22805 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022806 vim_free(varp->vval.v_string);
22807 varp->vval.v_string = NULL;
22808 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022809 case VAR_PARTIAL:
22810 partial_unref(varp->vval.v_partial);
22811 varp->vval.v_partial = NULL;
22812 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022813 case VAR_LIST:
22814 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022815 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022816 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022817 case VAR_DICT:
22818 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022819 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022820 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022821 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022822 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022823 varp->vval.v_number = 0;
22824 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022825 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022826#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022827 varp->vval.v_float = 0.0;
22828 break;
22829#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022830 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022831#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022832 job_unref(varp->vval.v_job);
22833 varp->vval.v_job = NULL;
22834#endif
22835 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022836 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022837#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022838 channel_unref(varp->vval.v_channel);
22839 varp->vval.v_channel = NULL;
22840#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022841 case VAR_UNKNOWN:
22842 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022843 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022844 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022845 }
22846}
22847
22848/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022849 * Set the value of a variable to NULL without freeing items.
22850 */
22851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022852init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022853{
22854 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022855 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022856}
22857
22858/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022859 * Get the number value of a variable.
22860 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022861 * For incompatible types, return 0.
22862 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22863 * caller of incompatible types: it sets *denote to TRUE if "denote"
22864 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022865 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022866 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022867get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022868{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022869 int error = FALSE;
22870
22871 return get_tv_number_chk(varp, &error); /* return 0L on error */
22872}
22873
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022874 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022875get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022876{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022877 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022878
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022879 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022880 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022881 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022882 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022883 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022884#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022885 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022886 break;
22887#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022888 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022889 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022890 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022891 break;
22892 case VAR_STRING:
22893 if (varp->vval.v_string != NULL)
22894 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022895 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022896 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022897 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022898 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022899 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022900 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022901 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022902 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022903 case VAR_SPECIAL:
22904 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22905 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022906 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022907#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022908 EMSG(_("E910: Using a Job as a Number"));
22909 break;
22910#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022911 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022912#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022913 EMSG(_("E913: Using a Channel as a Number"));
22914 break;
22915#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022916 case VAR_UNKNOWN:
22917 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022918 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022919 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022920 if (denote == NULL) /* useful for values that must be unsigned */
22921 n = -1;
22922 else
22923 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022924 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022925}
22926
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022927#ifdef FEAT_FLOAT
22928 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022929get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022930{
22931 switch (varp->v_type)
22932 {
22933 case VAR_NUMBER:
22934 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022935 case VAR_FLOAT:
22936 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022937 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022938 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022939 EMSG(_("E891: Using a Funcref as a Float"));
22940 break;
22941 case VAR_STRING:
22942 EMSG(_("E892: Using a String as a Float"));
22943 break;
22944 case VAR_LIST:
22945 EMSG(_("E893: Using a List as a Float"));
22946 break;
22947 case VAR_DICT:
22948 EMSG(_("E894: Using a Dictionary as a Float"));
22949 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022950 case VAR_SPECIAL:
22951 EMSG(_("E907: Using a special value as a Float"));
22952 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022953 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022954# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022955 EMSG(_("E911: Using a Job as a Float"));
22956 break;
22957# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022958 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022959# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022960 EMSG(_("E914: Using a Channel as a Float"));
22961 break;
22962# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022963 case VAR_UNKNOWN:
22964 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022965 break;
22966 }
22967 return 0;
22968}
22969#endif
22970
Bram Moolenaar071d4272004-06-13 20:20:40 +000022971/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022972 * Get the lnum from the first argument.
22973 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022974 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022975 */
22976 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022977get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022978{
Bram Moolenaar33570922005-01-25 22:26:29 +000022979 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022980 linenr_T lnum;
22981
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022982 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022983 if (lnum == 0) /* no valid number, try using line() */
22984 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022985 rettv.v_type = VAR_NUMBER;
22986 f_line(argvars, &rettv);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022987 lnum = (linenr_T)rettv.vval.v_number;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022988 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022989 }
22990 return lnum;
22991}
22992
22993/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022994 * Get the lnum from the first argument.
22995 * Also accepts "$", then "buf" is used.
22996 * Returns 0 on error.
22997 */
22998 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022999get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000023000{
23001 if (argvars[0].v_type == VAR_STRING
23002 && argvars[0].vval.v_string != NULL
23003 && argvars[0].vval.v_string[0] == '$'
23004 && buf != NULL)
23005 return buf->b_ml.ml_line_count;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020023006 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar661b1822005-07-28 22:36:45 +000023007}
23008
23009/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023010 * Get the string value of a variable.
23011 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000023012 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
23013 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023014 * If the String variable has never been set, return an empty string.
23015 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023016 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
23017 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023018 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010023019 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023020get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023021{
23022 static char_u mybuf[NUMBUFLEN];
23023
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023024 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023025}
23026
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010023027 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023028get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023029{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023030 char_u *res = get_tv_string_buf_chk(varp, buf);
23031
23032 return res != NULL ? res : (char_u *)"";
23033}
23034
Bram Moolenaar7d647822014-04-05 21:28:56 +020023035/*
23036 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
23037 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000023038 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023039get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023040{
23041 static char_u mybuf[NUMBUFLEN];
23042
23043 return get_tv_string_buf_chk(varp, mybuf);
23044}
23045
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023046 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023047get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023048{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023049 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023050 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023051 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020023052 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
23053 (varnumber_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023054 return buf;
23055 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023056 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023057 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023058 break;
23059 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023060 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000023061 break;
23062 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023063 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023064 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023065 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023066#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020023067 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023068 break;
23069#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023070 case VAR_STRING:
23071 if (varp->vval.v_string != NULL)
23072 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023073 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010023074 case VAR_SPECIAL:
23075 STRCPY(buf, get_var_special_name(varp->vval.v_number));
23076 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023077 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023078#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023079 {
23080 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010023081 char *status;
23082
23083 if (job == NULL)
23084 return (char_u *)"no process";
23085 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010023086 : job->jv_status == JOB_ENDED ? "dead"
23087 : "run";
23088# ifdef UNIX
23089 vim_snprintf((char *)buf, NUMBUFLEN,
23090 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010023091# elif defined(WIN32)
23092 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010023093 "process %ld %s",
23094 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010023095 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010023096# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010023097 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010023098 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
23099# endif
23100 return buf;
23101 }
23102#endif
23103 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010023104 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023105#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023106 {
23107 channel_T *channel = varp->vval.v_channel;
23108 char *status = channel_status(channel);
23109
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023110 if (channel == NULL)
23111 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
23112 else
23113 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010023114 "channel %d %s", channel->ch_id, status);
23115 return buf;
23116 }
23117#endif
23118 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023119 case VAR_UNKNOWN:
23120 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023121 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023122 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023123 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023124}
23125
23126/*
23127 * Find variable "name" in the list of variables.
23128 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023129 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000023130 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000023131 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023132 */
Bram Moolenaar33570922005-01-25 22:26:29 +000023133 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023134find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023135{
Bram Moolenaar071d4272004-06-13 20:20:40 +000023136 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023137 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023138
Bram Moolenaara7043832005-01-21 11:56:39 +000023139 ht = find_var_ht(name, &varname);
23140 if (htp != NULL)
23141 *htp = ht;
23142 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023143 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023144 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023145}
23146
23147/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020023148 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000023149 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023150 */
Bram Moolenaar33570922005-01-25 22:26:29 +000023151 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023152find_var_in_ht(
23153 hashtab_T *ht,
23154 int htname,
23155 char_u *varname,
23156 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000023157{
Bram Moolenaar33570922005-01-25 22:26:29 +000023158 hashitem_T *hi;
23159
23160 if (*varname == NUL)
23161 {
23162 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020023163 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000023164 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023165 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023166 case 'g': return &globvars_var;
23167 case 'v': return &vimvars_var;
23168 case 'b': return &curbuf->b_bufvar;
23169 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023170#ifdef FEAT_WINDOWS
23171 case 't': return &curtab->tp_winvar;
23172#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023173 case 'l': return current_funccal == NULL
23174 ? NULL : &current_funccal->l_vars_var;
23175 case 'a': return current_funccal == NULL
23176 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023177 }
23178 return NULL;
23179 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023180
23181 hi = hash_find(ht, varname);
23182 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023183 {
23184 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023185 * worked find the variable again. Don't auto-load a script if it was
23186 * loaded already, otherwise it would be loaded every time when
23187 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023188 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010023189 {
23190 /* Note: script_autoload() may make "hi" invalid. It must either
23191 * be obtained again or not used. */
23192 if (!script_autoload(varname, FALSE) || aborting())
23193 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023194 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010023195 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023196 if (HASHITEM_EMPTY(hi))
23197 return NULL;
23198 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023199 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023200}
23201
23202/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023203 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020023204 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000023205 * Set "varname" to the start of name without ':'.
23206 */
Bram Moolenaar33570922005-01-25 22:26:29 +000023207 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023208find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023209{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000023210 hashitem_T *hi;
23211
Bram Moolenaar73627d02015-08-11 15:46:09 +020023212 if (name[0] == NUL)
23213 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023214 if (name[1] != ':')
23215 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023216 /* The name must not start with a colon or #. */
23217 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023218 return NULL;
23219 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000023220
23221 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000023222 hi = hash_find(&compat_hashtab, name);
23223 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000023224 return &compat_hashtab;
23225
Bram Moolenaar071d4272004-06-13 20:20:40 +000023226 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023227 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023228 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023229 }
23230 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023231 if (*name == 'g') /* global variable */
23232 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023233 /* There must be no ':' or '#' in the rest of the name, unless g: is used
23234 */
23235 if (vim_strchr(name + 2, ':') != NULL
23236 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023237 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023238 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023239 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023240 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023241 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023242#ifdef FEAT_WINDOWS
23243 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023244 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023245#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000023246 if (*name == 'v') /* v: variable */
23247 return &vimvarht;
23248 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023249 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000023250 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023251 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023252 if (*name == 's' /* script variable */
23253 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
23254 return &SCRIPT_VARS(current_SID);
23255 return NULL;
23256}
23257
23258/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023259 * Get function call environment based on bactrace debug level
23260 */
23261 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023262get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023263{
23264 int i;
23265 funccall_T *funccal;
23266 funccall_T *temp_funccal;
23267
23268 funccal = current_funccal;
23269 if (debug_backtrace_level > 0)
23270 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010023271 for (i = 0; i < debug_backtrace_level; i++)
23272 {
23273 temp_funccal = funccal->caller;
23274 if (temp_funccal)
23275 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023276 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010023277 /* backtrace level overflow. reset to max */
23278 debug_backtrace_level = i;
23279 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023280 }
23281 return funccal;
23282}
23283
23284/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023285 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020023286 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023287 * Returns NULL when it doesn't exist.
23288 */
23289 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023290get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023291{
Bram Moolenaar33570922005-01-25 22:26:29 +000023292 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023293
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023294 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023295 if (v == NULL)
23296 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023297 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023298}
23299
23300/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023301 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000023302 * sourcing this script and when executing functions defined in the script.
23303 */
23304 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023305new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023306{
Bram Moolenaara7043832005-01-21 11:56:39 +000023307 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000023308 hashtab_T *ht;
23309 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000023310
Bram Moolenaar071d4272004-06-13 20:20:40 +000023311 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
23312 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023313 /* Re-allocating ga_data means that an ht_array pointing to
23314 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000023315 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000023316 for (i = 1; i <= ga_scripts.ga_len; ++i)
23317 {
23318 ht = &SCRIPT_VARS(i);
23319 if (ht->ht_mask == HT_INIT_SIZE - 1)
23320 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023321 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000023322 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000023323 }
23324
Bram Moolenaar071d4272004-06-13 20:20:40 +000023325 while (ga_scripts.ga_len < id)
23326 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023327 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023328 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023329 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023330 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023331 }
23332 }
23333}
23334
23335/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023336 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
23337 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023338 */
23339 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023340init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023341{
Bram Moolenaar33570922005-01-25 22:26:29 +000023342 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020023343 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023344 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023345 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023346 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023347 dict_var->di_tv.vval.v_dict = dict;
23348 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023349 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023350 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23351 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023352}
23353
23354/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020023355 * Unreference a dictionary initialized by init_var_dict().
23356 */
23357 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023358unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020023359{
23360 /* Now the dict needs to be freed if no one else is using it, go back to
23361 * normal reference counting. */
23362 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
23363 dict_unref(dict);
23364}
23365
23366/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023367 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000023368 * Frees all allocated variables and the value they contain.
23369 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023370 */
23371 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023372vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000023373{
23374 vars_clear_ext(ht, TRUE);
23375}
23376
23377/*
23378 * Like vars_clear(), but only free the value if "free_val" is TRUE.
23379 */
23380 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023381vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023382{
Bram Moolenaara7043832005-01-21 11:56:39 +000023383 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023384 hashitem_T *hi;
23385 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023386
Bram Moolenaar33570922005-01-25 22:26:29 +000023387 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023388 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000023389 for (hi = ht->ht_array; todo > 0; ++hi)
23390 {
23391 if (!HASHITEM_EMPTY(hi))
23392 {
23393 --todo;
23394
Bram Moolenaar33570922005-01-25 22:26:29 +000023395 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000023396 * ht_array might change then. hash_clear() takes care of it
23397 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023398 v = HI2DI(hi);
23399 if (free_val)
23400 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023401 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000023402 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000023403 }
23404 }
23405 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023406 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023407}
23408
Bram Moolenaara7043832005-01-21 11:56:39 +000023409/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023410 * Delete a variable from hashtab "ht" at item "hi".
23411 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000023412 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023413 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023414delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023415{
Bram Moolenaar33570922005-01-25 22:26:29 +000023416 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023417
23418 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000023419 clear_tv(&di->di_tv);
23420 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023421}
23422
23423/*
23424 * List the value of one internal variable.
23425 */
23426 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023427list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023428{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023429 char_u *tofree;
23430 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023431 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023432
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023433 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000023434 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023435 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023436 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023437}
23438
Bram Moolenaar071d4272004-06-13 20:20:40 +000023439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023440list_one_var_a(
23441 char_u *prefix,
23442 char_u *name,
23443 int type,
23444 char_u *string,
23445 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023446{
Bram Moolenaar31859182007-08-14 20:41:13 +000023447 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
23448 msg_start();
23449 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023450 if (name != NULL) /* "a:" vars don't have a name stored */
23451 msg_puts(name);
23452 msg_putchar(' ');
23453 msg_advance(22);
23454 if (type == VAR_NUMBER)
23455 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023456 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023457 msg_putchar('*');
23458 else if (type == VAR_LIST)
23459 {
23460 msg_putchar('[');
23461 if (*string == '[')
23462 ++string;
23463 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000023464 else if (type == VAR_DICT)
23465 {
23466 msg_putchar('{');
23467 if (*string == '{')
23468 ++string;
23469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023470 else
23471 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023472
Bram Moolenaar071d4272004-06-13 20:20:40 +000023473 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023474
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023475 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023476 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023477 if (*first)
23478 {
23479 msg_clr_eos();
23480 *first = FALSE;
23481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023482}
23483
23484/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023485 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023486 * If the variable already exists, the value is updated.
23487 * Otherwise the variable is created.
23488 */
23489 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023490set_var(
23491 char_u *name,
23492 typval_T *tv,
23493 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023494{
Bram Moolenaar33570922005-01-25 22:26:29 +000023495 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023496 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023497 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023498
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023499 ht = find_var_ht(name, &varname);
23500 if (ht == NULL || *varname == NUL)
23501 {
23502 EMSG2(_(e_illvar), name);
23503 return;
23504 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020023505 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023506
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023507 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
23508 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023509 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023510
Bram Moolenaar33570922005-01-25 22:26:29 +000023511 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023512 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023513 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023514 if (var_check_ro(v->di_flags, name, FALSE)
23515 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000023516 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023517
23518 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023519 * Handle setting internal v: variables separately where needed to
23520 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000023521 */
23522 if (ht == &vimvarht)
23523 {
23524 if (v->di_tv.v_type == VAR_STRING)
23525 {
23526 vim_free(v->di_tv.vval.v_string);
23527 if (copy || tv->v_type != VAR_STRING)
23528 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
23529 else
23530 {
23531 /* Take over the string to avoid an extra alloc/free. */
23532 v->di_tv.vval.v_string = tv->vval.v_string;
23533 tv->vval.v_string = NULL;
23534 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023535 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023536 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023537 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023538 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023539 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023540 if (STRCMP(varname, "searchforward") == 0)
23541 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010023542#ifdef FEAT_SEARCH_EXTRA
23543 else if (STRCMP(varname, "hlsearch") == 0)
23544 {
23545 no_hlsearch = !v->di_tv.vval.v_number;
23546 redraw_all_later(SOME_VALID);
23547 }
23548#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023549 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023550 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023551 else if (v->di_tv.v_type != tv->v_type)
23552 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000023553 }
23554
23555 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023556 }
23557 else /* add a new variable */
23558 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000023559 /* Can't add "v:" variable. */
23560 if (ht == &vimvarht)
23561 {
23562 EMSG2(_(e_illvar), name);
23563 return;
23564 }
23565
Bram Moolenaar92124a32005-06-17 22:03:40 +000023566 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023567 if (!valid_varname(varname))
23568 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000023569
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023570 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23571 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000023572 if (v == NULL)
23573 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023574 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023575 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023576 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023577 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023578 return;
23579 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023580 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023581 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023582
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023583 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023584 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023585 else
23586 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023587 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023588 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023589 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023591}
23592
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023593/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023594 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023595 * Also give an error message.
23596 */
23597 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023598var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023599{
23600 if (flags & DI_FLAGS_RO)
23601 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023602 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023603 return TRUE;
23604 }
23605 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23606 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023607 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023608 return TRUE;
23609 }
23610 return FALSE;
23611}
23612
23613/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023614 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23615 * Also give an error message.
23616 */
23617 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023618var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023619{
23620 if (flags & DI_FLAGS_FIX)
23621 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023622 EMSG2(_("E795: Cannot delete variable %s"),
23623 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023624 return TRUE;
23625 }
23626 return FALSE;
23627}
23628
23629/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023630 * Check if a funcref is assigned to a valid variable name.
23631 * Return TRUE and give an error if not.
23632 */
23633 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023634var_check_func_name(
23635 char_u *name, /* points to start of variable name */
23636 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023637{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023638 /* Allow for w: b: s: and t:. */
23639 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023640 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23641 ? name[2] : name[0]))
23642 {
23643 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23644 name);
23645 return TRUE;
23646 }
23647 /* Don't allow hiding a function. When "v" is not NULL we might be
23648 * assigning another function to the same var, the type is checked
23649 * below. */
23650 if (new_var && function_exists(name))
23651 {
23652 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23653 name);
23654 return TRUE;
23655 }
23656 return FALSE;
23657}
23658
23659/*
23660 * Check if a variable name is valid.
23661 * Return FALSE and give an error if not.
23662 */
23663 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023664valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023665{
23666 char_u *p;
23667
23668 for (p = varname; *p != NUL; ++p)
23669 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23670 && *p != AUTOLOAD_CHAR)
23671 {
23672 EMSG2(_(e_illvar), varname);
23673 return FALSE;
23674 }
23675 return TRUE;
23676}
23677
23678/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023679 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023680 * Also give an error message, using "name" or _("name") when use_gettext is
23681 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023682 */
23683 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023684tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023685{
23686 if (lock & VAR_LOCKED)
23687 {
23688 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023689 name == NULL ? (char_u *)_("Unknown")
23690 : use_gettext ? (char_u *)_(name)
23691 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023692 return TRUE;
23693 }
23694 if (lock & VAR_FIXED)
23695 {
23696 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023697 name == NULL ? (char_u *)_("Unknown")
23698 : use_gettext ? (char_u *)_(name)
23699 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023700 return TRUE;
23701 }
23702 return FALSE;
23703}
23704
23705/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023706 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023707 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023708 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023709 * It is OK for "from" and "to" to point to the same item. This is used to
23710 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023711 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023712 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023713copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023714{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023715 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023716 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023717 switch (from->v_type)
23718 {
23719 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023720 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023721 to->vval.v_number = from->vval.v_number;
23722 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023723 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023724#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023725 to->vval.v_float = from->vval.v_float;
23726 break;
23727#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023728 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023729#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023730 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023731 if (to->vval.v_job != NULL)
23732 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023733 break;
23734#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023735 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023736#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023737 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023738 if (to->vval.v_channel != NULL)
23739 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023740 break;
23741#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023742 case VAR_STRING:
23743 case VAR_FUNC:
23744 if (from->vval.v_string == NULL)
23745 to->vval.v_string = NULL;
23746 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023747 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023748 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023749 if (from->v_type == VAR_FUNC)
23750 func_ref(to->vval.v_string);
23751 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023752 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023753 case VAR_PARTIAL:
23754 if (from->vval.v_partial == NULL)
23755 to->vval.v_partial = NULL;
23756 else
23757 {
23758 to->vval.v_partial = from->vval.v_partial;
23759 ++to->vval.v_partial->pt_refcount;
23760 }
23761 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023762 case VAR_LIST:
23763 if (from->vval.v_list == NULL)
23764 to->vval.v_list = NULL;
23765 else
23766 {
23767 to->vval.v_list = from->vval.v_list;
23768 ++to->vval.v_list->lv_refcount;
23769 }
23770 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023771 case VAR_DICT:
23772 if (from->vval.v_dict == NULL)
23773 to->vval.v_dict = NULL;
23774 else
23775 {
23776 to->vval.v_dict = from->vval.v_dict;
23777 ++to->vval.v_dict->dv_refcount;
23778 }
23779 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023780 case VAR_UNKNOWN:
23781 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023782 break;
23783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023784}
23785
23786/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023787 * Make a copy of an item.
23788 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023789 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23790 * reference to an already copied list/dict can be used.
23791 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023792 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023793 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023794item_copy(
23795 typval_T *from,
23796 typval_T *to,
23797 int deep,
23798 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023799{
23800 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023801 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023802
Bram Moolenaar33570922005-01-25 22:26:29 +000023803 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023804 {
23805 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023806 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023807 }
23808 ++recurse;
23809
23810 switch (from->v_type)
23811 {
23812 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023813 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023814 case VAR_STRING:
23815 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023816 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010023817 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023818 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023819 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023820 copy_tv(from, to);
23821 break;
23822 case VAR_LIST:
23823 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023824 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023825 if (from->vval.v_list == NULL)
23826 to->vval.v_list = NULL;
23827 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23828 {
23829 /* use the copy made earlier */
23830 to->vval.v_list = from->vval.v_list->lv_copylist;
23831 ++to->vval.v_list->lv_refcount;
23832 }
23833 else
23834 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23835 if (to->vval.v_list == NULL)
23836 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023837 break;
23838 case VAR_DICT:
23839 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023840 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023841 if (from->vval.v_dict == NULL)
23842 to->vval.v_dict = NULL;
23843 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23844 {
23845 /* use the copy made earlier */
23846 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23847 ++to->vval.v_dict->dv_refcount;
23848 }
23849 else
23850 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23851 if (to->vval.v_dict == NULL)
23852 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023853 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023854 case VAR_UNKNOWN:
23855 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023856 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023857 }
23858 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023859 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023860}
23861
23862/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023863 * ":echo expr1 ..." print each argument separated with a space, add a
23864 * newline at the end.
23865 * ":echon expr1 ..." print each argument plain.
23866 */
23867 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023868ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023869{
23870 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023871 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023872 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023873 char_u *p;
23874 int needclr = TRUE;
23875 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023876 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023877
23878 if (eap->skip)
23879 ++emsg_skip;
23880 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23881 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023882 /* If eval1() causes an error message the text from the command may
23883 * still need to be cleared. E.g., "echo 22,44". */
23884 need_clr_eos = needclr;
23885
Bram Moolenaar071d4272004-06-13 20:20:40 +000023886 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023887 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023888 {
23889 /*
23890 * Report the invalid expression unless the expression evaluation
23891 * has been cancelled due to an aborting error, an interrupt, or an
23892 * exception.
23893 */
23894 if (!aborting())
23895 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023896 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023897 break;
23898 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023899 need_clr_eos = FALSE;
23900
Bram Moolenaar071d4272004-06-13 20:20:40 +000023901 if (!eap->skip)
23902 {
23903 if (atstart)
23904 {
23905 atstart = FALSE;
23906 /* Call msg_start() after eval1(), evaluating the expression
23907 * may cause a message to appear. */
23908 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023909 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023910 /* Mark the saved text as finishing the line, so that what
23911 * follows is displayed on a new line when scrolling back
23912 * at the more prompt. */
23913 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023914 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023916 }
23917 else if (eap->cmdidx == CMD_echo)
23918 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023919 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023920 if (p != NULL)
23921 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023922 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023923 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023924 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023925 if (*p != TAB && needclr)
23926 {
23927 /* remove any text still there from the command */
23928 msg_clr_eos();
23929 needclr = FALSE;
23930 }
23931 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023932 }
23933 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023934 {
23935#ifdef FEAT_MBYTE
23936 if (has_mbyte)
23937 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023938 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023939
23940 (void)msg_outtrans_len_attr(p, i, echo_attr);
23941 p += i - 1;
23942 }
23943 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023944#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023945 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023947 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023948 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023949 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023950 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023951 arg = skipwhite(arg);
23952 }
23953 eap->nextcmd = check_nextcmd(arg);
23954
23955 if (eap->skip)
23956 --emsg_skip;
23957 else
23958 {
23959 /* remove text that may still be there from the command */
23960 if (needclr)
23961 msg_clr_eos();
23962 if (eap->cmdidx == CMD_echo)
23963 msg_end();
23964 }
23965}
23966
23967/*
23968 * ":echohl {name}".
23969 */
23970 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023971ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023972{
23973 int id;
23974
23975 id = syn_name2id(eap->arg);
23976 if (id == 0)
23977 echo_attr = 0;
23978 else
23979 echo_attr = syn_id2attr(id);
23980}
23981
23982/*
23983 * ":execute expr1 ..." execute the result of an expression.
23984 * ":echomsg expr1 ..." Print a message
23985 * ":echoerr expr1 ..." Print an error
23986 * Each gets spaces around each argument and a newline at the end for
23987 * echo commands
23988 */
23989 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023990ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023991{
23992 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023993 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023994 int ret = OK;
23995 char_u *p;
23996 garray_T ga;
23997 int len;
23998 int save_did_emsg;
23999
24000 ga_init2(&ga, 1, 80);
24001
24002 if (eap->skip)
24003 ++emsg_skip;
24004 while (*arg != NUL && *arg != '|' && *arg != '\n')
24005 {
24006 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024007 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024008 {
24009 /*
24010 * Report the invalid expression unless the expression evaluation
24011 * has been cancelled due to an aborting error, an interrupt, or an
24012 * exception.
24013 */
24014 if (!aborting())
24015 EMSG2(_(e_invexpr2), p);
24016 ret = FAIL;
24017 break;
24018 }
24019
24020 if (!eap->skip)
24021 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024022 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024023 len = (int)STRLEN(p);
24024 if (ga_grow(&ga, len + 2) == FAIL)
24025 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024026 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024027 ret = FAIL;
24028 break;
24029 }
24030 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024031 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000024032 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024033 ga.ga_len += len;
24034 }
24035
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024036 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024037 arg = skipwhite(arg);
24038 }
24039
24040 if (ret != FAIL && ga.ga_data != NULL)
24041 {
24042 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000024043 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024044 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000024045 out_flush();
24046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024047 else if (eap->cmdidx == CMD_echoerr)
24048 {
24049 /* We don't want to abort following commands, restore did_emsg. */
24050 save_did_emsg = did_emsg;
24051 EMSG((char_u *)ga.ga_data);
24052 if (!force_abort)
24053 did_emsg = save_did_emsg;
24054 }
24055 else if (eap->cmdidx == CMD_execute)
24056 do_cmdline((char_u *)ga.ga_data,
24057 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
24058 }
24059
24060 ga_clear(&ga);
24061
24062 if (eap->skip)
24063 --emsg_skip;
24064
24065 eap->nextcmd = check_nextcmd(arg);
24066}
24067
24068/*
24069 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
24070 * "arg" points to the "&" or '+' when called, to "option" when returning.
24071 * Returns NULL when no option name found. Otherwise pointer to the char
24072 * after the option name.
24073 */
24074 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024075find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024076{
24077 char_u *p = *arg;
24078
24079 ++p;
24080 if (*p == 'g' && p[1] == ':')
24081 {
24082 *opt_flags = OPT_GLOBAL;
24083 p += 2;
24084 }
24085 else if (*p == 'l' && p[1] == ':')
24086 {
24087 *opt_flags = OPT_LOCAL;
24088 p += 2;
24089 }
24090 else
24091 *opt_flags = 0;
24092
24093 if (!ASCII_ISALPHA(*p))
24094 return NULL;
24095 *arg = p;
24096
24097 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
24098 p += 4; /* termcap option */
24099 else
24100 while (ASCII_ISALPHA(*p))
24101 ++p;
24102 return p;
24103}
24104
24105/*
24106 * ":function"
24107 */
24108 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024109ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024110{
24111 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024112 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024113 int j;
24114 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024115 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024116 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024117 char_u *name = NULL;
24118 char_u *p;
24119 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024120 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024121 garray_T newargs;
24122 garray_T newlines;
24123 int varargs = FALSE;
24124 int mustend = FALSE;
24125 int flags = 0;
24126 ufunc_T *fp;
24127 int indent;
24128 int nesting;
24129 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000024130 dictitem_T *v;
24131 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024132 static int func_nr = 0; /* number for nameless function */
24133 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024134 hashtab_T *ht;
24135 int todo;
24136 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024137 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024138
24139 /*
24140 * ":function" without argument: list functions.
24141 */
24142 if (ends_excmd(*eap->arg))
24143 {
24144 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024145 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024146 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000024147 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024148 {
24149 if (!HASHITEM_EMPTY(hi))
24150 {
24151 --todo;
24152 fp = HI2UF(hi);
24153 if (!isdigit(*fp->uf_name))
24154 list_func_head(fp, FALSE);
24155 }
24156 }
24157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024158 eap->nextcmd = check_nextcmd(eap->arg);
24159 return;
24160 }
24161
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024162 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024163 * ":function /pat": list functions matching pattern.
24164 */
24165 if (*eap->arg == '/')
24166 {
24167 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
24168 if (!eap->skip)
24169 {
24170 regmatch_T regmatch;
24171
24172 c = *p;
24173 *p = NUL;
24174 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
24175 *p = c;
24176 if (regmatch.regprog != NULL)
24177 {
24178 regmatch.rm_ic = p_ic;
24179
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024180 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024181 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
24182 {
24183 if (!HASHITEM_EMPTY(hi))
24184 {
24185 --todo;
24186 fp = HI2UF(hi);
24187 if (!isdigit(*fp->uf_name)
24188 && vim_regexec(&regmatch, fp->uf_name, 0))
24189 list_func_head(fp, FALSE);
24190 }
24191 }
Bram Moolenaar473de612013-06-08 18:19:48 +020024192 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024193 }
24194 }
24195 if (*p == '/')
24196 ++p;
24197 eap->nextcmd = check_nextcmd(p);
24198 return;
24199 }
24200
24201 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024202 * Get the function name. There are these situations:
24203 * func normal function name
24204 * "name" == func, "fudi.fd_dict" == NULL
24205 * dict.func new dictionary entry
24206 * "name" == NULL, "fudi.fd_dict" set,
24207 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
24208 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024209 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024210 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
24211 * dict.func existing dict entry that's not a Funcref
24212 * "name" == NULL, "fudi.fd_dict" set,
24213 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024214 * s:func script-local function name
24215 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024216 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024217 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010024218 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024219 paren = (vim_strchr(p, '(') != NULL);
24220 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024221 {
24222 /*
24223 * Return on an invalid expression in braces, unless the expression
24224 * evaluation has been cancelled due to an aborting error, an
24225 * interrupt, or an exception.
24226 */
24227 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024228 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024229 if (!eap->skip && fudi.fd_newkey != NULL)
24230 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024231 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024232 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024234 else
24235 eap->skip = TRUE;
24236 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000024237
Bram Moolenaar071d4272004-06-13 20:20:40 +000024238 /* An error in a function call during evaluation of an expression in magic
24239 * braces should not cause the function not to be defined. */
24240 saved_did_emsg = did_emsg;
24241 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024242
24243 /*
24244 * ":function func" with only function name: list function.
24245 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024246 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024247 {
24248 if (!ends_excmd(*skipwhite(p)))
24249 {
24250 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024251 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024252 }
24253 eap->nextcmd = check_nextcmd(p);
24254 if (eap->nextcmd != NULL)
24255 *p = NUL;
24256 if (!eap->skip && !got_int)
24257 {
24258 fp = find_func(name);
24259 if (fp != NULL)
24260 {
24261 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024262 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024263 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024264 if (FUNCLINE(fp, j) == NULL)
24265 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024266 msg_putchar('\n');
24267 msg_outnum((long)(j + 1));
24268 if (j < 9)
24269 msg_putchar(' ');
24270 if (j < 99)
24271 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024272 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024273 out_flush(); /* show a line at a time */
24274 ui_breakcheck();
24275 }
24276 if (!got_int)
24277 {
24278 msg_putchar('\n');
24279 msg_puts((char_u *)" endfunction");
24280 }
24281 }
24282 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024283 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024284 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024285 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024286 }
24287
24288 /*
24289 * ":function name(arg1, arg2)" Define function.
24290 */
24291 p = skipwhite(p);
24292 if (*p != '(')
24293 {
24294 if (!eap->skip)
24295 {
24296 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024297 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024298 }
24299 /* attempt to continue by skipping some text */
24300 if (vim_strchr(p, '(') != NULL)
24301 p = vim_strchr(p, '(');
24302 }
24303 p = skipwhite(p + 1);
24304
24305 ga_init2(&newargs, (int)sizeof(char_u *), 3);
24306 ga_init2(&newlines, (int)sizeof(char_u *), 3);
24307
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024308 if (!eap->skip)
24309 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024310 /* Check the name of the function. Unless it's a dictionary function
24311 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024312 if (name != NULL)
24313 arg = name;
24314 else
24315 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024316 if (arg != NULL && (fudi.fd_di == NULL
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +010024317 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
24318 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024319 {
24320 if (*arg == K_SPECIAL)
24321 j = 3;
24322 else
24323 j = 0;
24324 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
24325 : eval_isnamec(arg[j])))
24326 ++j;
24327 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000024328 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024329 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010024330 /* Disallow using the g: dict. */
24331 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
24332 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024333 }
24334
Bram Moolenaar071d4272004-06-13 20:20:40 +000024335 /*
24336 * Isolate the arguments: "arg1, arg2, ...)"
24337 */
24338 while (*p != ')')
24339 {
24340 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
24341 {
24342 varargs = TRUE;
24343 p += 3;
24344 mustend = TRUE;
24345 }
24346 else
24347 {
24348 arg = p;
24349 while (ASCII_ISALNUM(*p) || *p == '_')
24350 ++p;
24351 if (arg == p || isdigit(*arg)
24352 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
24353 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
24354 {
24355 if (!eap->skip)
24356 EMSG2(_("E125: Illegal argument: %s"), arg);
24357 break;
24358 }
24359 if (ga_grow(&newargs, 1) == FAIL)
24360 goto erret;
24361 c = *p;
24362 *p = NUL;
24363 arg = vim_strsave(arg);
24364 if (arg == NULL)
24365 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024366
24367 /* Check for duplicate argument name. */
24368 for (i = 0; i < newargs.ga_len; ++i)
24369 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
24370 {
24371 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010024372 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024373 goto erret;
24374 }
24375
Bram Moolenaar071d4272004-06-13 20:20:40 +000024376 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
24377 *p = c;
24378 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024379 if (*p == ',')
24380 ++p;
24381 else
24382 mustend = TRUE;
24383 }
24384 p = skipwhite(p);
24385 if (mustend && *p != ')')
24386 {
24387 if (!eap->skip)
24388 EMSG2(_(e_invarg2), eap->arg);
24389 break;
24390 }
24391 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020024392 if (*p != ')')
24393 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024394 ++p; /* skip the ')' */
24395
Bram Moolenaare9a41262005-01-15 22:18:47 +000024396 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024397 for (;;)
24398 {
24399 p = skipwhite(p);
24400 if (STRNCMP(p, "range", 5) == 0)
24401 {
24402 flags |= FC_RANGE;
24403 p += 5;
24404 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024405 else if (STRNCMP(p, "dict", 4) == 0)
24406 {
24407 flags |= FC_DICT;
24408 p += 4;
24409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024410 else if (STRNCMP(p, "abort", 5) == 0)
24411 {
24412 flags |= FC_ABORT;
24413 p += 5;
24414 }
24415 else
24416 break;
24417 }
24418
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024419 /* When there is a line break use what follows for the function body.
24420 * Makes 'exe "func Test()\n...\nendfunc"' work. */
24421 if (*p == '\n')
24422 line_arg = p + 1;
24423 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024424 EMSG(_(e_trailing));
24425
24426 /*
24427 * Read the body of the function, until ":endfunction" is found.
24428 */
24429 if (KeyTyped)
24430 {
24431 /* Check if the function already exists, don't let the user type the
24432 * whole function before telling him it doesn't work! For a script we
24433 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024434 if (!eap->skip && !eap->forceit)
24435 {
24436 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
24437 EMSG(_(e_funcdict));
24438 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024439 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024441
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024442 if (!eap->skip && did_emsg)
24443 goto erret;
24444
Bram Moolenaar071d4272004-06-13 20:20:40 +000024445 msg_putchar('\n'); /* don't overwrite the function name */
24446 cmdline_row = msg_row;
24447 }
24448
24449 indent = 2;
24450 nesting = 0;
24451 for (;;)
24452 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024453 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024454 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024455 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024456 saved_wait_return = FALSE;
24457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024458 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024459 sourcing_lnum_off = sourcing_lnum;
24460
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024461 if (line_arg != NULL)
24462 {
24463 /* Use eap->arg, split up in parts by line breaks. */
24464 theline = line_arg;
24465 p = vim_strchr(theline, '\n');
24466 if (p == NULL)
24467 line_arg += STRLEN(line_arg);
24468 else
24469 {
24470 *p = NUL;
24471 line_arg = p + 1;
24472 }
24473 }
24474 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024475 theline = getcmdline(':', 0L, indent);
24476 else
24477 theline = eap->getline(':', eap->cookie, indent);
24478 if (KeyTyped)
24479 lines_left = Rows - 1;
24480 if (theline == NULL)
24481 {
24482 EMSG(_("E126: Missing :endfunction"));
24483 goto erret;
24484 }
24485
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024486 /* Detect line continuation: sourcing_lnum increased more than one. */
24487 if (sourcing_lnum > sourcing_lnum_off + 1)
24488 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
24489 else
24490 sourcing_lnum_off = 0;
24491
Bram Moolenaar071d4272004-06-13 20:20:40 +000024492 if (skip_until != NULL)
24493 {
24494 /* between ":append" and "." and between ":python <<EOF" and "EOF"
24495 * don't check for ":endfunc". */
24496 if (STRCMP(theline, skip_until) == 0)
24497 {
24498 vim_free(skip_until);
24499 skip_until = NULL;
24500 }
24501 }
24502 else
24503 {
24504 /* skip ':' and blanks*/
24505 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
24506 ;
24507
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024508 /* Check for "endfunction". */
24509 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024510 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024511 if (line_arg == NULL)
24512 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024513 break;
24514 }
24515
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024516 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000024517 * at "end". */
24518 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
24519 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024520 else if (STRNCMP(p, "if", 2) == 0
24521 || STRNCMP(p, "wh", 2) == 0
24522 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000024523 || STRNCMP(p, "try", 3) == 0)
24524 indent += 2;
24525
24526 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024527 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024528 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024529 if (*p == '!')
24530 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024531 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010024532 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010024533 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000024534 {
Bram Moolenaaref923902014-12-13 21:00:55 +010024535 ++nesting;
24536 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024537 }
24538 }
24539
24540 /* Check for ":append" or ":insert". */
24541 p = skip_range(p, NULL);
24542 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
24543 || (p[0] == 'i'
24544 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
24545 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
24546 skip_until = vim_strsave((char_u *)".");
24547
24548 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
24549 arg = skipwhite(skiptowhite(p));
24550 if (arg[0] == '<' && arg[1] =='<'
24551 && ((p[0] == 'p' && p[1] == 'y'
24552 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
24553 || (p[0] == 'p' && p[1] == 'e'
24554 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
24555 || (p[0] == 't' && p[1] == 'c'
24556 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020024557 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
24558 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024559 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
24560 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024561 || (p[0] == 'm' && p[1] == 'z'
24562 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024563 ))
24564 {
24565 /* ":python <<" continues until a dot, like ":append" */
24566 p = skipwhite(arg + 2);
24567 if (*p == NUL)
24568 skip_until = vim_strsave((char_u *)".");
24569 else
24570 skip_until = vim_strsave(p);
24571 }
24572 }
24573
24574 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024575 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024576 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024577 if (line_arg == NULL)
24578 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024579 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024580 }
24581
24582 /* Copy the line to newly allocated memory. get_one_sourceline()
24583 * allocates 250 bytes per line, this saves 80% on average. The cost
24584 * is an extra alloc/free. */
24585 p = vim_strsave(theline);
24586 if (p != NULL)
24587 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024588 if (line_arg == NULL)
24589 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024590 theline = p;
24591 }
24592
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024593 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24594
24595 /* Add NULL lines for continuation lines, so that the line count is
24596 * equal to the index in the growarray. */
24597 while (sourcing_lnum_off-- > 0)
24598 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024599
24600 /* Check for end of eap->arg. */
24601 if (line_arg != NULL && *line_arg == NUL)
24602 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024603 }
24604
24605 /* Don't define the function when skipping commands or when an error was
24606 * detected. */
24607 if (eap->skip || did_emsg)
24608 goto erret;
24609
24610 /*
24611 * If there are no errors, add the function
24612 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024613 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024614 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024615 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024616 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024617 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024618 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024619 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024620 goto erret;
24621 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024622
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024623 fp = find_func(name);
24624 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024625 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024626 if (!eap->forceit)
24627 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024628 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024629 goto erret;
24630 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024631 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024632 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024633 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024634 name);
24635 goto erret;
24636 }
24637 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024638 ga_clear_strings(&(fp->uf_args));
24639 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024640 vim_free(name);
24641 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024643 }
24644 else
24645 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024646 char numbuf[20];
24647
24648 fp = NULL;
24649 if (fudi.fd_newkey == NULL && !eap->forceit)
24650 {
24651 EMSG(_(e_funcdict));
24652 goto erret;
24653 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024654 if (fudi.fd_di == NULL)
24655 {
24656 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024657 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024658 goto erret;
24659 }
24660 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024661 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024662 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024663
24664 /* Give the function a sequential number. Can only be used with a
24665 * Funcref! */
24666 vim_free(name);
24667 sprintf(numbuf, "%d", ++func_nr);
24668 name = vim_strsave((char_u *)numbuf);
24669 if (name == NULL)
24670 goto erret;
24671 }
24672
24673 if (fp == NULL)
24674 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024675 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024676 {
24677 int slen, plen;
24678 char_u *scriptname;
24679
24680 /* Check that the autoload name matches the script name. */
24681 j = FAIL;
24682 if (sourcing_name != NULL)
24683 {
24684 scriptname = autoload_name(name);
24685 if (scriptname != NULL)
24686 {
24687 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024688 plen = (int)STRLEN(p);
24689 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024690 if (slen > plen && fnamecmp(p,
24691 sourcing_name + slen - plen) == 0)
24692 j = OK;
24693 vim_free(scriptname);
24694 }
24695 }
24696 if (j == FAIL)
24697 {
24698 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24699 goto erret;
24700 }
24701 }
24702
24703 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024704 if (fp == NULL)
24705 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024706
24707 if (fudi.fd_dict != NULL)
24708 {
24709 if (fudi.fd_di == NULL)
24710 {
24711 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024712 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024713 if (fudi.fd_di == NULL)
24714 {
24715 vim_free(fp);
24716 goto erret;
24717 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024718 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24719 {
24720 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024721 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024722 goto erret;
24723 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024724 }
24725 else
24726 /* overwrite existing dict entry */
24727 clear_tv(&fudi.fd_di->di_tv);
24728 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024729 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024730 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024731 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024732
24733 /* behave like "dict" was used */
24734 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024735 }
24736
Bram Moolenaar071d4272004-06-13 20:20:40 +000024737 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024738 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024739 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24740 {
24741 vim_free(fp);
24742 goto erret;
24743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024744 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024745 fp->uf_args = newargs;
24746 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024747#ifdef FEAT_PROFILE
24748 fp->uf_tml_count = NULL;
24749 fp->uf_tml_total = NULL;
24750 fp->uf_tml_self = NULL;
24751 fp->uf_profiling = FALSE;
24752 if (prof_def_func())
24753 func_do_profile(fp);
24754#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024755 fp->uf_varargs = varargs;
24756 fp->uf_flags = flags;
24757 fp->uf_calls = 0;
24758 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024759 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024760
24761erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024762 ga_clear_strings(&newargs);
24763 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024764ret_free:
24765 vim_free(skip_until);
24766 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024767 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024768 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024769 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024770}
24771
24772/*
24773 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024774 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024775 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024776 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024777 * TFN_INT: internal function name OK
24778 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024779 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024780 * Advances "pp" to just after the function name (if no error).
24781 */
24782 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024783trans_function_name(
24784 char_u **pp,
24785 int skip, /* only find the end, don't evaluate */
24786 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010024787 funcdict_T *fdp, /* return: info about dictionary used */
24788 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024789{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024790 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024791 char_u *start;
24792 char_u *end;
24793 int lead;
24794 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024795 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024796 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024797
24798 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024799 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024800 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024801
24802 /* Check for hard coded <SNR>: already translated function ID (from a user
24803 * command). */
24804 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24805 && (*pp)[2] == (int)KE_SNR)
24806 {
24807 *pp += 3;
24808 len = get_id_len(pp) + 3;
24809 return vim_strnsave(start, len);
24810 }
24811
24812 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24813 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024814 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024815 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024816 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024817
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024818 /* Note that TFN_ flags use the same values as GLV_ flags. */
24819 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024820 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024821 if (end == start)
24822 {
24823 if (!skip)
24824 EMSG(_("E129: Function name required"));
24825 goto theend;
24826 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024827 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024828 {
24829 /*
24830 * Report an invalid expression in braces, unless the expression
24831 * evaluation has been cancelled due to an aborting error, an
24832 * interrupt, or an exception.
24833 */
24834 if (!aborting())
24835 {
24836 if (end != NULL)
24837 EMSG2(_(e_invarg2), start);
24838 }
24839 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024840 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024841 goto theend;
24842 }
24843
24844 if (lv.ll_tv != NULL)
24845 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024846 if (fdp != NULL)
24847 {
24848 fdp->fd_dict = lv.ll_dict;
24849 fdp->fd_newkey = lv.ll_newkey;
24850 lv.ll_newkey = NULL;
24851 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024852 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024853 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24854 {
24855 name = vim_strsave(lv.ll_tv->vval.v_string);
24856 *pp = end;
24857 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010024858 else if (lv.ll_tv->v_type == VAR_PARTIAL
24859 && lv.ll_tv->vval.v_partial != NULL)
24860 {
24861 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
24862 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010024863 if (partial != NULL)
24864 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010024865 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024866 else
24867 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024868 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24869 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024870 EMSG(_(e_funcref));
24871 else
24872 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024873 name = NULL;
24874 }
24875 goto theend;
24876 }
24877
24878 if (lv.ll_name == NULL)
24879 {
24880 /* Error found, but continue after the function name. */
24881 *pp = end;
24882 goto theend;
24883 }
24884
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024885 /* Check if the name is a Funcref. If so, use the value. */
24886 if (lv.ll_exp_name != NULL)
24887 {
24888 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010024889 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010024890 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024891 if (name == lv.ll_exp_name)
24892 name = NULL;
24893 }
24894 else
24895 {
24896 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010024897 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024898 if (name == *pp)
24899 name = NULL;
24900 }
24901 if (name != NULL)
24902 {
24903 name = vim_strsave(name);
24904 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024905 if (STRNCMP(name, "<SNR>", 5) == 0)
24906 {
24907 /* Change "<SNR>" to the byte sequence. */
24908 name[0] = K_SPECIAL;
24909 name[1] = KS_EXTRA;
24910 name[2] = (int)KE_SNR;
24911 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24912 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024913 goto theend;
24914 }
24915
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024916 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024917 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024918 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024919 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24920 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24921 {
24922 /* When there was "s:" already or the name expanded to get a
24923 * leading "s:" then remove it. */
24924 lv.ll_name += 2;
24925 len -= 2;
24926 lead = 2;
24927 }
24928 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024929 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024930 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024931 /* skip over "s:" and "g:" */
24932 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024933 lv.ll_name += 2;
24934 len = (int)(end - lv.ll_name);
24935 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024936
24937 /*
24938 * Copy the function name to allocated memory.
24939 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24940 * Accept <SNR>123_name() outside a script.
24941 */
24942 if (skip)
24943 lead = 0; /* do nothing */
24944 else if (lead > 0)
24945 {
24946 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024947 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24948 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024949 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024950 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024951 if (current_SID <= 0)
24952 {
24953 EMSG(_(e_usingsid));
24954 goto theend;
24955 }
24956 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24957 lead += (int)STRLEN(sid_buf);
24958 }
24959 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024960 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024961 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024962 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024963 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024964 goto theend;
24965 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024966 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024967 {
24968 char_u *cp = vim_strchr(lv.ll_name, ':');
24969
24970 if (cp != NULL && cp < end)
24971 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024972 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024973 goto theend;
24974 }
24975 }
24976
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024977 name = alloc((unsigned)(len + lead + 1));
24978 if (name != NULL)
24979 {
24980 if (lead > 0)
24981 {
24982 name[0] = K_SPECIAL;
24983 name[1] = KS_EXTRA;
24984 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024985 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024986 STRCPY(name + 3, sid_buf);
24987 }
24988 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024989 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024990 }
24991 *pp = end;
24992
24993theend:
24994 clear_lval(&lv);
24995 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024996}
24997
24998/*
24999 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
25000 * Return 2 if "p" starts with "s:".
25001 * Return 0 otherwise.
25002 */
25003 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025004eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025005{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010025006 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
25007 * the standard library function. */
25008 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
25009 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025010 return 5;
25011 if (p[0] == 's' && p[1] == ':')
25012 return 2;
25013 return 0;
25014}
25015
25016/*
25017 * Return TRUE if "p" starts with "<SID>" or "s:".
25018 * Only works if eval_fname_script() returned non-zero for "p"!
25019 */
25020 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025021eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025022{
25023 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
25024}
25025
25026/*
25027 * List the head of the function: "name(arg1, arg2)".
25028 */
25029 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025030list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025031{
25032 int j;
25033
25034 msg_start();
25035 if (indent)
25036 MSG_PUTS(" ");
25037 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025038 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025039 {
25040 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025041 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025042 }
25043 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025044 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025045 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025046 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025047 {
25048 if (j)
25049 MSG_PUTS(", ");
25050 msg_puts(FUNCARG(fp, j));
25051 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025052 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025053 {
25054 if (j)
25055 MSG_PUTS(", ");
25056 MSG_PUTS("...");
25057 }
25058 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020025059 if (fp->uf_flags & FC_ABORT)
25060 MSG_PUTS(" abort");
25061 if (fp->uf_flags & FC_RANGE)
25062 MSG_PUTS(" range");
25063 if (fp->uf_flags & FC_DICT)
25064 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025065 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000025066 if (p_verbose > 0)
25067 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025068}
25069
25070/*
25071 * Find a function by name, return pointer to it in ufuncs.
25072 * Return NULL for unknown function.
25073 */
25074 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025075find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025076{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025077 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025078
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025079 hi = hash_find(&func_hashtab, name);
25080 if (!HASHITEM_EMPTY(hi))
25081 return HI2UF(hi);
25082 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025083}
25084
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000025085#if defined(EXITFREE) || defined(PROTO)
25086 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025087free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000025088{
25089 hashitem_T *hi;
25090
25091 /* Need to start all over every time, because func_free() may change the
25092 * hash table. */
25093 while (func_hashtab.ht_used > 0)
25094 for (hi = func_hashtab.ht_array; ; ++hi)
25095 if (!HASHITEM_EMPTY(hi))
25096 {
25097 func_free(HI2UF(hi));
25098 break;
25099 }
25100}
25101#endif
25102
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025103 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025104translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025105{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025106 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025107 return find_internal_func(name) >= 0;
25108 return find_func(name) != NULL;
25109}
25110
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025111/*
25112 * Return TRUE if a function "name" exists.
25113 */
25114 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025115function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025116{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000025117 char_u *nm = name;
25118 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025119 int n = FALSE;
25120
Bram Moolenaar6d977d62014-01-14 15:24:39 +010025121 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010025122 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000025123 nm = skipwhite(nm);
25124
25125 /* Only accept "funcname", "funcname ", "funcname (..." and
25126 * "funcname(...", not "funcname!...". */
25127 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025128 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000025129 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025130 return n;
25131}
25132
Bram Moolenaara1544c02013-05-30 12:35:52 +020025133 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025134get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020025135{
25136 char_u *nm = name;
25137 char_u *p;
25138
Bram Moolenaar65639032016-03-16 21:40:30 +010025139 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020025140
25141 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025142 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020025143 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025144
Bram Moolenaara1544c02013-05-30 12:35:52 +020025145 vim_free(p);
25146 return NULL;
25147}
25148
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025149/*
25150 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025151 * lower case letter and doesn't contain AUTOLOAD_CHAR.
25152 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025153 */
25154 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025155builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025156{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025157 char_u *p;
25158
25159 if (!ASCII_ISLOWER(name[0]))
25160 return FALSE;
25161 p = vim_strchr(name, AUTOLOAD_CHAR);
25162 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025163}
25164
Bram Moolenaar05159a02005-02-26 23:04:13 +000025165#if defined(FEAT_PROFILE) || defined(PROTO)
25166/*
25167 * Start profiling function "fp".
25168 */
25169 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025170func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025171{
Bram Moolenaar904c6222010-07-24 16:57:39 +020025172 int len = fp->uf_lines.ga_len;
25173
25174 if (len == 0)
25175 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025176 fp->uf_tm_count = 0;
25177 profile_zero(&fp->uf_tm_self);
25178 profile_zero(&fp->uf_tm_total);
25179 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025180 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025181 if (fp->uf_tml_total == NULL)
25182 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025183 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025184 if (fp->uf_tml_self == NULL)
25185 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025186 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025187 fp->uf_tml_idx = -1;
25188 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
25189 || fp->uf_tml_self == NULL)
25190 return; /* out of memory */
25191
25192 fp->uf_profiling = TRUE;
25193}
25194
25195/*
25196 * Dump the profiling results for all functions in file "fd".
25197 */
25198 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025199func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025200{
25201 hashitem_T *hi;
25202 int todo;
25203 ufunc_T *fp;
25204 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000025205 ufunc_T **sorttab;
25206 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025207
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025208 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000025209 if (todo == 0)
25210 return; /* nothing to dump */
25211
Bram Moolenaare2e4b982015-06-09 20:30:51 +020025212 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000025213
Bram Moolenaar05159a02005-02-26 23:04:13 +000025214 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
25215 {
25216 if (!HASHITEM_EMPTY(hi))
25217 {
25218 --todo;
25219 fp = HI2UF(hi);
25220 if (fp->uf_profiling)
25221 {
Bram Moolenaar73830342005-02-28 22:48:19 +000025222 if (sorttab != NULL)
25223 sorttab[st_len++] = fp;
25224
Bram Moolenaar05159a02005-02-26 23:04:13 +000025225 if (fp->uf_name[0] == K_SPECIAL)
25226 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
25227 else
25228 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
25229 if (fp->uf_tm_count == 1)
25230 fprintf(fd, "Called 1 time\n");
25231 else
25232 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
25233 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
25234 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
25235 fprintf(fd, "\n");
25236 fprintf(fd, "count total (s) self (s)\n");
25237
25238 for (i = 0; i < fp->uf_lines.ga_len; ++i)
25239 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025240 if (FUNCLINE(fp, i) == NULL)
25241 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000025242 prof_func_line(fd, fp->uf_tml_count[i],
25243 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025244 fprintf(fd, "%s\n", FUNCLINE(fp, i));
25245 }
25246 fprintf(fd, "\n");
25247 }
25248 }
25249 }
Bram Moolenaar73830342005-02-28 22:48:19 +000025250
25251 if (sorttab != NULL && st_len > 0)
25252 {
25253 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
25254 prof_total_cmp);
25255 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
25256 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
25257 prof_self_cmp);
25258 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
25259 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000025260
25261 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025262}
Bram Moolenaar73830342005-02-28 22:48:19 +000025263
25264 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025265prof_sort_list(
25266 FILE *fd,
25267 ufunc_T **sorttab,
25268 int st_len,
25269 char *title,
25270 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000025271{
25272 int i;
25273 ufunc_T *fp;
25274
25275 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
25276 fprintf(fd, "count total (s) self (s) function\n");
25277 for (i = 0; i < 20 && i < st_len; ++i)
25278 {
25279 fp = sorttab[i];
25280 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
25281 prefer_self);
25282 if (fp->uf_name[0] == K_SPECIAL)
25283 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
25284 else
25285 fprintf(fd, " %s()\n", fp->uf_name);
25286 }
25287 fprintf(fd, "\n");
25288}
25289
25290/*
25291 * Print the count and times for one function or function line.
25292 */
25293 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025294prof_func_line(
25295 FILE *fd,
25296 int count,
25297 proftime_T *total,
25298 proftime_T *self,
25299 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000025300{
25301 if (count > 0)
25302 {
25303 fprintf(fd, "%5d ", count);
25304 if (prefer_self && profile_equal(total, self))
25305 fprintf(fd, " ");
25306 else
25307 fprintf(fd, "%s ", profile_msg(total));
25308 if (!prefer_self && profile_equal(total, self))
25309 fprintf(fd, " ");
25310 else
25311 fprintf(fd, "%s ", profile_msg(self));
25312 }
25313 else
25314 fprintf(fd, " ");
25315}
25316
25317/*
25318 * Compare function for total time sorting.
25319 */
25320 static int
25321#ifdef __BORLANDC__
25322_RTLENTRYF
25323#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025324prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025325{
25326 ufunc_T *p1, *p2;
25327
25328 p1 = *(ufunc_T **)s1;
25329 p2 = *(ufunc_T **)s2;
25330 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
25331}
25332
25333/*
25334 * Compare function for self time sorting.
25335 */
25336 static int
25337#ifdef __BORLANDC__
25338_RTLENTRYF
25339#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025340prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025341{
25342 ufunc_T *p1, *p2;
25343
25344 p1 = *(ufunc_T **)s1;
25345 p2 = *(ufunc_T **)s2;
25346 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
25347}
25348
Bram Moolenaar05159a02005-02-26 23:04:13 +000025349#endif
25350
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025351/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025352 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025353 * Return TRUE if a package was loaded.
25354 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020025355 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025356script_autoload(
25357 char_u *name,
25358 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025359{
25360 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025361 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025362 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025363 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025364
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025365 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025366 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025367 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025368 return FALSE;
25369
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025370 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025371
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025372 /* Find the name in the list of previously loaded package names. Skip
25373 * "autoload/", it's always the same. */
25374 for (i = 0; i < ga_loaded.ga_len; ++i)
25375 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
25376 break;
25377 if (!reload && i < ga_loaded.ga_len)
25378 ret = FALSE; /* was loaded already */
25379 else
25380 {
25381 /* Remember the name if it wasn't loaded already. */
25382 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
25383 {
25384 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
25385 tofree = NULL;
25386 }
25387
25388 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010025389 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025390 ret = TRUE;
25391 }
25392
25393 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025394 return ret;
25395}
25396
25397/*
25398 * Return the autoload script name for a function or variable name.
25399 * Returns NULL when out of memory.
25400 */
25401 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025402autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025403{
25404 char_u *p;
25405 char_u *scriptname;
25406
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025407 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025408 scriptname = alloc((unsigned)(STRLEN(name) + 14));
25409 if (scriptname == NULL)
25410 return FALSE;
25411 STRCPY(scriptname, "autoload/");
25412 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025413 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025414 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025415 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025416 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025417 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025418}
25419
Bram Moolenaar071d4272004-06-13 20:20:40 +000025420#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
25421
25422/*
25423 * Function given to ExpandGeneric() to obtain the list of user defined
25424 * function names.
25425 */
25426 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025427get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025428{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025429 static long_u done;
25430 static hashitem_T *hi;
25431 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025432
25433 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025434 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025435 done = 0;
25436 hi = func_hashtab.ht_array;
25437 }
25438 if (done < func_hashtab.ht_used)
25439 {
25440 if (done++ > 0)
25441 ++hi;
25442 while (HASHITEM_EMPTY(hi))
25443 ++hi;
25444 fp = HI2UF(hi);
25445
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025446 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010025447 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025448
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025449 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
25450 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025451
25452 cat_func_name(IObuff, fp);
25453 if (xp->xp_context != EXPAND_USER_FUNC)
25454 {
25455 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025456 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025457 STRCAT(IObuff, ")");
25458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025459 return IObuff;
25460 }
25461 return NULL;
25462}
25463
25464#endif /* FEAT_CMDL_COMPL */
25465
25466/*
25467 * Copy the function name of "fp" to buffer "buf".
25468 * "buf" must be able to hold the function name plus three bytes.
25469 * Takes care of script-local function names.
25470 */
25471 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025472cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025473{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025474 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025475 {
25476 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025477 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025478 }
25479 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025480 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025481}
25482
25483/*
25484 * ":delfunction {name}"
25485 */
25486 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025487ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025488{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025489 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025490 char_u *p;
25491 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000025492 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025493
25494 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010025495 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025496 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025497 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025498 {
25499 if (fudi.fd_dict != NULL && !eap->skip)
25500 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000025501 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025503 if (!ends_excmd(*skipwhite(p)))
25504 {
25505 vim_free(name);
25506 EMSG(_(e_trailing));
25507 return;
25508 }
25509 eap->nextcmd = check_nextcmd(p);
25510 if (eap->nextcmd != NULL)
25511 *p = NUL;
25512
25513 if (!eap->skip)
25514 fp = find_func(name);
25515 vim_free(name);
25516
25517 if (!eap->skip)
25518 {
25519 if (fp == NULL)
25520 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025521 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025522 return;
25523 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025524 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025525 {
25526 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
25527 return;
25528 }
25529
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025530 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025531 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025532 /* Delete the dict item that refers to the function, it will
25533 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000025534 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025535 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025536 else
25537 func_free(fp);
25538 }
25539}
25540
25541/*
25542 * Free a function and remove it from the list of functions.
25543 */
25544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025545func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025546{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025547 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025548
25549 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025550 ga_clear_strings(&(fp->uf_args));
25551 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025552#ifdef FEAT_PROFILE
25553 vim_free(fp->uf_tml_count);
25554 vim_free(fp->uf_tml_total);
25555 vim_free(fp->uf_tml_self);
25556#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025557
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025558 /* remove the function from the function hashtable */
25559 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
25560 if (HASHITEM_EMPTY(hi))
25561 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025562 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025563 hash_remove(&func_hashtab, hi);
25564
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025565 vim_free(fp);
25566}
25567
25568/*
25569 * Unreference a Function: decrement the reference count and free it when it
25570 * becomes zero. Only for numbered functions.
25571 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025572 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025573func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025574{
25575 ufunc_T *fp;
25576
25577 if (name != NULL && isdigit(*name))
25578 {
25579 fp = find_func(name);
25580 if (fp == NULL)
Bram Moolenaara9673212016-06-01 22:21:06 +020025581 {
Bram Moolenaarb89a25f2016-06-01 23:08:39 +020025582#ifdef EXITFREE
25583 if (!entered_free_all_mem)
25584#endif
Bram Moolenaara9673212016-06-01 22:21:06 +020025585 EMSG2(_(e_intern2), "func_unref()");
25586 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025587 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025588 {
25589 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025590 * when "uf_calls" becomes zero. */
25591 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025592 func_free(fp);
25593 }
25594 }
25595}
25596
25597/*
25598 * Count a reference to a Function.
25599 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025600 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025601func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025602{
25603 ufunc_T *fp;
25604
25605 if (name != NULL && isdigit(*name))
25606 {
25607 fp = find_func(name);
25608 if (fp == NULL)
25609 EMSG2(_(e_intern2), "func_ref()");
25610 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025611 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025612 }
25613}
25614
25615/*
25616 * Call a user function.
25617 */
25618 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025619call_user_func(
25620 ufunc_T *fp, /* pointer to function */
25621 int argcount, /* nr of args */
25622 typval_T *argvars, /* arguments */
25623 typval_T *rettv, /* return value */
25624 linenr_T firstline, /* first line of range */
25625 linenr_T lastline, /* last line of range */
25626 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025627{
Bram Moolenaar33570922005-01-25 22:26:29 +000025628 char_u *save_sourcing_name;
25629 linenr_T save_sourcing_lnum;
25630 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025631 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025632 int save_did_emsg;
25633 static int depth = 0;
25634 dictitem_T *v;
25635 int fixvar_idx = 0; /* index in fixvar[] */
25636 int i;
25637 int ai;
25638 char_u numbuf[NUMBUFLEN];
25639 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025640 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025641#ifdef FEAT_PROFILE
25642 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025643 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025644#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025645
25646 /* If depth of calling is getting too high, don't execute the function */
25647 if (depth >= p_mfd)
25648 {
25649 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025650 rettv->v_type = VAR_NUMBER;
25651 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025652 return;
25653 }
25654 ++depth;
25655
25656 line_breakcheck(); /* check for CTRL-C hit */
25657
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025658 fc = (funccall_T *)alloc(sizeof(funccall_T));
25659 fc->caller = current_funccal;
25660 current_funccal = fc;
25661 fc->func = fp;
25662 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025663 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025664 fc->linenr = 0;
25665 fc->returned = FALSE;
25666 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025667 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025668 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25669 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025670
Bram Moolenaar33570922005-01-25 22:26:29 +000025671 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025672 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025673 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25674 * each argument variable and saves a lot of time.
25675 */
25676 /*
25677 * Init l: variables.
25678 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025679 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025680 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025681 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025682 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25683 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025684 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025685 name = v->di_key;
25686 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025687 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025688 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025689 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025690 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025691 v->di_tv.vval.v_dict = selfdict;
25692 ++selfdict->dv_refcount;
25693 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025694
Bram Moolenaar33570922005-01-25 22:26:29 +000025695 /*
25696 * Init a: variables.
25697 * Set a:0 to "argcount".
25698 * Set a:000 to a list with room for the "..." arguments.
25699 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025700 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025701 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025702 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025703 /* Use "name" to avoid a warning from some compiler that checks the
25704 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025705 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025706 name = v->di_key;
25707 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025708 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025709 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025710 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025711 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025712 v->di_tv.vval.v_list = &fc->l_varlist;
25713 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25714 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25715 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025716
25717 /*
25718 * Set a:firstline to "firstline" and a:lastline to "lastline".
25719 * Set a:name to named arguments.
25720 * Set a:N to the "..." arguments.
25721 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025722 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025723 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025724 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025725 (varnumber_T)lastline);
25726 for (i = 0; i < argcount; ++i)
25727 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025728 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025729 if (ai < 0)
25730 /* named argument a:name */
25731 name = FUNCARG(fp, i);
25732 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025733 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025734 /* "..." argument a:1, a:2, etc. */
25735 sprintf((char *)numbuf, "%d", ai + 1);
25736 name = numbuf;
25737 }
25738 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25739 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025740 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025741 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25742 }
25743 else
25744 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025745 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25746 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025747 if (v == NULL)
25748 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025749 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025750 }
25751 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025752 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025753
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025754 /* Note: the values are copied directly to avoid alloc/free.
25755 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025756 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025757 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025758
25759 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25760 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025761 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25762 fc->l_listitems[ai].li_tv = argvars[i];
25763 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025764 }
25765 }
25766
Bram Moolenaar071d4272004-06-13 20:20:40 +000025767 /* Don't redraw while executing the function. */
25768 ++RedrawingDisabled;
25769 save_sourcing_name = sourcing_name;
25770 save_sourcing_lnum = sourcing_lnum;
25771 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025772 /* need space for function name + ("function " + 3) or "[number]" */
25773 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25774 + STRLEN(fp->uf_name) + 20;
25775 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025776 if (sourcing_name != NULL)
25777 {
25778 if (save_sourcing_name != NULL
25779 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025780 sprintf((char *)sourcing_name, "%s[%d]..",
25781 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025782 else
25783 STRCPY(sourcing_name, "function ");
25784 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25785
25786 if (p_verbose >= 12)
25787 {
25788 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025789 verbose_enter_scroll();
25790
Bram Moolenaar555b2802005-05-19 21:08:39 +000025791 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025792 if (p_verbose >= 14)
25793 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025794 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025795 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025796 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025797 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025798
25799 msg_puts((char_u *)"(");
25800 for (i = 0; i < argcount; ++i)
25801 {
25802 if (i > 0)
25803 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025804 if (argvars[i].v_type == VAR_NUMBER)
25805 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025806 else
25807 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025808 /* Do not want errors such as E724 here. */
25809 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025810 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025811 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025812 if (s != NULL)
25813 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025814 if (vim_strsize(s) > MSG_BUF_CLEN)
25815 {
25816 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25817 s = buf;
25818 }
25819 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025820 vim_free(tofree);
25821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025822 }
25823 }
25824 msg_puts((char_u *)")");
25825 }
25826 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025827
25828 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025829 --no_wait_return;
25830 }
25831 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025832#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025833 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025834 {
25835 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25836 func_do_profile(fp);
25837 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025838 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025839 {
25840 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025841 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025842 profile_zero(&fp->uf_tm_children);
25843 }
25844 script_prof_save(&wait_start);
25845 }
25846#endif
25847
Bram Moolenaar071d4272004-06-13 20:20:40 +000025848 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025849 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025850 save_did_emsg = did_emsg;
25851 did_emsg = FALSE;
25852
25853 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025854 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025855 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25856
25857 --RedrawingDisabled;
25858
25859 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025860 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025861 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025862 clear_tv(rettv);
25863 rettv->v_type = VAR_NUMBER;
25864 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025865 }
25866
Bram Moolenaar05159a02005-02-26 23:04:13 +000025867#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025868 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025869 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025870 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025871 profile_end(&call_start);
25872 profile_sub_wait(&wait_start, &call_start);
25873 profile_add(&fp->uf_tm_total, &call_start);
25874 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025875 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025876 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025877 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25878 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025879 }
25880 }
25881#endif
25882
Bram Moolenaar071d4272004-06-13 20:20:40 +000025883 /* when being verbose, mention the return value */
25884 if (p_verbose >= 12)
25885 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025886 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025887 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025888
Bram Moolenaar071d4272004-06-13 20:20:40 +000025889 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025890 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025891 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025892 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025893 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025894 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025895 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025896 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025897 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025898 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025899 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025900
Bram Moolenaar555b2802005-05-19 21:08:39 +000025901 /* The value may be very long. Skip the middle part, so that we
25902 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025903 * truncate it at the end. Don't want errors such as E724 here. */
25904 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025905 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025906 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025907 if (s != NULL)
25908 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025909 if (vim_strsize(s) > MSG_BUF_CLEN)
25910 {
25911 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25912 s = buf;
25913 }
25914 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025915 vim_free(tofree);
25916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025917 }
25918 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025919
25920 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025921 --no_wait_return;
25922 }
25923
25924 vim_free(sourcing_name);
25925 sourcing_name = save_sourcing_name;
25926 sourcing_lnum = save_sourcing_lnum;
25927 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025928#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025929 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025930 script_prof_restore(&wait_start);
25931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025932
25933 if (p_verbose >= 12 && sourcing_name != NULL)
25934 {
25935 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025936 verbose_enter_scroll();
25937
Bram Moolenaar555b2802005-05-19 21:08:39 +000025938 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025939 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025940
25941 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025942 --no_wait_return;
25943 }
25944
25945 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025946 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025947 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025948
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025949 /* If the a:000 list and the l: and a: dicts are not referenced we can
25950 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025951 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25952 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25953 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25954 {
25955 free_funccal(fc, FALSE);
25956 }
25957 else
25958 {
25959 hashitem_T *hi;
25960 listitem_T *li;
25961 int todo;
25962
25963 /* "fc" is still in use. This can happen when returning "a:000" or
25964 * assigning "l:" to a global variable.
25965 * Link "fc" in the list for garbage collection later. */
25966 fc->caller = previous_funccal;
25967 previous_funccal = fc;
25968
25969 /* Make a copy of the a: variables, since we didn't do that above. */
25970 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25971 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25972 {
25973 if (!HASHITEM_EMPTY(hi))
25974 {
25975 --todo;
25976 v = HI2DI(hi);
25977 copy_tv(&v->di_tv, &v->di_tv);
25978 }
25979 }
25980
25981 /* Make a copy of the a:000 items, since we didn't do that above. */
25982 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25983 copy_tv(&li->li_tv, &li->li_tv);
25984 }
25985}
25986
25987/*
25988 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025989 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025990 */
25991 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025992can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025993{
25994 return (fc->l_varlist.lv_copyID != copyID
25995 && fc->l_vars.dv_copyID != copyID
25996 && fc->l_avars.dv_copyID != copyID);
25997}
25998
25999/*
26000 * Free "fc" and what it contains.
26001 */
26002 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026003free_funccal(
26004 funccall_T *fc,
26005 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000026006{
26007 listitem_T *li;
26008
26009 /* The a: variables typevals may not have been allocated, only free the
26010 * allocated variables. */
26011 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
26012
26013 /* free all l: variables */
26014 vars_clear(&fc->l_vars.dv_hashtab);
26015
26016 /* Free the a:000 variables if they were allocated. */
26017 if (free_val)
26018 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
26019 clear_tv(&li->li_tv);
26020
26021 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026022}
26023
26024/*
Bram Moolenaar33570922005-01-25 22:26:29 +000026025 * Add a number variable "name" to dict "dp" with value "nr".
26026 */
26027 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026028add_nr_var(
26029 dict_T *dp,
26030 dictitem_T *v,
26031 char *name,
26032 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000026033{
26034 STRCPY(v->di_key, name);
26035 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
26036 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
26037 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000026038 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000026039 v->di_tv.vval.v_number = nr;
26040}
26041
26042/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000026043 * ":return [expr]"
26044 */
26045 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026046ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026047{
26048 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000026049 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026050 int returning = FALSE;
26051
26052 if (current_funccal == NULL)
26053 {
26054 EMSG(_("E133: :return not inside a function"));
26055 return;
26056 }
26057
26058 if (eap->skip)
26059 ++emsg_skip;
26060
26061 eap->nextcmd = NULL;
26062 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026063 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026064 {
26065 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026066 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026067 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026068 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026069 }
26070 /* It's safer to return also on error. */
26071 else if (!eap->skip)
26072 {
26073 /*
26074 * Return unless the expression evaluation has been cancelled due to an
26075 * aborting error, an interrupt, or an exception.
26076 */
26077 if (!aborting())
26078 returning = do_return(eap, FALSE, TRUE, NULL);
26079 }
26080
26081 /* When skipping or the return gets pending, advance to the next command
26082 * in this line (!returning). Otherwise, ignore the rest of the line.
26083 * Following lines will be ignored by get_func_line(). */
26084 if (returning)
26085 eap->nextcmd = NULL;
26086 else if (eap->nextcmd == NULL) /* no argument */
26087 eap->nextcmd = check_nextcmd(arg);
26088
26089 if (eap->skip)
26090 --emsg_skip;
26091}
26092
26093/*
26094 * Return from a function. Possibly makes the return pending. Also called
26095 * for a pending return at the ":endtry" or after returning from an extra
26096 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000026097 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026098 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026099 * FALSE when the return gets pending.
26100 */
26101 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026102do_return(
26103 exarg_T *eap,
26104 int reanimate,
26105 int is_cmd,
26106 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026107{
26108 int idx;
26109 struct condstack *cstack = eap->cstack;
26110
26111 if (reanimate)
26112 /* Undo the return. */
26113 current_funccal->returned = FALSE;
26114
26115 /*
26116 * Cleanup (and inactivate) conditionals, but stop when a try conditional
26117 * not in its finally clause (which then is to be executed next) is found.
26118 * In this case, make the ":return" pending for execution at the ":endtry".
26119 * Otherwise, return normally.
26120 */
26121 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
26122 if (idx >= 0)
26123 {
26124 cstack->cs_pending[idx] = CSTP_RETURN;
26125
26126 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026127 /* A pending return again gets pending. "rettv" points to an
26128 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000026129 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026130 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026131 else
26132 {
26133 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026134 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026135 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026136 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026137
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026138 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026139 {
26140 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026141 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000026142 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026143 else
26144 EMSG(_(e_outofmem));
26145 }
26146 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026147 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026148
26149 if (reanimate)
26150 {
26151 /* The pending return value could be overwritten by a ":return"
26152 * without argument in a finally clause; reset the default
26153 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026154 current_funccal->rettv->v_type = VAR_NUMBER;
26155 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026156 }
26157 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026158 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026159 }
26160 else
26161 {
26162 current_funccal->returned = TRUE;
26163
26164 /* If the return is carried out now, store the return value. For
26165 * a return immediately after reanimation, the value is already
26166 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026167 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026168 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026169 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000026170 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026171 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026172 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026173 }
26174 }
26175
26176 return idx < 0;
26177}
26178
26179/*
26180 * Free the variable with a pending return value.
26181 */
26182 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026183discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026184{
Bram Moolenaar33570922005-01-25 22:26:29 +000026185 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026186}
26187
26188/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026189 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000026190 * is an allocated string. Used by report_pending() for verbose messages.
26191 */
26192 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026193get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026194{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026195 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026196 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026197 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026198
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026199 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026200 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026201 if (s == NULL)
26202 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026203
26204 STRCPY(IObuff, ":return ");
26205 STRNCPY(IObuff + 8, s, IOSIZE - 8);
26206 if (STRLEN(s) + 8 >= IOSIZE)
26207 STRCPY(IObuff + IOSIZE - 4, "...");
26208 vim_free(tofree);
26209 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026210}
26211
26212/*
26213 * Get next function line.
26214 * Called by do_cmdline() to get the next line.
26215 * Returns allocated string, or NULL for end of function.
26216 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026217 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026218get_func_line(
26219 int c UNUSED,
26220 void *cookie,
26221 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026222{
Bram Moolenaar33570922005-01-25 22:26:29 +000026223 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026224 ufunc_T *fp = fcp->func;
26225 char_u *retval;
26226 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026227
26228 /* If breakpoints have been added/deleted need to check for it. */
26229 if (fcp->dbg_tick != debug_tick)
26230 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000026231 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026232 sourcing_lnum);
26233 fcp->dbg_tick = debug_tick;
26234 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000026235#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000026236 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026237 func_line_end(cookie);
26238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026239
Bram Moolenaar05159a02005-02-26 23:04:13 +000026240 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026241 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
26242 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026243 retval = NULL;
26244 else
26245 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026246 /* Skip NULL lines (continuation lines). */
26247 while (fcp->linenr < gap->ga_len
26248 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
26249 ++fcp->linenr;
26250 if (fcp->linenr >= gap->ga_len)
26251 retval = NULL;
26252 else
26253 {
26254 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
26255 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026256#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000026257 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026258 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026259#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026261 }
26262
26263 /* Did we encounter a breakpoint? */
26264 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
26265 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000026266 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026267 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000026268 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026269 sourcing_lnum);
26270 fcp->dbg_tick = debug_tick;
26271 }
26272
26273 return retval;
26274}
26275
Bram Moolenaar05159a02005-02-26 23:04:13 +000026276#if defined(FEAT_PROFILE) || defined(PROTO)
26277/*
26278 * Called when starting to read a function line.
26279 * "sourcing_lnum" must be correct!
26280 * When skipping lines it may not actually be executed, but we won't find out
26281 * until later and we need to store the time now.
26282 */
26283 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026284func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026285{
26286 funccall_T *fcp = (funccall_T *)cookie;
26287 ufunc_T *fp = fcp->func;
26288
26289 if (fp->uf_profiling && sourcing_lnum >= 1
26290 && sourcing_lnum <= fp->uf_lines.ga_len)
26291 {
26292 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026293 /* Skip continuation lines. */
26294 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
26295 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026296 fp->uf_tml_execed = FALSE;
26297 profile_start(&fp->uf_tml_start);
26298 profile_zero(&fp->uf_tml_children);
26299 profile_get_wait(&fp->uf_tml_wait);
26300 }
26301}
26302
26303/*
26304 * Called when actually executing a function line.
26305 */
26306 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026307func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026308{
26309 funccall_T *fcp = (funccall_T *)cookie;
26310 ufunc_T *fp = fcp->func;
26311
26312 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26313 fp->uf_tml_execed = TRUE;
26314}
26315
26316/*
26317 * Called when done with a function line.
26318 */
26319 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026320func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026321{
26322 funccall_T *fcp = (funccall_T *)cookie;
26323 ufunc_T *fp = fcp->func;
26324
26325 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26326 {
26327 if (fp->uf_tml_execed)
26328 {
26329 ++fp->uf_tml_count[fp->uf_tml_idx];
26330 profile_end(&fp->uf_tml_start);
26331 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026332 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000026333 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
26334 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026335 }
26336 fp->uf_tml_idx = -1;
26337 }
26338}
26339#endif
26340
Bram Moolenaar071d4272004-06-13 20:20:40 +000026341/*
26342 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026343 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000026344 */
26345 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026346func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026347{
Bram Moolenaar33570922005-01-25 22:26:29 +000026348 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026349
26350 /* Ignore the "abort" flag if the abortion behavior has been changed due to
26351 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026352 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000026353 || fcp->returned);
26354}
26355
26356/*
26357 * return TRUE if cookie indicates a function which "abort"s on errors.
26358 */
26359 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026360func_has_abort(
26361 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026362{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026363 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026364}
26365
26366#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
26367typedef enum
26368{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026369 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
26370 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
26371 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026372} var_flavour_T;
26373
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026374static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026375
26376 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010026377var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026378{
26379 char_u *p = varname;
26380
26381 if (ASCII_ISUPPER(*p))
26382 {
26383 while (*(++p))
26384 if (ASCII_ISLOWER(*p))
26385 return VAR_FLAVOUR_SESSION;
26386 return VAR_FLAVOUR_VIMINFO;
26387 }
26388 else
26389 return VAR_FLAVOUR_DEFAULT;
26390}
26391#endif
26392
26393#if defined(FEAT_VIMINFO) || defined(PROTO)
26394/*
26395 * Restore global vars that start with a capital from the viminfo file
26396 */
26397 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026398read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026399{
26400 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026401 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000026402 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026403 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026404
26405 if (!writing && (find_viminfo_parameter('!') != NULL))
26406 {
26407 tab = vim_strchr(virp->vir_line + 1, '\t');
26408 if (tab != NULL)
26409 {
26410 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026411 switch (*tab)
26412 {
26413 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026414#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026415 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026416#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026417 case 'D': type = VAR_DICT; break;
26418 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026419 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026421
26422 tab = vim_strchr(tab, '\t');
26423 if (tab != NULL)
26424 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026425 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026426 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026427 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026428 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026429#ifdef FEAT_FLOAT
26430 else if (type == VAR_FLOAT)
26431 (void)string2float(tab + 1, &tv.vval.v_float);
26432#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026433 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026434 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026435 if (type == VAR_DICT || type == VAR_LIST)
26436 {
26437 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
26438
26439 if (etv == NULL)
26440 /* Failed to parse back the dict or list, use it as a
26441 * string. */
26442 tv.v_type = VAR_STRING;
26443 else
26444 {
26445 vim_free(tv.vval.v_string);
26446 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010026447 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026448 }
26449 }
26450
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026451 /* when in a function use global variables */
26452 save_funccal = current_funccal;
26453 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026454 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026455 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026456
26457 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026458 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026459 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
26460 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026461 }
26462 }
26463 }
26464
26465 return viminfo_readline(virp);
26466}
26467
26468/*
26469 * Write global vars that start with a capital to the viminfo file
26470 */
26471 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026472write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026473{
Bram Moolenaar33570922005-01-25 22:26:29 +000026474 hashitem_T *hi;
26475 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026476 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010026477 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026478 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026479 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026480 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026481
26482 if (find_viminfo_parameter('!') == NULL)
26483 return;
26484
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020026485 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000026486
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026487 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026488 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026489 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026490 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026491 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026492 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026493 this_var = HI2DI(hi);
26494 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026495 {
Bram Moolenaar33570922005-01-25 22:26:29 +000026496 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000026497 {
26498 case VAR_STRING: s = "STR"; break;
26499 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026500 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026501 case VAR_DICT: s = "DIC"; break;
26502 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026503 case VAR_SPECIAL: s = "XPL"; break;
26504
26505 case VAR_UNKNOWN:
26506 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010026507 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010026508 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010026509 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010026510 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000026511 }
Bram Moolenaar33570922005-01-25 22:26:29 +000026512 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026513 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026514 if (p != NULL)
26515 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000026516 vim_free(tofree);
26517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026518 }
26519 }
26520}
26521#endif
26522
26523#if defined(FEAT_SESSION) || defined(PROTO)
26524 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026525store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026526{
Bram Moolenaar33570922005-01-25 22:26:29 +000026527 hashitem_T *hi;
26528 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026529 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026530 char_u *p, *t;
26531
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026532 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026533 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026534 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026535 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026536 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026537 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026538 this_var = HI2DI(hi);
26539 if ((this_var->di_tv.v_type == VAR_NUMBER
26540 || this_var->di_tv.v_type == VAR_STRING)
26541 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026542 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026543 /* Escape special characters with a backslash. Turn a LF and
26544 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000026545 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000026546 (char_u *)"\\\"\n\r");
26547 if (p == NULL) /* out of memory */
26548 break;
26549 for (t = p; *t != NUL; ++t)
26550 if (*t == '\n')
26551 *t = 'n';
26552 else if (*t == '\r')
26553 *t = 'r';
26554 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000026555 this_var->di_key,
26556 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26557 : ' ',
26558 p,
26559 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26560 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000026561 || put_eol(fd) == FAIL)
26562 {
26563 vim_free(p);
26564 return FAIL;
26565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026566 vim_free(p);
26567 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026568#ifdef FEAT_FLOAT
26569 else if (this_var->di_tv.v_type == VAR_FLOAT
26570 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
26571 {
26572 float_T f = this_var->di_tv.vval.v_float;
26573 int sign = ' ';
26574
26575 if (f < 0)
26576 {
26577 f = -f;
26578 sign = '-';
26579 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010026580 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026581 this_var->di_key, sign, f) < 0)
26582 || put_eol(fd) == FAIL)
26583 return FAIL;
26584 }
26585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026586 }
26587 }
26588 return OK;
26589}
26590#endif
26591
Bram Moolenaar661b1822005-07-28 22:36:45 +000026592/*
26593 * Display script name where an item was last set.
26594 * Should only be invoked when 'verbose' is non-zero.
26595 */
26596 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026597last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000026598{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026599 char_u *p;
26600
Bram Moolenaar661b1822005-07-28 22:36:45 +000026601 if (scriptID != 0)
26602 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026603 p = home_replace_save(NULL, get_scriptname(scriptID));
26604 if (p != NULL)
26605 {
26606 verbose_enter();
26607 MSG_PUTS(_("\n\tLast set from "));
26608 MSG_PUTS(p);
26609 vim_free(p);
26610 verbose_leave();
26611 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026612 }
26613}
26614
Bram Moolenaard812df62008-11-09 12:46:09 +000026615/*
26616 * List v:oldfiles in a nice way.
26617 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026618 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026619ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026620{
26621 list_T *l = vimvars[VV_OLDFILES].vv_list;
26622 listitem_T *li;
26623 int nr = 0;
26624
26625 if (l == NULL)
26626 msg((char_u *)_("No old files"));
26627 else
26628 {
26629 msg_start();
26630 msg_scroll = TRUE;
26631 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26632 {
26633 msg_outnum((long)++nr);
26634 MSG_PUTS(": ");
26635 msg_outtrans(get_tv_string(&li->li_tv));
26636 msg_putchar('\n');
26637 out_flush(); /* output one line at a time */
26638 ui_breakcheck();
26639 }
26640 /* Assume "got_int" was set to truncate the listing. */
26641 got_int = FALSE;
26642
26643#ifdef FEAT_BROWSE_CMD
26644 if (cmdmod.browse)
26645 {
26646 quit_more = FALSE;
26647 nr = prompt_for_number(FALSE);
26648 msg_starthere();
26649 if (nr > 0)
26650 {
26651 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26652 (long)nr);
26653
26654 if (p != NULL)
26655 {
26656 p = expand_env_save(p);
26657 eap->arg = p;
26658 eap->cmdidx = CMD_edit;
26659 cmdmod.browse = FALSE;
26660 do_exedit(eap, NULL);
26661 vim_free(p);
26662 }
26663 }
26664 }
26665#endif
26666 }
26667}
26668
Bram Moolenaar53744302015-07-17 17:38:22 +020026669/* reset v:option_new, v:option_old and v:option_type */
26670 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026671reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026672{
26673 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26674 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26675 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26676}
26677
26678
Bram Moolenaar071d4272004-06-13 20:20:40 +000026679#endif /* FEAT_EVAL */
26680
Bram Moolenaar071d4272004-06-13 20:20:40 +000026681
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026682#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026683
26684#ifdef WIN3264
26685/*
26686 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26687 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026688static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26689static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26690static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026691
26692/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026693 * Get the short path (8.3) for the filename in "fnamep".
26694 * Only works for a valid file name.
26695 * When the path gets longer "fnamep" is changed and the allocated buffer
26696 * is put in "bufp".
26697 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26698 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026699 */
26700 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026701get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026702{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026703 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026704 char_u *newbuf;
26705
26706 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026707 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026708 if (l > len - 1)
26709 {
26710 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026711 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026712 newbuf = vim_strnsave(*fnamep, l);
26713 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026714 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026715
26716 vim_free(*bufp);
26717 *fnamep = *bufp = newbuf;
26718
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026719 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026720 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026721 }
26722
26723 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026724 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026725}
26726
26727/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026728 * Get the short path (8.3) for the filename in "fname". The converted
26729 * path is returned in "bufp".
26730 *
26731 * Some of the directories specified in "fname" may not exist. This function
26732 * will shorten the existing directories at the beginning of the path and then
26733 * append the remaining non-existing path.
26734 *
26735 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026736 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026737 * bufp - Pointer to an allocated buffer for the filename.
26738 * fnamelen - Length of the filename pointed to by fname
26739 *
26740 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026741 */
26742 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026743shortpath_for_invalid_fname(
26744 char_u **fname,
26745 char_u **bufp,
26746 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026747{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026748 char_u *short_fname, *save_fname, *pbuf_unused;
26749 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026750 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026751 int old_len, len;
26752 int new_len, sfx_len;
26753 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026754
26755 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026756 old_len = *fnamelen;
26757 save_fname = vim_strnsave(*fname, old_len);
26758 pbuf_unused = NULL;
26759 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026760
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026761 endp = save_fname + old_len - 1; /* Find the end of the copy */
26762 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026763
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026764 /*
26765 * Try shortening the supplied path till it succeeds by removing one
26766 * directory at a time from the tail of the path.
26767 */
26768 len = 0;
26769 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026770 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026771 /* go back one path-separator */
26772 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26773 --endp;
26774 if (endp <= save_fname)
26775 break; /* processed the complete path */
26776
26777 /*
26778 * Replace the path separator with a NUL and try to shorten the
26779 * resulting path.
26780 */
26781 ch = *endp;
26782 *endp = 0;
26783 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026784 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026785 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26786 {
26787 retval = FAIL;
26788 goto theend;
26789 }
26790 *endp = ch; /* preserve the string */
26791
26792 if (len > 0)
26793 break; /* successfully shortened the path */
26794
26795 /* failed to shorten the path. Skip the path separator */
26796 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026797 }
26798
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026799 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026800 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026801 /*
26802 * Succeeded in shortening the path. Now concatenate the shortened
26803 * path with the remaining path at the tail.
26804 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026805
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026806 /* Compute the length of the new path. */
26807 sfx_len = (int)(save_endp - endp) + 1;
26808 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026809
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026810 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026811 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026812 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026813 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026814 /* There is not enough space in the currently allocated string,
26815 * copy it to a buffer big enough. */
26816 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026817 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026818 {
26819 retval = FAIL;
26820 goto theend;
26821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026822 }
26823 else
26824 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026825 /* Transfer short_fname to the main buffer (it's big enough),
26826 * unless get_short_pathname() did its work in-place. */
26827 *fname = *bufp = save_fname;
26828 if (short_fname != save_fname)
26829 vim_strncpy(save_fname, short_fname, len);
26830 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026831 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026832
26833 /* concat the not-shortened part of the path */
26834 vim_strncpy(*fname + len, endp, sfx_len);
26835 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026836 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026837
26838theend:
26839 vim_free(pbuf_unused);
26840 vim_free(save_fname);
26841
26842 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026843}
26844
26845/*
26846 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026847 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026848 */
26849 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026850shortpath_for_partial(
26851 char_u **fnamep,
26852 char_u **bufp,
26853 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026854{
26855 int sepcount, len, tflen;
26856 char_u *p;
26857 char_u *pbuf, *tfname;
26858 int hasTilde;
26859
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026860 /* Count up the path separators from the RHS.. so we know which part
26861 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026862 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026863 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026864 if (vim_ispathsep(*p))
26865 ++sepcount;
26866
26867 /* Need full path first (use expand_env() to remove a "~/") */
26868 hasTilde = (**fnamep == '~');
26869 if (hasTilde)
26870 pbuf = tfname = expand_env_save(*fnamep);
26871 else
26872 pbuf = tfname = FullName_save(*fnamep, FALSE);
26873
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026874 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026875
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026876 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26877 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026878
26879 if (len == 0)
26880 {
26881 /* Don't have a valid filename, so shorten the rest of the
26882 * path if we can. This CAN give us invalid 8.3 filenames, but
26883 * there's not a lot of point in guessing what it might be.
26884 */
26885 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026886 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26887 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026888 }
26889
26890 /* Count the paths backward to find the beginning of the desired string. */
26891 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026892 {
26893#ifdef FEAT_MBYTE
26894 if (has_mbyte)
26895 p -= mb_head_off(tfname, p);
26896#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026897 if (vim_ispathsep(*p))
26898 {
26899 if (sepcount == 0 || (hasTilde && sepcount == 1))
26900 break;
26901 else
26902 sepcount --;
26903 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026905 if (hasTilde)
26906 {
26907 --p;
26908 if (p >= tfname)
26909 *p = '~';
26910 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026911 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026912 }
26913 else
26914 ++p;
26915
26916 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26917 vim_free(*bufp);
26918 *fnamelen = (int)STRLEN(p);
26919 *bufp = pbuf;
26920 *fnamep = p;
26921
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026922 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026923}
26924#endif /* WIN3264 */
26925
26926/*
26927 * Adjust a filename, according to a string of modifiers.
26928 * *fnamep must be NUL terminated when called. When returning, the length is
26929 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026930 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026931 * When there is an error, *fnamep is set to NULL.
26932 */
26933 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026934modify_fname(
26935 char_u *src, /* string with modifiers */
26936 int *usedlen, /* characters after src that are used */
26937 char_u **fnamep, /* file name so far */
26938 char_u **bufp, /* buffer for allocated file name or NULL */
26939 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026940{
26941 int valid = 0;
26942 char_u *tail;
26943 char_u *s, *p, *pbuf;
26944 char_u dirname[MAXPATHL];
26945 int c;
26946 int has_fullname = 0;
26947#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026948 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026949 int has_shortname = 0;
26950#endif
26951
26952repeat:
26953 /* ":p" - full path/file_name */
26954 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26955 {
26956 has_fullname = 1;
26957
26958 valid |= VALID_PATH;
26959 *usedlen += 2;
26960
26961 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26962 if ((*fnamep)[0] == '~'
26963#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26964 && ((*fnamep)[1] == '/'
26965# ifdef BACKSLASH_IN_FILENAME
26966 || (*fnamep)[1] == '\\'
26967# endif
26968 || (*fnamep)[1] == NUL)
26969
26970#endif
26971 )
26972 {
26973 *fnamep = expand_env_save(*fnamep);
26974 vim_free(*bufp); /* free any allocated file name */
26975 *bufp = *fnamep;
26976 if (*fnamep == NULL)
26977 return -1;
26978 }
26979
26980 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026981 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026982 {
26983 if (vim_ispathsep(*p)
26984 && p[1] == '.'
26985 && (p[2] == NUL
26986 || vim_ispathsep(p[2])
26987 || (p[2] == '.'
26988 && (p[3] == NUL || vim_ispathsep(p[3])))))
26989 break;
26990 }
26991
26992 /* FullName_save() is slow, don't use it when not needed. */
26993 if (*p != NUL || !vim_isAbsName(*fnamep))
26994 {
26995 *fnamep = FullName_save(*fnamep, *p != NUL);
26996 vim_free(*bufp); /* free any allocated file name */
26997 *bufp = *fnamep;
26998 if (*fnamep == NULL)
26999 return -1;
27000 }
27001
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020027002#ifdef WIN3264
27003# if _WIN32_WINNT >= 0x0500
27004 if (vim_strchr(*fnamep, '~') != NULL)
27005 {
27006 /* Expand 8.3 filename to full path. Needed to make sure the same
27007 * file does not have two different names.
27008 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
27009 p = alloc(_MAX_PATH + 1);
27010 if (p != NULL)
27011 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010027012 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020027013 {
27014 vim_free(*bufp);
27015 *bufp = *fnamep = p;
27016 }
27017 else
27018 vim_free(p);
27019 }
27020 }
27021# endif
27022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000027023 /* Append a path separator to a directory. */
27024 if (mch_isdir(*fnamep))
27025 {
27026 /* Make room for one or two extra characters. */
27027 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
27028 vim_free(*bufp); /* free any allocated file name */
27029 *bufp = *fnamep;
27030 if (*fnamep == NULL)
27031 return -1;
27032 add_pathsep(*fnamep);
27033 }
27034 }
27035
27036 /* ":." - path relative to the current directory */
27037 /* ":~" - path relative to the home directory */
27038 /* ":8" - shortname path - postponed till after */
27039 while (src[*usedlen] == ':'
27040 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
27041 {
27042 *usedlen += 2;
27043 if (c == '8')
27044 {
27045#ifdef WIN3264
27046 has_shortname = 1; /* Postpone this. */
27047#endif
27048 continue;
27049 }
27050 pbuf = NULL;
27051 /* Need full path first (use expand_env() to remove a "~/") */
27052 if (!has_fullname)
27053 {
27054 if (c == '.' && **fnamep == '~')
27055 p = pbuf = expand_env_save(*fnamep);
27056 else
27057 p = pbuf = FullName_save(*fnamep, FALSE);
27058 }
27059 else
27060 p = *fnamep;
27061
27062 has_fullname = 0;
27063
27064 if (p != NULL)
27065 {
27066 if (c == '.')
27067 {
27068 mch_dirname(dirname, MAXPATHL);
27069 s = shorten_fname(p, dirname);
27070 if (s != NULL)
27071 {
27072 *fnamep = s;
27073 if (pbuf != NULL)
27074 {
27075 vim_free(*bufp); /* free any allocated file name */
27076 *bufp = pbuf;
27077 pbuf = NULL;
27078 }
27079 }
27080 }
27081 else
27082 {
27083 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
27084 /* Only replace it when it starts with '~' */
27085 if (*dirname == '~')
27086 {
27087 s = vim_strsave(dirname);
27088 if (s != NULL)
27089 {
27090 *fnamep = s;
27091 vim_free(*bufp);
27092 *bufp = s;
27093 }
27094 }
27095 }
27096 vim_free(pbuf);
27097 }
27098 }
27099
27100 tail = gettail(*fnamep);
27101 *fnamelen = (int)STRLEN(*fnamep);
27102
27103 /* ":h" - head, remove "/file_name", can be repeated */
27104 /* Don't remove the first "/" or "c:\" */
27105 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
27106 {
27107 valid |= VALID_HEAD;
27108 *usedlen += 2;
27109 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000027110 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000027111 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027112 *fnamelen = (int)(tail - *fnamep);
27113#ifdef VMS
27114 if (*fnamelen > 0)
27115 *fnamelen += 1; /* the path separator is part of the path */
27116#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000027117 if (*fnamelen == 0)
27118 {
27119 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
27120 p = vim_strsave((char_u *)".");
27121 if (p == NULL)
27122 return -1;
27123 vim_free(*bufp);
27124 *bufp = *fnamep = tail = p;
27125 *fnamelen = 1;
27126 }
27127 else
27128 {
27129 while (tail > s && !after_pathsep(s, tail))
27130 mb_ptr_back(*fnamep, tail);
27131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000027132 }
27133
27134 /* ":8" - shortname */
27135 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
27136 {
27137 *usedlen += 2;
27138#ifdef WIN3264
27139 has_shortname = 1;
27140#endif
27141 }
27142
27143#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020027144 /*
27145 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027146 */
27147 if (has_shortname)
27148 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027149 /* Copy the string if it is shortened by :h and when it wasn't copied
27150 * yet, because we are going to change it in place. Avoids changing
27151 * the buffer name for "%:8". */
27152 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027153 {
27154 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020027155 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027156 return -1;
27157 vim_free(*bufp);
27158 *bufp = *fnamep = p;
27159 }
27160
27161 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020027162 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000027163 if (!has_fullname && !vim_isAbsName(*fnamep))
27164 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027165 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027166 return -1;
27167 }
27168 else
27169 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027170 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027171
Bram Moolenaardc935552011-08-17 15:23:23 +020027172 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027173 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027174 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027175 return -1;
27176
27177 if (l == 0)
27178 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027179 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000027180 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027181 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027182 return -1;
27183 }
27184 *fnamelen = l;
27185 }
27186 }
27187#endif /* WIN3264 */
27188
27189 /* ":t" - tail, just the basename */
27190 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
27191 {
27192 *usedlen += 2;
27193 *fnamelen -= (int)(tail - *fnamep);
27194 *fnamep = tail;
27195 }
27196
27197 /* ":e" - extension, can be repeated */
27198 /* ":r" - root, without extension, can be repeated */
27199 while (src[*usedlen] == ':'
27200 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
27201 {
27202 /* find a '.' in the tail:
27203 * - for second :e: before the current fname
27204 * - otherwise: The last '.'
27205 */
27206 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
27207 s = *fnamep - 2;
27208 else
27209 s = *fnamep + *fnamelen - 1;
27210 for ( ; s > tail; --s)
27211 if (s[0] == '.')
27212 break;
27213 if (src[*usedlen + 1] == 'e') /* :e */
27214 {
27215 if (s > tail)
27216 {
27217 *fnamelen += (int)(*fnamep - (s + 1));
27218 *fnamep = s + 1;
27219#ifdef VMS
27220 /* cut version from the extension */
27221 s = *fnamep + *fnamelen - 1;
27222 for ( ; s > *fnamep; --s)
27223 if (s[0] == ';')
27224 break;
27225 if (s > *fnamep)
27226 *fnamelen = s - *fnamep;
27227#endif
27228 }
27229 else if (*fnamep <= tail)
27230 *fnamelen = 0;
27231 }
27232 else /* :r */
27233 {
27234 if (s > tail) /* remove one extension */
27235 *fnamelen = (int)(s - *fnamep);
27236 }
27237 *usedlen += 2;
27238 }
27239
27240 /* ":s?pat?foo?" - substitute */
27241 /* ":gs?pat?foo?" - global substitute */
27242 if (src[*usedlen] == ':'
27243 && (src[*usedlen + 1] == 's'
27244 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
27245 {
27246 char_u *str;
27247 char_u *pat;
27248 char_u *sub;
27249 int sep;
27250 char_u *flags;
27251 int didit = FALSE;
27252
27253 flags = (char_u *)"";
27254 s = src + *usedlen + 2;
27255 if (src[*usedlen + 1] == 'g')
27256 {
27257 flags = (char_u *)"g";
27258 ++s;
27259 }
27260
27261 sep = *s++;
27262 if (sep)
27263 {
27264 /* find end of pattern */
27265 p = vim_strchr(s, sep);
27266 if (p != NULL)
27267 {
27268 pat = vim_strnsave(s, (int)(p - s));
27269 if (pat != NULL)
27270 {
27271 s = p + 1;
27272 /* find end of substitution */
27273 p = vim_strchr(s, sep);
27274 if (p != NULL)
27275 {
27276 sub = vim_strnsave(s, (int)(p - s));
27277 str = vim_strnsave(*fnamep, *fnamelen);
27278 if (sub != NULL && str != NULL)
27279 {
27280 *usedlen = (int)(p + 1 - src);
27281 s = do_string_sub(str, pat, sub, flags);
27282 if (s != NULL)
27283 {
27284 *fnamep = s;
27285 *fnamelen = (int)STRLEN(s);
27286 vim_free(*bufp);
27287 *bufp = s;
27288 didit = TRUE;
27289 }
27290 }
27291 vim_free(sub);
27292 vim_free(str);
27293 }
27294 vim_free(pat);
27295 }
27296 }
27297 /* after using ":s", repeat all the modifiers */
27298 if (didit)
27299 goto repeat;
27300 }
27301 }
27302
Bram Moolenaar26df0922014-02-23 23:39:13 +010027303 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
27304 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010027305 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010027306 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027307 if (c != NUL)
27308 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027309 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027310 if (c != NUL)
27311 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027312 if (p == NULL)
27313 return -1;
27314 vim_free(*bufp);
27315 *bufp = *fnamep = p;
27316 *fnamelen = (int)STRLEN(p);
27317 *usedlen += 2;
27318 }
27319
Bram Moolenaar071d4272004-06-13 20:20:40 +000027320 return valid;
27321}
27322
27323/*
27324 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
27325 * "flags" can be "g" to do a global substitute.
27326 * Returns an allocated string, NULL for error.
27327 */
27328 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010027329do_string_sub(
27330 char_u *str,
27331 char_u *pat,
27332 char_u *sub,
27333 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027334{
27335 int sublen;
27336 regmatch_T regmatch;
27337 int i;
27338 int do_all;
27339 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027340 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027341 garray_T ga;
27342 char_u *ret;
27343 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027344 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027345
27346 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
27347 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027348 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027349
27350 ga_init2(&ga, 1, 200);
27351
27352 do_all = (flags[0] == 'g');
27353
27354 regmatch.rm_ic = p_ic;
27355 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
27356 if (regmatch.regprog != NULL)
27357 {
27358 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027359 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027360 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
27361 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010027362 /* Skip empty match except for first match. */
27363 if (regmatch.startp[0] == regmatch.endp[0])
27364 {
27365 if (zero_width == regmatch.startp[0])
27366 {
27367 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020027368 i = MB_PTR2LEN(tail);
27369 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
27370 (size_t)i);
27371 ga.ga_len += i;
27372 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027373 continue;
27374 }
27375 zero_width = regmatch.startp[0];
27376 }
27377
Bram Moolenaar071d4272004-06-13 20:20:40 +000027378 /*
27379 * Get some space for a temporary buffer to do the substitution
27380 * into. It will contain:
27381 * - The text up to where the match is.
27382 * - The substituted text.
27383 * - The text after the match.
27384 */
27385 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010027386 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000027387 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
27388 {
27389 ga_clear(&ga);
27390 break;
27391 }
27392
27393 /* copy the text up to where the match is */
27394 i = (int)(regmatch.startp[0] - tail);
27395 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
27396 /* add the substituted text */
27397 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
27398 + ga.ga_len + i, TRUE, TRUE, FALSE);
27399 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020027400 tail = regmatch.endp[0];
27401 if (*tail == NUL)
27402 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027403 if (!do_all)
27404 break;
27405 }
27406
27407 if (ga.ga_data != NULL)
27408 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
27409
Bram Moolenaar473de612013-06-08 18:19:48 +020027410 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027411 }
27412
27413 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
27414 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027415 if (p_cpo == empty_option)
27416 p_cpo = save_cpo;
27417 else
27418 /* Darn, evaluating {sub} expression changed the value. */
27419 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027420
27421 return ret;
27422}
27423
27424#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */