blob: 74ed263d51192e969ee245b9ca664bd9968dfd7c [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
11348/*
Bram Moolenaar1e5e1232016-07-07 17:33:02 +020011349 * "evalcmd()" function
11350 */
11351 static void
11352f_evalcmd(typval_T *argvars, typval_T *rettv)
11353{
11354 char_u *s;
11355
11356 rettv->vval.v_string = NULL;
11357 rettv->v_type = VAR_STRING;
11358
11359 s = get_tv_string_chk(&argvars[0]);
11360 if (s != NULL)
11361 {
11362 redir_vname = TRUE;
11363 redir_lval = (lval_T *)&redir_lval;
11364 ga_init2(&redir_ga, (int)sizeof(char), 500);
11365
11366 if (do_cmdline_cmd(s) == OK)
11367 rettv->vval.v_string = redir_ga.ga_data;
11368 else
11369 vim_free(redir_ga.ga_data);
11370
11371 redir_ga.ga_data = NULL;
11372 redir_vname = FALSE;
11373 redir_lval = NULL;
11374 }
11375
11376}
11377
11378/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011379 * "eventhandler()" function
11380 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011381 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011382f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011383{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011384 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011385}
11386
11387/*
11388 * "executable()" function
11389 */
11390 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011391f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011392{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011393 char_u *name = get_tv_string(&argvars[0]);
11394
11395 /* Check in $PATH and also check directly if there is a directory name. */
11396 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11397 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011398}
11399
11400/*
11401 * "exepath()" function
11402 */
11403 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011404f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011405{
11406 char_u *p = NULL;
11407
Bram Moolenaarb5971142015-03-21 17:32:19 +010011408 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011409 rettv->v_type = VAR_STRING;
11410 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011411}
11412
11413/*
11414 * "exists()" function
11415 */
11416 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011417f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011418{
11419 char_u *p;
11420 char_u *name;
11421 int n = FALSE;
11422 int len = 0;
11423
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011424 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011425 if (*p == '$') /* environment variable */
11426 {
11427 /* first try "normal" environment variables (fast) */
11428 if (mch_getenv(p + 1) != NULL)
11429 n = TRUE;
11430 else
11431 {
11432 /* try expanding things like $VIM and ${HOME} */
11433 p = expand_env_save(p);
11434 if (p != NULL && *p != '$')
11435 n = TRUE;
11436 vim_free(p);
11437 }
11438 }
11439 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011440 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011441 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011442 if (*skipwhite(p) != NUL)
11443 n = FALSE; /* trailing garbage */
11444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011445 else if (*p == '*') /* internal or user defined function */
11446 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011447 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011448 }
11449 else if (*p == ':')
11450 {
11451 n = cmd_exists(p + 1);
11452 }
11453 else if (*p == '#')
11454 {
11455#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011456 if (p[1] == '#')
11457 n = autocmd_supported(p + 2);
11458 else
11459 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011460#endif
11461 }
11462 else /* internal variable */
11463 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011464 char_u *tofree;
11465 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011466
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011467 /* get_name_len() takes care of expanding curly braces */
11468 name = p;
11469 len = get_name_len(&p, &tofree, TRUE, FALSE);
11470 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011471 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011472 if (tofree != NULL)
11473 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011474 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011475 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011476 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011477 /* handle d.key, l[idx], f(expr) */
11478 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11479 if (n)
11480 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011481 }
11482 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011483 if (*p != NUL)
11484 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011485
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011486 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011487 }
11488
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011489 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011490}
11491
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011492#ifdef FEAT_FLOAT
11493/*
11494 * "exp()" function
11495 */
11496 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011497f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011498{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011499 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011500
11501 rettv->v_type = VAR_FLOAT;
11502 if (get_float_arg(argvars, &f) == OK)
11503 rettv->vval.v_float = exp(f);
11504 else
11505 rettv->vval.v_float = 0.0;
11506}
11507#endif
11508
Bram Moolenaar071d4272004-06-13 20:20:40 +000011509/*
11510 * "expand()" function
11511 */
11512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011513f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011514{
11515 char_u *s;
11516 int len;
11517 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011518 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011519 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011520 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011521 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011522
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011523 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011524 if (argvars[1].v_type != VAR_UNKNOWN
11525 && argvars[2].v_type != VAR_UNKNOWN
11526 && get_tv_number_chk(&argvars[2], &error)
11527 && !error)
11528 {
11529 rettv->v_type = VAR_LIST;
11530 rettv->vval.v_list = NULL;
11531 }
11532
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011533 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011534 if (*s == '%' || *s == '#' || *s == '<')
11535 {
11536 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011537 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011538 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011539 if (rettv->v_type == VAR_LIST)
11540 {
11541 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11542 list_append_string(rettv->vval.v_list, result, -1);
11543 else
11544 vim_free(result);
11545 }
11546 else
11547 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011548 }
11549 else
11550 {
11551 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011552 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011553 if (argvars[1].v_type != VAR_UNKNOWN
11554 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011555 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011556 if (!error)
11557 {
11558 ExpandInit(&xpc);
11559 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011560 if (p_wic)
11561 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011562 if (rettv->v_type == VAR_STRING)
11563 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11564 options, WILD_ALL);
11565 else if (rettv_list_alloc(rettv) != FAIL)
11566 {
11567 int i;
11568
11569 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11570 for (i = 0; i < xpc.xp_numfiles; i++)
11571 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11572 ExpandCleanup(&xpc);
11573 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011574 }
11575 else
11576 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011577 }
11578}
11579
11580/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011581 * Go over all entries in "d2" and add them to "d1".
11582 * When "action" is "error" then a duplicate key is an error.
11583 * When "action" is "force" then a duplicate key is overwritten.
11584 * Otherwise duplicate keys are ignored ("action" is "keep").
11585 */
11586 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011587dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011588{
11589 dictitem_T *di1;
11590 hashitem_T *hi2;
11591 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011592 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011593
11594 todo = (int)d2->dv_hashtab.ht_used;
11595 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11596 {
11597 if (!HASHITEM_EMPTY(hi2))
11598 {
11599 --todo;
11600 di1 = dict_find(d1, hi2->hi_key, -1);
11601 if (d1->dv_scope != 0)
11602 {
11603 /* Disallow replacing a builtin function in l: and g:.
11604 * Check the key to be valid when adding to any
11605 * scope. */
11606 if (d1->dv_scope == VAR_DEF_SCOPE
11607 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11608 && var_check_func_name(hi2->hi_key,
11609 di1 == NULL))
11610 break;
11611 if (!valid_varname(hi2->hi_key))
11612 break;
11613 }
11614 if (di1 == NULL)
11615 {
11616 di1 = dictitem_copy(HI2DI(hi2));
11617 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11618 dictitem_free(di1);
11619 }
11620 else if (*action == 'e')
11621 {
11622 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11623 break;
11624 }
11625 else if (*action == 'f' && HI2DI(hi2) != di1)
11626 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011627 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11628 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011629 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011630 clear_tv(&di1->di_tv);
11631 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11632 }
11633 }
11634 }
11635}
11636
11637/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011638 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011639 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011640 */
11641 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011642f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011643{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011644 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011645
Bram Moolenaare9a41262005-01-15 22:18:47 +000011646 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011647 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011648 list_T *l1, *l2;
11649 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011650 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011651 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011652
Bram Moolenaare9a41262005-01-15 22:18:47 +000011653 l1 = argvars[0].vval.v_list;
11654 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011655 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011656 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011657 {
11658 if (argvars[2].v_type != VAR_UNKNOWN)
11659 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011660 before = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011661 if (error)
11662 return; /* type error; errmsg already given */
11663
Bram Moolenaar758711c2005-02-02 23:11:38 +000011664 if (before == l1->lv_len)
11665 item = NULL;
11666 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011667 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011668 item = list_find(l1, before);
11669 if (item == NULL)
11670 {
11671 EMSGN(_(e_listidx), before);
11672 return;
11673 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011674 }
11675 }
11676 else
11677 item = NULL;
11678 list_extend(l1, l2, item);
11679
Bram Moolenaare9a41262005-01-15 22:18:47 +000011680 copy_tv(&argvars[0], rettv);
11681 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011682 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011683 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11684 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011685 dict_T *d1, *d2;
11686 char_u *action;
11687 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011688
11689 d1 = argvars[0].vval.v_dict;
11690 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011691 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011692 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011693 {
11694 /* Check the third argument. */
11695 if (argvars[2].v_type != VAR_UNKNOWN)
11696 {
11697 static char *(av[]) = {"keep", "force", "error"};
11698
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011699 action = get_tv_string_chk(&argvars[2]);
11700 if (action == NULL)
11701 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011702 for (i = 0; i < 3; ++i)
11703 if (STRCMP(action, av[i]) == 0)
11704 break;
11705 if (i == 3)
11706 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011707 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011708 return;
11709 }
11710 }
11711 else
11712 action = (char_u *)"force";
11713
Bram Moolenaara9922d62013-05-30 13:01:18 +020011714 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011715
Bram Moolenaare9a41262005-01-15 22:18:47 +000011716 copy_tv(&argvars[0], rettv);
11717 }
11718 }
11719 else
11720 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011721}
11722
11723/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011724 * "feedkeys()" function
11725 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011726 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011727f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011728{
11729 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011730 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011731 char_u *keys, *flags;
11732 char_u nbuf[NUMBUFLEN];
11733 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011734 int execute = FALSE;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011735 int dangerous = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011736 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011737
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011738 /* This is not allowed in the sandbox. If the commands would still be
11739 * executed in the sandbox it would be OK, but it probably happens later,
11740 * when "sandbox" is no longer set. */
11741 if (check_secure())
11742 return;
11743
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011744 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011745
11746 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011747 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011748 flags = get_tv_string_buf(&argvars[1], nbuf);
11749 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011750 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011751 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011752 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011753 case 'n': remap = FALSE; break;
11754 case 'm': remap = TRUE; break;
11755 case 't': typed = TRUE; break;
11756 case 'i': insert = TRUE; break;
11757 case 'x': execute = TRUE; break;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011758 case '!': dangerous = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011759 }
11760 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011761 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011762
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011763 if (*keys != NUL || execute)
11764 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011765 /* Need to escape K_SPECIAL and CSI before putting the string in the
11766 * typeahead buffer. */
11767 keys_esc = vim_strsave_escape_csi(keys);
11768 if (keys_esc != NULL)
11769 {
11770 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011771 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011772 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011773 if (vgetc_busy)
11774 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011775 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011776 {
11777 int save_msg_scroll = msg_scroll;
11778
11779 /* Avoid a 1 second delay when the keys start Insert mode. */
11780 msg_scroll = FALSE;
Bram Moolenaar9bd547a2016-04-01 21:00:48 +020011781
Bram Moolenaar245c4102016-04-20 17:37:41 +020011782 if (!dangerous)
11783 ++ex_normal_busy;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011784 exec_normal(TRUE);
Bram Moolenaar245c4102016-04-20 17:37:41 +020011785 if (!dangerous)
11786 --ex_normal_busy;
Bram Moolenaar9e496852016-03-11 19:31:47 +010011787 msg_scroll |= save_msg_scroll;
11788 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011789 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011790 }
11791}
11792
11793/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011794 * "filereadable()" function
11795 */
11796 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011797f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011798{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011799 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011800 char_u *p;
11801 int n;
11802
Bram Moolenaarc236c162008-07-13 17:41:49 +000011803#ifndef O_NONBLOCK
11804# define O_NONBLOCK 0
11805#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011806 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011807 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11808 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011809 {
11810 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011811 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011812 }
11813 else
11814 n = FALSE;
11815
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011816 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011817}
11818
11819/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011820 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011821 * rights to write into.
11822 */
11823 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011824f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011825{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011826 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011827}
11828
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011829 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011830findfilendir(
11831 typval_T *argvars UNUSED,
11832 typval_T *rettv,
11833 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011834{
11835#ifdef FEAT_SEARCHPATH
11836 char_u *fname;
11837 char_u *fresult = NULL;
11838 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11839 char_u *p;
11840 char_u pathbuf[NUMBUFLEN];
11841 int count = 1;
11842 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011843 int error = FALSE;
11844#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011845
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011846 rettv->vval.v_string = NULL;
11847 rettv->v_type = VAR_STRING;
11848
11849#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011850 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011851
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011852 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011853 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011854 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11855 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011856 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011857 else
11858 {
11859 if (*p != NUL)
11860 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011861
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011862 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020011863 count = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011864 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011865 }
11866
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011867 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11868 error = TRUE;
11869
11870 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011871 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011872 do
11873 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011874 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011875 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011876 fresult = find_file_in_path_option(first ? fname : NULL,
11877 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011878 0, first, path,
11879 find_what,
11880 curbuf->b_ffname,
11881 find_what == FINDFILE_DIR
11882 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011883 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011884
11885 if (fresult != NULL && rettv->v_type == VAR_LIST)
11886 list_append_string(rettv->vval.v_list, fresult, -1);
11887
11888 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011889 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011890
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011891 if (rettv->v_type == VAR_STRING)
11892 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011893#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011894}
11895
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011896static void filter_map(typval_T *argvars, typval_T *rettv, int map);
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020011897static int filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011898
11899/*
11900 * Implementation of map() and filter().
11901 */
11902 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011903filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011904{
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020011905 typval_T *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011906 listitem_T *li, *nli;
11907 list_T *l = NULL;
11908 dictitem_T *di;
11909 hashtab_T *ht;
11910 hashitem_T *hi;
11911 dict_T *d = NULL;
11912 typval_T save_val;
11913 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011914 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011915 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011916 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011917 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011918 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011919 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011920 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011921
Bram Moolenaare9a41262005-01-15 22:18:47 +000011922 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011923 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011924 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011925 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011926 return;
11927 }
11928 else if (argvars[0].v_type == VAR_DICT)
11929 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011930 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011931 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011932 return;
11933 }
11934 else
11935 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011936 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011937 return;
11938 }
11939
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020011940 expr = &argvars[1];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011941 /* On type errors, the preceding call has already displayed an error
11942 * message. Avoid a misleading error message for an empty string that
11943 * was not passed as argument. */
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020011944 if (expr->v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011945 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011946 prepare_vimvar(VV_VAL, &save_val);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011947
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011948 /* We reset "did_emsg" to be able to detect whether an error
11949 * occurred during evaluation of the expression. */
11950 save_did_emsg = did_emsg;
11951 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011952
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011953 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011954 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011955 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011956 vimvars[VV_KEY].vv_type = VAR_STRING;
11957
11958 ht = &d->dv_hashtab;
11959 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011960 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011961 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011962 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011963 if (!HASHITEM_EMPTY(hi))
11964 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011965 int r;
11966
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011967 --todo;
11968 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011969 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011970 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11971 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011972 break;
11973 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011974 r = filter_map_one(&di->di_tv, expr, map, &rem);
11975 clear_tv(&vimvars[VV_KEY].vv_tv);
11976 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011977 break;
11978 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011979 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011980 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11981 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011982 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011983 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011984 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011985 }
11986 }
11987 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011988 }
11989 else
11990 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011991 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11992
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011993 for (li = l->lv_first; li != NULL; li = nli)
11994 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011995 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011996 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011997 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011998 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011999 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012000 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012001 break;
12002 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012003 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020012004 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012005 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012006 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012007
Bram Moolenaar627b1d32009-11-17 11:20:35 +000012008 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012009 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012010
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012011 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012012 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012013
12014 copy_tv(&argvars[0], rettv);
12015}
12016
12017 static int
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012018filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012019{
Bram Moolenaar33570922005-01-25 22:26:29 +000012020 typval_T rettv;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012021 typval_T argv[3];
Bram Moolenaare9a41262005-01-15 22:18:47 +000012022 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012023 int retval = FAIL;
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012024 int dummy;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012025
Bram Moolenaar33570922005-01-25 22:26:29 +000012026 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012027 argv[0] = vimvars[VV_KEY].vv_tv;
12028 argv[1] = vimvars[VV_VAL].vv_tv;
12029 s = expr->vval.v_string;
12030 if (expr->v_type == VAR_FUNC)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012031 {
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +020012032 if (call_func(s, (int)STRLEN(s),
12033 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
12034 goto theend;
12035 }
12036 else if (expr->v_type == VAR_PARTIAL)
12037 {
12038 partial_T *partial = expr->vval.v_partial;
12039
12040 s = partial->pt_name;
12041 if (call_func(s, (int)STRLEN(s),
12042 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, partial, NULL)
12043 == FAIL)
12044 goto theend;
12045 }
12046 else
12047 {
12048 s = skipwhite(s);
12049 if (eval1(&s, &rettv, TRUE) == FAIL)
12050 goto theend;
12051 if (*s != NUL) /* check for trailing chars after expr */
12052 {
12053 EMSG2(_(e_invexpr2), s);
12054 goto theend;
12055 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012056 }
12057 if (map)
12058 {
12059 /* map(): replace the list item value */
12060 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000012061 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012062 *tv = rettv;
12063 }
12064 else
12065 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012066 int error = FALSE;
12067
Bram Moolenaare9a41262005-01-15 22:18:47 +000012068 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012069 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012070 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012071 /* On type error, nothing has been removed; return FAIL to stop the
12072 * loop. The error message was given by get_tv_number_chk(). */
12073 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012074 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012075 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012076 retval = OK;
12077theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000012078 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012079 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012080}
12081
12082/*
12083 * "filter()" function
12084 */
12085 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012086f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012087{
12088 filter_map(argvars, rettv, FALSE);
12089}
12090
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012091/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012092 * "finddir({fname}[, {path}[, {count}]])" function
12093 */
12094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012095f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012096{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012097 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012098}
12099
12100/*
12101 * "findfile({fname}[, {path}[, {count}]])" function
12102 */
12103 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012104f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012105{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012106 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012107}
12108
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012109#ifdef FEAT_FLOAT
12110/*
12111 * "float2nr({float})" function
12112 */
12113 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012114f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012115{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012116 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012117
12118 if (get_float_arg(argvars, &f) == OK)
12119 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020012120# ifdef FEAT_NUM64
12121 if (f < -0x7fffffffffffffff)
12122 rettv->vval.v_number = -0x7fffffffffffffff;
12123 else if (f > 0x7fffffffffffffff)
12124 rettv->vval.v_number = 0x7fffffffffffffff;
12125 else
12126 rettv->vval.v_number = (varnumber_T)f;
12127# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012128 if (f < -0x7fffffff)
12129 rettv->vval.v_number = -0x7fffffff;
12130 else if (f > 0x7fffffff)
12131 rettv->vval.v_number = 0x7fffffff;
12132 else
12133 rettv->vval.v_number = (varnumber_T)f;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020012134# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012135 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012136}
12137
12138/*
12139 * "floor({float})" function
12140 */
12141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012142f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012143{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012144 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012145
12146 rettv->v_type = VAR_FLOAT;
12147 if (get_float_arg(argvars, &f) == OK)
12148 rettv->vval.v_float = floor(f);
12149 else
12150 rettv->vval.v_float = 0.0;
12151}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012152
12153/*
12154 * "fmod()" function
12155 */
12156 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012157f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012158{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012159 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012160
12161 rettv->v_type = VAR_FLOAT;
12162 if (get_float_arg(argvars, &fx) == OK
12163 && get_float_arg(&argvars[1], &fy) == OK)
12164 rettv->vval.v_float = fmod(fx, fy);
12165 else
12166 rettv->vval.v_float = 0.0;
12167}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012168#endif
12169
Bram Moolenaar0d660222005-01-07 21:51:51 +000012170/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012171 * "fnameescape({string})" function
12172 */
12173 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012174f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012175{
12176 rettv->vval.v_string = vim_strsave_fnameescape(
12177 get_tv_string(&argvars[0]), FALSE);
12178 rettv->v_type = VAR_STRING;
12179}
12180
12181/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012182 * "fnamemodify({fname}, {mods})" function
12183 */
12184 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012185f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012186{
12187 char_u *fname;
12188 char_u *mods;
12189 int usedlen = 0;
12190 int len;
12191 char_u *fbuf = NULL;
12192 char_u buf[NUMBUFLEN];
12193
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012194 fname = get_tv_string_chk(&argvars[0]);
12195 mods = get_tv_string_buf_chk(&argvars[1], buf);
12196 if (fname == NULL || mods == NULL)
12197 fname = NULL;
12198 else
12199 {
12200 len = (int)STRLEN(fname);
12201 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
12202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012203
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012204 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012205 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012206 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012207 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012208 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012209 vim_free(fbuf);
12210}
12211
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012212static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012213
12214/*
12215 * "foldclosed()" function
12216 */
12217 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012218foldclosed_both(
12219 typval_T *argvars UNUSED,
12220 typval_T *rettv,
12221 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012222{
12223#ifdef FEAT_FOLDING
12224 linenr_T lnum;
12225 linenr_T first, last;
12226
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012227 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012228 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12229 {
12230 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12231 {
12232 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012233 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012234 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012235 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012236 return;
12237 }
12238 }
12239#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012240 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012241}
12242
12243/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012244 * "foldclosed()" function
12245 */
12246 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012247f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012248{
12249 foldclosed_both(argvars, rettv, FALSE);
12250}
12251
12252/*
12253 * "foldclosedend()" function
12254 */
12255 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012256f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012257{
12258 foldclosed_both(argvars, rettv, TRUE);
12259}
12260
12261/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012262 * "foldlevel()" function
12263 */
12264 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012265f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012266{
12267#ifdef FEAT_FOLDING
12268 linenr_T lnum;
12269
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012270 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012271 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012272 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012274}
12275
12276/*
12277 * "foldtext()" function
12278 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012279 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012280f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012281{
12282#ifdef FEAT_FOLDING
12283 linenr_T lnum;
12284 char_u *s;
12285 char_u *r;
12286 int len;
12287 char *txt;
12288#endif
12289
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012290 rettv->v_type = VAR_STRING;
12291 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012292#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012293 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12294 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12295 <= curbuf->b_ml.ml_line_count
12296 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012297 {
12298 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012299 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12300 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012301 {
12302 if (!linewhite(lnum))
12303 break;
12304 ++lnum;
12305 }
12306
12307 /* Find interesting text in this line. */
12308 s = skipwhite(ml_get(lnum));
12309 /* skip C comment-start */
12310 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012311 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012312 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012313 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012314 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012315 {
12316 s = skipwhite(ml_get(lnum + 1));
12317 if (*s == '*')
12318 s = skipwhite(s + 1);
12319 }
12320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012321 txt = _("+-%s%3ld lines: ");
12322 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012323 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012324 + 20 /* for %3ld */
12325 + STRLEN(s))); /* concatenated */
12326 if (r != NULL)
12327 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012328 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12329 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12330 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012331 len = (int)STRLEN(r);
12332 STRCAT(r, s);
12333 /* remove 'foldmarker' and 'commentstring' */
12334 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012335 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012336 }
12337 }
12338#endif
12339}
12340
12341/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012342 * "foldtextresult(lnum)" function
12343 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012344 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012345f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012346{
12347#ifdef FEAT_FOLDING
12348 linenr_T lnum;
12349 char_u *text;
12350 char_u buf[51];
12351 foldinfo_T foldinfo;
12352 int fold_count;
12353#endif
12354
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012355 rettv->v_type = VAR_STRING;
12356 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012357#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012358 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012359 /* treat illegal types and illegal string values for {lnum} the same */
12360 if (lnum < 0)
12361 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012362 fold_count = foldedCount(curwin, lnum, &foldinfo);
12363 if (fold_count > 0)
12364 {
12365 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12366 &foldinfo, buf);
12367 if (text == buf)
12368 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012369 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012370 }
12371#endif
12372}
12373
12374/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012375 * "foreground()" function
12376 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012377 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012378f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012379{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012380#ifdef FEAT_GUI
12381 if (gui.in_use)
12382 gui_mch_set_foreground();
12383#else
12384# ifdef WIN32
12385 win32_set_foreground();
12386# endif
12387#endif
12388}
12389
12390/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012391 * "function()" function
12392 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012393 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012394f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012395{
12396 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012397 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012398 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012399 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012400
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012401 if (argvars[0].v_type == VAR_FUNC)
12402 {
12403 /* function(MyFunc, [arg], dict) */
12404 s = argvars[0].vval.v_string;
12405 }
12406 else if (argvars[0].v_type == VAR_PARTIAL
12407 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012408 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012409 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012410 arg_pt = argvars[0].vval.v_partial;
12411 s = arg_pt->pt_name;
12412 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012413 else
12414 {
12415 /* function('MyFunc', [arg], dict) */
12416 s = get_tv_string(&argvars[0]);
12417 use_string = TRUE;
12418 }
12419
12420 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012421 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012422 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012423 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
12424 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012425 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012426 else
12427 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012428 int dict_idx = 0;
12429 int arg_idx = 0;
12430 list_T *list = NULL;
12431
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012432 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012433 {
12434 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012435 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012436
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012437 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12438 * also be called from another script. Using trans_function_name()
12439 * would also work, but some plugins depend on the name being
12440 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012441 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012442 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
12443 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012444 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012445 STRCPY(name, sid_buf);
12446 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012447 }
12448 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012449 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012450 name = vim_strsave(s);
12451
12452 if (argvars[1].v_type != VAR_UNKNOWN)
12453 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012454 if (argvars[2].v_type != VAR_UNKNOWN)
12455 {
12456 /* function(name, [args], dict) */
12457 arg_idx = 1;
12458 dict_idx = 2;
12459 }
12460 else if (argvars[1].v_type == VAR_DICT)
12461 /* function(name, dict) */
12462 dict_idx = 1;
12463 else
12464 /* function(name, [args]) */
12465 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010012466 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012467 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012468 if (argvars[dict_idx].v_type != VAR_DICT)
12469 {
12470 EMSG(_("E922: expected a dict"));
12471 vim_free(name);
12472 return;
12473 }
12474 if (argvars[dict_idx].vval.v_dict == NULL)
12475 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012476 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012477 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012478 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012479 if (argvars[arg_idx].v_type != VAR_LIST)
12480 {
12481 EMSG(_("E923: Second argument of function() must be a list or a dict"));
12482 vim_free(name);
12483 return;
12484 }
12485 list = argvars[arg_idx].vval.v_list;
12486 if (list == NULL || list->lv_len == 0)
12487 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012488 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012489 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012490 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010012491 {
12492 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012493
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012494 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010012495 if (pt == NULL)
12496 vim_free(name);
12497 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012498 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012499 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012500 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012501 listitem_T *li;
12502 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012503 int arg_len = 0;
12504 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012505
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012506 if (arg_pt != NULL)
12507 arg_len = arg_pt->pt_argc;
12508 if (list != NULL)
12509 lv_len = list->lv_len;
12510 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012511 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012512 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012513 if (pt->pt_argv == NULL)
12514 {
12515 vim_free(pt);
12516 vim_free(name);
12517 return;
12518 }
12519 else
12520 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012521 for (i = 0; i < arg_len; i++)
12522 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
12523 if (lv_len > 0)
12524 for (li = list->lv_first; li != NULL;
12525 li = li->li_next)
12526 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012527 }
12528 }
12529
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012530 /* For "function(dict.func, [], dict)" and "func" is a partial
12531 * use "dict". That is backwards compatible. */
12532 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012533 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012534 /* The dict is bound explicitly, pt_auto is FALSE. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012535 pt->pt_dict = argvars[dict_idx].vval.v_dict;
12536 ++pt->pt_dict->dv_refcount;
12537 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012538 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012539 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012540 /* If the dict was bound automatically the result is also
12541 * bound automatically. */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012542 pt->pt_dict = arg_pt->pt_dict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020012543 pt->pt_auto = arg_pt->pt_auto;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012544 if (pt->pt_dict != NULL)
12545 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012546 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012547
12548 pt->pt_refcount = 1;
12549 pt->pt_name = name;
12550 func_ref(pt->pt_name);
12551 }
12552 rettv->v_type = VAR_PARTIAL;
12553 rettv->vval.v_partial = pt;
12554 }
12555 else
12556 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012557 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012558 rettv->v_type = VAR_FUNC;
12559 rettv->vval.v_string = name;
12560 func_ref(name);
12561 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012562 }
12563}
12564
12565/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012566 * "garbagecollect()" function
12567 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012568 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012569f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012570{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012571 /* This is postponed until we are back at the toplevel, because we may be
12572 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12573 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012574
12575 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12576 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012577}
12578
12579/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012580 * "get()" function
12581 */
12582 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012583f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012584{
Bram Moolenaar33570922005-01-25 22:26:29 +000012585 listitem_T *li;
12586 list_T *l;
12587 dictitem_T *di;
12588 dict_T *d;
12589 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012590
Bram Moolenaare9a41262005-01-15 22:18:47 +000012591 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012592 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012593 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012594 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012595 int error = FALSE;
12596
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020012597 li = list_find(l, (long)get_tv_number_chk(&argvars[1], &error));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012598 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012599 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012600 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012601 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012602 else if (argvars[0].v_type == VAR_DICT)
12603 {
12604 if ((d = argvars[0].vval.v_dict) != NULL)
12605 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012606 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012607 if (di != NULL)
12608 tv = &di->di_tv;
12609 }
12610 }
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012611 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012612 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012613 partial_T *pt;
12614 partial_T fref_pt;
12615
12616 if (argvars[0].v_type == VAR_PARTIAL)
12617 pt = argvars[0].vval.v_partial;
12618 else
12619 {
12620 vim_memset(&fref_pt, 0, sizeof(fref_pt));
12621 fref_pt.pt_name = argvars[0].vval.v_string;
12622 pt = &fref_pt;
12623 }
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012624
12625 if (pt != NULL)
12626 {
12627 char_u *what = get_tv_string(&argvars[1]);
12628
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012629 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012630 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012631 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012632 if (pt->pt_name == NULL)
12633 rettv->vval.v_string = NULL;
12634 else
12635 rettv->vval.v_string = vim_strsave(pt->pt_name);
12636 }
12637 else if (STRCMP(what, "dict") == 0)
12638 {
12639 rettv->v_type = VAR_DICT;
12640 rettv->vval.v_dict = pt->pt_dict;
12641 if (pt->pt_dict != NULL)
12642 ++pt->pt_dict->dv_refcount;
12643 }
12644 else if (STRCMP(what, "args") == 0)
12645 {
12646 rettv->v_type = VAR_LIST;
12647 if (rettv_list_alloc(rettv) == OK)
12648 {
12649 int i;
12650
12651 for (i = 0; i < pt->pt_argc; ++i)
12652 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
12653 }
12654 }
12655 else
12656 EMSG2(_(e_invarg2), what);
12657 return;
12658 }
12659 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012660 else
12661 EMSG2(_(e_listdictarg), "get()");
12662
12663 if (tv == NULL)
12664 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012665 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012666 copy_tv(&argvars[2], rettv);
12667 }
12668 else
12669 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012670}
12671
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012672static 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 +000012673
12674/*
12675 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012676 * Return a range (from start to end) of lines in rettv from the specified
12677 * buffer.
12678 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012679 */
12680 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012681get_buffer_lines(
12682 buf_T *buf,
12683 linenr_T start,
12684 linenr_T end,
12685 int retlist,
12686 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012687{
12688 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012689
Bram Moolenaar959a1432013-12-14 12:17:38 +010012690 rettv->v_type = VAR_STRING;
12691 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012692 if (retlist && rettv_list_alloc(rettv) == FAIL)
12693 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012694
12695 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12696 return;
12697
12698 if (!retlist)
12699 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012700 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12701 p = ml_get_buf(buf, start, FALSE);
12702 else
12703 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012704 rettv->vval.v_string = vim_strsave(p);
12705 }
12706 else
12707 {
12708 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012709 return;
12710
12711 if (start < 1)
12712 start = 1;
12713 if (end > buf->b_ml.ml_line_count)
12714 end = buf->b_ml.ml_line_count;
12715 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012716 if (list_append_string(rettv->vval.v_list,
12717 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012718 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012719 }
12720}
12721
12722/*
12723 * "getbufline()" function
12724 */
12725 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012726f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012727{
12728 linenr_T lnum;
12729 linenr_T end;
12730 buf_T *buf;
12731
12732 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12733 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012734 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012735 --emsg_off;
12736
Bram Moolenaar661b1822005-07-28 22:36:45 +000012737 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012738 if (argvars[2].v_type == VAR_UNKNOWN)
12739 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012740 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012741 end = get_tv_lnum_buf(&argvars[2], buf);
12742
Bram Moolenaar342337a2005-07-21 21:11:17 +000012743 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012744}
12745
Bram Moolenaar0d660222005-01-07 21:51:51 +000012746/*
12747 * "getbufvar()" function
12748 */
12749 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012750f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012751{
12752 buf_T *buf;
12753 buf_T *save_curbuf;
12754 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012755 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012756 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012757
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012758 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12759 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012760 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012761 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012762
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012763 rettv->v_type = VAR_STRING;
12764 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012765
12766 if (buf != NULL && varname != NULL)
12767 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012768 /* set curbuf to be our buf, temporarily */
12769 save_curbuf = curbuf;
12770 curbuf = buf;
12771
Bram Moolenaar0d660222005-01-07 21:51:51 +000012772 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012773 {
12774 if (get_option_tv(&varname, rettv, TRUE) == OK)
12775 done = TRUE;
12776 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012777 else if (STRCMP(varname, "changedtick") == 0)
12778 {
12779 rettv->v_type = VAR_NUMBER;
12780 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012781 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012782 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012783 else
12784 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012785 /* Look up the variable. */
12786 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12787 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12788 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012789 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012790 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012791 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012792 done = TRUE;
12793 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012794 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012795
12796 /* restore previous notion of curbuf */
12797 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012798 }
12799
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012800 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12801 /* use the default value */
12802 copy_tv(&argvars[2], rettv);
12803
Bram Moolenaar0d660222005-01-07 21:51:51 +000012804 --emsg_off;
12805}
12806
12807/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012808 * "getchar()" function
12809 */
12810 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012811f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012812{
12813 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012814 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012815
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012816 /* Position the cursor. Needed after a message that ends in a space. */
12817 windgoto(msg_row, msg_col);
12818
Bram Moolenaar071d4272004-06-13 20:20:40 +000012819 ++no_mapping;
12820 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012821 for (;;)
12822 {
12823 if (argvars[0].v_type == VAR_UNKNOWN)
12824 /* getchar(): blocking wait. */
12825 n = safe_vgetc();
12826 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12827 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012828 n = vpeekc_any();
12829 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012830 /* illegal argument or getchar(0) and no char avail: return zero */
12831 n = 0;
12832 else
12833 /* getchar(0) and char avail: return char */
12834 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012835
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012836 if (n == K_IGNORE)
12837 continue;
12838 break;
12839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012840 --no_mapping;
12841 --allow_keys;
12842
Bram Moolenaar219b8702006-11-01 14:32:36 +000012843 vimvars[VV_MOUSE_WIN].vv_nr = 0;
Bram Moolenaar511972d2016-06-04 18:09:59 +020012844 vimvars[VV_MOUSE_WINID].vv_nr = 0;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012845 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12846 vimvars[VV_MOUSE_COL].vv_nr = 0;
12847
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012848 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012849 if (IS_SPECIAL(n) || mod_mask != 0)
12850 {
12851 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12852 int i = 0;
12853
12854 /* Turn a special key into three bytes, plus modifier. */
12855 if (mod_mask != 0)
12856 {
12857 temp[i++] = K_SPECIAL;
12858 temp[i++] = KS_MODIFIER;
12859 temp[i++] = mod_mask;
12860 }
12861 if (IS_SPECIAL(n))
12862 {
12863 temp[i++] = K_SPECIAL;
12864 temp[i++] = K_SECOND(n);
12865 temp[i++] = K_THIRD(n);
12866 }
12867#ifdef FEAT_MBYTE
12868 else if (has_mbyte)
12869 i += (*mb_char2bytes)(n, temp + i);
12870#endif
12871 else
12872 temp[i++] = n;
12873 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012874 rettv->v_type = VAR_STRING;
12875 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012876
12877#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012878 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012879 {
12880 int row = mouse_row;
12881 int col = mouse_col;
12882 win_T *win;
12883 linenr_T lnum;
12884# ifdef FEAT_WINDOWS
12885 win_T *wp;
12886# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012887 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012888
12889 if (row >= 0 && col >= 0)
12890 {
12891 /* Find the window at the mouse coordinates and compute the
12892 * text position. */
12893 win = mouse_find_win(&row, &col);
12894 (void)mouse_comp_pos(win, &row, &col, &lnum);
12895# ifdef FEAT_WINDOWS
12896 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012897 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012898# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012899 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar511972d2016-06-04 18:09:59 +020012900 vimvars[VV_MOUSE_WINID].vv_nr = win->w_id;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012901 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12902 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12903 }
12904 }
12905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012906 }
12907}
12908
12909/*
12910 * "getcharmod()" function
12911 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012913f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012914{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012915 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012916}
12917
12918/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012919 * "getcharsearch()" function
12920 */
12921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012922f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012923{
12924 if (rettv_dict_alloc(rettv) != FAIL)
12925 {
12926 dict_T *dict = rettv->vval.v_dict;
12927
12928 dict_add_nr_str(dict, "char", 0L, last_csearch());
12929 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12930 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12931 }
12932}
12933
12934/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012935 * "getcmdline()" function
12936 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012938f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012939{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012940 rettv->v_type = VAR_STRING;
12941 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012942}
12943
12944/*
12945 * "getcmdpos()" function
12946 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012947 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012948f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012949{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012950 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012951}
12952
12953/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012954 * "getcmdtype()" function
12955 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012956 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012957f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012958{
12959 rettv->v_type = VAR_STRING;
12960 rettv->vval.v_string = alloc(2);
12961 if (rettv->vval.v_string != NULL)
12962 {
12963 rettv->vval.v_string[0] = get_cmdline_type();
12964 rettv->vval.v_string[1] = NUL;
12965 }
12966}
12967
12968/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012969 * "getcmdwintype()" function
12970 */
12971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012972f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012973{
12974 rettv->v_type = VAR_STRING;
12975 rettv->vval.v_string = NULL;
12976#ifdef FEAT_CMDWIN
12977 rettv->vval.v_string = alloc(2);
12978 if (rettv->vval.v_string != NULL)
12979 {
12980 rettv->vval.v_string[0] = cmdwin_type;
12981 rettv->vval.v_string[1] = NUL;
12982 }
12983#endif
12984}
12985
12986/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012987 * "getcwd()" function
12988 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012989 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012990f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012991{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012992 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012993 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012994
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012995 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012996 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012997
12998 wp = find_tabwin(&argvars[0], &argvars[1]);
12999 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013000 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010013001 if (wp->w_localdir != NULL)
13002 rettv->vval.v_string = vim_strsave(wp->w_localdir);
13003 else if(globaldir != NULL)
13004 rettv->vval.v_string = vim_strsave(globaldir);
13005 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020013006 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010013007 cwd = alloc(MAXPATHL);
13008 if (cwd != NULL)
13009 {
13010 if (mch_dirname(cwd, MAXPATHL) != FAIL)
13011 rettv->vval.v_string = vim_strsave(cwd);
13012 vim_free(cwd);
13013 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020013014 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010013015#ifdef BACKSLASH_IN_FILENAME
13016 if (rettv->vval.v_string != NULL)
13017 slash_adjust(rettv->vval.v_string);
13018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013019 }
13020}
13021
13022/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013023 * "getfontname()" function
13024 */
13025 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013026f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013027{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013028 rettv->v_type = VAR_STRING;
13029 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013030#ifdef FEAT_GUI
13031 if (gui.in_use)
13032 {
13033 GuiFont font;
13034 char_u *name = NULL;
13035
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013036 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013037 {
13038 /* Get the "Normal" font. Either the name saved by
13039 * hl_set_font_name() or from the font ID. */
13040 font = gui.norm_font;
13041 name = hl_get_font_name();
13042 }
13043 else
13044 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013045 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013046 if (STRCMP(name, "*") == 0) /* don't use font dialog */
13047 return;
13048 font = gui_mch_get_font(name, FALSE);
13049 if (font == NOFONT)
13050 return; /* Invalid font name, return empty string. */
13051 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013052 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013053 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000013054 gui_mch_free_font(font);
13055 }
13056#endif
13057}
13058
13059/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013060 * "getfperm({fname})" function
13061 */
13062 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013063f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013064{
13065 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020013066 stat_T st;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013067 char_u *perm = NULL;
13068 char_u flags[] = "rwx";
13069 int i;
13070
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013071 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013072
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013073 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013074 if (mch_stat((char *)fname, &st) >= 0)
13075 {
13076 perm = vim_strsave((char_u *)"---------");
13077 if (perm != NULL)
13078 {
13079 for (i = 0; i < 9; i++)
13080 {
13081 if (st.st_mode & (1 << (8 - i)))
13082 perm[i] = flags[i % 3];
13083 }
13084 }
13085 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013086 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013087}
13088
13089/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013090 * "getfsize({fname})" function
13091 */
13092 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013093f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013094{
13095 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020013096 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013097
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013098 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013099
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013100 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013101
13102 if (mch_stat((char *)fname, &st) >= 0)
13103 {
13104 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013105 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013106 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000013107 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013108 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000013109
13110 /* non-perfect check for overflow */
Bram Moolenaar8767f522016-07-01 17:17:39 +020013111 if ((off_T)rettv->vval.v_number != (off_T)st.st_size)
Bram Moolenaard827ada2007-06-19 15:19:55 +000013112 rettv->vval.v_number = -2;
13113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013114 }
13115 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013116 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013117}
13118
13119/*
13120 * "getftime({fname})" function
13121 */
13122 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013123f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013124{
13125 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020013126 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013127
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013128 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013129
13130 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013131 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013132 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013133 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013134}
13135
13136/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013137 * "getftype({fname})" function
13138 */
13139 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013140f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013141{
13142 char_u *fname;
Bram Moolenaar8767f522016-07-01 17:17:39 +020013143 stat_T st;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013144 char_u *type = NULL;
13145 char *t;
13146
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013147 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013148
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013149 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013150 if (mch_lstat((char *)fname, &st) >= 0)
13151 {
13152#ifdef S_ISREG
13153 if (S_ISREG(st.st_mode))
13154 t = "file";
13155 else if (S_ISDIR(st.st_mode))
13156 t = "dir";
13157# ifdef S_ISLNK
13158 else if (S_ISLNK(st.st_mode))
13159 t = "link";
13160# endif
13161# ifdef S_ISBLK
13162 else if (S_ISBLK(st.st_mode))
13163 t = "bdev";
13164# endif
13165# ifdef S_ISCHR
13166 else if (S_ISCHR(st.st_mode))
13167 t = "cdev";
13168# endif
13169# ifdef S_ISFIFO
13170 else if (S_ISFIFO(st.st_mode))
13171 t = "fifo";
13172# endif
13173# ifdef S_ISSOCK
13174 else if (S_ISSOCK(st.st_mode))
13175 t = "fifo";
13176# endif
13177 else
13178 t = "other";
13179#else
13180# ifdef S_IFMT
13181 switch (st.st_mode & S_IFMT)
13182 {
13183 case S_IFREG: t = "file"; break;
13184 case S_IFDIR: t = "dir"; break;
13185# ifdef S_IFLNK
13186 case S_IFLNK: t = "link"; break;
13187# endif
13188# ifdef S_IFBLK
13189 case S_IFBLK: t = "bdev"; break;
13190# endif
13191# ifdef S_IFCHR
13192 case S_IFCHR: t = "cdev"; break;
13193# endif
13194# ifdef S_IFIFO
13195 case S_IFIFO: t = "fifo"; break;
13196# endif
13197# ifdef S_IFSOCK
13198 case S_IFSOCK: t = "socket"; break;
13199# endif
13200 default: t = "other";
13201 }
13202# else
13203 if (mch_isdir(fname))
13204 t = "dir";
13205 else
13206 t = "file";
13207# endif
13208#endif
13209 type = vim_strsave((char_u *)t);
13210 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013211 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013212}
13213
13214/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013215 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000013216 */
13217 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013218f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013219{
13220 linenr_T lnum;
13221 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013222 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013223
13224 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013225 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000013226 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013227 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013228 retlist = FALSE;
13229 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013230 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000013231 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013232 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000013233 retlist = TRUE;
13234 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013235
Bram Moolenaar342337a2005-07-21 21:11:17 +000013236 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013237}
13238
13239/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013240 * "getmatches()" function
13241 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013243f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013244{
13245#ifdef FEAT_SEARCH_EXTRA
13246 dict_T *dict;
13247 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013248 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013249
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013250 if (rettv_list_alloc(rettv) == OK)
13251 {
13252 while (cur != NULL)
13253 {
13254 dict = dict_alloc();
13255 if (dict == NULL)
13256 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013257 if (cur->match.regprog == NULL)
13258 {
13259 /* match added with matchaddpos() */
13260 for (i = 0; i < MAXPOSMATCH; ++i)
13261 {
13262 llpos_T *llpos;
13263 char buf[6];
13264 list_T *l;
13265
13266 llpos = &cur->pos.pos[i];
13267 if (llpos->lnum == 0)
13268 break;
13269 l = list_alloc();
13270 if (l == NULL)
13271 break;
13272 list_append_number(l, (varnumber_T)llpos->lnum);
13273 if (llpos->col > 0)
13274 {
13275 list_append_number(l, (varnumber_T)llpos->col);
13276 list_append_number(l, (varnumber_T)llpos->len);
13277 }
13278 sprintf(buf, "pos%d", i + 1);
13279 dict_add_list(dict, buf, l);
13280 }
13281 }
13282 else
13283 {
13284 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
13285 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013286 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013287 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
13288 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar42356152016-03-31 22:27:40 +020013289# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +020013290 if (cur->conceal_char)
13291 {
13292 char_u buf[MB_MAXBYTES + 1];
13293
13294 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
13295 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
13296 }
13297# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013298 list_append_dict(rettv->vval.v_list, dict);
13299 cur = cur->next;
13300 }
13301 }
13302#endif
13303}
13304
13305/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000013306 * "getpid()" function
13307 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000013308 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013309f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013310{
13311 rettv->vval.v_number = mch_get_pid();
13312}
13313
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013315getpos_both(
13316 typval_T *argvars,
13317 typval_T *rettv,
13318 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013319{
Bram Moolenaara5525202006-03-02 22:52:09 +000013320 pos_T *fp;
13321 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013322 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013323
13324 if (rettv_list_alloc(rettv) == OK)
13325 {
13326 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013327 if (getcurpos)
13328 fp = &curwin->w_cursor;
13329 else
13330 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013331 if (fnum != -1)
13332 list_append_number(l, (varnumber_T)fnum);
13333 else
13334 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013335 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13336 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013337 list_append_number(l, (fp != NULL)
13338 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013339 : (varnumber_T)0);
13340 list_append_number(l,
13341#ifdef FEAT_VIRTUALEDIT
13342 (fp != NULL) ? (varnumber_T)fp->coladd :
13343#endif
13344 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013345 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013346 {
13347 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013348 list_append_number(l, curwin->w_curswant == MAXCOL ?
13349 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013350 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013351 }
13352 else
13353 rettv->vval.v_number = FALSE;
13354}
13355
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020013356
13357/*
13358 * "getcurpos()" function
13359 */
13360 static void
13361f_getcurpos(typval_T *argvars, typval_T *rettv)
13362{
13363 getpos_both(argvars, rettv, TRUE);
13364}
13365
13366/*
13367 * "getpos(string)" function
13368 */
13369 static void
13370f_getpos(typval_T *argvars, typval_T *rettv)
13371{
13372 getpos_both(argvars, rettv, FALSE);
13373}
13374
Bram Moolenaara5525202006-03-02 22:52:09 +000013375/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013376 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013377 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013378 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013379f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013380{
13381#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013382 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013383#endif
13384
Bram Moolenaar2641f772005-03-25 21:58:17 +000013385#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013386 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013387 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013388 wp = NULL;
13389 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13390 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013391 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013392 if (wp == NULL)
13393 return;
13394 }
13395
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013396 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013397 }
13398#endif
13399}
13400
13401/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013402 * "getreg()" function
13403 */
13404 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013405f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013406{
13407 char_u *strregname;
13408 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013409 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013410 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013411 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013412
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013413 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013414 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013415 strregname = get_tv_string_chk(&argvars[0]);
13416 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013417 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013418 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013419 arg2 = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013420 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013421 return_list = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013422 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013424 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013425 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013426
13427 if (error)
13428 return;
13429
Bram Moolenaar071d4272004-06-13 20:20:40 +000013430 regname = (strregname == NULL ? '"' : *strregname);
13431 if (regname == 0)
13432 regname = '"';
13433
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013434 if (return_list)
13435 {
13436 rettv->v_type = VAR_LIST;
13437 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13438 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013439 if (rettv->vval.v_list == NULL)
Bram Moolenaar8ed43912016-04-21 08:56:12 +020013440 (void)rettv_list_alloc(rettv);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013441 else
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013442 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013443 }
13444 else
13445 {
13446 rettv->v_type = VAR_STRING;
13447 rettv->vval.v_string = get_reg_contents(regname,
13448 arg2 ? GREG_EXPR_SRC : 0);
13449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013450}
13451
13452/*
13453 * "getregtype()" function
13454 */
13455 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013456f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013457{
13458 char_u *strregname;
13459 int regname;
13460 char_u buf[NUMBUFLEN + 2];
13461 long reglen = 0;
13462
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013463 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013464 {
13465 strregname = get_tv_string_chk(&argvars[0]);
13466 if (strregname == NULL) /* type error; errmsg already given */
13467 {
13468 rettv->v_type = VAR_STRING;
13469 rettv->vval.v_string = NULL;
13470 return;
13471 }
13472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013473 else
13474 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013475 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013476
13477 regname = (strregname == NULL ? '"' : *strregname);
13478 if (regname == 0)
13479 regname = '"';
13480
13481 buf[0] = NUL;
13482 buf[1] = NUL;
13483 switch (get_reg_type(regname, &reglen))
13484 {
13485 case MLINE: buf[0] = 'V'; break;
13486 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013487 case MBLOCK:
13488 buf[0] = Ctrl_V;
13489 sprintf((char *)buf + 1, "%ld", reglen + 1);
13490 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013491 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013492 rettv->v_type = VAR_STRING;
13493 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013494}
13495
13496/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013497 * "gettabvar()" function
13498 */
13499 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013500f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013501{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013502 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013503 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013504 dictitem_T *v;
13505 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013506 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013507
13508 rettv->v_type = VAR_STRING;
13509 rettv->vval.v_string = NULL;
13510
13511 varname = get_tv_string_chk(&argvars[1]);
13512 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13513 if (tp != NULL && varname != NULL)
13514 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013515 /* Set tp to be our tabpage, temporarily. Also set the window to the
13516 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013517 if (switch_win(&oldcurwin, &oldtabpage,
13518 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013519 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013520 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013521 /* look up the variable */
13522 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13523 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13524 if (v != NULL)
13525 {
13526 copy_tv(&v->di_tv, rettv);
13527 done = TRUE;
13528 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013529 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013530
13531 /* restore previous notion of curwin */
13532 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013533 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013534
13535 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013536 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013537}
13538
13539/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013540 * "gettabwinvar()" function
13541 */
13542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013543f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013544{
13545 getwinvar(argvars, rettv, 1);
13546}
13547
13548/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013549 * "getwinposx()" function
13550 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013551 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013552f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013553{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013554 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013555#ifdef FEAT_GUI
13556 if (gui.in_use)
13557 {
13558 int x, y;
13559
13560 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013561 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013562 }
13563#endif
13564}
13565
13566/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010013567 * "win_findbuf()" function
13568 */
13569 static void
13570f_win_findbuf(typval_T *argvars, typval_T *rettv)
13571{
13572 if (rettv_list_alloc(rettv) != FAIL)
13573 win_findbuf(argvars, rettv->vval.v_list);
13574}
13575
13576/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010013577 * "win_getid()" function
13578 */
13579 static void
13580f_win_getid(typval_T *argvars, typval_T *rettv)
13581{
13582 rettv->vval.v_number = win_getid(argvars);
13583}
13584
13585/*
13586 * "win_gotoid()" function
13587 */
13588 static void
13589f_win_gotoid(typval_T *argvars, typval_T *rettv)
13590{
13591 rettv->vval.v_number = win_gotoid(argvars);
13592}
13593
13594/*
13595 * "win_id2tabwin()" function
13596 */
13597 static void
13598f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13599{
13600 if (rettv_list_alloc(rettv) != FAIL)
13601 win_id2tabwin(argvars, rettv->vval.v_list);
13602}
13603
13604/*
13605 * "win_id2win()" function
13606 */
13607 static void
13608f_win_id2win(typval_T *argvars, typval_T *rettv)
13609{
13610 rettv->vval.v_number = win_id2win(argvars);
13611}
13612
13613/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013614 * "getwinposy()" function
13615 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013617f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013618{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013619 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013620#ifdef FEAT_GUI
13621 if (gui.in_use)
13622 {
13623 int x, y;
13624
13625 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013626 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013627 }
13628#endif
13629}
13630
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013631/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013632 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013633 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013634 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013635find_win_by_nr(
13636 typval_T *vp,
13637 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013638{
13639#ifdef FEAT_WINDOWS
13640 win_T *wp;
13641#endif
13642 int nr;
13643
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013644 nr = (int)get_tv_number_chk(vp, NULL);
Bram Moolenaara40058a2005-07-11 22:42:07 +000013645
13646#ifdef FEAT_WINDOWS
13647 if (nr < 0)
13648 return NULL;
13649 if (nr == 0)
13650 return curwin;
13651
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013652 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13653 wp != NULL; wp = wp->w_next)
Bram Moolenaar888ccac2016-06-04 18:49:36 +020013654 if (nr >= LOWEST_WIN_ID)
13655 {
13656 if (wp->w_id == nr)
13657 return wp;
13658 }
13659 else if (--nr <= 0)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013660 break;
Bram Moolenaar888ccac2016-06-04 18:49:36 +020013661 if (nr >= LOWEST_WIN_ID)
13662 return NULL;
Bram Moolenaara40058a2005-07-11 22:42:07 +000013663 return wp;
13664#else
Bram Moolenaar888ccac2016-06-04 18:49:36 +020013665 if (nr == 0 || nr == 1 || nr == curwin->w_id)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013666 return curwin;
13667 return NULL;
13668#endif
13669}
13670
Bram Moolenaar071d4272004-06-13 20:20:40 +000013671/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013672 * Find window specified by "wvp" in tabpage "tvp".
13673 */
13674 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013675find_tabwin(
13676 typval_T *wvp, /* VAR_UNKNOWN for current window */
13677 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013678{
13679 win_T *wp = NULL;
13680 tabpage_T *tp = NULL;
13681 long n;
13682
13683 if (wvp->v_type != VAR_UNKNOWN)
13684 {
13685 if (tvp->v_type != VAR_UNKNOWN)
13686 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020013687 n = (long)get_tv_number(tvp);
Bram Moolenaarc9703302016-01-17 21:49:33 +010013688 if (n >= 0)
13689 tp = find_tabpage(n);
13690 }
13691 else
13692 tp = curtab;
13693
13694 if (tp != NULL)
13695 wp = find_win_by_nr(wvp, tp);
13696 }
13697 else
13698 wp = curwin;
13699
13700 return wp;
13701}
13702
13703/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013704 * "getwinvar()" function
13705 */
13706 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013707f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013708{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013709 getwinvar(argvars, rettv, 0);
13710}
13711
13712/*
13713 * getwinvar() and gettabwinvar()
13714 */
13715 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013716getwinvar(
13717 typval_T *argvars,
13718 typval_T *rettv,
13719 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013720{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013721 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013722 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013723 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013724 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013725 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013726#ifdef FEAT_WINDOWS
13727 win_T *oldcurwin;
13728 tabpage_T *oldtabpage;
13729 int need_switch_win;
13730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013731
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013732#ifdef FEAT_WINDOWS
13733 if (off == 1)
13734 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13735 else
13736 tp = curtab;
13737#endif
13738 win = find_win_by_nr(&argvars[off], tp);
13739 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013740 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013741
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013742 rettv->v_type = VAR_STRING;
13743 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013744
13745 if (win != NULL && varname != NULL)
13746 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013747#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013748 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013749 * otherwise the window is not valid. Only do this when needed,
13750 * autocommands get blocked. */
13751 need_switch_win = !(tp == curtab && win == curwin);
13752 if (!need_switch_win
13753 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13754#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013755 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013756 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013757 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013758 if (get_option_tv(&varname, rettv, 1) == OK)
13759 done = TRUE;
13760 }
13761 else
13762 {
13763 /* Look up the variable. */
13764 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13765 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13766 varname, FALSE);
13767 if (v != NULL)
13768 {
13769 copy_tv(&v->di_tv, rettv);
13770 done = TRUE;
13771 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013773 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013774
Bram Moolenaarba117c22015-09-29 16:53:22 +020013775#ifdef FEAT_WINDOWS
13776 if (need_switch_win)
13777 /* restore previous notion of curwin */
13778 restore_win(oldcurwin, oldtabpage, TRUE);
13779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013780 }
13781
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013782 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13783 /* use the default return value */
13784 copy_tv(&argvars[off + 2], rettv);
13785
Bram Moolenaar071d4272004-06-13 20:20:40 +000013786 --emsg_off;
13787}
13788
13789/*
13790 * "glob()" function
13791 */
13792 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013793f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013794{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013795 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013796 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013797 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013798
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013799 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013800 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013801 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013802 if (argvars[1].v_type != VAR_UNKNOWN)
13803 {
13804 if (get_tv_number_chk(&argvars[1], &error))
13805 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013806 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013807 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013808 if (get_tv_number_chk(&argvars[2], &error))
13809 {
13810 rettv->v_type = VAR_LIST;
13811 rettv->vval.v_list = NULL;
13812 }
13813 if (argvars[3].v_type != VAR_UNKNOWN
13814 && get_tv_number_chk(&argvars[3], &error))
13815 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013816 }
13817 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013818 if (!error)
13819 {
13820 ExpandInit(&xpc);
13821 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013822 if (p_wic)
13823 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013824 if (rettv->v_type == VAR_STRING)
13825 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013826 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013827 else if (rettv_list_alloc(rettv) != FAIL)
13828 {
13829 int i;
13830
13831 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13832 NULL, options, WILD_ALL_KEEP);
13833 for (i = 0; i < xpc.xp_numfiles; i++)
13834 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13835
13836 ExpandCleanup(&xpc);
13837 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013838 }
13839 else
13840 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013841}
13842
13843/*
13844 * "globpath()" function
13845 */
13846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013847f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013848{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013849 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013850 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013851 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013852 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013853 garray_T ga;
13854 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013855
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013856 /* When the optional second argument is non-zero, don't remove matches
13857 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013858 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013859 if (argvars[2].v_type != VAR_UNKNOWN)
13860 {
13861 if (get_tv_number_chk(&argvars[2], &error))
13862 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013863 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013864 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013865 if (get_tv_number_chk(&argvars[3], &error))
13866 {
13867 rettv->v_type = VAR_LIST;
13868 rettv->vval.v_list = NULL;
13869 }
13870 if (argvars[4].v_type != VAR_UNKNOWN
13871 && get_tv_number_chk(&argvars[4], &error))
13872 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013873 }
13874 }
13875 if (file != NULL && !error)
13876 {
13877 ga_init2(&ga, (int)sizeof(char_u *), 10);
13878 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13879 if (rettv->v_type == VAR_STRING)
13880 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13881 else if (rettv_list_alloc(rettv) != FAIL)
13882 for (i = 0; i < ga.ga_len; ++i)
13883 list_append_string(rettv->vval.v_list,
13884 ((char_u **)(ga.ga_data))[i], -1);
13885 ga_clear_strings(&ga);
13886 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013887 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013888 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013889}
13890
13891/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013892 * "glob2regpat()" function
13893 */
13894 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013895f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013896{
13897 char_u *pat = get_tv_string_chk(&argvars[0]);
13898
13899 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013900 rettv->vval.v_string = (pat == NULL)
13901 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013902}
13903
13904/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013905 * "has()" function
13906 */
13907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013908f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013909{
13910 int i;
13911 char_u *name;
13912 int n = FALSE;
13913 static char *(has_list[]) =
13914 {
13915#ifdef AMIGA
13916 "amiga",
13917# ifdef FEAT_ARP
13918 "arp",
13919# endif
13920#endif
13921#ifdef __BEOS__
13922 "beos",
13923#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013924#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013925 "mac",
13926#endif
13927#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013928 "macunix", /* built with 'darwin' enabled */
13929#endif
13930#if defined(__APPLE__) && __APPLE__ == 1
13931 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013932#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013933#ifdef __QNX__
13934 "qnx",
13935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013936#ifdef UNIX
13937 "unix",
13938#endif
13939#ifdef VMS
13940 "vms",
13941#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013942#ifdef WIN32
13943 "win32",
13944#endif
13945#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13946 "win32unix",
13947#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013948#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013949 "win64",
13950#endif
13951#ifdef EBCDIC
13952 "ebcdic",
13953#endif
13954#ifndef CASE_INSENSITIVE_FILENAME
13955 "fname_case",
13956#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013957#ifdef HAVE_ACL
13958 "acl",
13959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013960#ifdef FEAT_ARABIC
13961 "arabic",
13962#endif
13963#ifdef FEAT_AUTOCMD
13964 "autocmd",
13965#endif
13966#ifdef FEAT_BEVAL
13967 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013968# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13969 "balloon_multiline",
13970# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013971#endif
13972#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13973 "builtin_terms",
13974# ifdef ALL_BUILTIN_TCAPS
13975 "all_builtin_terms",
13976# endif
13977#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013978#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13979 || defined(FEAT_GUI_W32) \
13980 || defined(FEAT_GUI_MOTIF))
13981 "browsefilter",
13982#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013983#ifdef FEAT_BYTEOFF
13984 "byte_offset",
13985#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013986#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013987 "channel",
13988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013989#ifdef FEAT_CINDENT
13990 "cindent",
13991#endif
13992#ifdef FEAT_CLIENTSERVER
13993 "clientserver",
13994#endif
13995#ifdef FEAT_CLIPBOARD
13996 "clipboard",
13997#endif
13998#ifdef FEAT_CMDL_COMPL
13999 "cmdline_compl",
14000#endif
14001#ifdef FEAT_CMDHIST
14002 "cmdline_hist",
14003#endif
14004#ifdef FEAT_COMMENTS
14005 "comments",
14006#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020014007#ifdef FEAT_CONCEAL
14008 "conceal",
14009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014010#ifdef FEAT_CRYPT
14011 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010014012 "crypt-blowfish",
14013 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014014#endif
14015#ifdef FEAT_CSCOPE
14016 "cscope",
14017#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020014018#ifdef FEAT_CURSORBIND
14019 "cursorbind",
14020#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000014021#ifdef CURSOR_SHAPE
14022 "cursorshape",
14023#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014024#ifdef DEBUG
14025 "debug",
14026#endif
14027#ifdef FEAT_CON_DIALOG
14028 "dialog_con",
14029#endif
14030#ifdef FEAT_GUI_DIALOG
14031 "dialog_gui",
14032#endif
14033#ifdef FEAT_DIFF
14034 "diff",
14035#endif
14036#ifdef FEAT_DIGRAPHS
14037 "digraphs",
14038#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020014039#ifdef FEAT_DIRECTX
14040 "directx",
14041#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014042#ifdef FEAT_DND
14043 "dnd",
14044#endif
14045#ifdef FEAT_EMACS_TAGS
14046 "emacs_tags",
14047#endif
14048 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010014049 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014050#ifdef FEAT_SEARCH_EXTRA
14051 "extra_search",
14052#endif
14053#ifdef FEAT_FKMAP
14054 "farsi",
14055#endif
14056#ifdef FEAT_SEARCHPATH
14057 "file_in_path",
14058#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020014059#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014060 "filterpipe",
14061#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014062#ifdef FEAT_FIND_ID
14063 "find_in_path",
14064#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014065#ifdef FEAT_FLOAT
14066 "float",
14067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014068#ifdef FEAT_FOLDING
14069 "folding",
14070#endif
14071#ifdef FEAT_FOOTER
14072 "footer",
14073#endif
14074#if !defined(USE_SYSTEM) && defined(UNIX)
14075 "fork",
14076#endif
14077#ifdef FEAT_GETTEXT
14078 "gettext",
14079#endif
14080#ifdef FEAT_GUI
14081 "gui",
14082#endif
14083#ifdef FEAT_GUI_ATHENA
14084# ifdef FEAT_GUI_NEXTAW
14085 "gui_neXtaw",
14086# else
14087 "gui_athena",
14088# endif
14089#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014090#ifdef FEAT_GUI_GTK
14091 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010014092# ifdef USE_GTK3
14093 "gui_gtk3",
14094# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000014095 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010014096# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014097#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000014098#ifdef FEAT_GUI_GNOME
14099 "gui_gnome",
14100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014101#ifdef FEAT_GUI_MAC
14102 "gui_mac",
14103#endif
14104#ifdef FEAT_GUI_MOTIF
14105 "gui_motif",
14106#endif
14107#ifdef FEAT_GUI_PHOTON
14108 "gui_photon",
14109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014110#ifdef FEAT_GUI_W32
14111 "gui_win32",
14112#endif
14113#ifdef FEAT_HANGULIN
14114 "hangul_input",
14115#endif
14116#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
14117 "iconv",
14118#endif
14119#ifdef FEAT_INS_EXPAND
14120 "insert_expand",
14121#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014122#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010014123 "job",
14124#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014125#ifdef FEAT_JUMPLIST
14126 "jumplist",
14127#endif
14128#ifdef FEAT_KEYMAP
14129 "keymap",
14130#endif
14131#ifdef FEAT_LANGMAP
14132 "langmap",
14133#endif
14134#ifdef FEAT_LIBCALL
14135 "libcall",
14136#endif
14137#ifdef FEAT_LINEBREAK
14138 "linebreak",
14139#endif
14140#ifdef FEAT_LISP
14141 "lispindent",
14142#endif
14143#ifdef FEAT_LISTCMDS
14144 "listcmds",
14145#endif
14146#ifdef FEAT_LOCALMAP
14147 "localmap",
14148#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014149#ifdef FEAT_LUA
14150# ifndef DYNAMIC_LUA
14151 "lua",
14152# endif
14153#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014154#ifdef FEAT_MENU
14155 "menu",
14156#endif
14157#ifdef FEAT_SESSION
14158 "mksession",
14159#endif
14160#ifdef FEAT_MODIFY_FNAME
14161 "modify_fname",
14162#endif
14163#ifdef FEAT_MOUSE
14164 "mouse",
14165#endif
14166#ifdef FEAT_MOUSESHAPE
14167 "mouseshape",
14168#endif
14169#if defined(UNIX) || defined(VMS)
14170# ifdef FEAT_MOUSE_DEC
14171 "mouse_dec",
14172# endif
14173# ifdef FEAT_MOUSE_GPM
14174 "mouse_gpm",
14175# endif
14176# ifdef FEAT_MOUSE_JSB
14177 "mouse_jsbterm",
14178# endif
14179# ifdef FEAT_MOUSE_NET
14180 "mouse_netterm",
14181# endif
14182# ifdef FEAT_MOUSE_PTERM
14183 "mouse_pterm",
14184# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020014185# ifdef FEAT_MOUSE_SGR
14186 "mouse_sgr",
14187# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014188# ifdef FEAT_SYSMOUSE
14189 "mouse_sysmouse",
14190# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020014191# ifdef FEAT_MOUSE_URXVT
14192 "mouse_urxvt",
14193# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014194# ifdef FEAT_MOUSE_XTERM
14195 "mouse_xterm",
14196# endif
14197#endif
14198#ifdef FEAT_MBYTE
14199 "multi_byte",
14200#endif
14201#ifdef FEAT_MBYTE_IME
14202 "multi_byte_ime",
14203#endif
14204#ifdef FEAT_MULTI_LANG
14205 "multi_lang",
14206#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014207#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000014208#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014209 "mzscheme",
14210#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014211#endif
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014212#ifdef FEAT_NUM64
14213 "num64",
14214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014215#ifdef FEAT_OLE
14216 "ole",
14217#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010014218 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014219#ifdef FEAT_PATH_EXTRA
14220 "path_extra",
14221#endif
14222#ifdef FEAT_PERL
14223#ifndef DYNAMIC_PERL
14224 "perl",
14225#endif
14226#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020014227#ifdef FEAT_PERSISTENT_UNDO
14228 "persistent_undo",
14229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014230#ifdef FEAT_PYTHON
14231#ifndef DYNAMIC_PYTHON
14232 "python",
14233#endif
14234#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014235#ifdef FEAT_PYTHON3
14236#ifndef DYNAMIC_PYTHON3
14237 "python3",
14238#endif
14239#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014240#ifdef FEAT_POSTSCRIPT
14241 "postscript",
14242#endif
14243#ifdef FEAT_PRINTER
14244 "printer",
14245#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000014246#ifdef FEAT_PROFILE
14247 "profile",
14248#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014249#ifdef FEAT_RELTIME
14250 "reltime",
14251#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014252#ifdef FEAT_QUICKFIX
14253 "quickfix",
14254#endif
14255#ifdef FEAT_RIGHTLEFT
14256 "rightleft",
14257#endif
14258#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
14259 "ruby",
14260#endif
14261#ifdef FEAT_SCROLLBIND
14262 "scrollbind",
14263#endif
14264#ifdef FEAT_CMDL_INFO
14265 "showcmd",
14266 "cmdline_info",
14267#endif
14268#ifdef FEAT_SIGNS
14269 "signs",
14270#endif
14271#ifdef FEAT_SMARTINDENT
14272 "smartindent",
14273#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000014274#ifdef STARTUPTIME
14275 "startuptime",
14276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014277#ifdef FEAT_STL_OPT
14278 "statusline",
14279#endif
14280#ifdef FEAT_SUN_WORKSHOP
14281 "sun_workshop",
14282#endif
14283#ifdef FEAT_NETBEANS_INTG
14284 "netbeans_intg",
14285#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014286#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000014287 "spell",
14288#endif
14289#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000014290 "syntax",
14291#endif
14292#if defined(USE_SYSTEM) || !defined(UNIX)
14293 "system",
14294#endif
14295#ifdef FEAT_TAG_BINS
14296 "tag_binary",
14297#endif
14298#ifdef FEAT_TAG_OLDSTATIC
14299 "tag_old_static",
14300#endif
14301#ifdef FEAT_TAG_ANYWHITE
14302 "tag_any_white",
14303#endif
14304#ifdef FEAT_TCL
14305# ifndef DYNAMIC_TCL
14306 "tcl",
14307# endif
14308#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020014309#ifdef FEAT_TERMGUICOLORS
14310 "termguicolors",
14311#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014312#ifdef TERMINFO
14313 "terminfo",
14314#endif
14315#ifdef FEAT_TERMRESPONSE
14316 "termresponse",
14317#endif
14318#ifdef FEAT_TEXTOBJ
14319 "textobjects",
14320#endif
14321#ifdef HAVE_TGETENT
14322 "tgetent",
14323#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010014324#ifdef FEAT_TIMERS
14325 "timers",
14326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014327#ifdef FEAT_TITLE
14328 "title",
14329#endif
14330#ifdef FEAT_TOOLBAR
14331 "toolbar",
14332#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014333#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14334 "unnamedplus",
14335#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014336#ifdef FEAT_USR_CMDS
14337 "user-commands", /* was accidentally included in 5.4 */
14338 "user_commands",
14339#endif
14340#ifdef FEAT_VIMINFO
14341 "viminfo",
14342#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010014343#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000014344 "vertsplit",
14345#endif
14346#ifdef FEAT_VIRTUALEDIT
14347 "virtualedit",
14348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014349 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014350#ifdef FEAT_VISUALEXTRA
14351 "visualextra",
14352#endif
14353#ifdef FEAT_VREPLACE
14354 "vreplace",
14355#endif
14356#ifdef FEAT_WILDIGN
14357 "wildignore",
14358#endif
14359#ifdef FEAT_WILDMENU
14360 "wildmenu",
14361#endif
14362#ifdef FEAT_WINDOWS
14363 "windows",
14364#endif
14365#ifdef FEAT_WAK
14366 "winaltkeys",
14367#endif
14368#ifdef FEAT_WRITEBACKUP
14369 "writebackup",
14370#endif
14371#ifdef FEAT_XIM
14372 "xim",
14373#endif
14374#ifdef FEAT_XFONTSET
14375 "xfontset",
14376#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014377#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014378 "xpm",
14379 "xpm_w32", /* for backward compatibility */
14380#else
14381# if defined(HAVE_XPM)
14382 "xpm",
14383# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014384#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014385#ifdef USE_XSMP
14386 "xsmp",
14387#endif
14388#ifdef USE_XSMP_INTERACT
14389 "xsmp_interact",
14390#endif
14391#ifdef FEAT_XCLIPBOARD
14392 "xterm_clipboard",
14393#endif
14394#ifdef FEAT_XTERM_SAVE
14395 "xterm_save",
14396#endif
14397#if defined(UNIX) && defined(FEAT_X11)
14398 "X11",
14399#endif
14400 NULL
14401 };
14402
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014403 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014404 for (i = 0; has_list[i] != NULL; ++i)
14405 if (STRICMP(name, has_list[i]) == 0)
14406 {
14407 n = TRUE;
14408 break;
14409 }
14410
14411 if (n == FALSE)
14412 {
14413 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014414 {
14415 if (name[5] == '-'
Bram Moolenaar819821c2016-03-26 21:24:14 +010014416 && STRLEN(name) >= 11
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014417 && vim_isdigit(name[6])
14418 && vim_isdigit(name[8])
14419 && vim_isdigit(name[10]))
14420 {
14421 int major = atoi((char *)name + 6);
14422 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014423
14424 /* Expect "patch-9.9.01234". */
14425 n = (major < VIM_VERSION_MAJOR
14426 || (major == VIM_VERSION_MAJOR
14427 && (minor < VIM_VERSION_MINOR
14428 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014429 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014430 }
14431 else
14432 n = has_patch(atoi((char *)name + 5));
14433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014434 else if (STRICMP(name, "vim_starting") == 0)
14435 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014436#ifdef FEAT_MBYTE
14437 else if (STRICMP(name, "multi_byte_encoding") == 0)
14438 n = has_mbyte;
14439#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014440#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14441 else if (STRICMP(name, "balloon_multiline") == 0)
14442 n = multiline_balloon_available();
14443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014444#ifdef DYNAMIC_TCL
14445 else if (STRICMP(name, "tcl") == 0)
14446 n = tcl_enabled(FALSE);
14447#endif
14448#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14449 else if (STRICMP(name, "iconv") == 0)
14450 n = iconv_enabled(FALSE);
14451#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014452#ifdef DYNAMIC_LUA
14453 else if (STRICMP(name, "lua") == 0)
14454 n = lua_enabled(FALSE);
14455#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014456#ifdef DYNAMIC_MZSCHEME
14457 else if (STRICMP(name, "mzscheme") == 0)
14458 n = mzscheme_enabled(FALSE);
14459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014460#ifdef DYNAMIC_RUBY
14461 else if (STRICMP(name, "ruby") == 0)
14462 n = ruby_enabled(FALSE);
14463#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014464#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014465#ifdef DYNAMIC_PYTHON
14466 else if (STRICMP(name, "python") == 0)
14467 n = python_enabled(FALSE);
14468#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014469#endif
14470#ifdef FEAT_PYTHON3
14471#ifdef DYNAMIC_PYTHON3
14472 else if (STRICMP(name, "python3") == 0)
14473 n = python3_enabled(FALSE);
14474#endif
14475#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014476#ifdef DYNAMIC_PERL
14477 else if (STRICMP(name, "perl") == 0)
14478 n = perl_enabled(FALSE);
14479#endif
14480#ifdef FEAT_GUI
14481 else if (STRICMP(name, "gui_running") == 0)
14482 n = (gui.in_use || gui.starting);
14483# ifdef FEAT_GUI_W32
14484 else if (STRICMP(name, "gui_win32s") == 0)
14485 n = gui_is_win32s();
14486# endif
14487# ifdef FEAT_BROWSE
14488 else if (STRICMP(name, "browse") == 0)
14489 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14490# endif
14491#endif
14492#ifdef FEAT_SYN_HL
14493 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014494 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014495#endif
14496#if defined(WIN3264)
14497 else if (STRICMP(name, "win95") == 0)
14498 n = mch_windows95();
14499#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014500#ifdef FEAT_NETBEANS_INTG
14501 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014502 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014503#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014504 }
14505
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014506 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014507}
14508
14509/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014510 * "has_key()" function
14511 */
14512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014513f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014514{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014515 if (argvars[0].v_type != VAR_DICT)
14516 {
14517 EMSG(_(e_dictreq));
14518 return;
14519 }
14520 if (argvars[0].vval.v_dict == NULL)
14521 return;
14522
14523 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014524 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014525}
14526
14527/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014528 * "haslocaldir()" function
14529 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014530 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014531f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014532{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014533 win_T *wp = NULL;
14534
14535 wp = find_tabwin(&argvars[0], &argvars[1]);
14536 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014537}
14538
14539/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014540 * "hasmapto()" function
14541 */
14542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014543f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014544{
14545 char_u *name;
14546 char_u *mode;
14547 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014548 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014549
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014550 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014551 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014552 mode = (char_u *)"nvo";
14553 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014554 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014555 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014556 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014557 abbr = (int)get_tv_number(&argvars[2]);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014559
Bram Moolenaar2c932302006-03-18 21:42:09 +000014560 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014561 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014562 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014563 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014564}
14565
14566/*
14567 * "histadd()" function
14568 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014569 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014570f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014571{
14572#ifdef FEAT_CMDHIST
14573 int histype;
14574 char_u *str;
14575 char_u buf[NUMBUFLEN];
14576#endif
14577
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014578 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014579 if (check_restricted() || check_secure())
14580 return;
14581#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014582 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14583 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014584 if (histype >= 0)
14585 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014586 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014587 if (*str != NUL)
14588 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014589 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014590 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014591 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014592 return;
14593 }
14594 }
14595#endif
14596}
14597
14598/*
14599 * "histdel()" function
14600 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014601 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014602f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014603{
14604#ifdef FEAT_CMDHIST
14605 int n;
14606 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014607 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014608
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014609 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14610 if (str == NULL)
14611 n = 0;
14612 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014613 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014614 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014615 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014616 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014617 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014618 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014619 else
14620 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014621 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014622 get_tv_string_buf(&argvars[1], buf));
14623 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014624#endif
14625}
14626
14627/*
14628 * "histget()" function
14629 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014630 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014631f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014632{
14633#ifdef FEAT_CMDHIST
14634 int type;
14635 int idx;
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 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014641 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014642 {
14643 type = get_histtype(str);
14644 if (argvars[1].v_type == VAR_UNKNOWN)
14645 idx = get_history_idx(type);
14646 else
14647 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14648 /* -1 on type error */
14649 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014651#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014652 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014653#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014654 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014655}
14656
14657/*
14658 * "histnr()" function
14659 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014660 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014661f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014662{
14663 int i;
14664
14665#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014666 char_u *history = get_tv_string_chk(&argvars[0]);
14667
14668 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014669 if (i >= HIST_CMD && i < HIST_COUNT)
14670 i = get_history_idx(i);
14671 else
14672#endif
14673 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014674 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014675}
14676
14677/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014678 * "highlightID(name)" function
14679 */
14680 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014681f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014682{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014683 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014684}
14685
14686/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014687 * "highlight_exists()" function
14688 */
14689 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014690f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014691{
14692 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14693}
14694
14695/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014696 * "hostname()" function
14697 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014698 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014699f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014700{
14701 char_u hostname[256];
14702
14703 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014704 rettv->v_type = VAR_STRING;
14705 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014706}
14707
14708/*
14709 * iconv() function
14710 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014711 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014712f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014713{
14714#ifdef FEAT_MBYTE
14715 char_u buf1[NUMBUFLEN];
14716 char_u buf2[NUMBUFLEN];
14717 char_u *from, *to, *str;
14718 vimconv_T vimconv;
14719#endif
14720
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014721 rettv->v_type = VAR_STRING;
14722 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014723
14724#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014725 str = get_tv_string(&argvars[0]);
14726 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14727 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014728 vimconv.vc_type = CONV_NONE;
14729 convert_setup(&vimconv, from, to);
14730
14731 /* If the encodings are equal, no conversion needed. */
14732 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014733 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014734 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014735 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014736
14737 convert_setup(&vimconv, NULL, NULL);
14738 vim_free(from);
14739 vim_free(to);
14740#endif
14741}
14742
14743/*
14744 * "indent()" function
14745 */
14746 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014747f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014748{
14749 linenr_T lnum;
14750
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014751 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014752 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014753 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014754 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014755 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014756}
14757
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014758/*
14759 * "index()" function
14760 */
14761 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014762f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014763{
Bram Moolenaar33570922005-01-25 22:26:29 +000014764 list_T *l;
14765 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014766 long idx = 0;
14767 int ic = FALSE;
14768
14769 rettv->vval.v_number = -1;
14770 if (argvars[0].v_type != VAR_LIST)
14771 {
14772 EMSG(_(e_listreq));
14773 return;
14774 }
14775 l = argvars[0].vval.v_list;
14776 if (l != NULL)
14777 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014778 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014779 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014780 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014781 int error = FALSE;
14782
Bram Moolenaar758711c2005-02-02 23:11:38 +000014783 /* Start at specified item. Use the cached index that list_find()
14784 * sets, so that a negative number also works. */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014785 item = list_find(l, (long)get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014786 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014787 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020014788 ic = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014789 if (error)
14790 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014791 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014792
Bram Moolenaar758711c2005-02-02 23:11:38 +000014793 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014794 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014795 {
14796 rettv->vval.v_number = idx;
14797 break;
14798 }
14799 }
14800}
14801
Bram Moolenaar071d4272004-06-13 20:20:40 +000014802static int inputsecret_flag = 0;
14803
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014804static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014805
Bram Moolenaar071d4272004-06-13 20:20:40 +000014806/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014807 * This function is used by f_input() and f_inputdialog() functions. The third
14808 * argument to f_input() specifies the type of completion to use at the
14809 * prompt. The third argument to f_inputdialog() specifies the value to return
14810 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014811 */
14812 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014813get_user_input(
14814 typval_T *argvars,
14815 typval_T *rettv,
14816 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014817{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014818 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014819 char_u *p = NULL;
14820 int c;
14821 char_u buf[NUMBUFLEN];
14822 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014823 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014824 int xp_type = EXPAND_NOTHING;
14825 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014826
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014827 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014828 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014829
14830#ifdef NO_CONSOLE_INPUT
14831 /* While starting up, there is no place to enter text. */
14832 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014833 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014834#endif
14835
14836 cmd_silent = FALSE; /* Want to see the prompt. */
14837 if (prompt != NULL)
14838 {
14839 /* Only the part of the message after the last NL is considered as
14840 * prompt for the command line */
14841 p = vim_strrchr(prompt, '\n');
14842 if (p == NULL)
14843 p = prompt;
14844 else
14845 {
14846 ++p;
14847 c = *p;
14848 *p = NUL;
14849 msg_start();
14850 msg_clr_eos();
14851 msg_puts_attr(prompt, echo_attr);
14852 msg_didout = FALSE;
14853 msg_starthere();
14854 *p = c;
14855 }
14856 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014857
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014858 if (argvars[1].v_type != VAR_UNKNOWN)
14859 {
14860 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14861 if (defstr != NULL)
14862 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014863
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014864 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014865 {
14866 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014867 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014868 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014869
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014870 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014871 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014872
Bram Moolenaar4463f292005-09-25 22:20:24 +000014873 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14874 if (xp_name == NULL)
14875 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014876
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014877 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014878
Bram Moolenaar4463f292005-09-25 22:20:24 +000014879 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14880 &xp_arg) == FAIL)
14881 return;
14882 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014883 }
14884
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014885 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014886 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014887 int save_ex_normal_busy = ex_normal_busy;
14888 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014889 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014890 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14891 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014892 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014893 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014894 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014895 && argvars[1].v_type != VAR_UNKNOWN
14896 && argvars[2].v_type != VAR_UNKNOWN)
14897 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14898 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014899
14900 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014901
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014902 /* since the user typed this, no need to wait for return */
14903 need_wait_return = FALSE;
14904 msg_didout = FALSE;
14905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014906 cmd_silent = cmd_silent_save;
14907}
14908
14909/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014910 * "input()" function
14911 * Also handles inputsecret() when inputsecret is set.
14912 */
14913 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014914f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014915{
14916 get_user_input(argvars, rettv, FALSE);
14917}
14918
14919/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014920 * "inputdialog()" function
14921 */
14922 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014923f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014924{
14925#if defined(FEAT_GUI_TEXTDIALOG)
14926 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14927 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14928 {
14929 char_u *message;
14930 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014931 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014932
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014933 message = get_tv_string_chk(&argvars[0]);
14934 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014935 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014936 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014937 else
14938 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014939 if (message != NULL && defstr != NULL
14940 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014941 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014942 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014943 else
14944 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014945 if (message != NULL && defstr != NULL
14946 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014947 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014948 rettv->vval.v_string = vim_strsave(
14949 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014950 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014951 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014952 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014953 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014954 }
14955 else
14956#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014957 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014958}
14959
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014960/*
14961 * "inputlist()" function
14962 */
14963 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014964f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014965{
14966 listitem_T *li;
14967 int selected;
14968 int mouse_used;
14969
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014970#ifdef NO_CONSOLE_INPUT
14971 /* While starting up, there is no place to enter text. */
14972 if (no_console_input())
14973 return;
14974#endif
14975 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14976 {
14977 EMSG2(_(e_listarg), "inputlist()");
14978 return;
14979 }
14980
14981 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014982 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014983 lines_left = Rows; /* avoid more prompt */
14984 msg_scroll = TRUE;
14985 msg_clr_eos();
14986
14987 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14988 {
14989 msg_puts(get_tv_string(&li->li_tv));
14990 msg_putchar('\n');
14991 }
14992
14993 /* Ask for choice. */
14994 selected = prompt_for_number(&mouse_used);
14995 if (mouse_used)
14996 selected -= lines_left;
14997
14998 rettv->vval.v_number = selected;
14999}
15000
15001
Bram Moolenaar071d4272004-06-13 20:20:40 +000015002static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
15003
15004/*
15005 * "inputrestore()" function
15006 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015007 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015008f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015009{
15010 if (ga_userinput.ga_len > 0)
15011 {
15012 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015013 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
15014 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015015 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015016 }
15017 else if (p_verbose > 1)
15018 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000015019 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015020 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015021 }
15022}
15023
15024/*
15025 * "inputsave()" function
15026 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015027 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015028f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015029{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015030 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015031 if (ga_grow(&ga_userinput, 1) == OK)
15032 {
15033 save_typeahead((tasave_T *)(ga_userinput.ga_data)
15034 + ga_userinput.ga_len);
15035 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015036 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015037 }
15038 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015039 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015040}
15041
15042/*
15043 * "inputsecret()" function
15044 */
15045 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015046f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015047{
15048 ++cmdline_star;
15049 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015050 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015051 --cmdline_star;
15052 --inputsecret_flag;
15053}
15054
15055/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015056 * "insert()" function
15057 */
15058 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015059f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015060{
15061 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015062 listitem_T *item;
15063 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015064 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015065
15066 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015067 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015068 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020015069 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015070 {
15071 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015072 before = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015073 if (error)
15074 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015075
Bram Moolenaar758711c2005-02-02 23:11:38 +000015076 if (before == l->lv_len)
15077 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015078 else
15079 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015080 item = list_find(l, before);
15081 if (item == NULL)
15082 {
15083 EMSGN(_(e_listidx), before);
15084 l = NULL;
15085 }
15086 }
15087 if (l != NULL)
15088 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015089 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015090 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015091 }
15092 }
15093}
15094
15095/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015096 * "invert(expr)" function
15097 */
15098 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015099f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015100{
15101 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
15102}
15103
15104/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015105 * "isdirectory()" function
15106 */
15107 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015108f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015109{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015110 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015111}
15112
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015113/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015114 * "islocked()" function
15115 */
15116 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015117f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015118{
15119 lval_T lv;
15120 char_u *end;
15121 dictitem_T *di;
15122
15123 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010015124 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
15125 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015126 if (end != NULL && lv.ll_name != NULL)
15127 {
15128 if (*end != NUL)
15129 EMSG(_(e_trailing));
15130 else
15131 {
15132 if (lv.ll_tv == NULL)
15133 {
15134 if (check_changedtick(lv.ll_name))
15135 rettv->vval.v_number = 1; /* always locked */
15136 else
15137 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010015138 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015139 if (di != NULL)
15140 {
15141 /* Consider a variable locked when:
15142 * 1. the variable itself is locked
15143 * 2. the value of the variable is locked.
15144 * 3. the List or Dict value is locked.
15145 */
15146 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
15147 || tv_islocked(&di->di_tv));
15148 }
15149 }
15150 }
15151 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015152 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015153 else if (lv.ll_newkey != NULL)
15154 EMSG2(_(e_dictkey), lv.ll_newkey);
15155 else if (lv.ll_list != NULL)
15156 /* List item. */
15157 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
15158 else
15159 /* Dictionary item. */
15160 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
15161 }
15162 }
15163
15164 clear_lval(&lv);
15165}
15166
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010015167#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
15168/*
15169 * "isnan()" function
15170 */
15171 static void
15172f_isnan(typval_T *argvars, typval_T *rettv)
15173{
15174 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
15175 && isnan(argvars[0].vval.v_float);
15176}
15177#endif
15178
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015179static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015180
15181/*
15182 * Turn a dict into a list:
15183 * "what" == 0: list of keys
15184 * "what" == 1: list of values
15185 * "what" == 2: list of items
15186 */
15187 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015188dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015189{
Bram Moolenaar33570922005-01-25 22:26:29 +000015190 list_T *l2;
15191 dictitem_T *di;
15192 hashitem_T *hi;
15193 listitem_T *li;
15194 listitem_T *li2;
15195 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015196 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015197
Bram Moolenaar8c711452005-01-14 21:53:12 +000015198 if (argvars[0].v_type != VAR_DICT)
15199 {
15200 EMSG(_(e_dictreq));
15201 return;
15202 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015203 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015204 return;
15205
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015206 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015207 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015208
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015209 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015210 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015211 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015212 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015213 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015214 --todo;
15215 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015216
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015217 li = listitem_alloc();
15218 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015219 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015220 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015221
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015222 if (what == 0)
15223 {
15224 /* keys() */
15225 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015226 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015227 li->li_tv.vval.v_string = vim_strsave(di->di_key);
15228 }
15229 else if (what == 1)
15230 {
15231 /* values() */
15232 copy_tv(&di->di_tv, &li->li_tv);
15233 }
15234 else
15235 {
15236 /* items() */
15237 l2 = list_alloc();
15238 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015239 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015240 li->li_tv.vval.v_list = l2;
15241 if (l2 == NULL)
15242 break;
15243 ++l2->lv_refcount;
15244
15245 li2 = listitem_alloc();
15246 if (li2 == NULL)
15247 break;
15248 list_append(l2, li2);
15249 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015250 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015251 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
15252
15253 li2 = listitem_alloc();
15254 if (li2 == NULL)
15255 break;
15256 list_append(l2, li2);
15257 copy_tv(&di->di_tv, &li2->li_tv);
15258 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015259 }
15260 }
15261}
15262
15263/*
15264 * "items(dict)" function
15265 */
15266 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015267f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015268{
15269 dict_list(argvars, rettv, 2);
15270}
15271
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010015272#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015273/*
15274 * Get the job from the argument.
15275 * Returns NULL if the job is invalid.
15276 */
15277 static job_T *
15278get_job_arg(typval_T *tv)
15279{
15280 job_T *job;
15281
15282 if (tv->v_type != VAR_JOB)
15283 {
15284 EMSG2(_(e_invarg2), get_tv_string(tv));
15285 return NULL;
15286 }
15287 job = tv->vval.v_job;
15288
15289 if (job == NULL)
15290 EMSG(_("E916: not a valid job"));
15291 return job;
15292}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015293
Bram Moolenaar835dc632016-02-07 14:27:38 +010015294/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015295 * "job_getchannel()" function
15296 */
15297 static void
15298f_job_getchannel(typval_T *argvars, typval_T *rettv)
15299{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015300 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015301
Bram Moolenaar65edff82016-02-21 16:40:11 +010015302 if (job != NULL)
15303 {
Bram Moolenaar77073442016-02-13 23:23:53 +010015304 rettv->v_type = VAR_CHANNEL;
15305 rettv->vval.v_channel = job->jv_channel;
15306 if (job->jv_channel != NULL)
15307 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015308 }
15309}
15310
15311/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010015312 * "job_info()" function
15313 */
15314 static void
15315f_job_info(typval_T *argvars, typval_T *rettv)
15316{
15317 job_T *job = get_job_arg(&argvars[0]);
15318
15319 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
15320 job_info(job, rettv->vval.v_dict);
15321}
15322
15323/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015324 * "job_setoptions()" function
15325 */
15326 static void
15327f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15328{
15329 job_T *job = get_job_arg(&argvars[0]);
15330 jobopt_T opt;
15331
15332 if (job == NULL)
15333 return;
15334 clear_job_options(&opt);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020015335 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
15336 job_set_options(job, &opt);
15337 free_job_options(&opt);
Bram Moolenaar65edff82016-02-21 16:40:11 +010015338}
15339
15340/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015341 * "job_start()" function
15342 */
15343 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010015344f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015345{
Bram Moolenaar835dc632016-02-07 14:27:38 +010015346 rettv->v_type = VAR_JOB;
Bram Moolenaar38499922016-04-22 20:46:52 +020015347 if (check_restricted() || check_secure())
15348 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015349 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015350}
15351
15352/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015353 * "job_status()" function
15354 */
15355 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015356f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015357{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015358 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015359
Bram Moolenaar65edff82016-02-21 16:40:11 +010015360 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015361 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015362 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010015363 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010015364 }
15365}
15366
15367/*
15368 * "job_stop()" function
15369 */
15370 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015371f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015372{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015373 job_T *job = get_job_arg(&argvars[0]);
15374
15375 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015376 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015377}
15378#endif
15379
Bram Moolenaar071d4272004-06-13 20:20:40 +000015380/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015381 * "join()" function
15382 */
15383 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015384f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015385{
15386 garray_T ga;
15387 char_u *sep;
15388
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015389 if (argvars[0].v_type != VAR_LIST)
15390 {
15391 EMSG(_(e_listreq));
15392 return;
15393 }
15394 if (argvars[0].vval.v_list == NULL)
15395 return;
15396 if (argvars[1].v_type == VAR_UNKNOWN)
15397 sep = (char_u *)" ";
15398 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015399 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015400
15401 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015402
15403 if (sep != NULL)
15404 {
15405 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar18dfb442016-05-31 22:31:23 +020015406 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015407 ga_append(&ga, NUL);
15408 rettv->vval.v_string = (char_u *)ga.ga_data;
15409 }
15410 else
15411 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015412}
15413
15414/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015415 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015416 */
15417 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015418f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015419{
15420 js_read_T reader;
15421
15422 reader.js_buf = get_tv_string(&argvars[0]);
15423 reader.js_fill = NULL;
15424 reader.js_used = 0;
15425 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15426 EMSG(_(e_invarg));
15427}
15428
15429/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015430 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015431 */
15432 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015433f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015434{
15435 rettv->v_type = VAR_STRING;
15436 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15437}
15438
15439/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015440 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015441 */
15442 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015443f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015444{
15445 js_read_T reader;
15446
15447 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015448 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015449 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015450 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015451 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015452}
15453
15454/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015455 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015456 */
15457 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015458f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015459{
15460 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015461 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015462}
15463
15464/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015465 * "keys()" function
15466 */
15467 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015468f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015469{
15470 dict_list(argvars, rettv, 0);
15471}
15472
15473/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015474 * "last_buffer_nr()" function.
15475 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015476 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015477f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015478{
15479 int n = 0;
15480 buf_T *buf;
15481
15482 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15483 if (n < buf->b_fnum)
15484 n = buf->b_fnum;
15485
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015486 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015487}
15488
15489/*
15490 * "len()" function
15491 */
15492 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015493f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015494{
15495 switch (argvars[0].v_type)
15496 {
15497 case VAR_STRING:
15498 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015499 rettv->vval.v_number = (varnumber_T)STRLEN(
15500 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015501 break;
15502 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015503 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015504 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015505 case VAR_DICT:
15506 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15507 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015508 case VAR_UNKNOWN:
15509 case VAR_SPECIAL:
15510 case VAR_FLOAT:
15511 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010015512 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015513 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015514 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015515 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015516 break;
15517 }
15518}
15519
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015520static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015521
15522 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015523libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015524{
15525#ifdef FEAT_LIBCALL
15526 char_u *string_in;
15527 char_u **string_result;
15528 int nr_result;
15529#endif
15530
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015531 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015532 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015533 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015534
15535 if (check_restricted() || check_secure())
15536 return;
15537
15538#ifdef FEAT_LIBCALL
15539 /* The first two args must be strings, otherwise its meaningless */
15540 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15541 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015542 string_in = NULL;
15543 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015544 string_in = argvars[2].vval.v_string;
15545 if (type == VAR_NUMBER)
15546 string_result = NULL;
15547 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015548 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015549 if (mch_libcall(argvars[0].vval.v_string,
15550 argvars[1].vval.v_string,
15551 string_in,
15552 argvars[2].vval.v_number,
15553 string_result,
15554 &nr_result) == OK
15555 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015556 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015557 }
15558#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015559}
15560
15561/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015562 * "libcall()" function
15563 */
15564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015565f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015566{
15567 libcall_common(argvars, rettv, VAR_STRING);
15568}
15569
15570/*
15571 * "libcallnr()" function
15572 */
15573 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015574f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015575{
15576 libcall_common(argvars, rettv, VAR_NUMBER);
15577}
15578
15579/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015580 * "line(string)" function
15581 */
15582 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015583f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015584{
15585 linenr_T lnum = 0;
15586 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015587 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015588
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015589 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015590 if (fp != NULL)
15591 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015592 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015593}
15594
15595/*
15596 * "line2byte(lnum)" function
15597 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015598 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015599f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015600{
15601#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015602 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015603#else
15604 linenr_T lnum;
15605
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015606 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015607 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015608 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015609 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015610 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15611 if (rettv->vval.v_number >= 0)
15612 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015613#endif
15614}
15615
15616/*
15617 * "lispindent(lnum)" function
15618 */
15619 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015620f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015621{
15622#ifdef FEAT_LISP
15623 pos_T pos;
15624 linenr_T lnum;
15625
15626 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015627 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015628 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15629 {
15630 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015631 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015632 curwin->w_cursor = pos;
15633 }
15634 else
15635#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015636 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015637}
15638
15639/*
15640 * "localtime()" function
15641 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015643f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015644{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015645 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015646}
15647
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015648static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015649
15650 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015651get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015652{
15653 char_u *keys;
15654 char_u *which;
15655 char_u buf[NUMBUFLEN];
15656 char_u *keys_buf = NULL;
15657 char_u *rhs;
15658 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015659 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015660 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015661 mapblock_T *mp;
15662 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015663
15664 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015665 rettv->v_type = VAR_STRING;
15666 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015667
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015668 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015669 if (*keys == NUL)
15670 return;
15671
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015672 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015673 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015674 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015675 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015676 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015677 abbr = (int)get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015678 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015679 get_dict = (int)get_tv_number(&argvars[3]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015680 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015682 else
15683 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015684 if (which == NULL)
15685 return;
15686
Bram Moolenaar071d4272004-06-13 20:20:40 +000015687 mode = get_map_mode(&which, 0);
15688
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015689 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015690 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015691 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015692
15693 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015694 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015695 /* Return a string. */
15696 if (rhs != NULL)
15697 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015698
Bram Moolenaarbd743252010-10-20 21:23:33 +020015699 }
15700 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15701 {
15702 /* Return a dictionary. */
15703 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15704 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15705 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015706
Bram Moolenaarbd743252010-10-20 21:23:33 +020015707 dict_add_nr_str(dict, "lhs", 0L, lhs);
15708 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15709 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15710 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15711 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15712 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15713 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015714 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015715 dict_add_nr_str(dict, "mode", 0L, mapmode);
15716
15717 vim_free(lhs);
15718 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015719 }
15720}
15721
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015722#ifdef FEAT_FLOAT
15723/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015724 * "log()" function
15725 */
15726 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015727f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015728{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015729 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015730
15731 rettv->v_type = VAR_FLOAT;
15732 if (get_float_arg(argvars, &f) == OK)
15733 rettv->vval.v_float = log(f);
15734 else
15735 rettv->vval.v_float = 0.0;
15736}
15737
15738/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015739 * "log10()" function
15740 */
15741 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015742f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015743{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015744 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015745
15746 rettv->v_type = VAR_FLOAT;
15747 if (get_float_arg(argvars, &f) == OK)
15748 rettv->vval.v_float = log10(f);
15749 else
15750 rettv->vval.v_float = 0.0;
15751}
15752#endif
15753
Bram Moolenaar1dced572012-04-05 16:54:08 +020015754#ifdef FEAT_LUA
15755/*
15756 * "luaeval()" function
15757 */
15758 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015759f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015760{
15761 char_u *str;
15762 char_u buf[NUMBUFLEN];
15763
15764 str = get_tv_string_buf(&argvars[0], buf);
15765 do_luaeval(str, argvars + 1, rettv);
15766}
15767#endif
15768
Bram Moolenaar071d4272004-06-13 20:20:40 +000015769/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015770 * "map()" function
15771 */
15772 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015773f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015774{
15775 filter_map(argvars, rettv, TRUE);
15776}
15777
15778/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015779 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015780 */
15781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015782f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015783{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015784 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015785}
15786
15787/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015788 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015789 */
15790 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015791f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015792{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015793 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015794}
15795
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015796static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015797
15798 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015799find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015800{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015801 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015802 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015803 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015804 char_u *pat;
15805 regmatch_T regmatch;
15806 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015807 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015808 char_u *save_cpo;
15809 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015810 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015811 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015812 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015813 list_T *l = NULL;
15814 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015815 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015816 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015817
15818 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15819 save_cpo = p_cpo;
15820 p_cpo = (char_u *)"";
15821
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015822 rettv->vval.v_number = -1;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015823 if (type == 3 || type == 4)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015824 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015825 /* type 3: return empty list when there are no matches.
15826 * type 4: return ["", -1, -1, -1] */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015827 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015828 goto theend;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015829 if (type == 4
15830 && (list_append_string(rettv->vval.v_list,
15831 (char_u *)"", 0) == FAIL
15832 || list_append_number(rettv->vval.v_list,
15833 (varnumber_T)-1) == FAIL
15834 || list_append_number(rettv->vval.v_list,
15835 (varnumber_T)-1) == FAIL
15836 || list_append_number(rettv->vval.v_list,
15837 (varnumber_T)-1) == FAIL))
15838 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020015839 list_free(rettv->vval.v_list);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015840 rettv->vval.v_list = NULL;
15841 goto theend;
15842 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015843 }
15844 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015845 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015846 rettv->v_type = VAR_STRING;
15847 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015849
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015850 if (argvars[0].v_type == VAR_LIST)
15851 {
15852 if ((l = argvars[0].vval.v_list) == NULL)
15853 goto theend;
15854 li = l->lv_first;
15855 }
15856 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015857 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015858 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015859 len = (long)STRLEN(str);
15860 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015861
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015862 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15863 if (pat == NULL)
15864 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015865
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015866 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015867 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015868 int error = FALSE;
15869
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015870 start = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015871 if (error)
15872 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015873 if (l != NULL)
15874 {
15875 li = list_find(l, start);
15876 if (li == NULL)
15877 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015878 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015879 }
15880 else
15881 {
15882 if (start < 0)
15883 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015884 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015885 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015886 /* When "count" argument is there ignore matches before "start",
15887 * otherwise skip part of the string. Differs when pattern is "^"
15888 * or "\<". */
15889 if (argvars[3].v_type != VAR_UNKNOWN)
15890 startcol = start;
15891 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015892 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015893 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015894 len -= start;
15895 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015896 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015897
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015898 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020015899 nth = (long)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015900 if (error)
15901 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015902 }
15903
15904 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15905 if (regmatch.regprog != NULL)
15906 {
15907 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015908
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015909 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015910 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015911 if (l != NULL)
15912 {
15913 if (li == NULL)
15914 {
15915 match = FALSE;
15916 break;
15917 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015918 vim_free(tofree);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015919 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015920 if (str == NULL)
15921 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015922 }
15923
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015924 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015925
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015926 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015927 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015928 if (l == NULL && !match)
15929 break;
15930
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015931 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015932 if (l != NULL)
15933 {
15934 li = li->li_next;
15935 ++idx;
15936 }
15937 else
15938 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015939#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015940 startcol = (colnr_T)(regmatch.startp[0]
15941 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015942#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015943 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015944#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015945 if (startcol > (colnr_T)len
15946 || str + startcol <= regmatch.startp[0])
15947 {
15948 match = FALSE;
15949 break;
15950 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015951 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015952 }
15953
15954 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015955 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015956 if (type == 4)
15957 {
15958 listitem_T *li1 = rettv->vval.v_list->lv_first;
15959 listitem_T *li2 = li1->li_next;
15960 listitem_T *li3 = li2->li_next;
15961 listitem_T *li4 = li3->li_next;
15962
Bram Moolenaar3c809342016-06-01 22:34:48 +020015963 vim_free(li1->li_tv.vval.v_string);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015964 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
15965 (int)(regmatch.endp[0] - regmatch.startp[0]));
15966 li3->li_tv.vval.v_number =
15967 (varnumber_T)(regmatch.startp[0] - expr);
15968 li4->li_tv.vval.v_number =
15969 (varnumber_T)(regmatch.endp[0] - expr);
15970 if (l != NULL)
15971 li2->li_tv.vval.v_number = (varnumber_T)idx;
15972 }
15973 else if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015974 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015975 int i;
15976
15977 /* return list with matched string and submatches */
15978 for (i = 0; i < NSUBEXP; ++i)
15979 {
15980 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015981 {
15982 if (list_append_string(rettv->vval.v_list,
15983 (char_u *)"", 0) == FAIL)
15984 break;
15985 }
15986 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015987 regmatch.startp[i],
15988 (int)(regmatch.endp[i] - regmatch.startp[i]))
15989 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015990 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015991 }
15992 }
15993 else if (type == 2)
15994 {
15995 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015996 if (l != NULL)
15997 copy_tv(&li->li_tv, rettv);
15998 else
15999 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000016000 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016001 }
16002 else if (l != NULL)
16003 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016004 else
16005 {
16006 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016007 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000016008 (varnumber_T)(regmatch.startp[0] - str);
16009 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016010 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000016011 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016012 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016013 }
16014 }
Bram Moolenaar473de612013-06-08 18:19:48 +020016015 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016016 }
16017
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016018 if (type == 4 && l == NULL)
16019 /* matchstrpos() without a list: drop the second item. */
16020 listitem_remove(rettv->vval.v_list,
16021 rettv->vval.v_list->lv_first->li_next);
16022
Bram Moolenaar071d4272004-06-13 20:20:40 +000016023theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016024 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016025 p_cpo = save_cpo;
16026}
16027
16028/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016029 * "match()" function
16030 */
16031 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016032f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016033{
16034 find_some_match(argvars, rettv, 1);
16035}
16036
16037/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016038 * "matchadd()" function
16039 */
16040 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016041f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016042{
16043#ifdef FEAT_SEARCH_EXTRA
16044 char_u buf[NUMBUFLEN];
16045 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
16046 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
16047 int prio = 10; /* default priority */
16048 int id = -1;
16049 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020016050 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016051
16052 rettv->vval.v_number = -1;
16053
16054 if (grp == NULL || pat == NULL)
16055 return;
16056 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016057 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016058 prio = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016059 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020016060 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016061 id = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020016062 if (argvars[4].v_type != VAR_UNKNOWN)
16063 {
16064 if (argvars[4].v_type != VAR_DICT)
16065 {
16066 EMSG(_(e_dictreq));
16067 return;
16068 }
16069 if (dict_find(argvars[4].vval.v_dict,
16070 (char_u *)"conceal", -1) != NULL)
16071 conceal_char = get_dict_string(argvars[4].vval.v_dict,
16072 (char_u *)"conceal", FALSE);
16073 }
16074 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000016075 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016076 if (error == TRUE)
16077 return;
16078 if (id >= 1 && id <= 3)
16079 {
16080 EMSGN("E798: ID is reserved for \":match\": %ld", id);
16081 return;
16082 }
16083
Bram Moolenaar6561d522015-07-21 15:48:27 +020016084 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
16085 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020016086#endif
16087}
16088
16089/*
16090 * "matchaddpos()" function
16091 */
16092 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016093f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020016094{
16095#ifdef FEAT_SEARCH_EXTRA
16096 char_u buf[NUMBUFLEN];
16097 char_u *group;
16098 int prio = 10;
16099 int id = -1;
16100 int error = FALSE;
16101 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020016102 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020016103
16104 rettv->vval.v_number = -1;
16105
16106 group = get_tv_string_buf_chk(&argvars[0], buf);
16107 if (group == NULL)
16108 return;
16109
16110 if (argvars[1].v_type != VAR_LIST)
16111 {
16112 EMSG2(_(e_listarg), "matchaddpos()");
16113 return;
16114 }
16115 l = argvars[1].vval.v_list;
16116 if (l == NULL)
16117 return;
16118
16119 if (argvars[2].v_type != VAR_UNKNOWN)
16120 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016121 prio = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaarb3414592014-06-17 17:48:32 +020016122 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020016123 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016124 id = (int)get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020016125 if (argvars[4].v_type != VAR_UNKNOWN)
16126 {
16127 if (argvars[4].v_type != VAR_DICT)
16128 {
16129 EMSG(_(e_dictreq));
16130 return;
16131 }
16132 if (dict_find(argvars[4].vval.v_dict,
16133 (char_u *)"conceal", -1) != NULL)
16134 conceal_char = get_dict_string(argvars[4].vval.v_dict,
16135 (char_u *)"conceal", FALSE);
16136 }
16137 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020016138 }
16139 if (error == TRUE)
16140 return;
16141
16142 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
16143 if (id == 1 || id == 2)
16144 {
16145 EMSGN("E798: ID is reserved for \":match\": %ld", id);
16146 return;
16147 }
16148
Bram Moolenaar6561d522015-07-21 15:48:27 +020016149 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
16150 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016151#endif
16152}
16153
16154/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016155 * "matcharg()" function
16156 */
16157 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016158f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016159{
16160 if (rettv_list_alloc(rettv) == OK)
16161 {
16162#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016163 int id = (int)get_tv_number(&argvars[0]);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016164 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016165
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016166 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016167 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016168 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
16169 {
16170 list_append_string(rettv->vval.v_list,
16171 syn_id2name(m->hlg_id), -1);
16172 list_append_string(rettv->vval.v_list, m->pattern, -1);
16173 }
16174 else
16175 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010016176 list_append_string(rettv->vval.v_list, NULL, -1);
16177 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016178 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016179 }
16180#endif
16181 }
16182}
16183
16184/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016185 * "matchdelete()" function
16186 */
16187 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016188f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016189{
16190#ifdef FEAT_SEARCH_EXTRA
16191 rettv->vval.v_number = match_delete(curwin,
16192 (int)get_tv_number(&argvars[0]), TRUE);
16193#endif
16194}
16195
16196/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016197 * "matchend()" function
16198 */
16199 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016200f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016201{
16202 find_some_match(argvars, rettv, 0);
16203}
16204
16205/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016206 * "matchlist()" function
16207 */
16208 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016209f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016210{
16211 find_some_match(argvars, rettv, 3);
16212}
16213
16214/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016215 * "matchstr()" function
16216 */
16217 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016218f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016219{
16220 find_some_match(argvars, rettv, 2);
16221}
16222
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016223/*
16224 * "matchstrpos()" function
16225 */
16226 static void
16227f_matchstrpos(typval_T *argvars, typval_T *rettv)
16228{
16229 find_some_match(argvars, rettv, 4);
16230}
16231
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016232static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016233
16234 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016235max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016236{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016237 varnumber_T n = 0;
16238 varnumber_T i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016239 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016240
16241 if (argvars[0].v_type == VAR_LIST)
16242 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016243 list_T *l;
16244 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016245
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016246 l = argvars[0].vval.v_list;
16247 if (l != NULL)
16248 {
16249 li = l->lv_first;
16250 if (li != NULL)
16251 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016252 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000016253 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016254 {
16255 li = li->li_next;
16256 if (li == NULL)
16257 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016258 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016259 if (domax ? i > n : i < n)
16260 n = i;
16261 }
16262 }
16263 }
16264 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016265 else if (argvars[0].v_type == VAR_DICT)
16266 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016267 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016268 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016269 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016270 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016271
16272 d = argvars[0].vval.v_dict;
16273 if (d != NULL)
16274 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016275 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016276 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016277 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016278 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016279 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016280 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016281 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016282 if (first)
16283 {
16284 n = i;
16285 first = FALSE;
16286 }
16287 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016288 n = i;
16289 }
16290 }
16291 }
16292 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016293 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016294 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016295 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016296}
16297
16298/*
16299 * "max()" function
16300 */
16301 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016302f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016303{
16304 max_min(argvars, rettv, TRUE);
16305}
16306
16307/*
16308 * "min()" function
16309 */
16310 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016311f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016312{
16313 max_min(argvars, rettv, FALSE);
16314}
16315
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016316static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016317
16318/*
16319 * Create the directory in which "dir" is located, and higher levels when
16320 * needed.
16321 */
16322 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016323mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016324{
16325 char_u *p;
16326 char_u *updir;
16327 int r = FAIL;
16328
16329 /* Get end of directory name in "dir".
16330 * We're done when it's "/" or "c:/". */
16331 p = gettail_sep(dir);
16332 if (p <= get_past_head(dir))
16333 return OK;
16334
16335 /* If the directory exists we're done. Otherwise: create it.*/
16336 updir = vim_strnsave(dir, (int)(p - dir));
16337 if (updir == NULL)
16338 return FAIL;
16339 if (mch_isdir(updir))
16340 r = OK;
16341 else if (mkdir_recurse(updir, prot) == OK)
16342 r = vim_mkdir_emsg(updir, prot);
16343 vim_free(updir);
16344 return r;
16345}
16346
16347#ifdef vim_mkdir
16348/*
16349 * "mkdir()" function
16350 */
16351 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016352f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016353{
16354 char_u *dir;
16355 char_u buf[NUMBUFLEN];
16356 int prot = 0755;
16357
16358 rettv->vval.v_number = FAIL;
16359 if (check_restricted() || check_secure())
16360 return;
16361
16362 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016363 if (*dir == NUL)
16364 rettv->vval.v_number = FAIL;
16365 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016366 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016367 if (*gettail(dir) == NUL)
16368 /* remove trailing slashes */
16369 *gettail_sep(dir) = NUL;
16370
16371 if (argvars[1].v_type != VAR_UNKNOWN)
16372 {
16373 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016374 prot = (int)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016375 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16376 mkdir_recurse(dir, prot);
16377 }
16378 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016379 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016380}
16381#endif
16382
Bram Moolenaar0d660222005-01-07 21:51:51 +000016383/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016384 * "mode()" function
16385 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016386 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016387f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016388{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016389 char_u buf[3];
16390
16391 buf[1] = NUL;
16392 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016393
Bram Moolenaare381d3d2016-07-07 14:50:41 +020016394 if (time_for_testing == 93784)
16395 {
16396 /* Testing the two-character code. */
16397 buf[0] = 'x';
16398 buf[1] = '!';
16399 }
16400 else if (VIsual_active)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016401 {
16402 if (VIsual_select)
16403 buf[0] = VIsual_mode + 's' - 'v';
16404 else
16405 buf[0] = VIsual_mode;
16406 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016407 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016408 || State == CONFIRM)
16409 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016410 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016411 if (State == ASKMORE)
16412 buf[1] = 'm';
16413 else if (State == CONFIRM)
16414 buf[1] = '?';
16415 }
16416 else if (State == EXTERNCMD)
16417 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016418 else if (State & INSERT)
16419 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016420#ifdef FEAT_VREPLACE
16421 if (State & VREPLACE_FLAG)
16422 {
16423 buf[0] = 'R';
16424 buf[1] = 'v';
16425 }
16426 else
16427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016428 if (State & REPLACE_FLAG)
16429 buf[0] = 'R';
16430 else
16431 buf[0] = 'i';
16432 }
16433 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016434 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016435 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016436 if (exmode_active)
16437 buf[1] = 'v';
16438 }
16439 else if (exmode_active)
16440 {
16441 buf[0] = 'c';
16442 buf[1] = 'e';
16443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016444 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016445 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016446 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016447 if (finish_op)
16448 buf[1] = 'o';
16449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016450
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016451 /* Clear out the minor mode when the argument is not a non-zero number or
16452 * non-empty string. */
16453 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016454 buf[1] = NUL;
16455
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016456 rettv->vval.v_string = vim_strsave(buf);
16457 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016458}
16459
Bram Moolenaar429fa852013-04-15 12:27:36 +020016460#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016461/*
16462 * "mzeval()" function
16463 */
16464 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016465f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016466{
16467 char_u *str;
16468 char_u buf[NUMBUFLEN];
16469
16470 str = get_tv_string_buf(&argvars[0], buf);
16471 do_mzeval(str, rettv);
16472}
Bram Moolenaar75676462013-01-30 14:55:42 +010016473
16474 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016475mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016476{
16477 typval_T argvars[3];
16478
16479 argvars[0].v_type = VAR_STRING;
16480 argvars[0].vval.v_string = name;
16481 copy_tv(args, &argvars[1]);
16482 argvars[2].v_type = VAR_UNKNOWN;
16483 f_call(argvars, rettv);
16484 clear_tv(&argvars[1]);
16485}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016486#endif
16487
Bram Moolenaar071d4272004-06-13 20:20:40 +000016488/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016489 * "nextnonblank()" function
16490 */
16491 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016492f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016493{
16494 linenr_T lnum;
16495
16496 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16497 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016498 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016499 {
16500 lnum = 0;
16501 break;
16502 }
16503 if (*skipwhite(ml_get(lnum)) != NUL)
16504 break;
16505 }
16506 rettv->vval.v_number = lnum;
16507}
16508
16509/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016510 * "nr2char()" function
16511 */
16512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016513f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016514{
16515 char_u buf[NUMBUFLEN];
16516
16517#ifdef FEAT_MBYTE
16518 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016519 {
16520 int utf8 = 0;
16521
16522 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016523 utf8 = (int)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaard35d7842013-01-23 17:17:10 +010016524 if (utf8)
16525 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16526 else
16527 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016529 else
16530#endif
16531 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016532 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016533 buf[1] = NUL;
16534 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016535 rettv->v_type = VAR_STRING;
16536 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016537}
16538
16539/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016540 * "or(expr, expr)" function
16541 */
16542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016543f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016544{
16545 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16546 | get_tv_number_chk(&argvars[1], NULL);
16547}
16548
16549/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016550 * "pathshorten()" function
16551 */
16552 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016553f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016554{
16555 char_u *p;
16556
16557 rettv->v_type = VAR_STRING;
16558 p = get_tv_string_chk(&argvars[0]);
16559 if (p == NULL)
16560 rettv->vval.v_string = NULL;
16561 else
16562 {
16563 p = vim_strsave(p);
16564 rettv->vval.v_string = p;
16565 if (p != NULL)
16566 shorten_dir(p);
16567 }
16568}
16569
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016570#ifdef FEAT_PERL
16571/*
16572 * "perleval()" function
16573 */
16574 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016575f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016576{
16577 char_u *str;
16578 char_u buf[NUMBUFLEN];
16579
16580 str = get_tv_string_buf(&argvars[0], buf);
16581 do_perleval(str, rettv);
16582}
16583#endif
16584
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016585#ifdef FEAT_FLOAT
16586/*
16587 * "pow()" function
16588 */
16589 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016590f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016591{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016592 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016593
16594 rettv->v_type = VAR_FLOAT;
16595 if (get_float_arg(argvars, &fx) == OK
16596 && get_float_arg(&argvars[1], &fy) == OK)
16597 rettv->vval.v_float = pow(fx, fy);
16598 else
16599 rettv->vval.v_float = 0.0;
16600}
16601#endif
16602
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016603/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016604 * "prevnonblank()" function
16605 */
16606 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016607f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016608{
16609 linenr_T lnum;
16610
16611 lnum = get_tv_lnum(argvars);
16612 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16613 lnum = 0;
16614 else
16615 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16616 --lnum;
16617 rettv->vval.v_number = lnum;
16618}
16619
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016620/* This dummy va_list is here because:
16621 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16622 * - locally in the function results in a "used before set" warning
16623 * - using va_start() to initialize it gives "function with fixed args" error */
16624static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016625
Bram Moolenaar8c711452005-01-14 21:53:12 +000016626/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016627 * "printf()" function
16628 */
16629 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016630f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016631{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016632 char_u buf[NUMBUFLEN];
16633 int len;
16634 char_u *s;
16635 int saved_did_emsg = did_emsg;
16636 char *fmt;
16637
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016638 rettv->v_type = VAR_STRING;
16639 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016640
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016641 /* Get the required length, allocate the buffer and do it for real. */
16642 did_emsg = FALSE;
16643 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16644 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16645 if (!did_emsg)
16646 {
16647 s = alloc(len + 1);
16648 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016649 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016650 rettv->vval.v_string = s;
16651 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016652 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016653 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016654 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016655}
16656
16657/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016658 * "pumvisible()" function
16659 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016660 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016661f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016662{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016663#ifdef FEAT_INS_EXPAND
16664 if (pum_visible())
16665 rettv->vval.v_number = 1;
16666#endif
16667}
16668
Bram Moolenaardb913952012-06-29 12:54:53 +020016669#ifdef FEAT_PYTHON3
16670/*
16671 * "py3eval()" function
16672 */
16673 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016674f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016675{
16676 char_u *str;
16677 char_u buf[NUMBUFLEN];
16678
16679 str = get_tv_string_buf(&argvars[0], buf);
16680 do_py3eval(str, rettv);
16681}
16682#endif
16683
16684#ifdef FEAT_PYTHON
16685/*
16686 * "pyeval()" function
16687 */
16688 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016689f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016690{
16691 char_u *str;
16692 char_u buf[NUMBUFLEN];
16693
16694 str = get_tv_string_buf(&argvars[0], buf);
16695 do_pyeval(str, rettv);
16696}
16697#endif
16698
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016699/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016700 * "range()" function
16701 */
16702 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016703f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016704{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016705 varnumber_T start;
16706 varnumber_T end;
16707 varnumber_T stride = 1;
16708 varnumber_T i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016709 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016710
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016711 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016712 if (argvars[1].v_type == VAR_UNKNOWN)
16713 {
16714 end = start - 1;
16715 start = 0;
16716 }
16717 else
16718 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016719 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016720 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016721 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016722 }
16723
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016724 if (error)
16725 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016726 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016727 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016728 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016729 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016730 else
16731 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016732 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016733 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016734 if (list_append_number(rettv->vval.v_list,
16735 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016736 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016737 }
16738}
16739
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016740/*
16741 * "readfile()" function
16742 */
16743 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016744f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016745{
16746 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016747 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016748 char_u *fname;
16749 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016750 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16751 int io_size = sizeof(buf);
16752 int readlen; /* size of last fread() */
16753 char_u *prev = NULL; /* previously read bytes, if any */
16754 long prevlen = 0; /* length of data in prev */
16755 long prevsize = 0; /* size of prev buffer */
16756 long maxline = MAXLNUM;
16757 long cnt = 0;
16758 char_u *p; /* position in buf */
16759 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016760
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016761 if (argvars[1].v_type != VAR_UNKNOWN)
16762 {
16763 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16764 binary = TRUE;
16765 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020016766 maxline = (long)get_tv_number(&argvars[2]);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016767 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016768
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016769 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016770 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016771
16772 /* Always open the file in binary mode, library functions have a mind of
16773 * their own about CR-LF conversion. */
16774 fname = get_tv_string(&argvars[0]);
16775 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16776 {
16777 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16778 return;
16779 }
16780
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016781 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016782 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016783 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016784
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016785 /* This for loop processes what was read, but is also entered at end
16786 * of file so that either:
16787 * - an incomplete line gets written
16788 * - a "binary" file gets an empty line at the end if it ends in a
16789 * newline. */
16790 for (p = buf, start = buf;
16791 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16792 ++p)
16793 {
16794 if (*p == '\n' || readlen <= 0)
16795 {
16796 listitem_T *li;
16797 char_u *s = NULL;
16798 long_u len = p - start;
16799
16800 /* Finished a line. Remove CRs before NL. */
16801 if (readlen > 0 && !binary)
16802 {
16803 while (len > 0 && start[len - 1] == '\r')
16804 --len;
16805 /* removal may cross back to the "prev" string */
16806 if (len == 0)
16807 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16808 --prevlen;
16809 }
16810 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016811 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016812 else
16813 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016814 /* Change "prev" buffer to be the right size. This way
16815 * the bytes are only copied once, and very long lines are
16816 * allocated only once. */
16817 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016818 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016819 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016820 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016821 prev = NULL; /* the list will own the string */
16822 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016823 }
16824 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016825 if (s == NULL)
16826 {
16827 do_outofmem_msg((long_u) prevlen + len + 1);
16828 failed = TRUE;
16829 break;
16830 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016831
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016832 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016833 {
16834 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016835 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016836 break;
16837 }
16838 li->li_tv.v_type = VAR_STRING;
16839 li->li_tv.v_lock = 0;
16840 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016841 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016842
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016843 start = p + 1; /* step over newline */
16844 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016845 break;
16846 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016847 else if (*p == NUL)
16848 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016849#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016850 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16851 * when finding the BF and check the previous two bytes. */
16852 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016853 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016854 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16855 * + 1, these may be in the "prev" string. */
16856 char_u back1 = p >= buf + 1 ? p[-1]
16857 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16858 char_u back2 = p >= buf + 2 ? p[-2]
16859 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16860 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016861
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016862 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016863 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016864 char_u *dest = p - 2;
16865
16866 /* Usually a BOM is at the beginning of a file, and so at
16867 * the beginning of a line; then we can just step over it.
16868 */
16869 if (start == dest)
16870 start = p + 1;
16871 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016872 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016873 /* have to shuffle buf to close gap */
16874 int adjust_prevlen = 0;
16875
16876 if (dest < buf)
16877 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016878 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016879 dest = buf;
16880 }
16881 if (readlen > p - buf + 1)
16882 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16883 readlen -= 3 - adjust_prevlen;
16884 prevlen -= adjust_prevlen;
16885 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016886 }
16887 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016888 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016889#endif
16890 } /* for */
16891
16892 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16893 break;
16894 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016895 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016896 /* There's part of a line in buf, store it in "prev". */
16897 if (p - start + prevlen >= prevsize)
16898 {
16899 /* need bigger "prev" buffer */
16900 char_u *newprev;
16901
16902 /* A common use case is ordinary text files and "prev" gets a
16903 * fragment of a line, so the first allocation is made
16904 * small, to avoid repeatedly 'allocing' large and
16905 * 'reallocing' small. */
16906 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016907 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016908 else
16909 {
16910 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016911 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016912 prevsize = grow50pc > growmin ? grow50pc : growmin;
16913 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016914 newprev = prev == NULL ? alloc(prevsize)
16915 : vim_realloc(prev, prevsize);
16916 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016917 {
16918 do_outofmem_msg((long_u)prevsize);
16919 failed = TRUE;
16920 break;
16921 }
16922 prev = newprev;
16923 }
16924 /* Add the line part to end of "prev". */
16925 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016926 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016927 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016928 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016929
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016930 /*
16931 * For a negative line count use only the lines at the end of the file,
16932 * free the rest.
16933 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016934 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016935 while (cnt > -maxline)
16936 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016937 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016938 --cnt;
16939 }
16940
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016941 if (failed)
16942 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020016943 list_free(rettv->vval.v_list);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016944 /* readfile doc says an empty list is returned on error */
16945 rettv->vval.v_list = list_alloc();
16946 }
16947
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016948 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016949 fclose(fd);
16950}
16951
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016952#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016953static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016954
16955/*
16956 * Convert a List to proftime_T.
16957 * Return FAIL when there is something wrong.
16958 */
16959 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016960list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016961{
16962 long n1, n2;
16963 int error = FALSE;
16964
16965 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16966 || arg->vval.v_list->lv_len != 2)
16967 return FAIL;
16968 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16969 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16970# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016971 tm->HighPart = n1;
16972 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016973# else
16974 tm->tv_sec = n1;
16975 tm->tv_usec = n2;
16976# endif
16977 return error ? FAIL : OK;
16978}
16979#endif /* FEAT_RELTIME */
16980
16981/*
16982 * "reltime()" function
16983 */
16984 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016985f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016986{
16987#ifdef FEAT_RELTIME
16988 proftime_T res;
16989 proftime_T start;
16990
16991 if (argvars[0].v_type == VAR_UNKNOWN)
16992 {
16993 /* No arguments: get current time. */
16994 profile_start(&res);
16995 }
16996 else if (argvars[1].v_type == VAR_UNKNOWN)
16997 {
16998 if (list2proftime(&argvars[0], &res) == FAIL)
16999 return;
17000 profile_end(&res);
17001 }
17002 else
17003 {
17004 /* Two arguments: compute the difference. */
17005 if (list2proftime(&argvars[0], &start) == FAIL
17006 || list2proftime(&argvars[1], &res) == FAIL)
17007 return;
17008 profile_sub(&res, &start);
17009 }
17010
17011 if (rettv_list_alloc(rettv) == OK)
17012 {
17013 long n1, n2;
17014
17015# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000017016 n1 = res.HighPart;
17017 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017018# else
17019 n1 = res.tv_sec;
17020 n2 = res.tv_usec;
17021# endif
17022 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
17023 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
17024 }
17025#endif
17026}
17027
Bram Moolenaar79c2c882016-02-07 21:19:28 +010017028#ifdef FEAT_FLOAT
17029/*
17030 * "reltimefloat()" function
17031 */
17032 static void
17033f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
17034{
17035# ifdef FEAT_RELTIME
17036 proftime_T tm;
17037# endif
17038
17039 rettv->v_type = VAR_FLOAT;
17040 rettv->vval.v_float = 0;
17041# ifdef FEAT_RELTIME
17042 if (list2proftime(&argvars[0], &tm) == OK)
17043 rettv->vval.v_float = profile_float(&tm);
17044# endif
17045}
17046#endif
17047
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017048/*
17049 * "reltimestr()" function
17050 */
17051 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017052f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017053{
17054#ifdef FEAT_RELTIME
17055 proftime_T tm;
17056#endif
17057
17058 rettv->v_type = VAR_STRING;
17059 rettv->vval.v_string = NULL;
17060#ifdef FEAT_RELTIME
17061 if (list2proftime(&argvars[0], &tm) == OK)
17062 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
17063#endif
17064}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000017065
Bram Moolenaar0d660222005-01-07 21:51:51 +000017066#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017067static void make_connection(void);
17068static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017069
17070 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017071make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017072{
17073 if (X_DISPLAY == NULL
17074# ifdef FEAT_GUI
17075 && !gui.in_use
17076# endif
17077 )
17078 {
17079 x_force_connect = TRUE;
17080 setup_term_clip();
17081 x_force_connect = FALSE;
17082 }
17083}
17084
17085 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017086check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017087{
17088 make_connection();
17089 if (X_DISPLAY == NULL)
17090 {
17091 EMSG(_("E240: No connection to Vim server"));
17092 return FAIL;
17093 }
17094 return OK;
17095}
17096#endif
17097
17098#ifdef FEAT_CLIENTSERVER
Bram Moolenaar0d660222005-01-07 21:51:51 +000017099 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017100remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017101{
17102 char_u *server_name;
17103 char_u *keys;
17104 char_u *r = NULL;
17105 char_u buf[NUMBUFLEN];
17106# ifdef WIN32
17107 HWND w;
17108# else
17109 Window w;
17110# endif
17111
17112 if (check_restricted() || check_secure())
17113 return;
17114
17115# ifdef FEAT_X11
17116 if (check_connection() == FAIL)
17117 return;
17118# endif
17119
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017120 server_name = get_tv_string_chk(&argvars[0]);
17121 if (server_name == NULL)
17122 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017123 keys = get_tv_string_buf(&argvars[1], buf);
17124# ifdef WIN32
17125 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
17126# else
17127 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
17128 < 0)
17129# endif
17130 {
17131 if (r != NULL)
17132 EMSG(r); /* sending worked but evaluation failed */
17133 else
17134 EMSG2(_("E241: Unable to send to %s"), server_name);
17135 return;
17136 }
17137
17138 rettv->vval.v_string = r;
17139
17140 if (argvars[2].v_type != VAR_UNKNOWN)
17141 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017142 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000017143 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017144 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017145
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017146 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000017147 v.di_tv.v_type = VAR_STRING;
17148 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017149 idvar = get_tv_string_chk(&argvars[2]);
17150 if (idvar != NULL)
17151 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017152 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017153 }
17154}
17155#endif
17156
17157/*
17158 * "remote_expr()" function
17159 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017160 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017161f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017162{
17163 rettv->v_type = VAR_STRING;
17164 rettv->vval.v_string = NULL;
17165#ifdef FEAT_CLIENTSERVER
17166 remote_common(argvars, rettv, TRUE);
17167#endif
17168}
17169
17170/*
17171 * "remote_foreground()" function
17172 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017173 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017174f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017175{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017176#ifdef FEAT_CLIENTSERVER
17177# ifdef WIN32
17178 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017179 {
17180 char_u *server_name = get_tv_string_chk(&argvars[0]);
17181
17182 if (server_name != NULL)
17183 serverForeground(server_name);
17184 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017185# else
17186 /* Send a foreground() expression to the server. */
17187 argvars[1].v_type = VAR_STRING;
17188 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
17189 argvars[2].v_type = VAR_UNKNOWN;
17190 remote_common(argvars, rettv, TRUE);
17191 vim_free(argvars[1].vval.v_string);
17192# endif
17193#endif
17194}
17195
Bram Moolenaar0d660222005-01-07 21:51:51 +000017196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017197f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017198{
17199#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000017200 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017201 char_u *s = NULL;
17202# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017203 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017204# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017205 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017206
17207 if (check_restricted() || check_secure())
17208 {
17209 rettv->vval.v_number = -1;
17210 return;
17211 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017212 serverid = get_tv_string_chk(&argvars[0]);
17213 if (serverid == NULL)
17214 {
17215 rettv->vval.v_number = -1;
17216 return; /* type error; errmsg already given */
17217 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017218# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017219 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017220 if (n == 0)
17221 rettv->vval.v_number = -1;
17222 else
17223 {
17224 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
17225 rettv->vval.v_number = (s != NULL);
17226 }
17227# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017228 if (check_connection() == FAIL)
17229 return;
17230
17231 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017232 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017233# endif
17234
17235 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
17236 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017237 char_u *retvar;
17238
Bram Moolenaar33570922005-01-25 22:26:29 +000017239 v.di_tv.v_type = VAR_STRING;
17240 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017241 retvar = get_tv_string_chk(&argvars[1]);
17242 if (retvar != NULL)
17243 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017244 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017245 }
17246#else
17247 rettv->vval.v_number = -1;
17248#endif
17249}
17250
Bram Moolenaar0d660222005-01-07 21:51:51 +000017251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017252f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017253{
17254 char_u *r = NULL;
17255
17256#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017257 char_u *serverid = get_tv_string_chk(&argvars[0]);
17258
17259 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000017260 {
17261# ifdef WIN32
17262 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017263 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017264
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017265 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017266 if (n != 0)
17267 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
17268 if (r == NULL)
17269# else
17270 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017271 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017272# endif
17273 EMSG(_("E277: Unable to read a server reply"));
17274 }
17275#endif
17276 rettv->v_type = VAR_STRING;
17277 rettv->vval.v_string = r;
17278}
17279
17280/*
17281 * "remote_send()" function
17282 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017283 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017284f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017285{
17286 rettv->v_type = VAR_STRING;
17287 rettv->vval.v_string = NULL;
17288#ifdef FEAT_CLIENTSERVER
17289 remote_common(argvars, rettv, FALSE);
17290#endif
17291}
17292
17293/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017294 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017295 */
17296 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017297f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017298{
Bram Moolenaar33570922005-01-25 22:26:29 +000017299 list_T *l;
17300 listitem_T *item, *item2;
17301 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017302 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017303 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017304 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017305 dict_T *d;
17306 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017307 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017308
Bram Moolenaar8c711452005-01-14 21:53:12 +000017309 if (argvars[0].v_type == VAR_DICT)
17310 {
17311 if (argvars[2].v_type != VAR_UNKNOWN)
17312 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017313 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017314 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017315 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017316 key = get_tv_string_chk(&argvars[1]);
17317 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017318 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017319 di = dict_find(d, key, -1);
17320 if (di == NULL)
17321 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017322 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17323 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017324 {
17325 *rettv = di->di_tv;
17326 init_tv(&di->di_tv);
17327 dictitem_remove(d, di);
17328 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017329 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017330 }
17331 }
17332 else if (argvars[0].v_type != VAR_LIST)
17333 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017334 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017335 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017336 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017337 int error = FALSE;
17338
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017339 idx = (long)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017340 if (error)
17341 ; /* type error: do nothing, errmsg already given */
17342 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017343 EMSGN(_(e_listidx), idx);
17344 else
17345 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017346 if (argvars[2].v_type == VAR_UNKNOWN)
17347 {
17348 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017349 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017350 *rettv = item->li_tv;
17351 vim_free(item);
17352 }
17353 else
17354 {
17355 /* Remove range of items, return list with values. */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017356 end = (long)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017357 if (error)
17358 ; /* type error: do nothing */
17359 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017360 EMSGN(_(e_listidx), end);
17361 else
17362 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017363 int cnt = 0;
17364
17365 for (li = item; li != NULL; li = li->li_next)
17366 {
17367 ++cnt;
17368 if (li == item2)
17369 break;
17370 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017371 if (li == NULL) /* didn't find "item2" after "item" */
17372 EMSG(_(e_invrange));
17373 else
17374 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017375 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017376 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017377 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017378 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017379 l->lv_first = item;
17380 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017381 item->li_prev = NULL;
17382 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017383 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017384 }
17385 }
17386 }
17387 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017388 }
17389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017390}
17391
17392/*
17393 * "rename({from}, {to})" function
17394 */
17395 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017396f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017397{
17398 char_u buf[NUMBUFLEN];
17399
17400 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017401 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017402 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017403 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17404 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017405}
17406
17407/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017408 * "repeat()" function
17409 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017410 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017411f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017412{
17413 char_u *p;
17414 int n;
17415 int slen;
17416 int len;
17417 char_u *r;
17418 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017419
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017420 n = (int)get_tv_number(&argvars[1]);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017421 if (argvars[0].v_type == VAR_LIST)
17422 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017423 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017424 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017425 if (list_extend(rettv->vval.v_list,
17426 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017427 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017428 }
17429 else
17430 {
17431 p = get_tv_string(&argvars[0]);
17432 rettv->v_type = VAR_STRING;
17433 rettv->vval.v_string = NULL;
17434
17435 slen = (int)STRLEN(p);
17436 len = slen * n;
17437 if (len <= 0)
17438 return;
17439
17440 r = alloc(len + 1);
17441 if (r != NULL)
17442 {
17443 for (i = 0; i < n; i++)
17444 mch_memmove(r + i * slen, p, (size_t)slen);
17445 r[len] = NUL;
17446 }
17447
17448 rettv->vval.v_string = r;
17449 }
17450}
17451
17452/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017453 * "resolve()" function
17454 */
17455 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017456f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017457{
17458 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017459#ifdef HAVE_READLINK
17460 char_u *buf = NULL;
17461#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017462
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017463 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017464#ifdef FEAT_SHORTCUT
17465 {
17466 char_u *v = NULL;
17467
17468 v = mch_resolve_shortcut(p);
17469 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017470 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017471 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017472 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017473 }
17474#else
17475# ifdef HAVE_READLINK
17476 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017477 char_u *cpy;
17478 int len;
17479 char_u *remain = NULL;
17480 char_u *q;
17481 int is_relative_to_current = FALSE;
17482 int has_trailing_pathsep = FALSE;
17483 int limit = 100;
17484
17485 p = vim_strsave(p);
17486
17487 if (p[0] == '.' && (vim_ispathsep(p[1])
17488 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17489 is_relative_to_current = TRUE;
17490
17491 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017492 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017493 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017494 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017495 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017497
17498 q = getnextcomp(p);
17499 if (*q != NUL)
17500 {
17501 /* Separate the first path component in "p", and keep the
17502 * remainder (beginning with the path separator). */
17503 remain = vim_strsave(q - 1);
17504 q[-1] = NUL;
17505 }
17506
Bram Moolenaard9462e32011-04-11 21:35:11 +020017507 buf = alloc(MAXPATHL + 1);
17508 if (buf == NULL)
17509 goto fail;
17510
Bram Moolenaar071d4272004-06-13 20:20:40 +000017511 for (;;)
17512 {
17513 for (;;)
17514 {
17515 len = readlink((char *)p, (char *)buf, MAXPATHL);
17516 if (len <= 0)
17517 break;
17518 buf[len] = NUL;
17519
17520 if (limit-- == 0)
17521 {
17522 vim_free(p);
17523 vim_free(remain);
17524 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017525 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017526 goto fail;
17527 }
17528
17529 /* Ensure that the result will have a trailing path separator
17530 * if the argument has one. */
17531 if (remain == NULL && has_trailing_pathsep)
17532 add_pathsep(buf);
17533
17534 /* Separate the first path component in the link value and
17535 * concatenate the remainders. */
17536 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17537 if (*q != NUL)
17538 {
17539 if (remain == NULL)
17540 remain = vim_strsave(q - 1);
17541 else
17542 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017543 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017544 if (cpy != NULL)
17545 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017546 vim_free(remain);
17547 remain = cpy;
17548 }
17549 }
17550 q[-1] = NUL;
17551 }
17552
17553 q = gettail(p);
17554 if (q > p && *q == NUL)
17555 {
17556 /* Ignore trailing path separator. */
17557 q[-1] = NUL;
17558 q = gettail(p);
17559 }
17560 if (q > p && !mch_isFullName(buf))
17561 {
17562 /* symlink is relative to directory of argument */
17563 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17564 if (cpy != NULL)
17565 {
17566 STRCPY(cpy, p);
17567 STRCPY(gettail(cpy), buf);
17568 vim_free(p);
17569 p = cpy;
17570 }
17571 }
17572 else
17573 {
17574 vim_free(p);
17575 p = vim_strsave(buf);
17576 }
17577 }
17578
17579 if (remain == NULL)
17580 break;
17581
17582 /* Append the first path component of "remain" to "p". */
17583 q = getnextcomp(remain + 1);
17584 len = q - remain - (*q != NUL);
17585 cpy = vim_strnsave(p, STRLEN(p) + len);
17586 if (cpy != NULL)
17587 {
17588 STRNCAT(cpy, remain, len);
17589 vim_free(p);
17590 p = cpy;
17591 }
17592 /* Shorten "remain". */
17593 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017594 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017595 else
17596 {
17597 vim_free(remain);
17598 remain = NULL;
17599 }
17600 }
17601
17602 /* If the result is a relative path name, make it explicitly relative to
17603 * the current directory if and only if the argument had this form. */
17604 if (!vim_ispathsep(*p))
17605 {
17606 if (is_relative_to_current
17607 && *p != NUL
17608 && !(p[0] == '.'
17609 && (p[1] == NUL
17610 || vim_ispathsep(p[1])
17611 || (p[1] == '.'
17612 && (p[2] == NUL
17613 || vim_ispathsep(p[2]))))))
17614 {
17615 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017616 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017617 if (cpy != NULL)
17618 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017619 vim_free(p);
17620 p = cpy;
17621 }
17622 }
17623 else if (!is_relative_to_current)
17624 {
17625 /* Strip leading "./". */
17626 q = p;
17627 while (q[0] == '.' && vim_ispathsep(q[1]))
17628 q += 2;
17629 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017630 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017631 }
17632 }
17633
17634 /* Ensure that the result will have no trailing path separator
17635 * if the argument had none. But keep "/" or "//". */
17636 if (!has_trailing_pathsep)
17637 {
17638 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017639 if (after_pathsep(p, q))
17640 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017641 }
17642
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017643 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017644 }
17645# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017646 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017647# endif
17648#endif
17649
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017650 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017651
17652#ifdef HAVE_READLINK
17653fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017654 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017655#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017656 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017657}
17658
17659/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017660 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017661 */
17662 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017663f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017664{
Bram Moolenaar33570922005-01-25 22:26:29 +000017665 list_T *l;
17666 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017667
Bram Moolenaar0d660222005-01-07 21:51:51 +000017668 if (argvars[0].v_type != VAR_LIST)
17669 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017670 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017671 && !tv_check_lock(l->lv_lock,
17672 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017673 {
17674 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017675 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017676 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017677 while (li != NULL)
17678 {
17679 ni = li->li_prev;
17680 list_append(l, li);
17681 li = ni;
17682 }
17683 rettv->vval.v_list = l;
17684 rettv->v_type = VAR_LIST;
17685 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017686 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017688}
17689
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017690#define SP_NOMOVE 0x01 /* don't move cursor */
17691#define SP_REPEAT 0x02 /* repeat to find outer pair */
17692#define SP_RETCOUNT 0x04 /* return matchcount */
17693#define SP_SETPCMARK 0x08 /* set previous context mark */
17694#define SP_START 0x10 /* accept match at start position */
17695#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17696#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017697#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017698
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017699static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017700
17701/*
17702 * Get flags for a search function.
17703 * Possibly sets "p_ws".
17704 * Returns BACKWARD, FORWARD or zero (for an error).
17705 */
17706 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017707get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017708{
17709 int dir = FORWARD;
17710 char_u *flags;
17711 char_u nbuf[NUMBUFLEN];
17712 int mask;
17713
17714 if (varp->v_type != VAR_UNKNOWN)
17715 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017716 flags = get_tv_string_buf_chk(varp, nbuf);
17717 if (flags == NULL)
17718 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017719 while (*flags != NUL)
17720 {
17721 switch (*flags)
17722 {
17723 case 'b': dir = BACKWARD; break;
17724 case 'w': p_ws = TRUE; break;
17725 case 'W': p_ws = FALSE; break;
17726 default: mask = 0;
17727 if (flagsp != NULL)
17728 switch (*flags)
17729 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017730 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017731 case 'e': mask = SP_END; break;
17732 case 'm': mask = SP_RETCOUNT; break;
17733 case 'n': mask = SP_NOMOVE; break;
17734 case 'p': mask = SP_SUBPAT; break;
17735 case 'r': mask = SP_REPEAT; break;
17736 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017737 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017738 }
17739 if (mask == 0)
17740 {
17741 EMSG2(_(e_invarg2), flags);
17742 dir = 0;
17743 }
17744 else
17745 *flagsp |= mask;
17746 }
17747 if (dir == 0)
17748 break;
17749 ++flags;
17750 }
17751 }
17752 return dir;
17753}
17754
Bram Moolenaar071d4272004-06-13 20:20:40 +000017755/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017756 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017757 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017758 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017759search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017760{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017761 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017762 char_u *pat;
17763 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017764 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017765 int save_p_ws = p_ws;
17766 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017767 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017768 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017769 proftime_T tm;
17770#ifdef FEAT_RELTIME
17771 long time_limit = 0;
17772#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017773 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017774 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017775
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017776 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017777 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017778 if (dir == 0)
17779 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017780 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017781 if (flags & SP_START)
17782 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017783 if (flags & SP_END)
17784 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017785 if (flags & SP_COLUMN)
17786 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017787
Bram Moolenaar76929292008-01-06 19:07:36 +000017788 /* Optional arguments: line number to stop searching and timeout. */
17789 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017790 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017791 lnum_stop = (long)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017792 if (lnum_stop < 0)
17793 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017794#ifdef FEAT_RELTIME
17795 if (argvars[3].v_type != VAR_UNKNOWN)
17796 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017797 time_limit = (long)get_tv_number_chk(&argvars[3], NULL);
Bram Moolenaar76929292008-01-06 19:07:36 +000017798 if (time_limit < 0)
17799 goto theend;
17800 }
17801#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017802 }
17803
Bram Moolenaar76929292008-01-06 19:07:36 +000017804#ifdef FEAT_RELTIME
17805 /* Set the time limit, if there is one. */
17806 profile_setlimit(time_limit, &tm);
17807#endif
17808
Bram Moolenaar231334e2005-07-25 20:46:57 +000017809 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017810 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017811 * Check to make sure only those flags are set.
17812 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17813 * flags cannot be set. Check for that condition also.
17814 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017815 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017816 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017817 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017818 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017819 goto theend;
17820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017821
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017822 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017823 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017824 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017825 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017826 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017827 if (flags & SP_SUBPAT)
17828 retval = subpatnum;
17829 else
17830 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017831 if (flags & SP_SETPCMARK)
17832 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017833 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017834 if (match_pos != NULL)
17835 {
17836 /* Store the match cursor position */
17837 match_pos->lnum = pos.lnum;
17838 match_pos->col = pos.col + 1;
17839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017840 /* "/$" will put the cursor after the end of the line, may need to
17841 * correct that here */
17842 check_cursor();
17843 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017844
17845 /* If 'n' flag is used: restore cursor position. */
17846 if (flags & SP_NOMOVE)
17847 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017848 else
17849 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017850theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017851 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017852
17853 return retval;
17854}
17855
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017856#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017857
17858/*
17859 * round() is not in C90, use ceil() or floor() instead.
17860 */
17861 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017862vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017863{
17864 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17865}
17866
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017867/*
17868 * "round({float})" function
17869 */
17870 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017871f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017872{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017873 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017874
17875 rettv->v_type = VAR_FLOAT;
17876 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017877 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017878 else
17879 rettv->vval.v_float = 0.0;
17880}
17881#endif
17882
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017883/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017884 * "screenattr()" function
17885 */
17886 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017887f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017888{
17889 int row;
17890 int col;
17891 int c;
17892
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017893 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
17894 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar9a773482013-06-11 18:40:13 +020017895 if (row < 0 || row >= screen_Rows
17896 || col < 0 || col >= screen_Columns)
17897 c = -1;
17898 else
17899 c = ScreenAttrs[LineOffset[row] + col];
17900 rettv->vval.v_number = c;
17901}
17902
17903/*
17904 * "screenchar()" function
17905 */
17906 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017907f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017908{
17909 int row;
17910 int col;
17911 int off;
17912 int c;
17913
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017914 row = (int)get_tv_number_chk(&argvars[0], NULL) - 1;
17915 col = (int)get_tv_number_chk(&argvars[1], NULL) - 1;
Bram Moolenaar9a773482013-06-11 18:40:13 +020017916 if (row < 0 || row >= screen_Rows
17917 || col < 0 || col >= screen_Columns)
17918 c = -1;
17919 else
17920 {
17921 off = LineOffset[row] + col;
17922#ifdef FEAT_MBYTE
17923 if (enc_utf8 && ScreenLinesUC[off] != 0)
17924 c = ScreenLinesUC[off];
17925 else
17926#endif
17927 c = ScreenLines[off];
17928 }
17929 rettv->vval.v_number = c;
17930}
17931
17932/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017933 * "screencol()" function
17934 *
17935 * First column is 1 to be consistent with virtcol().
17936 */
17937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017938f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017939{
17940 rettv->vval.v_number = screen_screencol() + 1;
17941}
17942
17943/*
17944 * "screenrow()" function
17945 */
17946 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017947f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017948{
17949 rettv->vval.v_number = screen_screenrow() + 1;
17950}
17951
17952/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017953 * "search()" function
17954 */
17955 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017956f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017957{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017958 int flags = 0;
17959
17960 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017961}
17962
Bram Moolenaar071d4272004-06-13 20:20:40 +000017963/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017964 * "searchdecl()" function
17965 */
17966 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017967f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017968{
17969 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017970 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017971 int error = FALSE;
17972 char_u *name;
17973
17974 rettv->vval.v_number = 1; /* default: FAIL */
17975
17976 name = get_tv_string_chk(&argvars[0]);
17977 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017978 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017979 locally = (int)get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017980 if (!error && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020017981 thisblock = (int)get_tv_number_chk(&argvars[2], &error) != 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017982 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017983 if (!error && name != NULL)
17984 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017985 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017986}
17987
17988/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017989 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017990 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017991 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017992searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017993{
17994 char_u *spat, *mpat, *epat;
17995 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017996 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017997 int dir;
17998 int flags = 0;
17999 char_u nbuf1[NUMBUFLEN];
18000 char_u nbuf2[NUMBUFLEN];
18001 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018002 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018003 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000018004 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018005
Bram Moolenaar071d4272004-06-13 20:20:40 +000018006 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018007 spat = get_tv_string_chk(&argvars[0]);
18008 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
18009 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
18010 if (spat == NULL || mpat == NULL || epat == NULL)
18011 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018012
Bram Moolenaar071d4272004-06-13 20:20:40 +000018013 /* Handle the optional fourth argument: flags */
18014 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018015 if (dir == 0)
18016 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018017
18018 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000018019 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
18020 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018021 if ((flags & (SP_END | SP_SUBPAT)) != 0
18022 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000018023 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018024 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000018025 goto theend;
18026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018027
Bram Moolenaar92de73d2008-01-22 10:59:38 +000018028 /* Using 'r' implies 'W', otherwise it doesn't work. */
18029 if (flags & SP_REPEAT)
18030 p_ws = FALSE;
18031
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018032 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018033 if (argvars[3].v_type == VAR_UNKNOWN
18034 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018035 skip = (char_u *)"";
18036 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018037 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018038 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018039 if (argvars[5].v_type != VAR_UNKNOWN)
18040 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018041 lnum_stop = (long)get_tv_number_chk(&argvars[5], NULL);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018042 if (lnum_stop < 0)
18043 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000018044#ifdef FEAT_RELTIME
18045 if (argvars[6].v_type != VAR_UNKNOWN)
18046 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018047 time_limit = (long)get_tv_number_chk(&argvars[6], NULL);
Bram Moolenaar76929292008-01-06 19:07:36 +000018048 if (time_limit < 0)
18049 goto theend;
18050 }
18051#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018052 }
18053 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018054 if (skip == NULL)
18055 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018056
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018057 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000018058 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018059
18060theend:
18061 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018062
18063 return retval;
18064}
18065
18066/*
18067 * "searchpair()" function
18068 */
18069 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018070f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018071{
18072 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
18073}
18074
18075/*
18076 * "searchpairpos()" function
18077 */
18078 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018079f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018080{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018081 pos_T match_pos;
18082 int lnum = 0;
18083 int col = 0;
18084
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018085 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018086 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018087
18088 if (searchpair_cmn(argvars, &match_pos) > 0)
18089 {
18090 lnum = match_pos.lnum;
18091 col = match_pos.col;
18092 }
18093
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018094 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18095 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018096}
18097
18098/*
18099 * Search for a start/middle/end thing.
18100 * Used by searchpair(), see its documentation for the details.
18101 * Returns 0 or -1 for no match,
18102 */
18103 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010018104do_searchpair(
18105 char_u *spat, /* start pattern */
18106 char_u *mpat, /* middle pattern */
18107 char_u *epat, /* end pattern */
18108 int dir, /* BACKWARD or FORWARD */
18109 char_u *skip, /* skip expression */
18110 int flags, /* SP_SETPCMARK and other SP_ values */
18111 pos_T *match_pos,
18112 linenr_T lnum_stop, /* stop at this line if not zero */
18113 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018114{
18115 char_u *save_cpo;
18116 char_u *pat, *pat2 = NULL, *pat3 = NULL;
18117 long retval = 0;
18118 pos_T pos;
18119 pos_T firstpos;
18120 pos_T foundpos;
18121 pos_T save_cursor;
18122 pos_T save_pos;
18123 int n;
18124 int r;
18125 int nest = 1;
18126 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018127 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000018128 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018129
18130 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18131 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018132 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018133
Bram Moolenaar76929292008-01-06 19:07:36 +000018134#ifdef FEAT_RELTIME
18135 /* Set the time limit, if there is one. */
18136 profile_setlimit(time_limit, &tm);
18137#endif
18138
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018139 /* Make two search patterns: start/end (pat2, for in nested pairs) and
18140 * start/middle/end (pat3, for the top pair). */
18141 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
18142 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
18143 if (pat2 == NULL || pat3 == NULL)
18144 goto theend;
18145 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
18146 if (*mpat == NUL)
18147 STRCPY(pat3, pat2);
18148 else
18149 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
18150 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018151 if (flags & SP_START)
18152 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018153
Bram Moolenaar071d4272004-06-13 20:20:40 +000018154 save_cursor = curwin->w_cursor;
18155 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018156 clearpos(&firstpos);
18157 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018158 pat = pat3;
18159 for (;;)
18160 {
18161 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000018162 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018163 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
18164 /* didn't find it or found the first match again: FAIL */
18165 break;
18166
18167 if (firstpos.lnum == 0)
18168 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000018169 if (equalpos(pos, foundpos))
18170 {
18171 /* Found the same position again. Can happen with a pattern that
18172 * has "\zs" at the end and searching backwards. Advance one
18173 * character and try again. */
18174 if (dir == BACKWARD)
18175 decl(&pos);
18176 else
18177 incl(&pos);
18178 }
18179 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018180
Bram Moolenaar92de73d2008-01-22 10:59:38 +000018181 /* clear the start flag to avoid getting stuck here */
18182 options &= ~SEARCH_START;
18183
Bram Moolenaar071d4272004-06-13 20:20:40 +000018184 /* If the skip pattern matches, ignore this match. */
18185 if (*skip != NUL)
18186 {
18187 save_pos = curwin->w_cursor;
18188 curwin->w_cursor = pos;
18189 r = eval_to_bool(skip, &err, NULL, FALSE);
18190 curwin->w_cursor = save_pos;
18191 if (err)
18192 {
18193 /* Evaluating {skip} caused an error, break here. */
18194 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018195 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018196 break;
18197 }
18198 if (r)
18199 continue;
18200 }
18201
18202 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
18203 {
18204 /* Found end when searching backwards or start when searching
18205 * forward: nested pair. */
18206 ++nest;
18207 pat = pat2; /* nested, don't search for middle */
18208 }
18209 else
18210 {
18211 /* Found end when searching forward or start when searching
18212 * backward: end of (nested) pair; or found middle in outer pair. */
18213 if (--nest == 1)
18214 pat = pat3; /* outer level, search for middle */
18215 }
18216
18217 if (nest == 0)
18218 {
18219 /* Found the match: return matchcount or line number. */
18220 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018221 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018222 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018223 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000018224 if (flags & SP_SETPCMARK)
18225 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018226 curwin->w_cursor = pos;
18227 if (!(flags & SP_REPEAT))
18228 break;
18229 nest = 1; /* search for next unmatched */
18230 }
18231 }
18232
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018233 if (match_pos != NULL)
18234 {
18235 /* Store the match cursor position */
18236 match_pos->lnum = curwin->w_cursor.lnum;
18237 match_pos->col = curwin->w_cursor.col + 1;
18238 }
18239
Bram Moolenaar071d4272004-06-13 20:20:40 +000018240 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018241 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018242 curwin->w_cursor = save_cursor;
18243
18244theend:
18245 vim_free(pat2);
18246 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018247 if (p_cpo == empty_option)
18248 p_cpo = save_cpo;
18249 else
18250 /* Darn, evaluating the {skip} expression changed the value. */
18251 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018252
18253 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018254}
18255
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018256/*
18257 * "searchpos()" function
18258 */
18259 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018260f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018261{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018262 pos_T match_pos;
18263 int lnum = 0;
18264 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018265 int n;
18266 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018267
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018268 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018269 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018270
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018271 n = search_cmn(argvars, &match_pos, &flags);
18272 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018273 {
18274 lnum = match_pos.lnum;
18275 col = match_pos.col;
18276 }
18277
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018278 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18279 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018280 if (flags & SP_SUBPAT)
18281 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018282}
18283
Bram Moolenaar0d660222005-01-07 21:51:51 +000018284 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018285f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018286{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018287#ifdef FEAT_CLIENTSERVER
18288 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018289 char_u *server = get_tv_string_chk(&argvars[0]);
18290 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018291
Bram Moolenaar0d660222005-01-07 21:51:51 +000018292 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018293 if (server == NULL || reply == NULL)
18294 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018295 if (check_restricted() || check_secure())
18296 return;
18297# ifdef FEAT_X11
18298 if (check_connection() == FAIL)
18299 return;
18300# endif
18301
18302 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018303 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018304 EMSG(_("E258: Unable to send to client"));
18305 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018306 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018307 rettv->vval.v_number = 0;
18308#else
18309 rettv->vval.v_number = -1;
18310#endif
18311}
18312
Bram Moolenaar0d660222005-01-07 21:51:51 +000018313 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018314f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018315{
18316 char_u *r = NULL;
18317
18318#ifdef FEAT_CLIENTSERVER
18319# ifdef WIN32
18320 r = serverGetVimNames();
18321# else
18322 make_connection();
18323 if (X_DISPLAY != NULL)
18324 r = serverGetVimNames(X_DISPLAY);
18325# endif
18326#endif
18327 rettv->v_type = VAR_STRING;
18328 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018329}
18330
18331/*
18332 * "setbufvar()" function
18333 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018334 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018335f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018336{
18337 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018338 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018339 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018340 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018341 char_u nbuf[NUMBUFLEN];
18342
18343 if (check_restricted() || check_secure())
18344 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018345 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18346 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018347 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018348 varp = &argvars[2];
18349
18350 if (buf != NULL && varname != NULL && varp != NULL)
18351 {
18352 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018353 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018354
18355 if (*varname == '&')
18356 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018357 long numval;
18358 char_u *strval;
18359 int error = FALSE;
18360
Bram Moolenaar071d4272004-06-13 20:20:40 +000018361 ++varname;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018362 numval = (long)get_tv_number_chk(varp, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018363 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018364 if (!error && strval != NULL)
18365 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018366 }
18367 else
18368 {
18369 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18370 if (bufvarname != NULL)
18371 {
18372 STRCPY(bufvarname, "b:");
18373 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018374 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018375 vim_free(bufvarname);
18376 }
18377 }
18378
18379 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018380 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018382}
18383
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018384 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018385f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018386{
18387 dict_T *d;
18388 dictitem_T *di;
18389 char_u *csearch;
18390
18391 if (argvars[0].v_type != VAR_DICT)
18392 {
18393 EMSG(_(e_dictreq));
18394 return;
18395 }
18396
18397 if ((d = argvars[0].vval.v_dict) != NULL)
18398 {
18399 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18400 if (csearch != NULL)
18401 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018402#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018403 if (enc_utf8)
18404 {
18405 int pcc[MAX_MCO];
18406 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018407
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018408 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18409 }
18410 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018411#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018412 set_last_csearch(PTR2CHAR(csearch),
18413 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018414 }
18415
18416 di = dict_find(d, (char_u *)"forward", -1);
18417 if (di != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020018418 set_csearch_direction((int)get_tv_number(&di->di_tv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018419 ? FORWARD : BACKWARD);
18420
18421 di = dict_find(d, (char_u *)"until", -1);
18422 if (di != NULL)
18423 set_csearch_until(!!get_tv_number(&di->di_tv));
18424 }
18425}
18426
Bram Moolenaar071d4272004-06-13 20:20:40 +000018427/*
18428 * "setcmdpos()" function
18429 */
18430 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018431f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018432{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018433 int pos = (int)get_tv_number(&argvars[0]) - 1;
18434
18435 if (pos >= 0)
18436 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018437}
18438
18439/*
Bram Moolenaar80492532016-03-08 17:08:53 +010018440 * "setfperm({fname}, {mode})" function
18441 */
18442 static void
18443f_setfperm(typval_T *argvars, typval_T *rettv)
18444{
18445 char_u *fname;
18446 char_u modebuf[NUMBUFLEN];
18447 char_u *mode_str;
18448 int i;
18449 int mask;
18450 int mode = 0;
18451
18452 rettv->vval.v_number = 0;
18453 fname = get_tv_string_chk(&argvars[0]);
18454 if (fname == NULL)
18455 return;
18456 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
18457 if (mode_str == NULL)
18458 return;
18459 if (STRLEN(mode_str) != 9)
18460 {
18461 EMSG2(_(e_invarg2), mode_str);
18462 return;
18463 }
18464
18465 mask = 1;
18466 for (i = 8; i >= 0; --i)
18467 {
18468 if (mode_str[i] != '-')
18469 mode |= mask;
18470 mask = mask << 1;
18471 }
18472 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
18473}
18474
18475/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018476 * "setline()" function
18477 */
18478 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018479f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018480{
18481 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018482 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018483 list_T *l = NULL;
18484 listitem_T *li = NULL;
18485 long added = 0;
18486 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018487
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018488 lnum = get_tv_lnum(&argvars[0]);
18489 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018490 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018491 l = argvars[1].vval.v_list;
18492 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018493 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018494 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018495 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018496
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018497 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018498 for (;;)
18499 {
18500 if (l != NULL)
18501 {
18502 /* list argument, get next string */
18503 if (li == NULL)
18504 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018505 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018506 li = li->li_next;
18507 }
18508
18509 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018510 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018511 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018512
18513 /* When coming here from Insert mode, sync undo, so that this can be
18514 * undone separately from what was previously inserted. */
18515 if (u_sync_once == 2)
18516 {
18517 u_sync_once = 1; /* notify that u_sync() was called */
18518 u_sync(TRUE);
18519 }
18520
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018521 if (lnum <= curbuf->b_ml.ml_line_count)
18522 {
18523 /* existing line, replace it */
18524 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18525 {
18526 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018527 if (lnum == curwin->w_cursor.lnum)
18528 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018529 rettv->vval.v_number = 0; /* OK */
18530 }
18531 }
18532 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18533 {
18534 /* lnum is one past the last line, append the line */
18535 ++added;
18536 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18537 rettv->vval.v_number = 0; /* OK */
18538 }
18539
18540 if (l == NULL) /* only one string argument */
18541 break;
18542 ++lnum;
18543 }
18544
18545 if (added > 0)
18546 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018547}
18548
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018549static 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 +000018550
Bram Moolenaar071d4272004-06-13 20:20:40 +000018551/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018552 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018553 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018554 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018555set_qf_ll_list(
18556 win_T *wp UNUSED,
18557 typval_T *list_arg UNUSED,
18558 typval_T *action_arg UNUSED,
18559 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018560{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018561#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018562 static char *e_invact = N_("E927: Invalid action: '%s'");
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018563 char_u *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018564 int action = 0;
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018565#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018566
Bram Moolenaar2641f772005-03-25 21:58:17 +000018567 rettv->vval.v_number = -1;
18568
18569#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018570 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018571 EMSG(_(e_listreq));
18572 else
18573 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018574 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018575
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018576 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018577 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018578 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018579 if (act == NULL)
18580 return; /* type error; errmsg already given */
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018581 if ((*act == 'a' || *act == 'r' || *act == ' ') && act[1] == NUL)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018582 action = *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018583 else
18584 EMSG2(_(e_invact), act);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018585 }
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018586 else if (action_arg->v_type == VAR_UNKNOWN)
18587 action = ' ';
18588 else
18589 EMSG(_(e_stringreq));
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018590
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018591 if (l != NULL && action && set_errorlist(wp, l, action,
Bram Moolenaar81484f42012-12-05 15:16:47 +010018592 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018593 rettv->vval.v_number = 0;
18594 }
18595#endif
18596}
18597
18598/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018599 * "setloclist()" function
18600 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018601 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018602f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018603{
18604 win_T *win;
18605
18606 rettv->vval.v_number = -1;
18607
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018608 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018609 if (win != NULL)
18610 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18611}
18612
18613/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018614 * "setmatches()" function
18615 */
18616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018617f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018618{
18619#ifdef FEAT_SEARCH_EXTRA
18620 list_T *l;
18621 listitem_T *li;
18622 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018623 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018624
18625 rettv->vval.v_number = -1;
18626 if (argvars[0].v_type != VAR_LIST)
18627 {
18628 EMSG(_(e_listreq));
18629 return;
18630 }
18631 if ((l = argvars[0].vval.v_list) != NULL)
18632 {
18633
18634 /* To some extent make sure that we are dealing with a list from
18635 * "getmatches()". */
18636 li = l->lv_first;
18637 while (li != NULL)
18638 {
18639 if (li->li_tv.v_type != VAR_DICT
18640 || (d = li->li_tv.vval.v_dict) == NULL)
18641 {
18642 EMSG(_(e_invarg));
18643 return;
18644 }
18645 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018646 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18647 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018648 && dict_find(d, (char_u *)"priority", -1) != NULL
18649 && dict_find(d, (char_u *)"id", -1) != NULL))
18650 {
18651 EMSG(_(e_invarg));
18652 return;
18653 }
18654 li = li->li_next;
18655 }
18656
18657 clear_matches(curwin);
18658 li = l->lv_first;
18659 while (li != NULL)
18660 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018661 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018662 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018663 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018664 char_u *group;
18665 int priority;
18666 int id;
18667 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018668
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018669 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018670 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18671 {
18672 if (s == NULL)
18673 {
18674 s = list_alloc();
18675 if (s == NULL)
18676 return;
18677 }
18678
18679 /* match from matchaddpos() */
18680 for (i = 1; i < 9; i++)
18681 {
18682 sprintf((char *)buf, (char *)"pos%d", i);
18683 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18684 {
18685 if (di->di_tv.v_type != VAR_LIST)
18686 return;
18687
18688 list_append_tv(s, &di->di_tv);
18689 s->lv_refcount++;
18690 }
18691 else
18692 break;
18693 }
18694 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018695
18696 group = get_dict_string(d, (char_u *)"group", FALSE);
18697 priority = (int)get_dict_number(d, (char_u *)"priority");
18698 id = (int)get_dict_number(d, (char_u *)"id");
18699 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18700 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18701 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018702 if (i == 0)
18703 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018704 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018705 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018706 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018707 }
18708 else
18709 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018710 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018711 list_unref(s);
18712 s = NULL;
18713 }
18714
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018715 li = li->li_next;
18716 }
18717 rettv->vval.v_number = 0;
18718 }
18719#endif
18720}
18721
18722/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018723 * "setpos()" function
18724 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018725 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018726f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018727{
18728 pos_T pos;
18729 int fnum;
18730 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018731 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018732
Bram Moolenaar08250432008-02-13 11:42:46 +000018733 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018734 name = get_tv_string_chk(argvars);
18735 if (name != NULL)
18736 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018737 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018738 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018739 if (--pos.col < 0)
18740 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018741 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018742 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018743 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018744 if (fnum == curbuf->b_fnum)
18745 {
18746 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018747 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018748 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018749 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018750 curwin->w_set_curswant = FALSE;
18751 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018752 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018753 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018754 }
18755 else
18756 EMSG(_(e_invarg));
18757 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018758 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18759 {
18760 /* set mark */
18761 if (setmark_pos(name[1], &pos, fnum) == OK)
18762 rettv->vval.v_number = 0;
18763 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018764 else
18765 EMSG(_(e_invarg));
18766 }
18767 }
18768}
18769
18770/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018771 * "setqflist()" function
18772 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018773 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018774f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018775{
18776 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18777}
18778
18779/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018780 * "setreg()" function
18781 */
18782 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018783f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018784{
18785 int regname;
18786 char_u *strregname;
18787 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018788 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018789 int append;
18790 char_u yank_type;
18791 long block_len;
18792
18793 block_len = -1;
18794 yank_type = MAUTO;
18795 append = FALSE;
18796
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018797 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018798 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018799
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018800 if (strregname == NULL)
18801 return; /* type error; errmsg already given */
18802 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018803 if (regname == 0 || regname == '@')
18804 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018805
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018806 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018807 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018808 stropt = get_tv_string_chk(&argvars[2]);
18809 if (stropt == NULL)
18810 return; /* type error */
18811 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018812 switch (*stropt)
18813 {
18814 case 'a': case 'A': /* append */
18815 append = TRUE;
18816 break;
18817 case 'v': case 'c': /* character-wise selection */
18818 yank_type = MCHAR;
18819 break;
18820 case 'V': case 'l': /* line-wise selection */
18821 yank_type = MLINE;
18822 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018823 case 'b': case Ctrl_V: /* block-wise selection */
18824 yank_type = MBLOCK;
18825 if (VIM_ISDIGIT(stropt[1]))
18826 {
18827 ++stropt;
18828 block_len = getdigits(&stropt) - 1;
18829 --stropt;
18830 }
18831 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018832 }
18833 }
18834
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018835 if (argvars[1].v_type == VAR_LIST)
18836 {
18837 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018838 char_u **allocval;
18839 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018840 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018841 char_u **curallocval;
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018842 list_T *ll = argvars[1].vval.v_list;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018843 listitem_T *li;
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018844 int len;
18845
18846 /* If the list is NULL handle like an empty list. */
18847 len = ll == NULL ? 0 : ll->lv_len;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018848
Bram Moolenaar7d647822014-04-05 21:28:56 +020018849 /* First half: use for pointers to result lines; second half: use for
18850 * pointers to allocated copies. */
18851 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018852 if (lstval == NULL)
18853 return;
18854 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018855 allocval = lstval + len + 2;
18856 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018857
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018858 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018859 li = li->li_next)
18860 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018861 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018862 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018863 goto free_lstval;
18864 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018865 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018866 /* Need to make a copy, next get_tv_string_buf_chk() will
18867 * overwrite the string. */
18868 strval = vim_strsave(buf);
18869 if (strval == NULL)
18870 goto free_lstval;
18871 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018872 }
18873 *curval++ = strval;
18874 }
18875 *curval++ = NULL;
18876
18877 write_reg_contents_lst(regname, lstval, -1,
18878 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018879free_lstval:
18880 while (curallocval > allocval)
18881 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018882 vim_free(lstval);
18883 }
18884 else
18885 {
18886 strval = get_tv_string_chk(&argvars[1]);
18887 if (strval == NULL)
18888 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018889 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018890 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018891 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018892 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018893}
18894
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018895/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018896 * "settabvar()" function
18897 */
18898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018899f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018900{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018901#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018902 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018903 tabpage_T *tp;
18904#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018905 char_u *varname, *tabvarname;
18906 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018907
18908 rettv->vval.v_number = 0;
18909
18910 if (check_restricted() || check_secure())
18911 return;
18912
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018913#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018914 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018915#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018916 varname = get_tv_string_chk(&argvars[1]);
18917 varp = &argvars[2];
18918
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018919 if (varname != NULL && varp != NULL
18920#ifdef FEAT_WINDOWS
18921 && tp != NULL
18922#endif
18923 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018924 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018925#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018926 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018927 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018928#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018929
18930 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18931 if (tabvarname != NULL)
18932 {
18933 STRCPY(tabvarname, "t:");
18934 STRCPY(tabvarname + 2, varname);
18935 set_var(tabvarname, varp, TRUE);
18936 vim_free(tabvarname);
18937 }
18938
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018939#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018940 /* Restore current tabpage */
18941 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018942 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018943#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018944 }
18945}
18946
18947/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018948 * "settabwinvar()" function
18949 */
18950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018951f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018952{
18953 setwinvar(argvars, rettv, 1);
18954}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018955
18956/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018957 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018958 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018960f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018961{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018962 setwinvar(argvars, rettv, 0);
18963}
18964
18965/*
18966 * "setwinvar()" and "settabwinvar()" functions
18967 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018968
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018969 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018970setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018971{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018972 win_T *win;
18973#ifdef FEAT_WINDOWS
18974 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018975 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018976 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018977#endif
18978 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018979 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018980 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018981 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018982
18983 if (check_restricted() || check_secure())
18984 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018985
18986#ifdef FEAT_WINDOWS
18987 if (off == 1)
18988 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18989 else
18990 tp = curtab;
18991#endif
18992 win = find_win_by_nr(&argvars[off], tp);
18993 varname = get_tv_string_chk(&argvars[off + 1]);
18994 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018995
18996 if (win != NULL && varname != NULL && varp != NULL)
18997 {
18998#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018999 need_switch_win = !(tp == curtab && win == curwin);
19000 if (!need_switch_win
19001 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019003 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020019004 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019005 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020019006 long numval;
19007 char_u *strval;
19008 int error = FALSE;
19009
19010 ++varname;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019011 numval = (long)get_tv_number_chk(varp, &error);
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020019012 strval = get_tv_string_buf_chk(varp, nbuf);
19013 if (!error && strval != NULL)
19014 set_option_value(varname, numval, strval, OPT_LOCAL);
19015 }
19016 else
19017 {
19018 winvarname = alloc((unsigned)STRLEN(varname) + 3);
19019 if (winvarname != NULL)
19020 {
19021 STRCPY(winvarname, "w:");
19022 STRCPY(winvarname + 2, varname);
19023 set_var(winvarname, varp, TRUE);
19024 vim_free(winvarname);
19025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019026 }
19027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019028#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020019029 if (need_switch_win)
19030 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019031#endif
19032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019033}
19034
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010019035#ifdef FEAT_CRYPT
19036/*
19037 * "sha256({string})" function
19038 */
19039 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019040f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010019041{
19042 char_u *p;
19043
19044 p = get_tv_string(&argvars[0]);
19045 rettv->vval.v_string = vim_strsave(
19046 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
19047 rettv->v_type = VAR_STRING;
19048}
19049#endif /* FEAT_CRYPT */
19050
Bram Moolenaar071d4272004-06-13 20:20:40 +000019051/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000019052 * "shellescape({string})" function
19053 */
19054 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019055f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000019056{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019057 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010019058 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000019059 rettv->v_type = VAR_STRING;
19060}
19061
19062/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020019063 * shiftwidth() function
19064 */
19065 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019066f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020019067{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010019068 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020019069}
19070
19071/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019072 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019073 */
19074 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019075f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019076{
Bram Moolenaar0d660222005-01-07 21:51:51 +000019077 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019078
Bram Moolenaar0d660222005-01-07 21:51:51 +000019079 p = get_tv_string(&argvars[0]);
19080 rettv->vval.v_string = vim_strsave(p);
19081 simplify_filename(rettv->vval.v_string); /* simplify in place */
19082 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019083}
19084
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019085#ifdef FEAT_FLOAT
19086/*
19087 * "sin()" function
19088 */
19089 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019090f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019091{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019092 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019093
19094 rettv->v_type = VAR_FLOAT;
19095 if (get_float_arg(argvars, &f) == OK)
19096 rettv->vval.v_float = sin(f);
19097 else
19098 rettv->vval.v_float = 0.0;
19099}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019100
19101/*
19102 * "sinh()" function
19103 */
19104 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019105f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019106{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019107 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019108
19109 rettv->v_type = VAR_FLOAT;
19110 if (get_float_arg(argvars, &f) == OK)
19111 rettv->vval.v_float = sinh(f);
19112 else
19113 rettv->vval.v_float = 0.0;
19114}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019115#endif
19116
Bram Moolenaar0d660222005-01-07 21:51:51 +000019117static int
19118#ifdef __BORLANDC__
19119 _RTLENTRYF
19120#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019121 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019122static int
19123#ifdef __BORLANDC__
19124 _RTLENTRYF
19125#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019126 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019127
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019128/* struct used in the array that's given to qsort() */
19129typedef struct
19130{
19131 listitem_T *item;
19132 int idx;
19133} sortItem_T;
19134
Bram Moolenaar0b962472016-02-22 22:51:33 +010019135/* struct storing information about current sort */
19136typedef struct
19137{
19138 int item_compare_ic;
19139 int item_compare_numeric;
19140 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019141#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019142 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019143#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019144 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019145 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019146 dict_T *item_compare_selfdict;
19147 int item_compare_func_err;
19148 int item_compare_keep_zero;
19149} sortinfo_T;
19150static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019151static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019152#define ITEM_COMPARE_FAIL 999
19153
Bram Moolenaar071d4272004-06-13 20:20:40 +000019154/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019155 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019156 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019157 static int
19158#ifdef __BORLANDC__
19159_RTLENTRYF
19160#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019161item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019162{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019163 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019164 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019165 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019166 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019167 int res;
19168 char_u numbuf1[NUMBUFLEN];
19169 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019170
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019171 si1 = (sortItem_T *)s1;
19172 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019173 tv1 = &si1->item->li_tv;
19174 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019175
Bram Moolenaar0b962472016-02-22 22:51:33 +010019176 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019177 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019178 varnumber_T v1 = get_tv_number(tv1);
19179 varnumber_T v2 = get_tv_number(tv2);
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019180
19181 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19182 }
19183
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019184#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019185 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019186 {
19187 float_T v1 = get_tv_float(tv1);
19188 float_T v2 = get_tv_float(tv2);
19189
19190 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19191 }
19192#endif
19193
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019194 /* tv2string() puts quotes around a string and allocates memory. Don't do
19195 * that for string variables. Use a single quote when comparing with a
19196 * non-string to do what the docs promise. */
19197 if (tv1->v_type == VAR_STRING)
19198 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019199 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019200 p1 = (char_u *)"'";
19201 else
19202 p1 = tv1->vval.v_string;
19203 }
19204 else
19205 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
19206 if (tv2->v_type == VAR_STRING)
19207 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019208 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019209 p2 = (char_u *)"'";
19210 else
19211 p2 = tv2->vval.v_string;
19212 }
19213 else
19214 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019215 if (p1 == NULL)
19216 p1 = (char_u *)"";
19217 if (p2 == NULL)
19218 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010019219 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019220 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019221 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019222 res = STRICMP(p1, p2);
19223 else
19224 res = STRCMP(p1, p2);
19225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019226 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020019227 {
19228 double n1, n2;
19229 n1 = strtod((char *)p1, (char **)&p1);
19230 n2 = strtod((char *)p2, (char **)&p2);
19231 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
19232 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019233
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019234 /* When the result would be zero, compare the item indexes. Makes the
19235 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019236 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019237 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019238
Bram Moolenaar0d660222005-01-07 21:51:51 +000019239 vim_free(tofree1);
19240 vim_free(tofree2);
19241 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019242}
19243
19244 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000019245#ifdef __BORLANDC__
19246_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000019247#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019248item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019249{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019250 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019251 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000019252 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019253 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000019254 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019255 char_u *func_name;
19256 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019257
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019258 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019259 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019260 return 0;
19261
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019262 si1 = (sortItem_T *)s1;
19263 si2 = (sortItem_T *)s2;
19264
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019265 if (partial == NULL)
19266 func_name = sortinfo->item_compare_func;
19267 else
19268 func_name = partial->pt_name;
19269
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019270 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019271 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019272 copy_tv(&si1->item->li_tv, &argv[0]);
19273 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019274
19275 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019276 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020019277 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019278 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019279 clear_tv(&argv[0]);
19280 clear_tv(&argv[1]);
19281
19282 if (res == FAIL)
19283 res = ITEM_COMPARE_FAIL;
19284 else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019285 res = (int)get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
Bram Moolenaar0b962472016-02-22 22:51:33 +010019286 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000019287 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019288 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019289
19290 /* When the result would be zero, compare the pointers themselves. Makes
19291 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019292 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019293 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019294
Bram Moolenaar0d660222005-01-07 21:51:51 +000019295 return res;
19296}
19297
19298/*
19299 * "sort({list})" function
19300 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019301 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019302do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019303{
Bram Moolenaar33570922005-01-25 22:26:29 +000019304 list_T *l;
19305 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019306 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019307 sortinfo_T *old_sortinfo;
19308 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019309 long len;
19310 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019311
Bram Moolenaar0b962472016-02-22 22:51:33 +010019312 /* Pointer to current info struct used in compare function. Save and
19313 * restore the current one for nested calls. */
19314 old_sortinfo = sortinfo;
19315 sortinfo = &info;
19316
Bram Moolenaar0d660222005-01-07 21:51:51 +000019317 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019318 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019319 else
19320 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019321 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019322 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019323 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19324 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019325 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019326 rettv->vval.v_list = l;
19327 rettv->v_type = VAR_LIST;
19328 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019329
Bram Moolenaar0d660222005-01-07 21:51:51 +000019330 len = list_len(l);
19331 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019332 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019333
Bram Moolenaar0b962472016-02-22 22:51:33 +010019334 info.item_compare_ic = FALSE;
19335 info.item_compare_numeric = FALSE;
19336 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019337#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019338 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019339#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019340 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019341 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019342 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019343 if (argvars[1].v_type != VAR_UNKNOWN)
19344 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019345 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019346 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019347 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019348 else if (argvars[1].v_type == VAR_PARTIAL)
19349 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019350 else
19351 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019352 int error = FALSE;
19353
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019354 i = (long)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019355 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019356 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019357 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019358 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010019359 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019360 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010019361 else if (i != 0)
19362 {
19363 EMSG(_(e_invarg));
19364 goto theend;
19365 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019366 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019367 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010019368 if (*info.item_compare_func == NUL)
19369 {
19370 /* empty string means default sort */
19371 info.item_compare_func = NULL;
19372 }
19373 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019374 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019375 info.item_compare_func = NULL;
19376 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019377 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019378 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019379 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019380 info.item_compare_func = NULL;
19381 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019382 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019383#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019384 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019385 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019386 info.item_compare_func = NULL;
19387 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019388 }
19389#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019390 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019391 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019392 info.item_compare_func = NULL;
19393 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019394 }
19395 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019396 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019397
19398 if (argvars[2].v_type != VAR_UNKNOWN)
19399 {
19400 /* optional third argument: {dict} */
19401 if (argvars[2].v_type != VAR_DICT)
19402 {
19403 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019404 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019405 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019406 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019407 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019409
Bram Moolenaar0d660222005-01-07 21:51:51 +000019410 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019411 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019412 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019413 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019414
Bram Moolenaar327aa022014-03-25 18:24:23 +010019415 i = 0;
19416 if (sort)
19417 {
19418 /* sort(): ptrs will be the list to sort */
19419 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019420 {
19421 ptrs[i].item = li;
19422 ptrs[i].idx = i;
19423 ++i;
19424 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019425
Bram Moolenaar0b962472016-02-22 22:51:33 +010019426 info.item_compare_func_err = FALSE;
19427 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019428 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019429 if ((info.item_compare_func != NULL
19430 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019431 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019432 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019433 EMSG(_("E702: Sort compare function failed"));
19434 else
19435 {
19436 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019437 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019438 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019439 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010019440 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019441
Bram Moolenaar0b962472016-02-22 22:51:33 +010019442 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019443 {
19444 /* Clear the List and append the items in sorted order. */
19445 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19446 l->lv_len = 0;
19447 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019448 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019449 }
19450 }
19451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019452 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019453 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019454 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019455
19456 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019457 info.item_compare_func_err = FALSE;
19458 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019459 item_compare_func_ptr = info.item_compare_func != NULL
19460 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019461 ? item_compare2 : item_compare;
19462
19463 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19464 li = li->li_next)
19465 {
19466 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19467 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019468 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019469 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019470 {
19471 EMSG(_("E882: Uniq compare function failed"));
19472 break;
19473 }
19474 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019475
Bram Moolenaar0b962472016-02-22 22:51:33 +010019476 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019477 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019478 while (--i >= 0)
19479 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019480 li = ptrs[i].item->li_next;
19481 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019482 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019483 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019484 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019485 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019486 list_fix_watch(l, li);
19487 listitem_free(li);
19488 l->lv_len--;
19489 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019490 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019491 }
19492
19493 vim_free(ptrs);
19494 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019495theend:
19496 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019497}
19498
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019499/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019500 * "sort({list})" function
19501 */
19502 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019503f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019504{
19505 do_sort_uniq(argvars, rettv, TRUE);
19506}
19507
19508/*
19509 * "uniq({list})" function
19510 */
19511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019512f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019513{
19514 do_sort_uniq(argvars, rettv, FALSE);
19515}
19516
19517/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019518 * "soundfold({word})" function
19519 */
19520 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019521f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019522{
19523 char_u *s;
19524
19525 rettv->v_type = VAR_STRING;
19526 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019527#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019528 rettv->vval.v_string = eval_soundfold(s);
19529#else
19530 rettv->vval.v_string = vim_strsave(s);
19531#endif
19532}
19533
19534/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019535 * "spellbadword()" function
19536 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019537 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019538f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019539{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019540 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019541 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019542 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019543
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019544 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019545 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019546
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019547#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019548 if (argvars[0].v_type == VAR_UNKNOWN)
19549 {
19550 /* Find the start and length of the badly spelled word. */
19551 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19552 if (len != 0)
19553 word = ml_get_cursor();
19554 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019555 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019556 {
19557 char_u *str = get_tv_string_chk(&argvars[0]);
19558 int capcol = -1;
19559
19560 if (str != NULL)
19561 {
19562 /* Check the argument for spelling. */
19563 while (*str != NUL)
19564 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019565 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019566 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019567 {
19568 word = str;
19569 break;
19570 }
19571 str += len;
19572 }
19573 }
19574 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019575#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019576
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019577 list_append_string(rettv->vval.v_list, word, len);
19578 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019579 attr == HLF_SPB ? "bad" :
19580 attr == HLF_SPR ? "rare" :
19581 attr == HLF_SPL ? "local" :
19582 attr == HLF_SPC ? "caps" :
19583 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019584}
19585
19586/*
19587 * "spellsuggest()" function
19588 */
19589 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019590f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019591{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019592#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019593 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019594 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019595 int maxcount;
19596 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019597 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019598 listitem_T *li;
19599 int need_capital = FALSE;
19600#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019601
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019602 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019603 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019604
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019605#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019606 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019607 {
19608 str = get_tv_string(&argvars[0]);
19609 if (argvars[1].v_type != VAR_UNKNOWN)
19610 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019611 maxcount = (int)get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019612 if (maxcount <= 0)
19613 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019614 if (argvars[2].v_type != VAR_UNKNOWN)
19615 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019616 need_capital = (int)get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019617 if (typeerr)
19618 return;
19619 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019620 }
19621 else
19622 maxcount = 25;
19623
Bram Moolenaar4770d092006-01-12 23:22:24 +000019624 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019625
19626 for (i = 0; i < ga.ga_len; ++i)
19627 {
19628 str = ((char_u **)ga.ga_data)[i];
19629
19630 li = listitem_alloc();
19631 if (li == NULL)
19632 vim_free(str);
19633 else
19634 {
19635 li->li_tv.v_type = VAR_STRING;
19636 li->li_tv.v_lock = 0;
19637 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019638 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019639 }
19640 }
19641 ga_clear(&ga);
19642 }
19643#endif
19644}
19645
Bram Moolenaar0d660222005-01-07 21:51:51 +000019646 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019647f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019648{
19649 char_u *str;
19650 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019651 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019652 regmatch_T regmatch;
19653 char_u patbuf[NUMBUFLEN];
19654 char_u *save_cpo;
19655 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019656 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019657 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019658 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019659
19660 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19661 save_cpo = p_cpo;
19662 p_cpo = (char_u *)"";
19663
19664 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019665 if (argvars[1].v_type != VAR_UNKNOWN)
19666 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019667 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19668 if (pat == NULL)
19669 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019670 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019671 keepempty = (int)get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019672 }
19673 if (pat == NULL || *pat == NUL)
19674 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019675
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019676 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019677 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019678 if (typeerr)
19679 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019680
Bram Moolenaar0d660222005-01-07 21:51:51 +000019681 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19682 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019683 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019684 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019685 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019686 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019687 if (*str == NUL)
19688 match = FALSE; /* empty item at the end */
19689 else
19690 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019691 if (match)
19692 end = regmatch.startp[0];
19693 else
19694 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019695 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19696 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019697 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019698 if (list_append_string(rettv->vval.v_list, str,
19699 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019700 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019701 }
19702 if (!match)
19703 break;
19704 /* Advance to just after the match. */
19705 if (regmatch.endp[0] > str)
19706 col = 0;
19707 else
19708 {
19709 /* Don't get stuck at the same match. */
19710#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019711 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019712#else
19713 col = 1;
19714#endif
19715 }
19716 str = regmatch.endp[0];
19717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019718
Bram Moolenaar473de612013-06-08 18:19:48 +020019719 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019721
Bram Moolenaar0d660222005-01-07 21:51:51 +000019722 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019723}
19724
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019725#ifdef FEAT_FLOAT
19726/*
19727 * "sqrt()" function
19728 */
19729 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019730f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019731{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019732 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019733
19734 rettv->v_type = VAR_FLOAT;
19735 if (get_float_arg(argvars, &f) == OK)
19736 rettv->vval.v_float = sqrt(f);
19737 else
19738 rettv->vval.v_float = 0.0;
19739}
19740
19741/*
19742 * "str2float()" function
19743 */
19744 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019745f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019746{
19747 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19748
19749 if (*p == '+')
19750 p = skipwhite(p + 1);
19751 (void)string2float(p, &rettv->vval.v_float);
19752 rettv->v_type = VAR_FLOAT;
19753}
19754#endif
19755
Bram Moolenaar2c932302006-03-18 21:42:09 +000019756/*
19757 * "str2nr()" function
19758 */
19759 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019760f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019761{
19762 int base = 10;
19763 char_u *p;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019764 varnumber_T n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019765 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019766
19767 if (argvars[1].v_type != VAR_UNKNOWN)
19768 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019769 base = (int)get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019770 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019771 {
19772 EMSG(_(e_invarg));
19773 return;
19774 }
19775 }
19776
19777 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019778 if (*p == '+')
19779 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019780 switch (base)
19781 {
19782 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19783 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19784 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19785 default: what = 0;
19786 }
19787 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019788 rettv->vval.v_number = n;
19789}
19790
Bram Moolenaar071d4272004-06-13 20:20:40 +000019791#ifdef HAVE_STRFTIME
19792/*
19793 * "strftime({format}[, {time}])" function
19794 */
19795 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019796f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019797{
19798 char_u result_buf[256];
19799 struct tm *curtime;
19800 time_t seconds;
19801 char_u *p;
19802
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019803 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019804
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019805 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019806 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019807 seconds = time(NULL);
19808 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019809 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019810 curtime = localtime(&seconds);
19811 /* MSVC returns NULL for an invalid value of seconds. */
19812 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019813 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019814 else
19815 {
19816# ifdef FEAT_MBYTE
19817 vimconv_T conv;
19818 char_u *enc;
19819
19820 conv.vc_type = CONV_NONE;
19821 enc = enc_locale();
19822 convert_setup(&conv, p_enc, enc);
19823 if (conv.vc_type != CONV_NONE)
19824 p = string_convert(&conv, p, NULL);
19825# endif
19826 if (p != NULL)
19827 (void)strftime((char *)result_buf, sizeof(result_buf),
19828 (char *)p, curtime);
19829 else
19830 result_buf[0] = NUL;
19831
19832# ifdef FEAT_MBYTE
19833 if (conv.vc_type != CONV_NONE)
19834 vim_free(p);
19835 convert_setup(&conv, enc, p_enc);
19836 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019837 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019838 else
19839# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019840 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019841
19842# ifdef FEAT_MBYTE
19843 /* Release conversion descriptors */
19844 convert_setup(&conv, NULL, NULL);
19845 vim_free(enc);
19846# endif
19847 }
19848}
19849#endif
19850
19851/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019852 * "strgetchar()" function
19853 */
19854 static void
19855f_strgetchar(typval_T *argvars, typval_T *rettv)
19856{
19857 char_u *str;
19858 int len;
19859 int error = FALSE;
19860 int charidx;
19861
19862 rettv->vval.v_number = -1;
19863 str = get_tv_string_chk(&argvars[0]);
19864 if (str == NULL)
19865 return;
19866 len = (int)STRLEN(str);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019867 charidx = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019868 if (error)
19869 return;
19870#ifdef FEAT_MBYTE
19871 {
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019872 int byteidx = 0;
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019873
19874 while (charidx >= 0 && byteidx < len)
19875 {
19876 if (charidx == 0)
19877 {
19878 rettv->vval.v_number = mb_ptr2char(str + byteidx);
19879 break;
19880 }
19881 --charidx;
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019882 byteidx += mb_cptr2len(str + byteidx);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019883 }
19884 }
19885#else
19886 if (charidx < len)
19887 rettv->vval.v_number = str[charidx];
19888#endif
19889}
19890
19891/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019892 * "stridx()" function
19893 */
19894 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019895f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019896{
19897 char_u buf[NUMBUFLEN];
19898 char_u *needle;
19899 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019900 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019901 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019902 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019903
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019904 needle = get_tv_string_chk(&argvars[1]);
19905 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019906 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019907 if (needle == NULL || haystack == NULL)
19908 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019909
Bram Moolenaar33570922005-01-25 22:26:29 +000019910 if (argvars[2].v_type != VAR_UNKNOWN)
19911 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019912 int error = FALSE;
19913
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019914 start_idx = (int)get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019915 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019916 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019917 if (start_idx >= 0)
19918 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019919 }
19920
19921 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19922 if (pos != NULL)
19923 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019924}
19925
19926/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019927 * "string()" function
19928 */
19929 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019930f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019931{
19932 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019933 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019934
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019935 rettv->v_type = VAR_STRING;
Bram Moolenaar24c77a12016-03-24 21:23:06 +010019936 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
19937 get_copyID());
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019938 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019939 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019940 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019941}
19942
19943/*
19944 * "strlen()" function
19945 */
19946 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019947f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019948{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019949 rettv->vval.v_number = (varnumber_T)(STRLEN(
19950 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019951}
19952
19953/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019954 * "strchars()" function
19955 */
19956 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019957f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019958{
19959 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019960 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019961#ifdef FEAT_MBYTE
19962 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019963 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019964#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019965
19966 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019967 skipcc = (int)get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019968 if (skipcc < 0 || skipcc > 1)
19969 EMSG(_(e_invarg));
19970 else
19971 {
19972#ifdef FEAT_MBYTE
19973 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19974 while (*s != NUL)
19975 {
19976 func_mb_ptr2char_adv(&s);
19977 ++len;
19978 }
19979 rettv->vval.v_number = len;
19980#else
19981 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19982#endif
19983 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019984}
19985
19986/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019987 * "strdisplaywidth()" function
19988 */
19989 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019990f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019991{
19992 char_u *s = get_tv_string(&argvars[0]);
19993 int col = 0;
19994
19995 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020019996 col = (int)get_tv_number(&argvars[1]);
Bram Moolenaardc536092010-07-18 15:45:49 +020019997
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019998 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019999}
20000
20001/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020020002 * "strwidth()" function
20003 */
20004 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020005f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020020006{
20007 char_u *s = get_tv_string(&argvars[0]);
20008
20009 rettv->vval.v_number = (varnumber_T)(
20010#ifdef FEAT_MBYTE
20011 mb_string2cells(s, -1)
20012#else
20013 STRLEN(s)
20014#endif
20015 );
20016}
20017
20018/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020019 * "strcharpart()" function
20020 */
20021 static void
20022f_strcharpart(typval_T *argvars, typval_T *rettv)
20023{
20024#ifdef FEAT_MBYTE
20025 char_u *p;
20026 int nchar;
20027 int nbyte = 0;
20028 int charlen;
20029 int len = 0;
20030 int slen;
20031 int error = FALSE;
20032
20033 p = get_tv_string(&argvars[0]);
20034 slen = (int)STRLEN(p);
20035
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020036 nchar = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020037 if (!error)
20038 {
20039 if (nchar > 0)
20040 while (nchar > 0 && nbyte < slen)
20041 {
Bram Moolenaarfca66002016-04-23 15:30:09 +020020042 nbyte += mb_cptr2len(p + nbyte);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020043 --nchar;
20044 }
20045 else
20046 nbyte = nchar;
20047 if (argvars[2].v_type != VAR_UNKNOWN)
20048 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020049 charlen = (int)get_tv_number(&argvars[2]);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020050 while (charlen > 0 && nbyte + len < slen)
20051 {
Bram Moolenaar73dfe912016-04-23 13:54:48 +020020052 int off = nbyte + len;
20053
20054 if (off < 0)
20055 len += 1;
20056 else
Bram Moolenaarfca66002016-04-23 15:30:09 +020020057 len += mb_cptr2len(p + off);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020020058 --charlen;
20059 }
20060 }
20061 else
20062 len = slen - nbyte; /* default: all bytes that are available. */
20063 }
20064
20065 /*
20066 * Only return the overlap between the specified part and the actual
20067 * string.
20068 */
20069 if (nbyte < 0)
20070 {
20071 len += nbyte;
20072 nbyte = 0;
20073 }
20074 else if (nbyte > slen)
20075 nbyte = slen;
20076 if (len < 0)
20077 len = 0;
20078 else if (nbyte + len > slen)
20079 len = slen - nbyte;
20080
20081 rettv->v_type = VAR_STRING;
20082 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
20083#else
20084 f_strpart(argvars, rettv);
20085#endif
20086}
20087
20088/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020089 * "strpart()" function
20090 */
20091 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020092f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020093{
20094 char_u *p;
20095 int n;
20096 int len;
20097 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020098 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020099
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020100 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020101 slen = (int)STRLEN(p);
20102
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020103 n = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020104 if (error)
20105 len = 0;
20106 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020107 len = (int)get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020108 else
20109 len = slen - n; /* default len: all bytes that are available. */
20110
20111 /*
20112 * Only return the overlap between the specified part and the actual
20113 * string.
20114 */
20115 if (n < 0)
20116 {
20117 len += n;
20118 n = 0;
20119 }
20120 else if (n > slen)
20121 n = slen;
20122 if (len < 0)
20123 len = 0;
20124 else if (n + len > slen)
20125 len = slen - n;
20126
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020127 rettv->v_type = VAR_STRING;
20128 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020129}
20130
20131/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000020132 * "strridx()" function
20133 */
20134 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020135f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020136{
20137 char_u buf[NUMBUFLEN];
20138 char_u *needle;
20139 char_u *haystack;
20140 char_u *rest;
20141 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020142 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000020143
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020144 needle = get_tv_string_chk(&argvars[1]);
20145 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020146
20147 rettv->vval.v_number = -1;
20148 if (needle == NULL || haystack == NULL)
20149 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020150
20151 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020152 if (argvars[2].v_type != VAR_UNKNOWN)
20153 {
20154 /* Third argument: upper limit for index */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020155 end_idx = (int)get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020156 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020157 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020158 }
20159 else
20160 end_idx = haystack_len;
20161
Bram Moolenaar0d660222005-01-07 21:51:51 +000020162 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020163 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000020164 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020165 lastmatch = haystack + end_idx;
20166 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020167 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000020168 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000020169 for (rest = haystack; *rest != '\0'; ++rest)
20170 {
20171 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000020172 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020173 break;
20174 lastmatch = rest;
20175 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000020176 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020177
20178 if (lastmatch == NULL)
20179 rettv->vval.v_number = -1;
20180 else
20181 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
20182}
20183
20184/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020185 * "strtrans()" function
20186 */
20187 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020188f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020189{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020190 rettv->v_type = VAR_STRING;
20191 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020192}
20193
20194/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000020195 * "submatch()" function
20196 */
20197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020198f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020199{
Bram Moolenaar41571762014-04-02 19:00:58 +020020200 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020020201 int no;
20202 int retList = 0;
20203
20204 no = (int)get_tv_number_chk(&argvars[0], &error);
20205 if (error)
20206 return;
20207 error = FALSE;
20208 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020209 retList = (int)get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar41571762014-04-02 19:00:58 +020020210 if (error)
20211 return;
20212
20213 if (retList == 0)
20214 {
20215 rettv->v_type = VAR_STRING;
20216 rettv->vval.v_string = reg_submatch(no);
20217 }
20218 else
20219 {
20220 rettv->v_type = VAR_LIST;
20221 rettv->vval.v_list = reg_submatch_list(no);
20222 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020223}
20224
20225/*
20226 * "substitute()" function
20227 */
20228 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020229f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020230{
20231 char_u patbuf[NUMBUFLEN];
20232 char_u subbuf[NUMBUFLEN];
20233 char_u flagsbuf[NUMBUFLEN];
20234
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020235 char_u *str = get_tv_string_chk(&argvars[0]);
20236 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
20237 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
20238 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
20239
Bram Moolenaar0d660222005-01-07 21:51:51 +000020240 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020241 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
20242 rettv->vval.v_string = NULL;
20243 else
20244 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000020245}
20246
20247/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020248 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000020249 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020251f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020252{
20253 int id = 0;
20254#ifdef FEAT_SYN_HL
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020255 linenr_T lnum;
20256 colnr_T col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020257 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020258 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020259
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020260 lnum = get_tv_lnum(argvars); /* -1 on type error */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020261 col = (linenr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20262 trans = (int)get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020263
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020264 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020265 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020266 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020267#endif
20268
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020269 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020270}
20271
20272/*
20273 * "synIDattr(id, what [, mode])" function
20274 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020275 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020276f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020277{
20278 char_u *p = NULL;
20279#ifdef FEAT_SYN_HL
20280 int id;
20281 char_u *what;
20282 char_u *mode;
20283 char_u modebuf[NUMBUFLEN];
20284 int modec;
20285
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020286 id = (int)get_tv_number(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020287 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020288 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020289 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020290 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020291 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020020292 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000020293 modec = 0; /* replace invalid with current */
20294 }
20295 else
20296 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +020020297#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaarda5b3dc2016-04-23 15:19:02 +020020298 if (USE_24BIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020299 modec = 'g';
20300 else
20301#endif
20302 if (t_colors > 1)
20303 modec = 'c';
20304 else
20305 modec = 't';
20306 }
20307
20308
20309 switch (TOLOWER_ASC(what[0]))
20310 {
20311 case 'b':
20312 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
20313 p = highlight_color(id, what, modec);
20314 else /* bold */
20315 p = highlight_has_attr(id, HL_BOLD, modec);
20316 break;
20317
Bram Moolenaar12682fd2010-03-10 13:43:49 +010020318 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020319 p = highlight_color(id, what, modec);
20320 break;
20321
20322 case 'i':
20323 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
20324 p = highlight_has_attr(id, HL_INVERSE, modec);
20325 else /* italic */
20326 p = highlight_has_attr(id, HL_ITALIC, modec);
20327 break;
20328
20329 case 'n': /* name */
20330 p = get_highlight_name(NULL, id - 1);
20331 break;
20332
20333 case 'r': /* reverse */
20334 p = highlight_has_attr(id, HL_INVERSE, modec);
20335 break;
20336
Bram Moolenaar6f507d62008-11-28 10:16:05 +000020337 case 's':
20338 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
20339 p = highlight_color(id, what, modec);
20340 else /* standout */
20341 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020342 break;
20343
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000020344 case 'u':
20345 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
20346 /* underline */
20347 p = highlight_has_attr(id, HL_UNDERLINE, modec);
20348 else
20349 /* undercurl */
20350 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020351 break;
20352 }
20353
20354 if (p != NULL)
20355 p = vim_strsave(p);
20356#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020357 rettv->v_type = VAR_STRING;
20358 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020359}
20360
20361/*
20362 * "synIDtrans(id)" function
20363 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020364 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020365f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020366{
20367 int id;
20368
20369#ifdef FEAT_SYN_HL
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020370 id = (int)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020371
20372 if (id > 0)
20373 id = syn_get_final_id(id);
20374 else
20375#endif
20376 id = 0;
20377
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020378 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020379}
20380
20381/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020382 * "synconcealed(lnum, col)" function
20383 */
20384 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020385f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020386{
20387#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020388 linenr_T lnum;
20389 colnr_T col;
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020390 int syntax_flags = 0;
20391 int cchar;
20392 int matchid = 0;
20393 char_u str[NUMBUFLEN];
20394#endif
20395
20396 rettv->v_type = VAR_LIST;
20397 rettv->vval.v_list = NULL;
20398
20399#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20400 lnum = get_tv_lnum(argvars); /* -1 on type error */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020401 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020402
20403 vim_memset(str, NUL, sizeof(str));
20404
20405 if (rettv_list_alloc(rettv) != FAIL)
20406 {
20407 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20408 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20409 && curwin->w_p_cole > 0)
20410 {
20411 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20412 syntax_flags = get_syntax_info(&matchid);
20413
20414 /* get the conceal character */
20415 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20416 {
20417 cchar = syn_get_sub_char();
20418 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20419 cchar = lcs_conceal;
20420 if (cchar != NUL)
20421 {
20422# ifdef FEAT_MBYTE
20423 if (has_mbyte)
20424 (*mb_char2bytes)(cchar, str);
20425 else
20426# endif
20427 str[0] = cchar;
20428 }
20429 }
20430 }
20431
20432 list_append_number(rettv->vval.v_list,
20433 (syntax_flags & HL_CONCEAL) != 0);
20434 /* -1 to auto-determine strlen */
20435 list_append_string(rettv->vval.v_list, str, -1);
20436 list_append_number(rettv->vval.v_list, matchid);
20437 }
20438#endif
20439}
20440
20441/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020442 * "synstack(lnum, col)" function
20443 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020444 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020445f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020446{
20447#ifdef FEAT_SYN_HL
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020448 linenr_T lnum;
20449 colnr_T col;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020450 int i;
20451 int id;
20452#endif
20453
20454 rettv->v_type = VAR_LIST;
20455 rettv->vval.v_list = NULL;
20456
20457#ifdef FEAT_SYN_HL
20458 lnum = get_tv_lnum(argvars); /* -1 on type error */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020459 col = (colnr_T)get_tv_number(&argvars[1]) - 1; /* -1 on type error */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020460
20461 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020462 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020463 && rettv_list_alloc(rettv) != FAIL)
20464 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020465 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020466 for (i = 0; ; ++i)
20467 {
20468 id = syn_get_stack_item(i);
20469 if (id < 0)
20470 break;
20471 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20472 break;
20473 }
20474 }
20475#endif
20476}
20477
Bram Moolenaar071d4272004-06-13 20:20:40 +000020478 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020479get_cmd_output_as_rettv(
20480 typval_T *argvars,
20481 typval_T *rettv,
20482 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020483{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020484 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020485 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020486 char_u *infile = NULL;
20487 char_u buf[NUMBUFLEN];
20488 int err = FALSE;
20489 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020490 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020491 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020492
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020493 rettv->v_type = VAR_STRING;
20494 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020495 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020496 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020497
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020498 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020499 {
20500 /*
20501 * Write the string to a temp file, to be used for input of the shell
20502 * command.
20503 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020504 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020505 {
20506 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020507 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020508 }
20509
20510 fd = mch_fopen((char *)infile, WRITEBIN);
20511 if (fd == NULL)
20512 {
20513 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020514 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020515 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020516 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020517 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020518 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20519 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020520 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020521 else
20522 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020523 size_t len;
20524
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020525 p = get_tv_string_buf_chk(&argvars[1], buf);
20526 if (p == NULL)
20527 {
20528 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020529 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020530 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020531 len = STRLEN(p);
20532 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020533 err = TRUE;
20534 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020535 if (fclose(fd) != 0)
20536 err = TRUE;
20537 if (err)
20538 {
20539 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020540 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020541 }
20542 }
20543
Bram Moolenaar52a72462014-08-29 15:53:52 +020020544 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20545 * echoes typeahead, that messes up the display. */
20546 if (!msg_silent)
20547 flags += SHELL_COOKED;
20548
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020549 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020550 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020551 int len;
20552 listitem_T *li;
20553 char_u *s = NULL;
20554 char_u *start;
20555 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020556 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020557
Bram Moolenaar52a72462014-08-29 15:53:52 +020020558 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020559 if (res == NULL)
20560 goto errret;
20561
20562 list = list_alloc();
20563 if (list == NULL)
20564 goto errret;
20565
20566 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020567 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020568 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020569 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020570 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020571 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020572
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020573 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020574 if (s == NULL)
20575 goto errret;
20576
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020577 for (p = s; start < end; ++p, ++start)
20578 *p = *start == NUL ? NL : *start;
20579 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020580
20581 li = listitem_alloc();
20582 if (li == NULL)
20583 {
20584 vim_free(s);
20585 goto errret;
20586 }
20587 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020588 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020589 li->li_tv.vval.v_string = s;
20590 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020591 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020592
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020593 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020594 rettv->v_type = VAR_LIST;
20595 rettv->vval.v_list = list;
20596 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020597 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020598 else
20599 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020600 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020601#ifdef USE_CR
20602 /* translate <CR> into <NL> */
20603 if (res != NULL)
20604 {
20605 char_u *s;
20606
20607 for (s = res; *s; ++s)
20608 {
20609 if (*s == CAR)
20610 *s = NL;
20611 }
20612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020613#else
20614# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020615 /* translate <CR><NL> into <NL> */
20616 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020617 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020618 char_u *s, *d;
20619
20620 d = res;
20621 for (s = res; *s; ++s)
20622 {
20623 if (s[0] == CAR && s[1] == NL)
20624 ++s;
20625 *d++ = *s;
20626 }
20627 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020629# endif
20630#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020631 rettv->vval.v_string = res;
20632 res = NULL;
20633 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020634
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020635errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020636 if (infile != NULL)
20637 {
20638 mch_remove(infile);
20639 vim_free(infile);
20640 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020641 if (res != NULL)
20642 vim_free(res);
20643 if (list != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020020644 list_free(list);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020645}
20646
20647/*
20648 * "system()" function
20649 */
20650 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020651f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020652{
20653 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20654}
20655
20656/*
20657 * "systemlist()" function
20658 */
20659 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020660f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020661{
20662 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020663}
20664
20665/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020666 * "tabpagebuflist()" function
20667 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020668 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020669f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020670{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020671#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020672 tabpage_T *tp;
20673 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020674
20675 if (argvars[0].v_type == VAR_UNKNOWN)
20676 wp = firstwin;
20677 else
20678 {
20679 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20680 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020681 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020682 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020683 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020684 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020685 for (; wp != NULL; wp = wp->w_next)
20686 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020687 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020688 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020689 }
20690#endif
20691}
20692
20693
20694/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020695 * "tabpagenr()" function
20696 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020698f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020699{
20700 int nr = 1;
20701#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020702 char_u *arg;
20703
20704 if (argvars[0].v_type != VAR_UNKNOWN)
20705 {
20706 arg = get_tv_string_chk(&argvars[0]);
20707 nr = 0;
20708 if (arg != NULL)
20709 {
20710 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020711 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020712 else
20713 EMSG2(_(e_invexpr2), arg);
20714 }
20715 }
20716 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020717 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020718#endif
20719 rettv->vval.v_number = nr;
20720}
20721
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020722
20723#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020724static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020725
20726/*
20727 * Common code for tabpagewinnr() and winnr().
20728 */
20729 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020730get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020731{
20732 win_T *twin;
20733 int nr = 1;
20734 win_T *wp;
20735 char_u *arg;
20736
20737 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20738 if (argvar->v_type != VAR_UNKNOWN)
20739 {
20740 arg = get_tv_string_chk(argvar);
20741 if (arg == NULL)
20742 nr = 0; /* type error; errmsg already given */
20743 else if (STRCMP(arg, "$") == 0)
20744 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20745 else if (STRCMP(arg, "#") == 0)
20746 {
20747 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20748 if (twin == NULL)
20749 nr = 0;
20750 }
20751 else
20752 {
20753 EMSG2(_(e_invexpr2), arg);
20754 nr = 0;
20755 }
20756 }
20757
20758 if (nr > 0)
20759 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20760 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020761 {
20762 if (wp == NULL)
20763 {
20764 /* didn't find it in this tabpage */
20765 nr = 0;
20766 break;
20767 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020768 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020769 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020770 return nr;
20771}
20772#endif
20773
20774/*
20775 * "tabpagewinnr()" function
20776 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020777 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020778f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020779{
20780 int nr = 1;
20781#ifdef FEAT_WINDOWS
20782 tabpage_T *tp;
20783
20784 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20785 if (tp == NULL)
20786 nr = 0;
20787 else
20788 nr = get_winnr(tp, &argvars[1]);
20789#endif
20790 rettv->vval.v_number = nr;
20791}
20792
20793
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020794/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020795 * "tagfiles()" function
20796 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020797 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020798f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020799{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020800 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020801 tagname_T tn;
20802 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020803
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020804 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020805 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020806 fname = alloc(MAXPATHL);
20807 if (fname == NULL)
20808 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020809
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020810 for (first = TRUE; ; first = FALSE)
20811 if (get_tagfname(&tn, first, fname) == FAIL
20812 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020813 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020814 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020815 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020816}
20817
20818/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020819 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020820 */
20821 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020822f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020823{
20824 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020825
20826 tag_pattern = get_tv_string(&argvars[0]);
20827
20828 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020829 if (*tag_pattern == NUL)
20830 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020831
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020832 if (rettv_list_alloc(rettv) == OK)
20833 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020834}
20835
20836/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020837 * "tempname()" function
20838 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020839 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020840f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020841{
20842 static int x = 'A';
20843
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020844 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020845 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020846
20847 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20848 * names. Skip 'I' and 'O', they are used for shell redirection. */
20849 do
20850 {
20851 if (x == 'Z')
20852 x = '0';
20853 else if (x == '9')
20854 x = 'A';
20855 else
20856 {
20857#ifdef EBCDIC
20858 if (x == 'I')
20859 x = 'J';
20860 else if (x == 'R')
20861 x = 'S';
20862 else
20863#endif
20864 ++x;
20865 }
20866 } while (x == 'I' || x == 'O');
20867}
20868
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020869#ifdef FEAT_FLOAT
20870/*
20871 * "tan()" function
20872 */
20873 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020874f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020875{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020876 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020877
20878 rettv->v_type = VAR_FLOAT;
20879 if (get_float_arg(argvars, &f) == OK)
20880 rettv->vval.v_float = tan(f);
20881 else
20882 rettv->vval.v_float = 0.0;
20883}
20884
20885/*
20886 * "tanh()" function
20887 */
20888 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020889f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020890{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020891 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020892
20893 rettv->v_type = VAR_FLOAT;
20894 if (get_float_arg(argvars, &f) == OK)
20895 rettv->vval.v_float = tanh(f);
20896 else
20897 rettv->vval.v_float = 0.0;
20898}
20899#endif
20900
Bram Moolenaar574860b2016-05-24 17:33:34 +020020901/*
Bram Moolenaar8e8df252016-05-25 21:23:21 +020020902 * "test_alloc_fail(id, countdown, repeat)" function
20903 */
20904 static void
20905f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
20906{
20907 if (argvars[0].v_type != VAR_NUMBER
20908 || argvars[0].vval.v_number <= 0
20909 || argvars[1].v_type != VAR_NUMBER
20910 || argvars[1].vval.v_number < 0
20911 || argvars[2].v_type != VAR_NUMBER)
20912 EMSG(_(e_invarg));
20913 else
20914 {
20915 alloc_fail_id = argvars[0].vval.v_number;
20916 if (alloc_fail_id >= aid_last)
20917 EMSG(_(e_invarg));
20918 alloc_fail_countdown = argvars[1].vval.v_number;
20919 alloc_fail_repeat = argvars[2].vval.v_number;
20920 did_outofmem_msg = FALSE;
20921 }
20922}
20923
20924/*
20925 * "test_disable_char_avail({expr})" function
20926 */
20927 static void
20928f_test_disable_char_avail(typval_T *argvars, typval_T *rettv UNUSED)
20929{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020020930 disable_char_avail_for_testing = (int)get_tv_number(&argvars[0]);
Bram Moolenaar8e8df252016-05-25 21:23:21 +020020931}
20932
20933/*
Bram Moolenaar574860b2016-05-24 17:33:34 +020020934 * "test_garbagecollect_now()" function
20935 */
20936 static void
20937f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20938{
20939 /* This is dangerous, any Lists and Dicts used internally may be freed
20940 * while still in use. */
20941 garbage_collect(TRUE);
20942}
20943
20944#ifdef FEAT_JOB_CHANNEL
20945 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020020946f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020020947{
20948 rettv->v_type = VAR_CHANNEL;
20949 rettv->vval.v_channel = NULL;
20950}
20951#endif
20952
20953 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020020954f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020020955{
20956 rettv->v_type = VAR_DICT;
20957 rettv->vval.v_dict = NULL;
20958}
20959
20960#ifdef FEAT_JOB_CHANNEL
20961 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020020962f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020020963{
20964 rettv->v_type = VAR_JOB;
20965 rettv->vval.v_job = NULL;
20966}
20967#endif
20968
20969 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020020970f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020020971{
20972 rettv->v_type = VAR_LIST;
20973 rettv->vval.v_list = NULL;
20974}
20975
20976 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020020977f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020020978{
20979 rettv->v_type = VAR_PARTIAL;
20980 rettv->vval.v_partial = NULL;
20981}
20982
20983 static void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020020984f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar574860b2016-05-24 17:33:34 +020020985{
20986 rettv->v_type = VAR_STRING;
20987 rettv->vval.v_string = NULL;
20988}
20989
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020020990 static void
20991f_test_settime(typval_T *argvars, typval_T *rettv UNUSED)
20992{
20993 time_for_testing = (time_t)get_tv_number(&argvars[0]);
20994}
20995
Bram Moolenaar975b5272016-03-15 23:10:59 +010020996#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
20997/*
20998 * Get a callback from "arg". It can be a Funcref or a function name.
20999 * When "arg" is zero return an empty string.
21000 * Return NULL for an invalid argument.
21001 */
21002 char_u *
21003get_callback(typval_T *arg, partial_T **pp)
21004{
21005 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
21006 {
21007 *pp = arg->vval.v_partial;
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010021008 ++(*pp)->pt_refcount;
Bram Moolenaar975b5272016-03-15 23:10:59 +010021009 return (*pp)->pt_name;
21010 }
21011 *pp = NULL;
21012 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
21013 return arg->vval.v_string;
21014 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
21015 return (char_u *)"";
21016 EMSG(_("E921: Invalid callback argument"));
21017 return NULL;
21018}
21019#endif
21020
21021#ifdef FEAT_TIMERS
21022/*
21023 * "timer_start(time, callback [, options])" function
21024 */
21025 static void
21026f_timer_start(typval_T *argvars, typval_T *rettv)
21027{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021028 long msec = (long)get_tv_number(&argvars[0]);
Bram Moolenaar975b5272016-03-15 23:10:59 +010021029 timer_T *timer;
21030 int repeat = 0;
21031 char_u *callback;
21032 dict_T *dict;
21033
Bram Moolenaar38499922016-04-22 20:46:52 +020021034 if (check_secure())
21035 return;
Bram Moolenaar975b5272016-03-15 23:10:59 +010021036 if (argvars[2].v_type != VAR_UNKNOWN)
21037 {
21038 if (argvars[2].v_type != VAR_DICT
21039 || (dict = argvars[2].vval.v_dict) == NULL)
21040 {
21041 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
21042 return;
21043 }
21044 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
21045 repeat = get_dict_number(dict, (char_u *)"repeat");
21046 }
21047
21048 timer = create_timer(msec, repeat);
21049 callback = get_callback(&argvars[1], &timer->tr_partial);
21050 if (callback == NULL)
21051 {
21052 stop_timer(timer);
21053 rettv->vval.v_number = -1;
21054 }
21055 else
21056 {
21057 timer->tr_callback = vim_strsave(callback);
21058 rettv->vval.v_number = timer->tr_id;
21059 }
21060}
21061
21062/*
21063 * "timer_stop(timer)" function
21064 */
21065 static void
21066f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
21067{
Bram Moolenaare40d75f2016-05-15 18:00:19 +020021068 timer_T *timer;
Bram Moolenaar975b5272016-03-15 23:10:59 +010021069
Bram Moolenaare40d75f2016-05-15 18:00:19 +020021070 if (argvars[0].v_type != VAR_NUMBER)
21071 {
21072 EMSG(_(e_number_exp));
21073 return;
21074 }
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021075 timer = find_timer((int)get_tv_number(&argvars[0]));
Bram Moolenaar975b5272016-03-15 23:10:59 +010021076 if (timer != NULL)
21077 stop_timer(timer);
21078}
21079#endif
21080
Bram Moolenaard52d9742005-08-21 22:20:28 +000021081/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021082 * "tolower(string)" function
21083 */
21084 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021085f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021086{
21087 char_u *p;
21088
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021089 p = vim_strsave(get_tv_string(&argvars[0]));
21090 rettv->v_type = VAR_STRING;
21091 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021092
21093 if (p != NULL)
21094 while (*p != NUL)
21095 {
21096#ifdef FEAT_MBYTE
21097 int l;
21098
21099 if (enc_utf8)
21100 {
21101 int c, lc;
21102
21103 c = utf_ptr2char(p);
21104 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021105 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021106 /* TODO: reallocate string when byte count changes. */
21107 if (utf_char2len(lc) == l)
21108 utf_char2bytes(lc, p);
21109 p += l;
21110 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021111 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021112 p += l; /* skip multi-byte character */
21113 else
21114#endif
21115 {
21116 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
21117 ++p;
21118 }
21119 }
21120}
21121
21122/*
21123 * "toupper(string)" function
21124 */
21125 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021126f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021127{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021128 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021129 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021130}
21131
21132/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000021133 * "tr(string, fromstr, tostr)" function
21134 */
21135 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021136f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021137{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021138 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021139 char_u *fromstr;
21140 char_u *tostr;
21141 char_u *p;
21142#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000021143 int inlen;
21144 int fromlen;
21145 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021146 int idx;
21147 char_u *cpstr;
21148 int cplen;
21149 int first = TRUE;
21150#endif
21151 char_u buf[NUMBUFLEN];
21152 char_u buf2[NUMBUFLEN];
21153 garray_T ga;
21154
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021155 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021156 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
21157 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021158
21159 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021160 rettv->v_type = VAR_STRING;
21161 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021162 if (fromstr == NULL || tostr == NULL)
21163 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000021164 ga_init2(&ga, (int)sizeof(char), 80);
21165
21166#ifdef FEAT_MBYTE
21167 if (!has_mbyte)
21168#endif
21169 /* not multi-byte: fromstr and tostr must be the same length */
21170 if (STRLEN(fromstr) != STRLEN(tostr))
21171 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000021172#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000021173error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000021174#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000021175 EMSG2(_(e_invarg2), fromstr);
21176 ga_clear(&ga);
21177 return;
21178 }
21179
21180 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021181 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021182 {
21183#ifdef FEAT_MBYTE
21184 if (has_mbyte)
21185 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021186 inlen = (*mb_ptr2len)(in_str);
21187 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021188 cplen = inlen;
21189 idx = 0;
21190 for (p = fromstr; *p != NUL; p += fromlen)
21191 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021192 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021193 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021194 {
21195 for (p = tostr; *p != NUL; p += tolen)
21196 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021197 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021198 if (idx-- == 0)
21199 {
21200 cplen = tolen;
21201 cpstr = p;
21202 break;
21203 }
21204 }
21205 if (*p == NUL) /* tostr is shorter than fromstr */
21206 goto error;
21207 break;
21208 }
21209 ++idx;
21210 }
21211
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021212 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021213 {
21214 /* Check that fromstr and tostr have the same number of
21215 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021216 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000021217 first = FALSE;
21218 for (p = tostr; *p != NUL; p += tolen)
21219 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021220 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021221 --idx;
21222 }
21223 if (idx != 0)
21224 goto error;
21225 }
21226
Bram Moolenaarcde88542015-08-11 19:14:00 +020021227 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000021228 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021229 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021230
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021231 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021232 }
21233 else
21234#endif
21235 {
21236 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021237 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021238 if (p != NULL)
21239 ga_append(&ga, tostr[p - fromstr]);
21240 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021241 ga_append(&ga, *in_str);
21242 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021243 }
21244 }
21245
Bram Moolenaar61b974b2006-12-05 09:32:29 +000021246 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020021247 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000021248 ga_append(&ga, NUL);
21249
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021250 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021251}
21252
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021253#ifdef FEAT_FLOAT
21254/*
21255 * "trunc({float})" function
21256 */
21257 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021258f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021259{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010021260 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021261
21262 rettv->v_type = VAR_FLOAT;
21263 if (get_float_arg(argvars, &f) == OK)
21264 /* trunc() is not in C90, use floor() or ceil() instead. */
21265 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
21266 else
21267 rettv->vval.v_float = 0.0;
21268}
21269#endif
21270
Bram Moolenaar8299df92004-07-10 09:47:34 +000021271/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021272 * "type(expr)" function
21273 */
21274 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021275f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021276{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010021277 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021278
21279 switch (argvars[0].v_type)
21280 {
21281 case VAR_NUMBER: n = 0; break;
21282 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010021283 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021284 case VAR_FUNC: n = 2; break;
21285 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021286 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021287 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010021288 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010021289 if (argvars[0].vval.v_number == VVAL_FALSE
21290 || argvars[0].vval.v_number == VVAL_TRUE)
21291 n = 6;
21292 else
21293 n = 7;
21294 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021295 case VAR_JOB: n = 8; break;
21296 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021297 case VAR_UNKNOWN:
21298 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
21299 n = -1;
21300 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021301 }
21302 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021303}
21304
21305/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021306 * "undofile(name)" function
21307 */
21308 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021309f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021310{
21311 rettv->v_type = VAR_STRING;
21312#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021313 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021314 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021315
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021316 if (*fname == NUL)
21317 {
21318 /* If there is no file name there will be no undo file. */
21319 rettv->vval.v_string = NULL;
21320 }
21321 else
21322 {
21323 char_u *ffname = FullName_save(fname, FALSE);
21324
21325 if (ffname != NULL)
21326 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
21327 vim_free(ffname);
21328 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021329 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021330#else
21331 rettv->vval.v_string = NULL;
21332#endif
21333}
21334
21335/*
Bram Moolenaara800b422010-06-27 01:15:55 +020021336 * "undotree()" function
21337 */
21338 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021339f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020021340{
21341 if (rettv_dict_alloc(rettv) == OK)
21342 {
21343 dict_T *dict = rettv->vval.v_dict;
21344 list_T *list;
21345
Bram Moolenaar730cde92010-06-27 05:18:54 +020021346 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021347 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021348 dict_add_nr_str(dict, "save_last",
21349 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021350 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
21351 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021352 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021353
21354 list = list_alloc();
21355 if (list != NULL)
21356 {
21357 u_eval_tree(curbuf->b_u_oldhead, list);
21358 dict_add_list(dict, "entries", list);
21359 }
21360 }
21361}
21362
21363/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000021364 * "values(dict)" function
21365 */
21366 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021367f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000021368{
21369 dict_list(argvars, rettv, 1);
21370}
21371
21372/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021373 * "virtcol(string)" function
21374 */
21375 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021376f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021377{
21378 colnr_T vcol = 0;
21379 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021380 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021381
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021382 fp = var2fpos(&argvars[0], FALSE, &fnum);
21383 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
21384 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021385 {
21386 getvvcol(curwin, fp, NULL, NULL, &vcol);
21387 ++vcol;
21388 }
21389
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021390 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021391}
21392
21393/*
21394 * "visualmode()" function
21395 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021396 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010021397f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021398{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021399 char_u str[2];
21400
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021401 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021402 str[0] = curbuf->b_visual_mode_eval;
21403 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021404 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021405
21406 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000021407 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021408 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021409}
21410
21411/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021412 * "wildmenumode()" function
21413 */
21414 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021415f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021416{
21417#ifdef FEAT_WILDMENU
21418 if (wild_menu_showing)
21419 rettv->vval.v_number = 1;
21420#endif
21421}
21422
21423/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021424 * "winbufnr(nr)" function
21425 */
21426 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021427f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021428{
21429 win_T *wp;
21430
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021431 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021432 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021433 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021434 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021435 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021436}
21437
21438/*
21439 * "wincol()" function
21440 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021442f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021443{
21444 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021445 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021446}
21447
21448/*
21449 * "winheight(nr)" function
21450 */
21451 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021452f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021453{
21454 win_T *wp;
21455
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021456 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021457 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021458 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021459 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021460 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021461}
21462
21463/*
21464 * "winline()" function
21465 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021466 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021467f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021468{
21469 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021470 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021471}
21472
21473/*
21474 * "winnr()" function
21475 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021476 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021477f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021478{
21479 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021480
Bram Moolenaar071d4272004-06-13 20:20:40 +000021481#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021482 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021483#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021484 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021485}
21486
21487/*
21488 * "winrestcmd()" function
21489 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021490 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021491f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021492{
21493#ifdef FEAT_WINDOWS
21494 win_T *wp;
21495 int winnr = 1;
21496 garray_T ga;
21497 char_u buf[50];
21498
21499 ga_init2(&ga, (int)sizeof(char), 70);
21500 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21501 {
21502 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21503 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021504 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21505 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021506 ++winnr;
21507 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021508 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021509
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021510 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021511#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021512 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021513#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021514 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021515}
21516
21517/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021518 * "winrestview()" function
21519 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021520 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021521f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021522{
21523 dict_T *dict;
21524
21525 if (argvars[0].v_type != VAR_DICT
21526 || (dict = argvars[0].vval.v_dict) == NULL)
21527 EMSG(_(e_invarg));
21528 else
21529 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021530 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021531 curwin->w_cursor.lnum = (linenr_T)get_dict_number(dict, (char_u *)"lnum");
Bram Moolenaar82c25852014-05-28 16:47:16 +020021532 if (dict_find(dict, (char_u *)"col", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021533 curwin->w_cursor.col = (colnr_T)get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021534#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021535 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021536 curwin->w_cursor.coladd = (colnr_T)get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021537#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021538 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21539 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021540 curwin->w_curswant = (colnr_T)get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar82c25852014-05-28 16:47:16 +020021541 curwin->w_set_curswant = FALSE;
21542 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021543
Bram Moolenaar82c25852014-05-28 16:47:16 +020021544 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021545 set_topline(curwin, (linenr_T)get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021546#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021547 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021548 curwin->w_topfill = (int)get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021549#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021550 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021551 curwin->w_leftcol = (colnr_T)get_dict_number(dict, (char_u *)"leftcol");
Bram Moolenaar82c25852014-05-28 16:47:16 +020021552 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020021553 curwin->w_skipcol = (colnr_T)get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021554
21555 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021556 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021557# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020021558 win_new_width(curwin, W_WIDTH(curwin));
21559# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021560 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021561
Bram Moolenaarb851a962014-10-31 15:45:52 +010021562 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021563 curwin->w_topline = 1;
21564 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21565 curwin->w_topline = curbuf->b_ml.ml_line_count;
21566#ifdef FEAT_DIFF
21567 check_topfill(curwin, TRUE);
21568#endif
21569 }
21570}
21571
21572/*
21573 * "winsaveview()" function
21574 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021575 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021576f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021577{
21578 dict_T *dict;
21579
Bram Moolenaara800b422010-06-27 01:15:55 +020021580 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021581 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021582 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021583
21584 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21585 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21586#ifdef FEAT_VIRTUALEDIT
21587 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21588#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021589 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021590 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21591
21592 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21593#ifdef FEAT_DIFF
21594 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21595#endif
21596 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21597 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21598}
21599
21600/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021601 * "winwidth(nr)" function
21602 */
21603 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021604f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021605{
21606 win_T *wp;
21607
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021608 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021609 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021610 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021611 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021612#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021613 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021614#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021615 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021616#endif
21617}
21618
Bram Moolenaar071d4272004-06-13 20:20:40 +000021619/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021620 * "wordcount()" function
21621 */
21622 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021623f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021624{
21625 if (rettv_dict_alloc(rettv) == FAIL)
21626 return;
21627 cursor_pos_info(rettv->vval.v_dict);
21628}
21629
21630/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021631 * Write list of strings to file
21632 */
21633 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021634write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021635{
21636 listitem_T *li;
21637 int c;
21638 int ret = OK;
21639 char_u *s;
21640
21641 for (li = list->lv_first; li != NULL; li = li->li_next)
21642 {
21643 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21644 {
21645 if (*s == '\n')
21646 c = putc(NUL, fd);
21647 else
21648 c = putc(*s, fd);
21649 if (c == EOF)
21650 {
21651 ret = FAIL;
21652 break;
21653 }
21654 }
21655 if (!binary || li->li_next != NULL)
21656 if (putc('\n', fd) == EOF)
21657 {
21658 ret = FAIL;
21659 break;
21660 }
21661 if (ret == FAIL)
21662 {
21663 EMSG(_(e_write));
21664 break;
21665 }
21666 }
21667 return ret;
21668}
21669
21670/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021671 * "writefile()" function
21672 */
21673 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021674f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021675{
21676 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021677 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021678 char_u *fname;
21679 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021680 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021681
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021682 if (check_restricted() || check_secure())
21683 return;
21684
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021685 if (argvars[0].v_type != VAR_LIST)
21686 {
21687 EMSG2(_(e_listarg), "writefile()");
21688 return;
21689 }
21690 if (argvars[0].vval.v_list == NULL)
21691 return;
21692
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021693 if (argvars[2].v_type != VAR_UNKNOWN)
21694 {
21695 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21696 binary = TRUE;
21697 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21698 append = TRUE;
21699 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021700
21701 /* Always open the file in binary mode, library functions have a mind of
21702 * their own about CR-LF conversion. */
21703 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021704 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21705 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021706 {
21707 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21708 ret = -1;
21709 }
21710 else
21711 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021712 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21713 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021714 fclose(fd);
21715 }
21716
21717 rettv->vval.v_number = ret;
21718}
21719
21720/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021721 * "xor(expr, expr)" function
21722 */
21723 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021724f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021725{
21726 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21727 ^ get_tv_number_chk(&argvars[1], NULL);
21728}
21729
21730
21731/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021732 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021733 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021734 */
21735 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021736var2fpos(
21737 typval_T *varp,
21738 int dollar_lnum, /* TRUE when $ is last line */
21739 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021740{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021741 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021742 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021743 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021744
Bram Moolenaara5525202006-03-02 22:52:09 +000021745 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021746 if (varp->v_type == VAR_LIST)
21747 {
21748 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021749 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021750 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021751 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021752
21753 l = varp->vval.v_list;
21754 if (l == NULL)
21755 return NULL;
21756
21757 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021758 pos.lnum = list_find_nr(l, 0L, &error);
21759 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021760 return NULL; /* invalid line number */
21761
21762 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021763 pos.col = list_find_nr(l, 1L, &error);
21764 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021765 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021766 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021767
21768 /* We accept "$" for the column number: last column. */
21769 li = list_find(l, 1L);
21770 if (li != NULL && li->li_tv.v_type == VAR_STRING
21771 && li->li_tv.vval.v_string != NULL
21772 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21773 pos.col = len + 1;
21774
Bram Moolenaara5525202006-03-02 22:52:09 +000021775 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021776 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021777 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021778 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021779
Bram Moolenaara5525202006-03-02 22:52:09 +000021780#ifdef FEAT_VIRTUALEDIT
21781 /* Get the virtual offset. Defaults to zero. */
21782 pos.coladd = list_find_nr(l, 2L, &error);
21783 if (error)
21784 pos.coladd = 0;
21785#endif
21786
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021787 return &pos;
21788 }
21789
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021790 name = get_tv_string_chk(varp);
21791 if (name == NULL)
21792 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021793 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021794 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021795 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21796 {
21797 if (VIsual_active)
21798 return &VIsual;
21799 return &curwin->w_cursor;
21800 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021801 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021802 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021803 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021804 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21805 return NULL;
21806 return pp;
21807 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021808
21809#ifdef FEAT_VIRTUALEDIT
21810 pos.coladd = 0;
21811#endif
21812
Bram Moolenaar477933c2007-07-17 14:32:23 +000021813 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021814 {
21815 pos.col = 0;
21816 if (name[1] == '0') /* "w0": first visible line */
21817 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021818 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021819 pos.lnum = curwin->w_topline;
21820 return &pos;
21821 }
21822 else if (name[1] == '$') /* "w$": last visible line */
21823 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021824 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021825 pos.lnum = curwin->w_botline - 1;
21826 return &pos;
21827 }
21828 }
21829 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021830 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021831 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021832 {
21833 pos.lnum = curbuf->b_ml.ml_line_count;
21834 pos.col = 0;
21835 }
21836 else
21837 {
21838 pos.lnum = curwin->w_cursor.lnum;
21839 pos.col = (colnr_T)STRLEN(ml_get_curline());
21840 }
21841 return &pos;
21842 }
21843 return NULL;
21844}
21845
21846/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021847 * Convert list in "arg" into a position and optional file number.
21848 * When "fnump" is NULL there is no file number, only 3 items.
21849 * Note that the column is passed on as-is, the caller may want to decrement
21850 * it to use 1 for the first column.
21851 * Return FAIL when conversion is not possible, doesn't check the position for
21852 * validity.
21853 */
21854 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021855list2fpos(
21856 typval_T *arg,
21857 pos_T *posp,
21858 int *fnump,
21859 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021860{
21861 list_T *l = arg->vval.v_list;
21862 long i = 0;
21863 long n;
21864
Bram Moolenaar493c1782014-05-28 14:34:46 +020021865 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21866 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021867 if (arg->v_type != VAR_LIST
21868 || l == NULL
21869 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021870 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021871 return FAIL;
21872
21873 if (fnump != NULL)
21874 {
21875 n = list_find_nr(l, i++, NULL); /* fnum */
21876 if (n < 0)
21877 return FAIL;
21878 if (n == 0)
21879 n = curbuf->b_fnum; /* current buffer */
21880 *fnump = n;
21881 }
21882
21883 n = list_find_nr(l, i++, NULL); /* lnum */
21884 if (n < 0)
21885 return FAIL;
21886 posp->lnum = n;
21887
21888 n = list_find_nr(l, i++, NULL); /* col */
21889 if (n < 0)
21890 return FAIL;
21891 posp->col = n;
21892
21893#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021894 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021895 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021896 posp->coladd = 0;
21897 else
21898 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021899#endif
21900
Bram Moolenaar493c1782014-05-28 14:34:46 +020021901 if (curswantp != NULL)
21902 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21903
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021904 return OK;
21905}
21906
21907/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021908 * Get the length of an environment variable name.
21909 * Advance "arg" to the first character after the name.
21910 * Return 0 for error.
21911 */
21912 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021913get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021914{
21915 char_u *p;
21916 int len;
21917
21918 for (p = *arg; vim_isIDc(*p); ++p)
21919 ;
21920 if (p == *arg) /* no name found */
21921 return 0;
21922
21923 len = (int)(p - *arg);
21924 *arg = p;
21925 return len;
21926}
21927
21928/*
21929 * Get the length of the name of a function or internal variable.
21930 * "arg" is advanced to the first non-white character after the name.
21931 * Return 0 if something is wrong.
21932 */
21933 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021934get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021935{
21936 char_u *p;
21937 int len;
21938
21939 /* Find the end of the name. */
21940 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021941 {
21942 if (*p == ':')
21943 {
21944 /* "s:" is start of "s:var", but "n:" is not and can be used in
21945 * slice "[n:]". Also "xx:" is not a namespace. */
21946 len = (int)(p - *arg);
21947 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21948 || len > 1)
21949 break;
21950 }
21951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021952 if (p == *arg) /* no name found */
21953 return 0;
21954
21955 len = (int)(p - *arg);
21956 *arg = skipwhite(p);
21957
21958 return len;
21959}
21960
21961/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021962 * Get the length of the name of a variable or function.
21963 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021964 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021965 * Return -1 if curly braces expansion failed.
21966 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021967 * If the name contains 'magic' {}'s, expand them and return the
21968 * expanded name in an allocated string via 'alias' - caller must free.
21969 */
21970 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021971get_name_len(
21972 char_u **arg,
21973 char_u **alias,
21974 int evaluate,
21975 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021976{
21977 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021978 char_u *p;
21979 char_u *expr_start;
21980 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021981
21982 *alias = NULL; /* default to no alias */
21983
21984 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21985 && (*arg)[2] == (int)KE_SNR)
21986 {
21987 /* hard coded <SNR>, already translated */
21988 *arg += 3;
21989 return get_id_len(arg) + 3;
21990 }
21991 len = eval_fname_script(*arg);
21992 if (len > 0)
21993 {
21994 /* literal "<SID>", "s:" or "<SNR>" */
21995 *arg += len;
21996 }
21997
Bram Moolenaar071d4272004-06-13 20:20:40 +000021998 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021999 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022000 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022001 p = find_name_end(*arg, &expr_start, &expr_end,
22002 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022003 if (expr_start != NULL)
22004 {
22005 char_u *temp_string;
22006
22007 if (!evaluate)
22008 {
22009 len += (int)(p - *arg);
22010 *arg = skipwhite(p);
22011 return len;
22012 }
22013
22014 /*
22015 * Include any <SID> etc in the expanded string:
22016 * Thus the -len here.
22017 */
22018 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
22019 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022020 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022021 *alias = temp_string;
22022 *arg = skipwhite(p);
22023 return (int)STRLEN(temp_string);
22024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022025
22026 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022027 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022028 EMSG2(_(e_invexpr2), *arg);
22029
22030 return len;
22031}
22032
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022033/*
22034 * Find the end of a variable or function name, taking care of magic braces.
22035 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
22036 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022037 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022038 * Return a pointer to just after the name. Equal to "arg" if there is no
22039 * valid name.
22040 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022041 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022042find_name_end(
22043 char_u *arg,
22044 char_u **expr_start,
22045 char_u **expr_end,
22046 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022047{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022048 int mb_nest = 0;
22049 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022050 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022051 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022052
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022053 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022054 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022055 *expr_start = NULL;
22056 *expr_end = NULL;
22057 }
22058
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022059 /* Quick check for valid starting character. */
22060 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
22061 return arg;
22062
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022063 for (p = arg; *p != NUL
22064 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022065 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022066 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022067 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000022068 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022069 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000022070 if (*p == '\'')
22071 {
22072 /* skip over 'string' to avoid counting [ and ] inside it. */
22073 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
22074 ;
22075 if (*p == NUL)
22076 break;
22077 }
22078 else if (*p == '"')
22079 {
22080 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
22081 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
22082 if (*p == '\\' && p[1] != NUL)
22083 ++p;
22084 if (*p == NUL)
22085 break;
22086 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022087 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
22088 {
22089 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010022090 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022091 len = (int)(p - arg);
22092 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010022093 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010022094 break;
22095 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000022096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022097 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022098 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022099 if (*p == '[')
22100 ++br_nest;
22101 else if (*p == ']')
22102 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022103 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000022104
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022105 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022106 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022107 if (*p == '{')
22108 {
22109 mb_nest++;
22110 if (expr_start != NULL && *expr_start == NULL)
22111 *expr_start = p;
22112 }
22113 else if (*p == '}')
22114 {
22115 mb_nest--;
22116 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
22117 *expr_end = p;
22118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022120 }
22121
22122 return p;
22123}
22124
22125/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022126 * Expands out the 'magic' {}'s in a variable/function name.
22127 * Note that this can call itself recursively, to deal with
22128 * constructs like foo{bar}{baz}{bam}
22129 * The four pointer arguments point to "foo{expre}ss{ion}bar"
22130 * "in_start" ^
22131 * "expr_start" ^
22132 * "expr_end" ^
22133 * "in_end" ^
22134 *
22135 * Returns a new allocated string, which the caller must free.
22136 * Returns NULL for failure.
22137 */
22138 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022139make_expanded_name(
22140 char_u *in_start,
22141 char_u *expr_start,
22142 char_u *expr_end,
22143 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022144{
22145 char_u c1;
22146 char_u *retval = NULL;
22147 char_u *temp_result;
22148 char_u *nextcmd = NULL;
22149
22150 if (expr_end == NULL || in_end == NULL)
22151 return NULL;
22152 *expr_start = NUL;
22153 *expr_end = NUL;
22154 c1 = *in_end;
22155 *in_end = NUL;
22156
Bram Moolenaar362e1a32006-03-06 23:29:24 +000022157 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022158 if (temp_result != NULL && nextcmd == NULL)
22159 {
22160 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
22161 + (in_end - expr_end) + 1));
22162 if (retval != NULL)
22163 {
22164 STRCPY(retval, in_start);
22165 STRCAT(retval, temp_result);
22166 STRCAT(retval, expr_end + 1);
22167 }
22168 }
22169 vim_free(temp_result);
22170
22171 *in_end = c1; /* put char back for error messages */
22172 *expr_start = '{';
22173 *expr_end = '}';
22174
22175 if (retval != NULL)
22176 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022177 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022178 if (expr_start != NULL)
22179 {
22180 /* Further expansion! */
22181 temp_result = make_expanded_name(retval, expr_start,
22182 expr_end, temp_result);
22183 vim_free(retval);
22184 retval = temp_result;
22185 }
22186 }
22187
22188 return retval;
22189}
22190
22191/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022192 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022193 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022194 */
22195 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022196eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022197{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022198 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
22199}
22200
22201/*
22202 * Return TRUE if character "c" can be used as the first character in a
22203 * variable or function name (excluding '{' and '}').
22204 */
22205 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022206eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022207{
22208 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000022209}
22210
22211/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022212 * Set number v: variable to "val".
22213 */
22214 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022215set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022216{
Bram Moolenaare9a41262005-01-15 22:18:47 +000022217 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022218}
22219
22220/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022221 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022222 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022223 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022224get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022225{
Bram Moolenaare9a41262005-01-15 22:18:47 +000022226 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022227}
22228
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022229/*
22230 * Get string v: variable value. Uses a static buffer, can only be used once.
22231 */
22232 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022233get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022234{
22235 return get_tv_string(&vimvars[idx].vv_tv);
22236}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022237
Bram Moolenaar071d4272004-06-13 20:20:40 +000022238/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022239 * Get List v: variable value. Caller must take care of reference count when
22240 * needed.
22241 */
22242 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022243get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000022244{
22245 return vimvars[idx].vv_list;
22246}
22247
22248/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022249 * Set v:char to character "c".
22250 */
22251 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022252set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022253{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020022254 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022255
22256#ifdef FEAT_MBYTE
22257 if (has_mbyte)
22258 buf[(*mb_char2bytes)(c, buf)] = NUL;
22259 else
22260#endif
22261 {
22262 buf[0] = c;
22263 buf[1] = NUL;
22264 }
22265 set_vim_var_string(VV_CHAR, buf, -1);
22266}
22267
22268/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022269 * Set v:count to "count" and v:count1 to "count1".
22270 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022271 */
22272 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022273set_vcount(
22274 long count,
22275 long count1,
22276 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022277{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022278 if (set_prevcount)
22279 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022280 vimvars[VV_COUNT].vv_nr = count;
22281 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022282}
22283
22284/*
22285 * Set string v: variable to a copy of "val".
22286 */
22287 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022288set_vim_var_string(
22289 int idx,
22290 char_u *val,
22291 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022292{
Bram Moolenaara542c682016-01-31 16:28:04 +010022293 clear_tv(&vimvars[idx].vv_di.di_tv);
22294 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022295 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022296 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022297 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022298 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022299 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022300 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022301}
22302
22303/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022304 * Set List v: variable to "val".
22305 */
22306 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022307set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000022308{
Bram Moolenaara542c682016-01-31 16:28:04 +010022309 clear_tv(&vimvars[idx].vv_di.di_tv);
22310 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000022311 vimvars[idx].vv_list = val;
22312 if (val != NULL)
22313 ++val->lv_refcount;
22314}
22315
22316/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020022317 * Set Dictionary v: variable to "val".
22318 */
22319 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022320set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020022321{
22322 int todo;
22323 hashitem_T *hi;
22324
Bram Moolenaara542c682016-01-31 16:28:04 +010022325 clear_tv(&vimvars[idx].vv_di.di_tv);
22326 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020022327 vimvars[idx].vv_dict = val;
22328 if (val != NULL)
22329 {
22330 ++val->dv_refcount;
22331
22332 /* Set readonly */
22333 todo = (int)val->dv_hashtab.ht_used;
22334 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
22335 {
22336 if (HASHITEM_EMPTY(hi))
22337 continue;
22338 --todo;
22339 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22340 }
22341 }
22342}
22343
22344/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022345 * Set v:register if needed.
22346 */
22347 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022348set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022349{
22350 char_u regname;
22351
22352 if (c == 0 || c == ' ')
22353 regname = '"';
22354 else
22355 regname = c;
22356 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000022357 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022358 set_vim_var_string(VV_REG, &regname, 1);
22359}
22360
22361/*
22362 * Get or set v:exception. If "oldval" == NULL, return the current value.
22363 * Otherwise, restore the value to "oldval" and return NULL.
22364 * Must always be called in pairs to save and restore v:exception! Does not
22365 * take care of memory allocations.
22366 */
22367 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022368v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022369{
22370 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022371 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022372
Bram Moolenaare9a41262005-01-15 22:18:47 +000022373 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022374 return NULL;
22375}
22376
22377/*
22378 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
22379 * Otherwise, restore the value to "oldval" and return NULL.
22380 * Must always be called in pairs to save and restore v:throwpoint! Does not
22381 * take care of memory allocations.
22382 */
22383 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022384v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022385{
22386 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022387 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022388
Bram Moolenaare9a41262005-01-15 22:18:47 +000022389 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022390 return NULL;
22391}
22392
22393#if defined(FEAT_AUTOCMD) || defined(PROTO)
22394/*
22395 * Set v:cmdarg.
22396 * If "eap" != NULL, use "eap" to generate the value and return the old value.
22397 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
22398 * Must always be called in pairs!
22399 */
22400 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022401set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022402{
22403 char_u *oldval;
22404 char_u *newval;
22405 unsigned len;
22406
Bram Moolenaare9a41262005-01-15 22:18:47 +000022407 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022408 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022409 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022410 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000022411 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022412 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022413 }
22414
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022415 if (eap->force_bin == FORCE_BIN)
22416 len = 6;
22417 else if (eap->force_bin == FORCE_NOBIN)
22418 len = 8;
22419 else
22420 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022421
22422 if (eap->read_edit)
22423 len += 7;
22424
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022425 if (eap->force_ff != 0)
22426 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
22427# ifdef FEAT_MBYTE
22428 if (eap->force_enc != 0)
22429 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022430 if (eap->bad_char != 0)
22431 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022432# endif
22433
22434 newval = alloc(len + 1);
22435 if (newval == NULL)
22436 return NULL;
22437
22438 if (eap->force_bin == FORCE_BIN)
22439 sprintf((char *)newval, " ++bin");
22440 else if (eap->force_bin == FORCE_NOBIN)
22441 sprintf((char *)newval, " ++nobin");
22442 else
22443 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022444
22445 if (eap->read_edit)
22446 STRCAT(newval, " ++edit");
22447
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022448 if (eap->force_ff != 0)
22449 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
22450 eap->cmd + eap->force_ff);
22451# ifdef FEAT_MBYTE
22452 if (eap->force_enc != 0)
22453 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
22454 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022455 if (eap->bad_char == BAD_KEEP)
22456 STRCPY(newval + STRLEN(newval), " ++bad=keep");
22457 else if (eap->bad_char == BAD_DROP)
22458 STRCPY(newval + STRLEN(newval), " ++bad=drop");
22459 else if (eap->bad_char != 0)
22460 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022461# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022462 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022463 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022464}
22465#endif
22466
22467/*
22468 * Get the value of internal variable "name".
22469 * Return OK or FAIL.
22470 */
22471 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022472get_var_tv(
22473 char_u *name,
22474 int len, /* length of "name" */
22475 typval_T *rettv, /* NULL when only checking existence */
22476 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
22477 int verbose, /* may give error message */
22478 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022479{
22480 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000022481 typval_T *tv = NULL;
22482 typval_T atv;
22483 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022484 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022485
22486 /* truncate the name, so that we can use strcmp() */
22487 cc = name[len];
22488 name[len] = NUL;
22489
22490 /*
22491 * Check for "b:changedtick".
22492 */
22493 if (STRCMP(name, "b:changedtick") == 0)
22494 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022495 atv.v_type = VAR_NUMBER;
22496 atv.vval.v_number = curbuf->b_changedtick;
22497 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022498 }
22499
22500 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022501 * Check for user-defined variables.
22502 */
22503 else
22504 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022505 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022506 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022507 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022508 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022509 if (dip != NULL)
22510 *dip = v;
22511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022512 }
22513
Bram Moolenaare9a41262005-01-15 22:18:47 +000022514 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022515 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022516 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022517 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022518 ret = FAIL;
22519 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022520 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022521 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022522
22523 name[len] = cc;
22524
22525 return ret;
22526}
22527
22528/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022529 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22530 * Also handle function call with Funcref variable: func(expr)
22531 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22532 */
22533 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022534handle_subscript(
22535 char_u **arg,
22536 typval_T *rettv,
22537 int evaluate, /* do more than finding the end */
22538 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022539{
22540 int ret = OK;
22541 dict_T *selfdict = NULL;
22542 char_u *s;
22543 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022544 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022545
22546 while (ret == OK
22547 && (**arg == '['
22548 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022549 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
22550 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022551 && !vim_iswhite(*(*arg - 1)))
22552 {
22553 if (**arg == '(')
22554 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010022555 partial_T *pt = NULL;
22556
Bram Moolenaard9fba312005-06-26 22:34:35 +000022557 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022558 if (evaluate)
22559 {
22560 functv = *rettv;
22561 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022562
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022563 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022564 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022565 {
22566 pt = functv.vval.v_partial;
22567 s = pt->pt_name;
22568 }
22569 else
22570 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022571 }
22572 else
22573 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022574 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022575 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022576 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000022577
22578 /* Clear the funcref afterwards, so that deleting it while
22579 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022580 if (evaluate)
22581 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022582
22583 /* Stop the expression evaluation when immediately aborting on
22584 * error, or when an interrupt occurred or an exception was thrown
22585 * but not caught. */
22586 if (aborting())
22587 {
22588 if (ret == OK)
22589 clear_tv(rettv);
22590 ret = FAIL;
22591 }
22592 dict_unref(selfdict);
22593 selfdict = NULL;
22594 }
22595 else /* **arg == '[' || **arg == '.' */
22596 {
22597 dict_unref(selfdict);
22598 if (rettv->v_type == VAR_DICT)
22599 {
22600 selfdict = rettv->vval.v_dict;
22601 if (selfdict != NULL)
22602 ++selfdict->dv_refcount;
22603 }
22604 else
22605 selfdict = NULL;
22606 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22607 {
22608 clear_tv(rettv);
22609 ret = FAIL;
22610 }
22611 }
22612 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022613
Bram Moolenaar1d429612016-05-24 15:44:17 +020022614 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
22615 * Don't do this when "Func" is already a partial that was bound
22616 * explicitly (pt_auto is FALSE). */
22617 if (selfdict != NULL
22618 && (rettv->v_type == VAR_FUNC
22619 || (rettv->v_type == VAR_PARTIAL
22620 && (rettv->vval.v_partial->pt_auto
22621 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022622 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022623 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
22624 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022625 char_u *tofree = NULL;
22626 ufunc_T *fp;
22627 char_u fname_buf[FLEN_FIXED + 1];
22628 int error;
22629
22630 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022631 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022632 fp = find_func(fname);
22633 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022634
Bram Moolenaar65639032016-03-16 21:40:30 +010022635 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022636 {
Bram Moolenaar65639032016-03-16 21:40:30 +010022637 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
22638
22639 if (pt != NULL)
22640 {
22641 pt->pt_refcount = 1;
22642 pt->pt_dict = selfdict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020022643 pt->pt_auto = TRUE;
Bram Moolenaar65639032016-03-16 21:40:30 +010022644 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022645 if (rettv->v_type == VAR_FUNC)
22646 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022647 /* Just a function: Take over the function name and use
22648 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022649 pt->pt_name = rettv->vval.v_string;
22650 }
22651 else
22652 {
22653 partial_T *ret_pt = rettv->vval.v_partial;
22654 int i;
22655
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022656 /* Partial: copy the function name, use selfdict and copy
22657 * args. Can't take over name or args, the partial might
22658 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022659 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022660 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022661 if (ret_pt->pt_argc > 0)
22662 {
22663 pt->pt_argv = (typval_T *)alloc(
22664 sizeof(typval_T) * ret_pt->pt_argc);
22665 if (pt->pt_argv == NULL)
22666 /* out of memory: drop the arguments */
22667 pt->pt_argc = 0;
22668 else
22669 {
22670 pt->pt_argc = ret_pt->pt_argc;
22671 for (i = 0; i < pt->pt_argc; i++)
22672 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
22673 }
22674 }
22675 partial_unref(ret_pt);
22676 }
Bram Moolenaar65639032016-03-16 21:40:30 +010022677 rettv->v_type = VAR_PARTIAL;
22678 rettv->vval.v_partial = pt;
22679 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022680 }
22681 }
22682
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022683 dict_unref(selfdict);
22684 return ret;
22685}
22686
22687/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022688 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022689 * value).
22690 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022691 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022692alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022693{
Bram Moolenaar33570922005-01-25 22:26:29 +000022694 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022695}
22696
22697/*
22698 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022699 * The string "s" must have been allocated, it is consumed.
22700 * Return NULL for out of memory, the variable otherwise.
22701 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022702 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022703alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022704{
Bram Moolenaar33570922005-01-25 22:26:29 +000022705 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022706
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022707 rettv = alloc_tv();
22708 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022709 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022710 rettv->v_type = VAR_STRING;
22711 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022712 }
22713 else
22714 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022715 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022716}
22717
22718/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022719 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022720 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022721 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022722free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022723{
22724 if (varp != NULL)
22725 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022726 switch (varp->v_type)
22727 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022728 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022729 func_unref(varp->vval.v_string);
22730 /*FALLTHROUGH*/
22731 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022732 vim_free(varp->vval.v_string);
22733 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022734 case VAR_PARTIAL:
22735 partial_unref(varp->vval.v_partial);
22736 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022737 case VAR_LIST:
22738 list_unref(varp->vval.v_list);
22739 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022740 case VAR_DICT:
22741 dict_unref(varp->vval.v_dict);
22742 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022743 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022744#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022745 job_unref(varp->vval.v_job);
22746 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022747#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022748 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022749#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022750 channel_unref(varp->vval.v_channel);
22751 break;
22752#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022753 case VAR_NUMBER:
22754 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022755 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022756 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022757 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022759 vim_free(varp);
22760 }
22761}
22762
22763/*
22764 * Free the memory for a variable value and set the value to NULL or 0.
22765 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022766 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022767clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022768{
22769 if (varp != NULL)
22770 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022771 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022772 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022773 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022774 func_unref(varp->vval.v_string);
22775 /*FALLTHROUGH*/
22776 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022777 vim_free(varp->vval.v_string);
22778 varp->vval.v_string = NULL;
22779 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022780 case VAR_PARTIAL:
22781 partial_unref(varp->vval.v_partial);
22782 varp->vval.v_partial = NULL;
22783 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022784 case VAR_LIST:
22785 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022786 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022787 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022788 case VAR_DICT:
22789 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022790 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022791 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022792 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022793 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022794 varp->vval.v_number = 0;
22795 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022796 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022797#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022798 varp->vval.v_float = 0.0;
22799 break;
22800#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022801 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022802#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022803 job_unref(varp->vval.v_job);
22804 varp->vval.v_job = NULL;
22805#endif
22806 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022807 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022808#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022809 channel_unref(varp->vval.v_channel);
22810 varp->vval.v_channel = NULL;
22811#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022812 case VAR_UNKNOWN:
22813 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022814 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022815 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022816 }
22817}
22818
22819/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022820 * Set the value of a variable to NULL without freeing items.
22821 */
22822 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022823init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022824{
22825 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022826 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022827}
22828
22829/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022830 * Get the number value of a variable.
22831 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022832 * For incompatible types, return 0.
22833 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22834 * caller of incompatible types: it sets *denote to TRUE if "denote"
22835 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022836 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022837 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022838get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022839{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022840 int error = FALSE;
22841
22842 return get_tv_number_chk(varp, &error); /* return 0L on error */
22843}
22844
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022845 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022846get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022847{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022848 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022849
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022850 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022851 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022852 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022853 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022854 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022855#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022856 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022857 break;
22858#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022859 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022860 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022861 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022862 break;
22863 case VAR_STRING:
22864 if (varp->vval.v_string != NULL)
22865 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022866 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022867 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022868 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022869 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022870 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022871 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022872 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022873 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022874 case VAR_SPECIAL:
22875 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22876 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022877 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022878#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022879 EMSG(_("E910: Using a Job as a Number"));
22880 break;
22881#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022882 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022883#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022884 EMSG(_("E913: Using a Channel as a Number"));
22885 break;
22886#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022887 case VAR_UNKNOWN:
22888 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022889 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022890 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022891 if (denote == NULL) /* useful for values that must be unsigned */
22892 n = -1;
22893 else
22894 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022895 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022896}
22897
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022898#ifdef FEAT_FLOAT
22899 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022900get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022901{
22902 switch (varp->v_type)
22903 {
22904 case VAR_NUMBER:
22905 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022906 case VAR_FLOAT:
22907 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022908 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022909 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022910 EMSG(_("E891: Using a Funcref as a Float"));
22911 break;
22912 case VAR_STRING:
22913 EMSG(_("E892: Using a String as a Float"));
22914 break;
22915 case VAR_LIST:
22916 EMSG(_("E893: Using a List as a Float"));
22917 break;
22918 case VAR_DICT:
22919 EMSG(_("E894: Using a Dictionary as a Float"));
22920 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022921 case VAR_SPECIAL:
22922 EMSG(_("E907: Using a special value as a Float"));
22923 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022924 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022925# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022926 EMSG(_("E911: Using a Job as a Float"));
22927 break;
22928# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022929 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022930# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022931 EMSG(_("E914: Using a Channel as a Float"));
22932 break;
22933# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022934 case VAR_UNKNOWN:
22935 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022936 break;
22937 }
22938 return 0;
22939}
22940#endif
22941
Bram Moolenaar071d4272004-06-13 20:20:40 +000022942/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022943 * Get the lnum from the first argument.
22944 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022945 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022946 */
22947 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022948get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022949{
Bram Moolenaar33570922005-01-25 22:26:29 +000022950 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022951 linenr_T lnum;
22952
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022953 lnum = (linenr_T)get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022954 if (lnum == 0) /* no valid number, try using line() */
22955 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022956 rettv.v_type = VAR_NUMBER;
22957 f_line(argvars, &rettv);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022958 lnum = (linenr_T)rettv.vval.v_number;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022959 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022960 }
22961 return lnum;
22962}
22963
22964/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022965 * Get the lnum from the first argument.
22966 * Also accepts "$", then "buf" is used.
22967 * Returns 0 on error.
22968 */
22969 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022970get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022971{
22972 if (argvars[0].v_type == VAR_STRING
22973 && argvars[0].vval.v_string != NULL
22974 && argvars[0].vval.v_string[0] == '$'
22975 && buf != NULL)
22976 return buf->b_ml.ml_line_count;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020022977 return (linenr_T)get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar661b1822005-07-28 22:36:45 +000022978}
22979
22980/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022981 * Get the string value of a variable.
22982 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022983 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22984 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022985 * If the String variable has never been set, return an empty string.
22986 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022987 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22988 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022989 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022990 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022991get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022992{
22993 static char_u mybuf[NUMBUFLEN];
22994
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022995 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022996}
22997
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022998 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022999get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023000{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023001 char_u *res = get_tv_string_buf_chk(varp, buf);
23002
23003 return res != NULL ? res : (char_u *)"";
23004}
23005
Bram Moolenaar7d647822014-04-05 21:28:56 +020023006/*
23007 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
23008 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000023009 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023010get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023011{
23012 static char_u mybuf[NUMBUFLEN];
23013
23014 return get_tv_string_buf_chk(varp, mybuf);
23015}
23016
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023017 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023018get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023019{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023020 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023021 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023022 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +020023023 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
23024 (varnumber_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023025 return buf;
23026 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023027 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023028 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023029 break;
23030 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023031 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000023032 break;
23033 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023034 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023035 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023036 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023037#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020023038 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023039 break;
23040#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023041 case VAR_STRING:
23042 if (varp->vval.v_string != NULL)
23043 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023044 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010023045 case VAR_SPECIAL:
23046 STRCPY(buf, get_var_special_name(varp->vval.v_number));
23047 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023048 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023049#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023050 {
23051 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010023052 char *status;
23053
23054 if (job == NULL)
23055 return (char_u *)"no process";
23056 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010023057 : job->jv_status == JOB_ENDED ? "dead"
23058 : "run";
23059# ifdef UNIX
23060 vim_snprintf((char *)buf, NUMBUFLEN,
23061 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010023062# elif defined(WIN32)
23063 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010023064 "process %ld %s",
23065 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010023066 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010023067# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010023068 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010023069 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
23070# endif
23071 return buf;
23072 }
23073#endif
23074 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010023075 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023076#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023077 {
23078 channel_T *channel = varp->vval.v_channel;
23079 char *status = channel_status(channel);
23080
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023081 if (channel == NULL)
23082 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
23083 else
23084 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010023085 "channel %d %s", channel->ch_id, status);
23086 return buf;
23087 }
23088#endif
23089 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023090 case VAR_UNKNOWN:
23091 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023092 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023093 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000023094 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023095}
23096
23097/*
23098 * Find variable "name" in the list of variables.
23099 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023100 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000023101 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000023102 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023103 */
Bram Moolenaar33570922005-01-25 22:26:29 +000023104 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023105find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023106{
Bram Moolenaar071d4272004-06-13 20:20:40 +000023107 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023108 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023109
Bram Moolenaara7043832005-01-21 11:56:39 +000023110 ht = find_var_ht(name, &varname);
23111 if (htp != NULL)
23112 *htp = ht;
23113 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023114 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023115 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023116}
23117
23118/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020023119 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000023120 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023121 */
Bram Moolenaar33570922005-01-25 22:26:29 +000023122 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023123find_var_in_ht(
23124 hashtab_T *ht,
23125 int htname,
23126 char_u *varname,
23127 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000023128{
Bram Moolenaar33570922005-01-25 22:26:29 +000023129 hashitem_T *hi;
23130
23131 if (*varname == NUL)
23132 {
23133 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020023134 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000023135 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023136 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023137 case 'g': return &globvars_var;
23138 case 'v': return &vimvars_var;
23139 case 'b': return &curbuf->b_bufvar;
23140 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023141#ifdef FEAT_WINDOWS
23142 case 't': return &curtab->tp_winvar;
23143#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023144 case 'l': return current_funccal == NULL
23145 ? NULL : &current_funccal->l_vars_var;
23146 case 'a': return current_funccal == NULL
23147 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023148 }
23149 return NULL;
23150 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023151
23152 hi = hash_find(ht, varname);
23153 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023154 {
23155 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023156 * worked find the variable again. Don't auto-load a script if it was
23157 * loaded already, otherwise it would be loaded every time when
23158 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023159 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010023160 {
23161 /* Note: script_autoload() may make "hi" invalid. It must either
23162 * be obtained again or not used. */
23163 if (!script_autoload(varname, FALSE) || aborting())
23164 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023165 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010023166 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023167 if (HASHITEM_EMPTY(hi))
23168 return NULL;
23169 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023170 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023171}
23172
23173/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023174 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020023175 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000023176 * Set "varname" to the start of name without ':'.
23177 */
Bram Moolenaar33570922005-01-25 22:26:29 +000023178 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023179find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023180{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000023181 hashitem_T *hi;
23182
Bram Moolenaar73627d02015-08-11 15:46:09 +020023183 if (name[0] == NUL)
23184 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023185 if (name[1] != ':')
23186 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023187 /* The name must not start with a colon or #. */
23188 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023189 return NULL;
23190 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000023191
23192 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000023193 hi = hash_find(&compat_hashtab, name);
23194 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000023195 return &compat_hashtab;
23196
Bram Moolenaar071d4272004-06-13 20:20:40 +000023197 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023198 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023199 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023200 }
23201 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023202 if (*name == 'g') /* global variable */
23203 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023204 /* There must be no ':' or '#' in the rest of the name, unless g: is used
23205 */
23206 if (vim_strchr(name + 2, ':') != NULL
23207 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023208 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023209 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023210 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023211 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023212 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023213#ifdef FEAT_WINDOWS
23214 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023215 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023216#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000023217 if (*name == 'v') /* v: variable */
23218 return &vimvarht;
23219 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023220 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000023221 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023222 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023223 if (*name == 's' /* script variable */
23224 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
23225 return &SCRIPT_VARS(current_SID);
23226 return NULL;
23227}
23228
23229/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023230 * Get function call environment based on bactrace debug level
23231 */
23232 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023233get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023234{
23235 int i;
23236 funccall_T *funccal;
23237 funccall_T *temp_funccal;
23238
23239 funccal = current_funccal;
23240 if (debug_backtrace_level > 0)
23241 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010023242 for (i = 0; i < debug_backtrace_level; i++)
23243 {
23244 temp_funccal = funccal->caller;
23245 if (temp_funccal)
23246 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023247 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010023248 /* backtrace level overflow. reset to max */
23249 debug_backtrace_level = i;
23250 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023251 }
23252 return funccal;
23253}
23254
23255/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023256 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020023257 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023258 * Returns NULL when it doesn't exist.
23259 */
23260 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023261get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023262{
Bram Moolenaar33570922005-01-25 22:26:29 +000023263 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023264
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023265 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023266 if (v == NULL)
23267 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023268 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023269}
23270
23271/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023272 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000023273 * sourcing this script and when executing functions defined in the script.
23274 */
23275 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023276new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023277{
Bram Moolenaara7043832005-01-21 11:56:39 +000023278 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000023279 hashtab_T *ht;
23280 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000023281
Bram Moolenaar071d4272004-06-13 20:20:40 +000023282 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
23283 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023284 /* Re-allocating ga_data means that an ht_array pointing to
23285 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000023286 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000023287 for (i = 1; i <= ga_scripts.ga_len; ++i)
23288 {
23289 ht = &SCRIPT_VARS(i);
23290 if (ht->ht_mask == HT_INIT_SIZE - 1)
23291 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023292 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000023293 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000023294 }
23295
Bram Moolenaar071d4272004-06-13 20:20:40 +000023296 while (ga_scripts.ga_len < id)
23297 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023298 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023299 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023300 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023301 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023302 }
23303 }
23304}
23305
23306/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023307 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
23308 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023309 */
23310 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023311init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023312{
Bram Moolenaar33570922005-01-25 22:26:29 +000023313 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020023314 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023315 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023316 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023317 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023318 dict_var->di_tv.vval.v_dict = dict;
23319 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023320 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023321 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23322 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023323}
23324
23325/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020023326 * Unreference a dictionary initialized by init_var_dict().
23327 */
23328 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023329unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020023330{
23331 /* Now the dict needs to be freed if no one else is using it, go back to
23332 * normal reference counting. */
23333 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
23334 dict_unref(dict);
23335}
23336
23337/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023338 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000023339 * Frees all allocated variables and the value they contain.
23340 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023341 */
23342 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023343vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000023344{
23345 vars_clear_ext(ht, TRUE);
23346}
23347
23348/*
23349 * Like vars_clear(), but only free the value if "free_val" is TRUE.
23350 */
23351 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023352vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023353{
Bram Moolenaara7043832005-01-21 11:56:39 +000023354 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023355 hashitem_T *hi;
23356 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023357
Bram Moolenaar33570922005-01-25 22:26:29 +000023358 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023359 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000023360 for (hi = ht->ht_array; todo > 0; ++hi)
23361 {
23362 if (!HASHITEM_EMPTY(hi))
23363 {
23364 --todo;
23365
Bram Moolenaar33570922005-01-25 22:26:29 +000023366 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000023367 * ht_array might change then. hash_clear() takes care of it
23368 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023369 v = HI2DI(hi);
23370 if (free_val)
23371 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023372 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000023373 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000023374 }
23375 }
23376 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023377 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023378}
23379
Bram Moolenaara7043832005-01-21 11:56:39 +000023380/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023381 * Delete a variable from hashtab "ht" at item "hi".
23382 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000023383 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023384 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023385delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023386{
Bram Moolenaar33570922005-01-25 22:26:29 +000023387 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023388
23389 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000023390 clear_tv(&di->di_tv);
23391 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023392}
23393
23394/*
23395 * List the value of one internal variable.
23396 */
23397 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023398list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023399{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023400 char_u *tofree;
23401 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023402 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023403
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023404 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000023405 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023406 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023407 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023408}
23409
Bram Moolenaar071d4272004-06-13 20:20:40 +000023410 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023411list_one_var_a(
23412 char_u *prefix,
23413 char_u *name,
23414 int type,
23415 char_u *string,
23416 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023417{
Bram Moolenaar31859182007-08-14 20:41:13 +000023418 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
23419 msg_start();
23420 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023421 if (name != NULL) /* "a:" vars don't have a name stored */
23422 msg_puts(name);
23423 msg_putchar(' ');
23424 msg_advance(22);
23425 if (type == VAR_NUMBER)
23426 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023427 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023428 msg_putchar('*');
23429 else if (type == VAR_LIST)
23430 {
23431 msg_putchar('[');
23432 if (*string == '[')
23433 ++string;
23434 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000023435 else if (type == VAR_DICT)
23436 {
23437 msg_putchar('{');
23438 if (*string == '{')
23439 ++string;
23440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023441 else
23442 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023443
Bram Moolenaar071d4272004-06-13 20:20:40 +000023444 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023445
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023446 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023447 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023448 if (*first)
23449 {
23450 msg_clr_eos();
23451 *first = FALSE;
23452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023453}
23454
23455/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023456 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023457 * If the variable already exists, the value is updated.
23458 * Otherwise the variable is created.
23459 */
23460 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023461set_var(
23462 char_u *name,
23463 typval_T *tv,
23464 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023465{
Bram Moolenaar33570922005-01-25 22:26:29 +000023466 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023467 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023468 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023469
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023470 ht = find_var_ht(name, &varname);
23471 if (ht == NULL || *varname == NUL)
23472 {
23473 EMSG2(_(e_illvar), name);
23474 return;
23475 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020023476 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023477
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023478 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
23479 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023480 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023481
Bram Moolenaar33570922005-01-25 22:26:29 +000023482 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023483 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023484 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023485 if (var_check_ro(v->di_flags, name, FALSE)
23486 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000023487 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023488
23489 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023490 * Handle setting internal v: variables separately where needed to
23491 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000023492 */
23493 if (ht == &vimvarht)
23494 {
23495 if (v->di_tv.v_type == VAR_STRING)
23496 {
23497 vim_free(v->di_tv.vval.v_string);
23498 if (copy || tv->v_type != VAR_STRING)
23499 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
23500 else
23501 {
23502 /* Take over the string to avoid an extra alloc/free. */
23503 v->di_tv.vval.v_string = tv->vval.v_string;
23504 tv->vval.v_string = NULL;
23505 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023506 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023507 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023508 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023509 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023510 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023511 if (STRCMP(varname, "searchforward") == 0)
23512 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010023513#ifdef FEAT_SEARCH_EXTRA
23514 else if (STRCMP(varname, "hlsearch") == 0)
23515 {
23516 no_hlsearch = !v->di_tv.vval.v_number;
23517 redraw_all_later(SOME_VALID);
23518 }
23519#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023520 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023521 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023522 else if (v->di_tv.v_type != tv->v_type)
23523 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000023524 }
23525
23526 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023527 }
23528 else /* add a new variable */
23529 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000023530 /* Can't add "v:" variable. */
23531 if (ht == &vimvarht)
23532 {
23533 EMSG2(_(e_illvar), name);
23534 return;
23535 }
23536
Bram Moolenaar92124a32005-06-17 22:03:40 +000023537 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023538 if (!valid_varname(varname))
23539 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000023540
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023541 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23542 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000023543 if (v == NULL)
23544 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023545 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023546 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023547 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023548 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023549 return;
23550 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023551 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023552 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023553
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023554 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023555 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023556 else
23557 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023558 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023559 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023560 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023562}
23563
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023564/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023565 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023566 * Also give an error message.
23567 */
23568 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023569var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023570{
23571 if (flags & DI_FLAGS_RO)
23572 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023573 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023574 return TRUE;
23575 }
23576 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23577 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023578 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023579 return TRUE;
23580 }
23581 return FALSE;
23582}
23583
23584/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023585 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23586 * Also give an error message.
23587 */
23588 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023589var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023590{
23591 if (flags & DI_FLAGS_FIX)
23592 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023593 EMSG2(_("E795: Cannot delete variable %s"),
23594 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023595 return TRUE;
23596 }
23597 return FALSE;
23598}
23599
23600/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023601 * Check if a funcref is assigned to a valid variable name.
23602 * Return TRUE and give an error if not.
23603 */
23604 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023605var_check_func_name(
23606 char_u *name, /* points to start of variable name */
23607 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023608{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023609 /* Allow for w: b: s: and t:. */
23610 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023611 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23612 ? name[2] : name[0]))
23613 {
23614 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23615 name);
23616 return TRUE;
23617 }
23618 /* Don't allow hiding a function. When "v" is not NULL we might be
23619 * assigning another function to the same var, the type is checked
23620 * below. */
23621 if (new_var && function_exists(name))
23622 {
23623 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23624 name);
23625 return TRUE;
23626 }
23627 return FALSE;
23628}
23629
23630/*
23631 * Check if a variable name is valid.
23632 * Return FALSE and give an error if not.
23633 */
23634 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023635valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023636{
23637 char_u *p;
23638
23639 for (p = varname; *p != NUL; ++p)
23640 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23641 && *p != AUTOLOAD_CHAR)
23642 {
23643 EMSG2(_(e_illvar), varname);
23644 return FALSE;
23645 }
23646 return TRUE;
23647}
23648
23649/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023650 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023651 * Also give an error message, using "name" or _("name") when use_gettext is
23652 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023653 */
23654 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023655tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023656{
23657 if (lock & VAR_LOCKED)
23658 {
23659 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023660 name == NULL ? (char_u *)_("Unknown")
23661 : use_gettext ? (char_u *)_(name)
23662 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023663 return TRUE;
23664 }
23665 if (lock & VAR_FIXED)
23666 {
23667 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023668 name == NULL ? (char_u *)_("Unknown")
23669 : use_gettext ? (char_u *)_(name)
23670 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023671 return TRUE;
23672 }
23673 return FALSE;
23674}
23675
23676/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023677 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023678 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023679 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023680 * It is OK for "from" and "to" to point to the same item. This is used to
23681 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023682 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023683 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023684copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023685{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023686 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023687 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023688 switch (from->v_type)
23689 {
23690 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023691 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023692 to->vval.v_number = from->vval.v_number;
23693 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023694 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023695#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023696 to->vval.v_float = from->vval.v_float;
23697 break;
23698#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023699 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023700#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023701 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023702 if (to->vval.v_job != NULL)
23703 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023704 break;
23705#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023706 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023707#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023708 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023709 if (to->vval.v_channel != NULL)
23710 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023711 break;
23712#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023713 case VAR_STRING:
23714 case VAR_FUNC:
23715 if (from->vval.v_string == NULL)
23716 to->vval.v_string = NULL;
23717 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023718 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023719 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023720 if (from->v_type == VAR_FUNC)
23721 func_ref(to->vval.v_string);
23722 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023723 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023724 case VAR_PARTIAL:
23725 if (from->vval.v_partial == NULL)
23726 to->vval.v_partial = NULL;
23727 else
23728 {
23729 to->vval.v_partial = from->vval.v_partial;
23730 ++to->vval.v_partial->pt_refcount;
23731 }
23732 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023733 case VAR_LIST:
23734 if (from->vval.v_list == NULL)
23735 to->vval.v_list = NULL;
23736 else
23737 {
23738 to->vval.v_list = from->vval.v_list;
23739 ++to->vval.v_list->lv_refcount;
23740 }
23741 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023742 case VAR_DICT:
23743 if (from->vval.v_dict == NULL)
23744 to->vval.v_dict = NULL;
23745 else
23746 {
23747 to->vval.v_dict = from->vval.v_dict;
23748 ++to->vval.v_dict->dv_refcount;
23749 }
23750 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023751 case VAR_UNKNOWN:
23752 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023753 break;
23754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023755}
23756
23757/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023758 * Make a copy of an item.
23759 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023760 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23761 * reference to an already copied list/dict can be used.
23762 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023763 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023764 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023765item_copy(
23766 typval_T *from,
23767 typval_T *to,
23768 int deep,
23769 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023770{
23771 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023772 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023773
Bram Moolenaar33570922005-01-25 22:26:29 +000023774 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023775 {
23776 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023777 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023778 }
23779 ++recurse;
23780
23781 switch (from->v_type)
23782 {
23783 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023784 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023785 case VAR_STRING:
23786 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023787 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010023788 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023789 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023790 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023791 copy_tv(from, to);
23792 break;
23793 case VAR_LIST:
23794 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023795 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023796 if (from->vval.v_list == NULL)
23797 to->vval.v_list = NULL;
23798 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23799 {
23800 /* use the copy made earlier */
23801 to->vval.v_list = from->vval.v_list->lv_copylist;
23802 ++to->vval.v_list->lv_refcount;
23803 }
23804 else
23805 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23806 if (to->vval.v_list == NULL)
23807 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023808 break;
23809 case VAR_DICT:
23810 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023811 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023812 if (from->vval.v_dict == NULL)
23813 to->vval.v_dict = NULL;
23814 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23815 {
23816 /* use the copy made earlier */
23817 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23818 ++to->vval.v_dict->dv_refcount;
23819 }
23820 else
23821 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23822 if (to->vval.v_dict == NULL)
23823 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023824 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023825 case VAR_UNKNOWN:
23826 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023827 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023828 }
23829 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023830 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023831}
23832
23833/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023834 * ":echo expr1 ..." print each argument separated with a space, add a
23835 * newline at the end.
23836 * ":echon expr1 ..." print each argument plain.
23837 */
23838 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023839ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023840{
23841 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023842 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023843 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023844 char_u *p;
23845 int needclr = TRUE;
23846 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023847 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023848
23849 if (eap->skip)
23850 ++emsg_skip;
23851 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23852 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023853 /* If eval1() causes an error message the text from the command may
23854 * still need to be cleared. E.g., "echo 22,44". */
23855 need_clr_eos = needclr;
23856
Bram Moolenaar071d4272004-06-13 20:20:40 +000023857 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023858 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023859 {
23860 /*
23861 * Report the invalid expression unless the expression evaluation
23862 * has been cancelled due to an aborting error, an interrupt, or an
23863 * exception.
23864 */
23865 if (!aborting())
23866 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023867 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023868 break;
23869 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023870 need_clr_eos = FALSE;
23871
Bram Moolenaar071d4272004-06-13 20:20:40 +000023872 if (!eap->skip)
23873 {
23874 if (atstart)
23875 {
23876 atstart = FALSE;
23877 /* Call msg_start() after eval1(), evaluating the expression
23878 * may cause a message to appear. */
23879 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023880 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023881 /* Mark the saved text as finishing the line, so that what
23882 * follows is displayed on a new line when scrolling back
23883 * at the more prompt. */
23884 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023885 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023887 }
23888 else if (eap->cmdidx == CMD_echo)
23889 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023890 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023891 if (p != NULL)
23892 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023893 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023894 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023895 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023896 if (*p != TAB && needclr)
23897 {
23898 /* remove any text still there from the command */
23899 msg_clr_eos();
23900 needclr = FALSE;
23901 }
23902 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023903 }
23904 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023905 {
23906#ifdef FEAT_MBYTE
23907 if (has_mbyte)
23908 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023909 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023910
23911 (void)msg_outtrans_len_attr(p, i, echo_attr);
23912 p += i - 1;
23913 }
23914 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023915#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023916 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023918 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023919 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023920 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023921 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023922 arg = skipwhite(arg);
23923 }
23924 eap->nextcmd = check_nextcmd(arg);
23925
23926 if (eap->skip)
23927 --emsg_skip;
23928 else
23929 {
23930 /* remove text that may still be there from the command */
23931 if (needclr)
23932 msg_clr_eos();
23933 if (eap->cmdidx == CMD_echo)
23934 msg_end();
23935 }
23936}
23937
23938/*
23939 * ":echohl {name}".
23940 */
23941 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023942ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023943{
23944 int id;
23945
23946 id = syn_name2id(eap->arg);
23947 if (id == 0)
23948 echo_attr = 0;
23949 else
23950 echo_attr = syn_id2attr(id);
23951}
23952
23953/*
23954 * ":execute expr1 ..." execute the result of an expression.
23955 * ":echomsg expr1 ..." Print a message
23956 * ":echoerr expr1 ..." Print an error
23957 * Each gets spaces around each argument and a newline at the end for
23958 * echo commands
23959 */
23960 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023961ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023962{
23963 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023964 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023965 int ret = OK;
23966 char_u *p;
23967 garray_T ga;
23968 int len;
23969 int save_did_emsg;
23970
23971 ga_init2(&ga, 1, 80);
23972
23973 if (eap->skip)
23974 ++emsg_skip;
23975 while (*arg != NUL && *arg != '|' && *arg != '\n')
23976 {
23977 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023978 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023979 {
23980 /*
23981 * Report the invalid expression unless the expression evaluation
23982 * has been cancelled due to an aborting error, an interrupt, or an
23983 * exception.
23984 */
23985 if (!aborting())
23986 EMSG2(_(e_invexpr2), p);
23987 ret = FAIL;
23988 break;
23989 }
23990
23991 if (!eap->skip)
23992 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023993 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023994 len = (int)STRLEN(p);
23995 if (ga_grow(&ga, len + 2) == FAIL)
23996 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023997 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023998 ret = FAIL;
23999 break;
24000 }
24001 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024002 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000024003 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024004 ga.ga_len += len;
24005 }
24006
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024007 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024008 arg = skipwhite(arg);
24009 }
24010
24011 if (ret != FAIL && ga.ga_data != NULL)
24012 {
24013 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000024014 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024015 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000024016 out_flush();
24017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024018 else if (eap->cmdidx == CMD_echoerr)
24019 {
24020 /* We don't want to abort following commands, restore did_emsg. */
24021 save_did_emsg = did_emsg;
24022 EMSG((char_u *)ga.ga_data);
24023 if (!force_abort)
24024 did_emsg = save_did_emsg;
24025 }
24026 else if (eap->cmdidx == CMD_execute)
24027 do_cmdline((char_u *)ga.ga_data,
24028 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
24029 }
24030
24031 ga_clear(&ga);
24032
24033 if (eap->skip)
24034 --emsg_skip;
24035
24036 eap->nextcmd = check_nextcmd(arg);
24037}
24038
24039/*
24040 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
24041 * "arg" points to the "&" or '+' when called, to "option" when returning.
24042 * Returns NULL when no option name found. Otherwise pointer to the char
24043 * after the option name.
24044 */
24045 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024046find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024047{
24048 char_u *p = *arg;
24049
24050 ++p;
24051 if (*p == 'g' && p[1] == ':')
24052 {
24053 *opt_flags = OPT_GLOBAL;
24054 p += 2;
24055 }
24056 else if (*p == 'l' && p[1] == ':')
24057 {
24058 *opt_flags = OPT_LOCAL;
24059 p += 2;
24060 }
24061 else
24062 *opt_flags = 0;
24063
24064 if (!ASCII_ISALPHA(*p))
24065 return NULL;
24066 *arg = p;
24067
24068 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
24069 p += 4; /* termcap option */
24070 else
24071 while (ASCII_ISALPHA(*p))
24072 ++p;
24073 return p;
24074}
24075
24076/*
24077 * ":function"
24078 */
24079 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024080ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024081{
24082 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024083 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024084 int j;
24085 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024086 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024087 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024088 char_u *name = NULL;
24089 char_u *p;
24090 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024091 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024092 garray_T newargs;
24093 garray_T newlines;
24094 int varargs = FALSE;
24095 int mustend = FALSE;
24096 int flags = 0;
24097 ufunc_T *fp;
24098 int indent;
24099 int nesting;
24100 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000024101 dictitem_T *v;
24102 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024103 static int func_nr = 0; /* number for nameless function */
24104 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024105 hashtab_T *ht;
24106 int todo;
24107 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024108 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024109
24110 /*
24111 * ":function" without argument: list functions.
24112 */
24113 if (ends_excmd(*eap->arg))
24114 {
24115 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024116 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024117 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000024118 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024119 {
24120 if (!HASHITEM_EMPTY(hi))
24121 {
24122 --todo;
24123 fp = HI2UF(hi);
24124 if (!isdigit(*fp->uf_name))
24125 list_func_head(fp, FALSE);
24126 }
24127 }
24128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024129 eap->nextcmd = check_nextcmd(eap->arg);
24130 return;
24131 }
24132
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024133 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024134 * ":function /pat": list functions matching pattern.
24135 */
24136 if (*eap->arg == '/')
24137 {
24138 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
24139 if (!eap->skip)
24140 {
24141 regmatch_T regmatch;
24142
24143 c = *p;
24144 *p = NUL;
24145 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
24146 *p = c;
24147 if (regmatch.regprog != NULL)
24148 {
24149 regmatch.rm_ic = p_ic;
24150
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024151 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024152 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
24153 {
24154 if (!HASHITEM_EMPTY(hi))
24155 {
24156 --todo;
24157 fp = HI2UF(hi);
24158 if (!isdigit(*fp->uf_name)
24159 && vim_regexec(&regmatch, fp->uf_name, 0))
24160 list_func_head(fp, FALSE);
24161 }
24162 }
Bram Moolenaar473de612013-06-08 18:19:48 +020024163 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024164 }
24165 }
24166 if (*p == '/')
24167 ++p;
24168 eap->nextcmd = check_nextcmd(p);
24169 return;
24170 }
24171
24172 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024173 * Get the function name. There are these situations:
24174 * func normal function name
24175 * "name" == func, "fudi.fd_dict" == NULL
24176 * dict.func new dictionary entry
24177 * "name" == NULL, "fudi.fd_dict" set,
24178 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
24179 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024180 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024181 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
24182 * dict.func existing dict entry that's not a Funcref
24183 * "name" == NULL, "fudi.fd_dict" set,
24184 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024185 * s:func script-local function name
24186 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024187 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024188 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010024189 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024190 paren = (vim_strchr(p, '(') != NULL);
24191 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024192 {
24193 /*
24194 * Return on an invalid expression in braces, unless the expression
24195 * evaluation has been cancelled due to an aborting error, an
24196 * interrupt, or an exception.
24197 */
24198 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024199 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024200 if (!eap->skip && fudi.fd_newkey != NULL)
24201 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024202 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024203 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024205 else
24206 eap->skip = TRUE;
24207 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000024208
Bram Moolenaar071d4272004-06-13 20:20:40 +000024209 /* An error in a function call during evaluation of an expression in magic
24210 * braces should not cause the function not to be defined. */
24211 saved_did_emsg = did_emsg;
24212 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024213
24214 /*
24215 * ":function func" with only function name: list function.
24216 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024217 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024218 {
24219 if (!ends_excmd(*skipwhite(p)))
24220 {
24221 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024222 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024223 }
24224 eap->nextcmd = check_nextcmd(p);
24225 if (eap->nextcmd != NULL)
24226 *p = NUL;
24227 if (!eap->skip && !got_int)
24228 {
24229 fp = find_func(name);
24230 if (fp != NULL)
24231 {
24232 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024233 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024234 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024235 if (FUNCLINE(fp, j) == NULL)
24236 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024237 msg_putchar('\n');
24238 msg_outnum((long)(j + 1));
24239 if (j < 9)
24240 msg_putchar(' ');
24241 if (j < 99)
24242 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024243 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024244 out_flush(); /* show a line at a time */
24245 ui_breakcheck();
24246 }
24247 if (!got_int)
24248 {
24249 msg_putchar('\n');
24250 msg_puts((char_u *)" endfunction");
24251 }
24252 }
24253 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024254 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024255 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024256 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024257 }
24258
24259 /*
24260 * ":function name(arg1, arg2)" Define function.
24261 */
24262 p = skipwhite(p);
24263 if (*p != '(')
24264 {
24265 if (!eap->skip)
24266 {
24267 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024268 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024269 }
24270 /* attempt to continue by skipping some text */
24271 if (vim_strchr(p, '(') != NULL)
24272 p = vim_strchr(p, '(');
24273 }
24274 p = skipwhite(p + 1);
24275
24276 ga_init2(&newargs, (int)sizeof(char_u *), 3);
24277 ga_init2(&newlines, (int)sizeof(char_u *), 3);
24278
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024279 if (!eap->skip)
24280 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024281 /* Check the name of the function. Unless it's a dictionary function
24282 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024283 if (name != NULL)
24284 arg = name;
24285 else
24286 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024287 if (arg != NULL && (fudi.fd_di == NULL
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +010024288 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
24289 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024290 {
24291 if (*arg == K_SPECIAL)
24292 j = 3;
24293 else
24294 j = 0;
24295 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
24296 : eval_isnamec(arg[j])))
24297 ++j;
24298 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000024299 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024300 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010024301 /* Disallow using the g: dict. */
24302 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
24303 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024304 }
24305
Bram Moolenaar071d4272004-06-13 20:20:40 +000024306 /*
24307 * Isolate the arguments: "arg1, arg2, ...)"
24308 */
24309 while (*p != ')')
24310 {
24311 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
24312 {
24313 varargs = TRUE;
24314 p += 3;
24315 mustend = TRUE;
24316 }
24317 else
24318 {
24319 arg = p;
24320 while (ASCII_ISALNUM(*p) || *p == '_')
24321 ++p;
24322 if (arg == p || isdigit(*arg)
24323 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
24324 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
24325 {
24326 if (!eap->skip)
24327 EMSG2(_("E125: Illegal argument: %s"), arg);
24328 break;
24329 }
24330 if (ga_grow(&newargs, 1) == FAIL)
24331 goto erret;
24332 c = *p;
24333 *p = NUL;
24334 arg = vim_strsave(arg);
24335 if (arg == NULL)
24336 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024337
24338 /* Check for duplicate argument name. */
24339 for (i = 0; i < newargs.ga_len; ++i)
24340 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
24341 {
24342 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010024343 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024344 goto erret;
24345 }
24346
Bram Moolenaar071d4272004-06-13 20:20:40 +000024347 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
24348 *p = c;
24349 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024350 if (*p == ',')
24351 ++p;
24352 else
24353 mustend = TRUE;
24354 }
24355 p = skipwhite(p);
24356 if (mustend && *p != ')')
24357 {
24358 if (!eap->skip)
24359 EMSG2(_(e_invarg2), eap->arg);
24360 break;
24361 }
24362 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020024363 if (*p != ')')
24364 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024365 ++p; /* skip the ')' */
24366
Bram Moolenaare9a41262005-01-15 22:18:47 +000024367 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024368 for (;;)
24369 {
24370 p = skipwhite(p);
24371 if (STRNCMP(p, "range", 5) == 0)
24372 {
24373 flags |= FC_RANGE;
24374 p += 5;
24375 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024376 else if (STRNCMP(p, "dict", 4) == 0)
24377 {
24378 flags |= FC_DICT;
24379 p += 4;
24380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024381 else if (STRNCMP(p, "abort", 5) == 0)
24382 {
24383 flags |= FC_ABORT;
24384 p += 5;
24385 }
24386 else
24387 break;
24388 }
24389
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024390 /* When there is a line break use what follows for the function body.
24391 * Makes 'exe "func Test()\n...\nendfunc"' work. */
24392 if (*p == '\n')
24393 line_arg = p + 1;
24394 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024395 EMSG(_(e_trailing));
24396
24397 /*
24398 * Read the body of the function, until ":endfunction" is found.
24399 */
24400 if (KeyTyped)
24401 {
24402 /* Check if the function already exists, don't let the user type the
24403 * whole function before telling him it doesn't work! For a script we
24404 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024405 if (!eap->skip && !eap->forceit)
24406 {
24407 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
24408 EMSG(_(e_funcdict));
24409 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024410 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024412
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024413 if (!eap->skip && did_emsg)
24414 goto erret;
24415
Bram Moolenaar071d4272004-06-13 20:20:40 +000024416 msg_putchar('\n'); /* don't overwrite the function name */
24417 cmdline_row = msg_row;
24418 }
24419
24420 indent = 2;
24421 nesting = 0;
24422 for (;;)
24423 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024424 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024425 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024426 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024427 saved_wait_return = FALSE;
24428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024429 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024430 sourcing_lnum_off = sourcing_lnum;
24431
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024432 if (line_arg != NULL)
24433 {
24434 /* Use eap->arg, split up in parts by line breaks. */
24435 theline = line_arg;
24436 p = vim_strchr(theline, '\n');
24437 if (p == NULL)
24438 line_arg += STRLEN(line_arg);
24439 else
24440 {
24441 *p = NUL;
24442 line_arg = p + 1;
24443 }
24444 }
24445 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024446 theline = getcmdline(':', 0L, indent);
24447 else
24448 theline = eap->getline(':', eap->cookie, indent);
24449 if (KeyTyped)
24450 lines_left = Rows - 1;
24451 if (theline == NULL)
24452 {
24453 EMSG(_("E126: Missing :endfunction"));
24454 goto erret;
24455 }
24456
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024457 /* Detect line continuation: sourcing_lnum increased more than one. */
24458 if (sourcing_lnum > sourcing_lnum_off + 1)
24459 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
24460 else
24461 sourcing_lnum_off = 0;
24462
Bram Moolenaar071d4272004-06-13 20:20:40 +000024463 if (skip_until != NULL)
24464 {
24465 /* between ":append" and "." and between ":python <<EOF" and "EOF"
24466 * don't check for ":endfunc". */
24467 if (STRCMP(theline, skip_until) == 0)
24468 {
24469 vim_free(skip_until);
24470 skip_until = NULL;
24471 }
24472 }
24473 else
24474 {
24475 /* skip ':' and blanks*/
24476 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
24477 ;
24478
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024479 /* Check for "endfunction". */
24480 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024481 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024482 if (line_arg == NULL)
24483 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024484 break;
24485 }
24486
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024487 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000024488 * at "end". */
24489 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
24490 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024491 else if (STRNCMP(p, "if", 2) == 0
24492 || STRNCMP(p, "wh", 2) == 0
24493 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000024494 || STRNCMP(p, "try", 3) == 0)
24495 indent += 2;
24496
24497 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024498 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024499 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024500 if (*p == '!')
24501 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024502 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010024503 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010024504 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000024505 {
Bram Moolenaaref923902014-12-13 21:00:55 +010024506 ++nesting;
24507 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024508 }
24509 }
24510
24511 /* Check for ":append" or ":insert". */
24512 p = skip_range(p, NULL);
24513 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
24514 || (p[0] == 'i'
24515 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
24516 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
24517 skip_until = vim_strsave((char_u *)".");
24518
24519 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
24520 arg = skipwhite(skiptowhite(p));
24521 if (arg[0] == '<' && arg[1] =='<'
24522 && ((p[0] == 'p' && p[1] == 'y'
24523 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
24524 || (p[0] == 'p' && p[1] == 'e'
24525 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
24526 || (p[0] == 't' && p[1] == 'c'
24527 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020024528 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
24529 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024530 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
24531 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024532 || (p[0] == 'm' && p[1] == 'z'
24533 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024534 ))
24535 {
24536 /* ":python <<" continues until a dot, like ":append" */
24537 p = skipwhite(arg + 2);
24538 if (*p == NUL)
24539 skip_until = vim_strsave((char_u *)".");
24540 else
24541 skip_until = vim_strsave(p);
24542 }
24543 }
24544
24545 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024546 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024547 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024548 if (line_arg == NULL)
24549 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024550 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024551 }
24552
24553 /* Copy the line to newly allocated memory. get_one_sourceline()
24554 * allocates 250 bytes per line, this saves 80% on average. The cost
24555 * is an extra alloc/free. */
24556 p = vim_strsave(theline);
24557 if (p != NULL)
24558 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024559 if (line_arg == NULL)
24560 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024561 theline = p;
24562 }
24563
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024564 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24565
24566 /* Add NULL lines for continuation lines, so that the line count is
24567 * equal to the index in the growarray. */
24568 while (sourcing_lnum_off-- > 0)
24569 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024570
24571 /* Check for end of eap->arg. */
24572 if (line_arg != NULL && *line_arg == NUL)
24573 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024574 }
24575
24576 /* Don't define the function when skipping commands or when an error was
24577 * detected. */
24578 if (eap->skip || did_emsg)
24579 goto erret;
24580
24581 /*
24582 * If there are no errors, add the function
24583 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024584 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024585 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024586 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024587 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024588 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024589 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024590 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024591 goto erret;
24592 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024593
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024594 fp = find_func(name);
24595 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024596 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024597 if (!eap->forceit)
24598 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024599 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024600 goto erret;
24601 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024602 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024603 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024604 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024605 name);
24606 goto erret;
24607 }
24608 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024609 ga_clear_strings(&(fp->uf_args));
24610 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024611 vim_free(name);
24612 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024614 }
24615 else
24616 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024617 char numbuf[20];
24618
24619 fp = NULL;
24620 if (fudi.fd_newkey == NULL && !eap->forceit)
24621 {
24622 EMSG(_(e_funcdict));
24623 goto erret;
24624 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024625 if (fudi.fd_di == NULL)
24626 {
24627 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024628 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024629 goto erret;
24630 }
24631 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024632 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024633 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024634
24635 /* Give the function a sequential number. Can only be used with a
24636 * Funcref! */
24637 vim_free(name);
24638 sprintf(numbuf, "%d", ++func_nr);
24639 name = vim_strsave((char_u *)numbuf);
24640 if (name == NULL)
24641 goto erret;
24642 }
24643
24644 if (fp == NULL)
24645 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024646 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024647 {
24648 int slen, plen;
24649 char_u *scriptname;
24650
24651 /* Check that the autoload name matches the script name. */
24652 j = FAIL;
24653 if (sourcing_name != NULL)
24654 {
24655 scriptname = autoload_name(name);
24656 if (scriptname != NULL)
24657 {
24658 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024659 plen = (int)STRLEN(p);
24660 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024661 if (slen > plen && fnamecmp(p,
24662 sourcing_name + slen - plen) == 0)
24663 j = OK;
24664 vim_free(scriptname);
24665 }
24666 }
24667 if (j == FAIL)
24668 {
24669 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24670 goto erret;
24671 }
24672 }
24673
24674 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024675 if (fp == NULL)
24676 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024677
24678 if (fudi.fd_dict != NULL)
24679 {
24680 if (fudi.fd_di == NULL)
24681 {
24682 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024683 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024684 if (fudi.fd_di == NULL)
24685 {
24686 vim_free(fp);
24687 goto erret;
24688 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024689 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24690 {
24691 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024692 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024693 goto erret;
24694 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024695 }
24696 else
24697 /* overwrite existing dict entry */
24698 clear_tv(&fudi.fd_di->di_tv);
24699 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024700 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024701 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024702 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024703
24704 /* behave like "dict" was used */
24705 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024706 }
24707
Bram Moolenaar071d4272004-06-13 20:20:40 +000024708 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024709 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024710 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24711 {
24712 vim_free(fp);
24713 goto erret;
24714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024715 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024716 fp->uf_args = newargs;
24717 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024718#ifdef FEAT_PROFILE
24719 fp->uf_tml_count = NULL;
24720 fp->uf_tml_total = NULL;
24721 fp->uf_tml_self = NULL;
24722 fp->uf_profiling = FALSE;
24723 if (prof_def_func())
24724 func_do_profile(fp);
24725#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024726 fp->uf_varargs = varargs;
24727 fp->uf_flags = flags;
24728 fp->uf_calls = 0;
24729 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024730 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024731
24732erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024733 ga_clear_strings(&newargs);
24734 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024735ret_free:
24736 vim_free(skip_until);
24737 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024738 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024739 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024740 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024741}
24742
24743/*
24744 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024745 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024746 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024747 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024748 * TFN_INT: internal function name OK
24749 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024750 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024751 * Advances "pp" to just after the function name (if no error).
24752 */
24753 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024754trans_function_name(
24755 char_u **pp,
24756 int skip, /* only find the end, don't evaluate */
24757 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010024758 funcdict_T *fdp, /* return: info about dictionary used */
24759 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024760{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024761 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024762 char_u *start;
24763 char_u *end;
24764 int lead;
24765 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024766 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024767 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024768
24769 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024770 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024771 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024772
24773 /* Check for hard coded <SNR>: already translated function ID (from a user
24774 * command). */
24775 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24776 && (*pp)[2] == (int)KE_SNR)
24777 {
24778 *pp += 3;
24779 len = get_id_len(pp) + 3;
24780 return vim_strnsave(start, len);
24781 }
24782
24783 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24784 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024785 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024786 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024787 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024788
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024789 /* Note that TFN_ flags use the same values as GLV_ flags. */
24790 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024791 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024792 if (end == start)
24793 {
24794 if (!skip)
24795 EMSG(_("E129: Function name required"));
24796 goto theend;
24797 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024798 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024799 {
24800 /*
24801 * Report an invalid expression in braces, unless the expression
24802 * evaluation has been cancelled due to an aborting error, an
24803 * interrupt, or an exception.
24804 */
24805 if (!aborting())
24806 {
24807 if (end != NULL)
24808 EMSG2(_(e_invarg2), start);
24809 }
24810 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024811 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024812 goto theend;
24813 }
24814
24815 if (lv.ll_tv != NULL)
24816 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024817 if (fdp != NULL)
24818 {
24819 fdp->fd_dict = lv.ll_dict;
24820 fdp->fd_newkey = lv.ll_newkey;
24821 lv.ll_newkey = NULL;
24822 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024823 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024824 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24825 {
24826 name = vim_strsave(lv.ll_tv->vval.v_string);
24827 *pp = end;
24828 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010024829 else if (lv.ll_tv->v_type == VAR_PARTIAL
24830 && lv.ll_tv->vval.v_partial != NULL)
24831 {
24832 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
24833 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010024834 if (partial != NULL)
24835 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010024836 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024837 else
24838 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024839 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24840 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024841 EMSG(_(e_funcref));
24842 else
24843 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024844 name = NULL;
24845 }
24846 goto theend;
24847 }
24848
24849 if (lv.ll_name == NULL)
24850 {
24851 /* Error found, but continue after the function name. */
24852 *pp = end;
24853 goto theend;
24854 }
24855
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024856 /* Check if the name is a Funcref. If so, use the value. */
24857 if (lv.ll_exp_name != NULL)
24858 {
24859 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010024860 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010024861 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024862 if (name == lv.ll_exp_name)
24863 name = NULL;
24864 }
24865 else
24866 {
24867 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010024868 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024869 if (name == *pp)
24870 name = NULL;
24871 }
24872 if (name != NULL)
24873 {
24874 name = vim_strsave(name);
24875 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024876 if (STRNCMP(name, "<SNR>", 5) == 0)
24877 {
24878 /* Change "<SNR>" to the byte sequence. */
24879 name[0] = K_SPECIAL;
24880 name[1] = KS_EXTRA;
24881 name[2] = (int)KE_SNR;
24882 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24883 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024884 goto theend;
24885 }
24886
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024887 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024888 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024889 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024890 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24891 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24892 {
24893 /* When there was "s:" already or the name expanded to get a
24894 * leading "s:" then remove it. */
24895 lv.ll_name += 2;
24896 len -= 2;
24897 lead = 2;
24898 }
24899 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024900 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024901 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024902 /* skip over "s:" and "g:" */
24903 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024904 lv.ll_name += 2;
24905 len = (int)(end - lv.ll_name);
24906 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024907
24908 /*
24909 * Copy the function name to allocated memory.
24910 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24911 * Accept <SNR>123_name() outside a script.
24912 */
24913 if (skip)
24914 lead = 0; /* do nothing */
24915 else if (lead > 0)
24916 {
24917 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024918 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24919 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024920 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024921 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024922 if (current_SID <= 0)
24923 {
24924 EMSG(_(e_usingsid));
24925 goto theend;
24926 }
24927 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24928 lead += (int)STRLEN(sid_buf);
24929 }
24930 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024931 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024932 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024933 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024934 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024935 goto theend;
24936 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024937 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024938 {
24939 char_u *cp = vim_strchr(lv.ll_name, ':');
24940
24941 if (cp != NULL && cp < end)
24942 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024943 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024944 goto theend;
24945 }
24946 }
24947
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024948 name = alloc((unsigned)(len + lead + 1));
24949 if (name != NULL)
24950 {
24951 if (lead > 0)
24952 {
24953 name[0] = K_SPECIAL;
24954 name[1] = KS_EXTRA;
24955 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024956 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024957 STRCPY(name + 3, sid_buf);
24958 }
24959 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024960 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024961 }
24962 *pp = end;
24963
24964theend:
24965 clear_lval(&lv);
24966 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024967}
24968
24969/*
24970 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24971 * Return 2 if "p" starts with "s:".
24972 * Return 0 otherwise.
24973 */
24974 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024975eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024976{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024977 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24978 * the standard library function. */
24979 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24980 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024981 return 5;
24982 if (p[0] == 's' && p[1] == ':')
24983 return 2;
24984 return 0;
24985}
24986
24987/*
24988 * Return TRUE if "p" starts with "<SID>" or "s:".
24989 * Only works if eval_fname_script() returned non-zero for "p"!
24990 */
24991 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024992eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024993{
24994 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24995}
24996
24997/*
24998 * List the head of the function: "name(arg1, arg2)".
24999 */
25000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025001list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025002{
25003 int j;
25004
25005 msg_start();
25006 if (indent)
25007 MSG_PUTS(" ");
25008 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025009 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025010 {
25011 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025012 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025013 }
25014 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025015 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025016 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025017 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025018 {
25019 if (j)
25020 MSG_PUTS(", ");
25021 msg_puts(FUNCARG(fp, j));
25022 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025023 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025024 {
25025 if (j)
25026 MSG_PUTS(", ");
25027 MSG_PUTS("...");
25028 }
25029 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020025030 if (fp->uf_flags & FC_ABORT)
25031 MSG_PUTS(" abort");
25032 if (fp->uf_flags & FC_RANGE)
25033 MSG_PUTS(" range");
25034 if (fp->uf_flags & FC_DICT)
25035 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025036 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000025037 if (p_verbose > 0)
25038 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025039}
25040
25041/*
25042 * Find a function by name, return pointer to it in ufuncs.
25043 * Return NULL for unknown function.
25044 */
25045 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025046find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025047{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025048 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025049
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025050 hi = hash_find(&func_hashtab, name);
25051 if (!HASHITEM_EMPTY(hi))
25052 return HI2UF(hi);
25053 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025054}
25055
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000025056#if defined(EXITFREE) || defined(PROTO)
25057 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025058free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000025059{
25060 hashitem_T *hi;
25061
25062 /* Need to start all over every time, because func_free() may change the
25063 * hash table. */
25064 while (func_hashtab.ht_used > 0)
25065 for (hi = func_hashtab.ht_array; ; ++hi)
25066 if (!HASHITEM_EMPTY(hi))
25067 {
25068 func_free(HI2UF(hi));
25069 break;
25070 }
25071}
25072#endif
25073
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025074 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025075translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025076{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025077 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025078 return find_internal_func(name) >= 0;
25079 return find_func(name) != NULL;
25080}
25081
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025082/*
25083 * Return TRUE if a function "name" exists.
25084 */
25085 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025086function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025087{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000025088 char_u *nm = name;
25089 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025090 int n = FALSE;
25091
Bram Moolenaar6d977d62014-01-14 15:24:39 +010025092 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010025093 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000025094 nm = skipwhite(nm);
25095
25096 /* Only accept "funcname", "funcname ", "funcname (..." and
25097 * "funcname(...", not "funcname!...". */
25098 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025099 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000025100 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025101 return n;
25102}
25103
Bram Moolenaara1544c02013-05-30 12:35:52 +020025104 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025105get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020025106{
25107 char_u *nm = name;
25108 char_u *p;
25109
Bram Moolenaar65639032016-03-16 21:40:30 +010025110 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020025111
25112 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025113 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020025114 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020025115
Bram Moolenaara1544c02013-05-30 12:35:52 +020025116 vim_free(p);
25117 return NULL;
25118}
25119
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025120/*
25121 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025122 * lower case letter and doesn't contain AUTOLOAD_CHAR.
25123 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025124 */
25125 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025126builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025127{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020025128 char_u *p;
25129
25130 if (!ASCII_ISLOWER(name[0]))
25131 return FALSE;
25132 p = vim_strchr(name, AUTOLOAD_CHAR);
25133 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025134}
25135
Bram Moolenaar05159a02005-02-26 23:04:13 +000025136#if defined(FEAT_PROFILE) || defined(PROTO)
25137/*
25138 * Start profiling function "fp".
25139 */
25140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025141func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025142{
Bram Moolenaar904c6222010-07-24 16:57:39 +020025143 int len = fp->uf_lines.ga_len;
25144
25145 if (len == 0)
25146 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025147 fp->uf_tm_count = 0;
25148 profile_zero(&fp->uf_tm_self);
25149 profile_zero(&fp->uf_tm_total);
25150 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025151 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025152 if (fp->uf_tml_total == NULL)
25153 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025154 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025155 if (fp->uf_tml_self == NULL)
25156 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025157 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025158 fp->uf_tml_idx = -1;
25159 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
25160 || fp->uf_tml_self == NULL)
25161 return; /* out of memory */
25162
25163 fp->uf_profiling = TRUE;
25164}
25165
25166/*
25167 * Dump the profiling results for all functions in file "fd".
25168 */
25169 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025170func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025171{
25172 hashitem_T *hi;
25173 int todo;
25174 ufunc_T *fp;
25175 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000025176 ufunc_T **sorttab;
25177 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025178
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025179 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000025180 if (todo == 0)
25181 return; /* nothing to dump */
25182
Bram Moolenaare2e4b982015-06-09 20:30:51 +020025183 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000025184
Bram Moolenaar05159a02005-02-26 23:04:13 +000025185 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
25186 {
25187 if (!HASHITEM_EMPTY(hi))
25188 {
25189 --todo;
25190 fp = HI2UF(hi);
25191 if (fp->uf_profiling)
25192 {
Bram Moolenaar73830342005-02-28 22:48:19 +000025193 if (sorttab != NULL)
25194 sorttab[st_len++] = fp;
25195
Bram Moolenaar05159a02005-02-26 23:04:13 +000025196 if (fp->uf_name[0] == K_SPECIAL)
25197 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
25198 else
25199 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
25200 if (fp->uf_tm_count == 1)
25201 fprintf(fd, "Called 1 time\n");
25202 else
25203 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
25204 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
25205 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
25206 fprintf(fd, "\n");
25207 fprintf(fd, "count total (s) self (s)\n");
25208
25209 for (i = 0; i < fp->uf_lines.ga_len; ++i)
25210 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025211 if (FUNCLINE(fp, i) == NULL)
25212 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000025213 prof_func_line(fd, fp->uf_tml_count[i],
25214 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025215 fprintf(fd, "%s\n", FUNCLINE(fp, i));
25216 }
25217 fprintf(fd, "\n");
25218 }
25219 }
25220 }
Bram Moolenaar73830342005-02-28 22:48:19 +000025221
25222 if (sorttab != NULL && st_len > 0)
25223 {
25224 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
25225 prof_total_cmp);
25226 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
25227 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
25228 prof_self_cmp);
25229 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
25230 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000025231
25232 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025233}
Bram Moolenaar73830342005-02-28 22:48:19 +000025234
25235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025236prof_sort_list(
25237 FILE *fd,
25238 ufunc_T **sorttab,
25239 int st_len,
25240 char *title,
25241 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000025242{
25243 int i;
25244 ufunc_T *fp;
25245
25246 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
25247 fprintf(fd, "count total (s) self (s) function\n");
25248 for (i = 0; i < 20 && i < st_len; ++i)
25249 {
25250 fp = sorttab[i];
25251 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
25252 prefer_self);
25253 if (fp->uf_name[0] == K_SPECIAL)
25254 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
25255 else
25256 fprintf(fd, " %s()\n", fp->uf_name);
25257 }
25258 fprintf(fd, "\n");
25259}
25260
25261/*
25262 * Print the count and times for one function or function line.
25263 */
25264 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025265prof_func_line(
25266 FILE *fd,
25267 int count,
25268 proftime_T *total,
25269 proftime_T *self,
25270 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000025271{
25272 if (count > 0)
25273 {
25274 fprintf(fd, "%5d ", count);
25275 if (prefer_self && profile_equal(total, self))
25276 fprintf(fd, " ");
25277 else
25278 fprintf(fd, "%s ", profile_msg(total));
25279 if (!prefer_self && profile_equal(total, self))
25280 fprintf(fd, " ");
25281 else
25282 fprintf(fd, "%s ", profile_msg(self));
25283 }
25284 else
25285 fprintf(fd, " ");
25286}
25287
25288/*
25289 * Compare function for total time sorting.
25290 */
25291 static int
25292#ifdef __BORLANDC__
25293_RTLENTRYF
25294#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025295prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025296{
25297 ufunc_T *p1, *p2;
25298
25299 p1 = *(ufunc_T **)s1;
25300 p2 = *(ufunc_T **)s2;
25301 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
25302}
25303
25304/*
25305 * Compare function for self time sorting.
25306 */
25307 static int
25308#ifdef __BORLANDC__
25309_RTLENTRYF
25310#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025311prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025312{
25313 ufunc_T *p1, *p2;
25314
25315 p1 = *(ufunc_T **)s1;
25316 p2 = *(ufunc_T **)s2;
25317 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
25318}
25319
Bram Moolenaar05159a02005-02-26 23:04:13 +000025320#endif
25321
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025322/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025323 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025324 * Return TRUE if a package was loaded.
25325 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020025326 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025327script_autoload(
25328 char_u *name,
25329 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025330{
25331 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025332 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025333 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025334 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025335
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025336 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025337 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025338 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025339 return FALSE;
25340
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025341 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025342
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025343 /* Find the name in the list of previously loaded package names. Skip
25344 * "autoload/", it's always the same. */
25345 for (i = 0; i < ga_loaded.ga_len; ++i)
25346 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
25347 break;
25348 if (!reload && i < ga_loaded.ga_len)
25349 ret = FALSE; /* was loaded already */
25350 else
25351 {
25352 /* Remember the name if it wasn't loaded already. */
25353 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
25354 {
25355 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
25356 tofree = NULL;
25357 }
25358
25359 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010025360 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025361 ret = TRUE;
25362 }
25363
25364 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025365 return ret;
25366}
25367
25368/*
25369 * Return the autoload script name for a function or variable name.
25370 * Returns NULL when out of memory.
25371 */
25372 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025373autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025374{
25375 char_u *p;
25376 char_u *scriptname;
25377
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025378 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025379 scriptname = alloc((unsigned)(STRLEN(name) + 14));
25380 if (scriptname == NULL)
25381 return FALSE;
25382 STRCPY(scriptname, "autoload/");
25383 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025384 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025385 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025386 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025387 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025388 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025389}
25390
Bram Moolenaar071d4272004-06-13 20:20:40 +000025391#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
25392
25393/*
25394 * Function given to ExpandGeneric() to obtain the list of user defined
25395 * function names.
25396 */
25397 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025398get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025399{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025400 static long_u done;
25401 static hashitem_T *hi;
25402 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025403
25404 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025405 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025406 done = 0;
25407 hi = func_hashtab.ht_array;
25408 }
25409 if (done < func_hashtab.ht_used)
25410 {
25411 if (done++ > 0)
25412 ++hi;
25413 while (HASHITEM_EMPTY(hi))
25414 ++hi;
25415 fp = HI2UF(hi);
25416
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025417 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010025418 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025419
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025420 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
25421 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025422
25423 cat_func_name(IObuff, fp);
25424 if (xp->xp_context != EXPAND_USER_FUNC)
25425 {
25426 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025427 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025428 STRCAT(IObuff, ")");
25429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025430 return IObuff;
25431 }
25432 return NULL;
25433}
25434
25435#endif /* FEAT_CMDL_COMPL */
25436
25437/*
25438 * Copy the function name of "fp" to buffer "buf".
25439 * "buf" must be able to hold the function name plus three bytes.
25440 * Takes care of script-local function names.
25441 */
25442 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025443cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025444{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025445 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025446 {
25447 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025448 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025449 }
25450 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025451 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025452}
25453
25454/*
25455 * ":delfunction {name}"
25456 */
25457 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025458ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025459{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025460 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025461 char_u *p;
25462 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000025463 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025464
25465 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010025466 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025467 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025468 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025469 {
25470 if (fudi.fd_dict != NULL && !eap->skip)
25471 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000025472 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025474 if (!ends_excmd(*skipwhite(p)))
25475 {
25476 vim_free(name);
25477 EMSG(_(e_trailing));
25478 return;
25479 }
25480 eap->nextcmd = check_nextcmd(p);
25481 if (eap->nextcmd != NULL)
25482 *p = NUL;
25483
25484 if (!eap->skip)
25485 fp = find_func(name);
25486 vim_free(name);
25487
25488 if (!eap->skip)
25489 {
25490 if (fp == NULL)
25491 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025492 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025493 return;
25494 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025495 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025496 {
25497 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
25498 return;
25499 }
25500
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025501 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025502 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025503 /* Delete the dict item that refers to the function, it will
25504 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000025505 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025506 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025507 else
25508 func_free(fp);
25509 }
25510}
25511
25512/*
25513 * Free a function and remove it from the list of functions.
25514 */
25515 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025516func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025517{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025518 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025519
25520 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025521 ga_clear_strings(&(fp->uf_args));
25522 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025523#ifdef FEAT_PROFILE
25524 vim_free(fp->uf_tml_count);
25525 vim_free(fp->uf_tml_total);
25526 vim_free(fp->uf_tml_self);
25527#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025528
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025529 /* remove the function from the function hashtable */
25530 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
25531 if (HASHITEM_EMPTY(hi))
25532 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025533 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025534 hash_remove(&func_hashtab, hi);
25535
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025536 vim_free(fp);
25537}
25538
25539/*
25540 * Unreference a Function: decrement the reference count and free it when it
25541 * becomes zero. Only for numbered functions.
25542 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025543 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025544func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025545{
25546 ufunc_T *fp;
25547
25548 if (name != NULL && isdigit(*name))
25549 {
25550 fp = find_func(name);
25551 if (fp == NULL)
Bram Moolenaara9673212016-06-01 22:21:06 +020025552 {
Bram Moolenaarb89a25f2016-06-01 23:08:39 +020025553#ifdef EXITFREE
25554 if (!entered_free_all_mem)
25555#endif
Bram Moolenaara9673212016-06-01 22:21:06 +020025556 EMSG2(_(e_intern2), "func_unref()");
25557 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025558 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025559 {
25560 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025561 * when "uf_calls" becomes zero. */
25562 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025563 func_free(fp);
25564 }
25565 }
25566}
25567
25568/*
25569 * Count a reference to a Function.
25570 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025571 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025572func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025573{
25574 ufunc_T *fp;
25575
25576 if (name != NULL && isdigit(*name))
25577 {
25578 fp = find_func(name);
25579 if (fp == NULL)
25580 EMSG2(_(e_intern2), "func_ref()");
25581 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025582 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025583 }
25584}
25585
25586/*
25587 * Call a user function.
25588 */
25589 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025590call_user_func(
25591 ufunc_T *fp, /* pointer to function */
25592 int argcount, /* nr of args */
25593 typval_T *argvars, /* arguments */
25594 typval_T *rettv, /* return value */
25595 linenr_T firstline, /* first line of range */
25596 linenr_T lastline, /* last line of range */
25597 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025598{
Bram Moolenaar33570922005-01-25 22:26:29 +000025599 char_u *save_sourcing_name;
25600 linenr_T save_sourcing_lnum;
25601 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025602 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025603 int save_did_emsg;
25604 static int depth = 0;
25605 dictitem_T *v;
25606 int fixvar_idx = 0; /* index in fixvar[] */
25607 int i;
25608 int ai;
25609 char_u numbuf[NUMBUFLEN];
25610 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025611 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025612#ifdef FEAT_PROFILE
25613 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025614 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025616
25617 /* If depth of calling is getting too high, don't execute the function */
25618 if (depth >= p_mfd)
25619 {
25620 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025621 rettv->v_type = VAR_NUMBER;
25622 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025623 return;
25624 }
25625 ++depth;
25626
25627 line_breakcheck(); /* check for CTRL-C hit */
25628
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025629 fc = (funccall_T *)alloc(sizeof(funccall_T));
25630 fc->caller = current_funccal;
25631 current_funccal = fc;
25632 fc->func = fp;
25633 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025634 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025635 fc->linenr = 0;
25636 fc->returned = FALSE;
25637 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025638 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025639 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25640 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025641
Bram Moolenaar33570922005-01-25 22:26:29 +000025642 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025643 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025644 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25645 * each argument variable and saves a lot of time.
25646 */
25647 /*
25648 * Init l: variables.
25649 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025650 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025651 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025652 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025653 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25654 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025655 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025656 name = v->di_key;
25657 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025658 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025659 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025660 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025661 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025662 v->di_tv.vval.v_dict = selfdict;
25663 ++selfdict->dv_refcount;
25664 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025665
Bram Moolenaar33570922005-01-25 22:26:29 +000025666 /*
25667 * Init a: variables.
25668 * Set a:0 to "argcount".
25669 * Set a:000 to a list with room for the "..." arguments.
25670 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025671 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025672 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025673 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025674 /* Use "name" to avoid a warning from some compiler that checks the
25675 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025676 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025677 name = v->di_key;
25678 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025679 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025680 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025681 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025682 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025683 v->di_tv.vval.v_list = &fc->l_varlist;
25684 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25685 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25686 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025687
25688 /*
25689 * Set a:firstline to "firstline" and a:lastline to "lastline".
25690 * Set a:name to named arguments.
25691 * Set a:N to the "..." arguments.
25692 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025693 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025694 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025695 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025696 (varnumber_T)lastline);
25697 for (i = 0; i < argcount; ++i)
25698 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025699 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025700 if (ai < 0)
25701 /* named argument a:name */
25702 name = FUNCARG(fp, i);
25703 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025704 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025705 /* "..." argument a:1, a:2, etc. */
25706 sprintf((char *)numbuf, "%d", ai + 1);
25707 name = numbuf;
25708 }
25709 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25710 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025711 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025712 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25713 }
25714 else
25715 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025716 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25717 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025718 if (v == NULL)
25719 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025720 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025721 }
25722 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025723 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025724
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025725 /* Note: the values are copied directly to avoid alloc/free.
25726 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025727 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025728 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025729
25730 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25731 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025732 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25733 fc->l_listitems[ai].li_tv = argvars[i];
25734 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025735 }
25736 }
25737
Bram Moolenaar071d4272004-06-13 20:20:40 +000025738 /* Don't redraw while executing the function. */
25739 ++RedrawingDisabled;
25740 save_sourcing_name = sourcing_name;
25741 save_sourcing_lnum = sourcing_lnum;
25742 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025743 /* need space for function name + ("function " + 3) or "[number]" */
25744 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25745 + STRLEN(fp->uf_name) + 20;
25746 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025747 if (sourcing_name != NULL)
25748 {
25749 if (save_sourcing_name != NULL
25750 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025751 sprintf((char *)sourcing_name, "%s[%d]..",
25752 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025753 else
25754 STRCPY(sourcing_name, "function ");
25755 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25756
25757 if (p_verbose >= 12)
25758 {
25759 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025760 verbose_enter_scroll();
25761
Bram Moolenaar555b2802005-05-19 21:08:39 +000025762 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025763 if (p_verbose >= 14)
25764 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025765 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025766 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025767 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025768 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025769
25770 msg_puts((char_u *)"(");
25771 for (i = 0; i < argcount; ++i)
25772 {
25773 if (i > 0)
25774 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025775 if (argvars[i].v_type == VAR_NUMBER)
25776 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025777 else
25778 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025779 /* Do not want errors such as E724 here. */
25780 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025781 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025782 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025783 if (s != NULL)
25784 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025785 if (vim_strsize(s) > MSG_BUF_CLEN)
25786 {
25787 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25788 s = buf;
25789 }
25790 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025791 vim_free(tofree);
25792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025793 }
25794 }
25795 msg_puts((char_u *)")");
25796 }
25797 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025798
25799 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025800 --no_wait_return;
25801 }
25802 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025803#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025804 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025805 {
25806 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25807 func_do_profile(fp);
25808 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025809 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025810 {
25811 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025812 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025813 profile_zero(&fp->uf_tm_children);
25814 }
25815 script_prof_save(&wait_start);
25816 }
25817#endif
25818
Bram Moolenaar071d4272004-06-13 20:20:40 +000025819 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025820 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025821 save_did_emsg = did_emsg;
25822 did_emsg = FALSE;
25823
25824 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025825 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025826 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25827
25828 --RedrawingDisabled;
25829
25830 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025831 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025832 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025833 clear_tv(rettv);
25834 rettv->v_type = VAR_NUMBER;
25835 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025836 }
25837
Bram Moolenaar05159a02005-02-26 23:04:13 +000025838#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025839 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025840 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025841 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025842 profile_end(&call_start);
25843 profile_sub_wait(&wait_start, &call_start);
25844 profile_add(&fp->uf_tm_total, &call_start);
25845 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025846 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025847 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025848 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25849 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025850 }
25851 }
25852#endif
25853
Bram Moolenaar071d4272004-06-13 20:20:40 +000025854 /* when being verbose, mention the return value */
25855 if (p_verbose >= 12)
25856 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025857 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025858 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025859
Bram Moolenaar071d4272004-06-13 20:20:40 +000025860 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025861 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025862 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025863 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025864 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025865 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025866 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025867 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025868 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025869 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025870 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025871
Bram Moolenaar555b2802005-05-19 21:08:39 +000025872 /* The value may be very long. Skip the middle part, so that we
25873 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025874 * truncate it at the end. Don't want errors such as E724 here. */
25875 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025876 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025877 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025878 if (s != NULL)
25879 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025880 if (vim_strsize(s) > MSG_BUF_CLEN)
25881 {
25882 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25883 s = buf;
25884 }
25885 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025886 vim_free(tofree);
25887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025888 }
25889 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025890
25891 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025892 --no_wait_return;
25893 }
25894
25895 vim_free(sourcing_name);
25896 sourcing_name = save_sourcing_name;
25897 sourcing_lnum = save_sourcing_lnum;
25898 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025899#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025900 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025901 script_prof_restore(&wait_start);
25902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025903
25904 if (p_verbose >= 12 && sourcing_name != NULL)
25905 {
25906 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025907 verbose_enter_scroll();
25908
Bram Moolenaar555b2802005-05-19 21:08:39 +000025909 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025910 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025911
25912 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025913 --no_wait_return;
25914 }
25915
25916 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025917 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025918 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025919
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025920 /* If the a:000 list and the l: and a: dicts are not referenced we can
25921 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025922 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25923 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25924 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25925 {
25926 free_funccal(fc, FALSE);
25927 }
25928 else
25929 {
25930 hashitem_T *hi;
25931 listitem_T *li;
25932 int todo;
25933
25934 /* "fc" is still in use. This can happen when returning "a:000" or
25935 * assigning "l:" to a global variable.
25936 * Link "fc" in the list for garbage collection later. */
25937 fc->caller = previous_funccal;
25938 previous_funccal = fc;
25939
25940 /* Make a copy of the a: variables, since we didn't do that above. */
25941 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25942 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25943 {
25944 if (!HASHITEM_EMPTY(hi))
25945 {
25946 --todo;
25947 v = HI2DI(hi);
25948 copy_tv(&v->di_tv, &v->di_tv);
25949 }
25950 }
25951
25952 /* Make a copy of the a:000 items, since we didn't do that above. */
25953 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25954 copy_tv(&li->li_tv, &li->li_tv);
25955 }
25956}
25957
25958/*
25959 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025960 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025961 */
25962 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025963can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025964{
25965 return (fc->l_varlist.lv_copyID != copyID
25966 && fc->l_vars.dv_copyID != copyID
25967 && fc->l_avars.dv_copyID != copyID);
25968}
25969
25970/*
25971 * Free "fc" and what it contains.
25972 */
25973 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025974free_funccal(
25975 funccall_T *fc,
25976 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025977{
25978 listitem_T *li;
25979
25980 /* The a: variables typevals may not have been allocated, only free the
25981 * allocated variables. */
25982 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25983
25984 /* free all l: variables */
25985 vars_clear(&fc->l_vars.dv_hashtab);
25986
25987 /* Free the a:000 variables if they were allocated. */
25988 if (free_val)
25989 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25990 clear_tv(&li->li_tv);
25991
25992 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025993}
25994
25995/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025996 * Add a number variable "name" to dict "dp" with value "nr".
25997 */
25998 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025999add_nr_var(
26000 dict_T *dp,
26001 dictitem_T *v,
26002 char *name,
26003 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000026004{
26005 STRCPY(v->di_key, name);
26006 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
26007 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
26008 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000026009 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000026010 v->di_tv.vval.v_number = nr;
26011}
26012
26013/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000026014 * ":return [expr]"
26015 */
26016 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026017ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026018{
26019 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000026020 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026021 int returning = FALSE;
26022
26023 if (current_funccal == NULL)
26024 {
26025 EMSG(_("E133: :return not inside a function"));
26026 return;
26027 }
26028
26029 if (eap->skip)
26030 ++emsg_skip;
26031
26032 eap->nextcmd = NULL;
26033 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026034 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026035 {
26036 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026037 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026038 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026039 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026040 }
26041 /* It's safer to return also on error. */
26042 else if (!eap->skip)
26043 {
26044 /*
26045 * Return unless the expression evaluation has been cancelled due to an
26046 * aborting error, an interrupt, or an exception.
26047 */
26048 if (!aborting())
26049 returning = do_return(eap, FALSE, TRUE, NULL);
26050 }
26051
26052 /* When skipping or the return gets pending, advance to the next command
26053 * in this line (!returning). Otherwise, ignore the rest of the line.
26054 * Following lines will be ignored by get_func_line(). */
26055 if (returning)
26056 eap->nextcmd = NULL;
26057 else if (eap->nextcmd == NULL) /* no argument */
26058 eap->nextcmd = check_nextcmd(arg);
26059
26060 if (eap->skip)
26061 --emsg_skip;
26062}
26063
26064/*
26065 * Return from a function. Possibly makes the return pending. Also called
26066 * for a pending return at the ":endtry" or after returning from an extra
26067 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000026068 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026069 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026070 * FALSE when the return gets pending.
26071 */
26072 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026073do_return(
26074 exarg_T *eap,
26075 int reanimate,
26076 int is_cmd,
26077 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026078{
26079 int idx;
26080 struct condstack *cstack = eap->cstack;
26081
26082 if (reanimate)
26083 /* Undo the return. */
26084 current_funccal->returned = FALSE;
26085
26086 /*
26087 * Cleanup (and inactivate) conditionals, but stop when a try conditional
26088 * not in its finally clause (which then is to be executed next) is found.
26089 * In this case, make the ":return" pending for execution at the ":endtry".
26090 * Otherwise, return normally.
26091 */
26092 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
26093 if (idx >= 0)
26094 {
26095 cstack->cs_pending[idx] = CSTP_RETURN;
26096
26097 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026098 /* A pending return again gets pending. "rettv" points to an
26099 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000026100 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026101 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026102 else
26103 {
26104 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026105 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026106 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026107 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026108
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026109 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026110 {
26111 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026112 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000026113 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026114 else
26115 EMSG(_(e_outofmem));
26116 }
26117 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026118 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026119
26120 if (reanimate)
26121 {
26122 /* The pending return value could be overwritten by a ":return"
26123 * without argument in a finally clause; reset the default
26124 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026125 current_funccal->rettv->v_type = VAR_NUMBER;
26126 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026127 }
26128 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026129 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026130 }
26131 else
26132 {
26133 current_funccal->returned = TRUE;
26134
26135 /* If the return is carried out now, store the return value. For
26136 * a return immediately after reanimation, the value is already
26137 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026138 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026139 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026140 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000026141 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026142 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026143 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026144 }
26145 }
26146
26147 return idx < 0;
26148}
26149
26150/*
26151 * Free the variable with a pending return value.
26152 */
26153 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026154discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026155{
Bram Moolenaar33570922005-01-25 22:26:29 +000026156 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026157}
26158
26159/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026160 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000026161 * is an allocated string. Used by report_pending() for verbose messages.
26162 */
26163 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026164get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026165{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026166 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026167 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026168 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026169
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026170 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026171 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026172 if (s == NULL)
26173 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026174
26175 STRCPY(IObuff, ":return ");
26176 STRNCPY(IObuff + 8, s, IOSIZE - 8);
26177 if (STRLEN(s) + 8 >= IOSIZE)
26178 STRCPY(IObuff + IOSIZE - 4, "...");
26179 vim_free(tofree);
26180 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026181}
26182
26183/*
26184 * Get next function line.
26185 * Called by do_cmdline() to get the next line.
26186 * Returns allocated string, or NULL for end of function.
26187 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026188 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026189get_func_line(
26190 int c UNUSED,
26191 void *cookie,
26192 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026193{
Bram Moolenaar33570922005-01-25 22:26:29 +000026194 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026195 ufunc_T *fp = fcp->func;
26196 char_u *retval;
26197 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026198
26199 /* If breakpoints have been added/deleted need to check for it. */
26200 if (fcp->dbg_tick != debug_tick)
26201 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000026202 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026203 sourcing_lnum);
26204 fcp->dbg_tick = debug_tick;
26205 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000026206#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000026207 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026208 func_line_end(cookie);
26209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026210
Bram Moolenaar05159a02005-02-26 23:04:13 +000026211 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026212 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
26213 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026214 retval = NULL;
26215 else
26216 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026217 /* Skip NULL lines (continuation lines). */
26218 while (fcp->linenr < gap->ga_len
26219 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
26220 ++fcp->linenr;
26221 if (fcp->linenr >= gap->ga_len)
26222 retval = NULL;
26223 else
26224 {
26225 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
26226 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026227#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000026228 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026229 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026230#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026232 }
26233
26234 /* Did we encounter a breakpoint? */
26235 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
26236 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000026237 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026238 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000026239 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026240 sourcing_lnum);
26241 fcp->dbg_tick = debug_tick;
26242 }
26243
26244 return retval;
26245}
26246
Bram Moolenaar05159a02005-02-26 23:04:13 +000026247#if defined(FEAT_PROFILE) || defined(PROTO)
26248/*
26249 * Called when starting to read a function line.
26250 * "sourcing_lnum" must be correct!
26251 * When skipping lines it may not actually be executed, but we won't find out
26252 * until later and we need to store the time now.
26253 */
26254 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026255func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026256{
26257 funccall_T *fcp = (funccall_T *)cookie;
26258 ufunc_T *fp = fcp->func;
26259
26260 if (fp->uf_profiling && sourcing_lnum >= 1
26261 && sourcing_lnum <= fp->uf_lines.ga_len)
26262 {
26263 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026264 /* Skip continuation lines. */
26265 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
26266 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026267 fp->uf_tml_execed = FALSE;
26268 profile_start(&fp->uf_tml_start);
26269 profile_zero(&fp->uf_tml_children);
26270 profile_get_wait(&fp->uf_tml_wait);
26271 }
26272}
26273
26274/*
26275 * Called when actually executing a function line.
26276 */
26277 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026278func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026279{
26280 funccall_T *fcp = (funccall_T *)cookie;
26281 ufunc_T *fp = fcp->func;
26282
26283 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26284 fp->uf_tml_execed = TRUE;
26285}
26286
26287/*
26288 * Called when done with a function line.
26289 */
26290 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026291func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026292{
26293 funccall_T *fcp = (funccall_T *)cookie;
26294 ufunc_T *fp = fcp->func;
26295
26296 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26297 {
26298 if (fp->uf_tml_execed)
26299 {
26300 ++fp->uf_tml_count[fp->uf_tml_idx];
26301 profile_end(&fp->uf_tml_start);
26302 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026303 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000026304 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
26305 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026306 }
26307 fp->uf_tml_idx = -1;
26308 }
26309}
26310#endif
26311
Bram Moolenaar071d4272004-06-13 20:20:40 +000026312/*
26313 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026314 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000026315 */
26316 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026317func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026318{
Bram Moolenaar33570922005-01-25 22:26:29 +000026319 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026320
26321 /* Ignore the "abort" flag if the abortion behavior has been changed due to
26322 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026323 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000026324 || fcp->returned);
26325}
26326
26327/*
26328 * return TRUE if cookie indicates a function which "abort"s on errors.
26329 */
26330 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026331func_has_abort(
26332 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026333{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026334 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026335}
26336
26337#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
26338typedef enum
26339{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026340 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
26341 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
26342 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026343} var_flavour_T;
26344
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026345static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026346
26347 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010026348var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026349{
26350 char_u *p = varname;
26351
26352 if (ASCII_ISUPPER(*p))
26353 {
26354 while (*(++p))
26355 if (ASCII_ISLOWER(*p))
26356 return VAR_FLAVOUR_SESSION;
26357 return VAR_FLAVOUR_VIMINFO;
26358 }
26359 else
26360 return VAR_FLAVOUR_DEFAULT;
26361}
26362#endif
26363
26364#if defined(FEAT_VIMINFO) || defined(PROTO)
26365/*
26366 * Restore global vars that start with a capital from the viminfo file
26367 */
26368 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026369read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026370{
26371 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026372 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000026373 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026374 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026375
26376 if (!writing && (find_viminfo_parameter('!') != NULL))
26377 {
26378 tab = vim_strchr(virp->vir_line + 1, '\t');
26379 if (tab != NULL)
26380 {
26381 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026382 switch (*tab)
26383 {
26384 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026385#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026386 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026387#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026388 case 'D': type = VAR_DICT; break;
26389 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026390 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026392
26393 tab = vim_strchr(tab, '\t');
26394 if (tab != NULL)
26395 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026396 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026397 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026398 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026399 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026400#ifdef FEAT_FLOAT
26401 else if (type == VAR_FLOAT)
26402 (void)string2float(tab + 1, &tv.vval.v_float);
26403#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026404 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026405 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026406 if (type == VAR_DICT || type == VAR_LIST)
26407 {
26408 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
26409
26410 if (etv == NULL)
26411 /* Failed to parse back the dict or list, use it as a
26412 * string. */
26413 tv.v_type = VAR_STRING;
26414 else
26415 {
26416 vim_free(tv.vval.v_string);
26417 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010026418 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026419 }
26420 }
26421
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026422 /* when in a function use global variables */
26423 save_funccal = current_funccal;
26424 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026425 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026426 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026427
26428 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026429 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026430 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
26431 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026432 }
26433 }
26434 }
26435
26436 return viminfo_readline(virp);
26437}
26438
26439/*
26440 * Write global vars that start with a capital to the viminfo file
26441 */
26442 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026443write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026444{
Bram Moolenaar33570922005-01-25 22:26:29 +000026445 hashitem_T *hi;
26446 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026447 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010026448 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026449 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026450 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026451 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026452
26453 if (find_viminfo_parameter('!') == NULL)
26454 return;
26455
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020026456 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000026457
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026458 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026459 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026460 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026461 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026462 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026463 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026464 this_var = HI2DI(hi);
26465 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026466 {
Bram Moolenaar33570922005-01-25 22:26:29 +000026467 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000026468 {
26469 case VAR_STRING: s = "STR"; break;
26470 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026471 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026472 case VAR_DICT: s = "DIC"; break;
26473 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026474 case VAR_SPECIAL: s = "XPL"; break;
26475
26476 case VAR_UNKNOWN:
26477 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010026478 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010026479 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010026480 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010026481 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000026482 }
Bram Moolenaar33570922005-01-25 22:26:29 +000026483 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026484 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026485 if (p != NULL)
26486 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000026487 vim_free(tofree);
26488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026489 }
26490 }
26491}
26492#endif
26493
26494#if defined(FEAT_SESSION) || defined(PROTO)
26495 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026496store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026497{
Bram Moolenaar33570922005-01-25 22:26:29 +000026498 hashitem_T *hi;
26499 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026500 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026501 char_u *p, *t;
26502
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026503 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026504 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026505 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026506 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026507 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026508 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026509 this_var = HI2DI(hi);
26510 if ((this_var->di_tv.v_type == VAR_NUMBER
26511 || this_var->di_tv.v_type == VAR_STRING)
26512 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026513 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026514 /* Escape special characters with a backslash. Turn a LF and
26515 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000026516 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000026517 (char_u *)"\\\"\n\r");
26518 if (p == NULL) /* out of memory */
26519 break;
26520 for (t = p; *t != NUL; ++t)
26521 if (*t == '\n')
26522 *t = 'n';
26523 else if (*t == '\r')
26524 *t = 'r';
26525 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000026526 this_var->di_key,
26527 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26528 : ' ',
26529 p,
26530 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26531 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000026532 || put_eol(fd) == FAIL)
26533 {
26534 vim_free(p);
26535 return FAIL;
26536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026537 vim_free(p);
26538 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026539#ifdef FEAT_FLOAT
26540 else if (this_var->di_tv.v_type == VAR_FLOAT
26541 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
26542 {
26543 float_T f = this_var->di_tv.vval.v_float;
26544 int sign = ' ';
26545
26546 if (f < 0)
26547 {
26548 f = -f;
26549 sign = '-';
26550 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010026551 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026552 this_var->di_key, sign, f) < 0)
26553 || put_eol(fd) == FAIL)
26554 return FAIL;
26555 }
26556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026557 }
26558 }
26559 return OK;
26560}
26561#endif
26562
Bram Moolenaar661b1822005-07-28 22:36:45 +000026563/*
26564 * Display script name where an item was last set.
26565 * Should only be invoked when 'verbose' is non-zero.
26566 */
26567 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026568last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000026569{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026570 char_u *p;
26571
Bram Moolenaar661b1822005-07-28 22:36:45 +000026572 if (scriptID != 0)
26573 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026574 p = home_replace_save(NULL, get_scriptname(scriptID));
26575 if (p != NULL)
26576 {
26577 verbose_enter();
26578 MSG_PUTS(_("\n\tLast set from "));
26579 MSG_PUTS(p);
26580 vim_free(p);
26581 verbose_leave();
26582 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026583 }
26584}
26585
Bram Moolenaard812df62008-11-09 12:46:09 +000026586/*
26587 * List v:oldfiles in a nice way.
26588 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026589 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026590ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026591{
26592 list_T *l = vimvars[VV_OLDFILES].vv_list;
26593 listitem_T *li;
26594 int nr = 0;
26595
26596 if (l == NULL)
26597 msg((char_u *)_("No old files"));
26598 else
26599 {
26600 msg_start();
26601 msg_scroll = TRUE;
26602 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26603 {
26604 msg_outnum((long)++nr);
26605 MSG_PUTS(": ");
26606 msg_outtrans(get_tv_string(&li->li_tv));
26607 msg_putchar('\n');
26608 out_flush(); /* output one line at a time */
26609 ui_breakcheck();
26610 }
26611 /* Assume "got_int" was set to truncate the listing. */
26612 got_int = FALSE;
26613
26614#ifdef FEAT_BROWSE_CMD
26615 if (cmdmod.browse)
26616 {
26617 quit_more = FALSE;
26618 nr = prompt_for_number(FALSE);
26619 msg_starthere();
26620 if (nr > 0)
26621 {
26622 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26623 (long)nr);
26624
26625 if (p != NULL)
26626 {
26627 p = expand_env_save(p);
26628 eap->arg = p;
26629 eap->cmdidx = CMD_edit;
26630 cmdmod.browse = FALSE;
26631 do_exedit(eap, NULL);
26632 vim_free(p);
26633 }
26634 }
26635 }
26636#endif
26637 }
26638}
26639
Bram Moolenaar53744302015-07-17 17:38:22 +020026640/* reset v:option_new, v:option_old and v:option_type */
26641 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026642reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026643{
26644 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26645 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26646 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26647}
26648
26649
Bram Moolenaar071d4272004-06-13 20:20:40 +000026650#endif /* FEAT_EVAL */
26651
Bram Moolenaar071d4272004-06-13 20:20:40 +000026652
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026653#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026654
26655#ifdef WIN3264
26656/*
26657 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26658 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026659static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26660static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26661static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026662
26663/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026664 * Get the short path (8.3) for the filename in "fnamep".
26665 * Only works for a valid file name.
26666 * When the path gets longer "fnamep" is changed and the allocated buffer
26667 * is put in "bufp".
26668 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26669 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026670 */
26671 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026672get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026673{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026674 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026675 char_u *newbuf;
26676
26677 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026678 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026679 if (l > len - 1)
26680 {
26681 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026682 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026683 newbuf = vim_strnsave(*fnamep, l);
26684 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026685 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026686
26687 vim_free(*bufp);
26688 *fnamep = *bufp = newbuf;
26689
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026690 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026691 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026692 }
26693
26694 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026695 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026696}
26697
26698/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026699 * Get the short path (8.3) for the filename in "fname". The converted
26700 * path is returned in "bufp".
26701 *
26702 * Some of the directories specified in "fname" may not exist. This function
26703 * will shorten the existing directories at the beginning of the path and then
26704 * append the remaining non-existing path.
26705 *
26706 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026707 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026708 * bufp - Pointer to an allocated buffer for the filename.
26709 * fnamelen - Length of the filename pointed to by fname
26710 *
26711 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026712 */
26713 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026714shortpath_for_invalid_fname(
26715 char_u **fname,
26716 char_u **bufp,
26717 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026718{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026719 char_u *short_fname, *save_fname, *pbuf_unused;
26720 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026721 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026722 int old_len, len;
26723 int new_len, sfx_len;
26724 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026725
26726 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026727 old_len = *fnamelen;
26728 save_fname = vim_strnsave(*fname, old_len);
26729 pbuf_unused = NULL;
26730 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026731
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026732 endp = save_fname + old_len - 1; /* Find the end of the copy */
26733 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026734
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026735 /*
26736 * Try shortening the supplied path till it succeeds by removing one
26737 * directory at a time from the tail of the path.
26738 */
26739 len = 0;
26740 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026741 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026742 /* go back one path-separator */
26743 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26744 --endp;
26745 if (endp <= save_fname)
26746 break; /* processed the complete path */
26747
26748 /*
26749 * Replace the path separator with a NUL and try to shorten the
26750 * resulting path.
26751 */
26752 ch = *endp;
26753 *endp = 0;
26754 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026755 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026756 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26757 {
26758 retval = FAIL;
26759 goto theend;
26760 }
26761 *endp = ch; /* preserve the string */
26762
26763 if (len > 0)
26764 break; /* successfully shortened the path */
26765
26766 /* failed to shorten the path. Skip the path separator */
26767 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026768 }
26769
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026770 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026771 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026772 /*
26773 * Succeeded in shortening the path. Now concatenate the shortened
26774 * path with the remaining path at the tail.
26775 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026776
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026777 /* Compute the length of the new path. */
26778 sfx_len = (int)(save_endp - endp) + 1;
26779 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026780
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026781 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026782 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026783 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026784 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026785 /* There is not enough space in the currently allocated string,
26786 * copy it to a buffer big enough. */
26787 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026788 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026789 {
26790 retval = FAIL;
26791 goto theend;
26792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026793 }
26794 else
26795 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026796 /* Transfer short_fname to the main buffer (it's big enough),
26797 * unless get_short_pathname() did its work in-place. */
26798 *fname = *bufp = save_fname;
26799 if (short_fname != save_fname)
26800 vim_strncpy(save_fname, short_fname, len);
26801 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026802 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026803
26804 /* concat the not-shortened part of the path */
26805 vim_strncpy(*fname + len, endp, sfx_len);
26806 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026807 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026808
26809theend:
26810 vim_free(pbuf_unused);
26811 vim_free(save_fname);
26812
26813 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026814}
26815
26816/*
26817 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026818 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026819 */
26820 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026821shortpath_for_partial(
26822 char_u **fnamep,
26823 char_u **bufp,
26824 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026825{
26826 int sepcount, len, tflen;
26827 char_u *p;
26828 char_u *pbuf, *tfname;
26829 int hasTilde;
26830
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026831 /* Count up the path separators from the RHS.. so we know which part
26832 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026833 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026834 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026835 if (vim_ispathsep(*p))
26836 ++sepcount;
26837
26838 /* Need full path first (use expand_env() to remove a "~/") */
26839 hasTilde = (**fnamep == '~');
26840 if (hasTilde)
26841 pbuf = tfname = expand_env_save(*fnamep);
26842 else
26843 pbuf = tfname = FullName_save(*fnamep, FALSE);
26844
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026845 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026846
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026847 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26848 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026849
26850 if (len == 0)
26851 {
26852 /* Don't have a valid filename, so shorten the rest of the
26853 * path if we can. This CAN give us invalid 8.3 filenames, but
26854 * there's not a lot of point in guessing what it might be.
26855 */
26856 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026857 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26858 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026859 }
26860
26861 /* Count the paths backward to find the beginning of the desired string. */
26862 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026863 {
26864#ifdef FEAT_MBYTE
26865 if (has_mbyte)
26866 p -= mb_head_off(tfname, p);
26867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026868 if (vim_ispathsep(*p))
26869 {
26870 if (sepcount == 0 || (hasTilde && sepcount == 1))
26871 break;
26872 else
26873 sepcount --;
26874 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026876 if (hasTilde)
26877 {
26878 --p;
26879 if (p >= tfname)
26880 *p = '~';
26881 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026882 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026883 }
26884 else
26885 ++p;
26886
26887 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26888 vim_free(*bufp);
26889 *fnamelen = (int)STRLEN(p);
26890 *bufp = pbuf;
26891 *fnamep = p;
26892
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026893 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026894}
26895#endif /* WIN3264 */
26896
26897/*
26898 * Adjust a filename, according to a string of modifiers.
26899 * *fnamep must be NUL terminated when called. When returning, the length is
26900 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026901 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026902 * When there is an error, *fnamep is set to NULL.
26903 */
26904 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026905modify_fname(
26906 char_u *src, /* string with modifiers */
26907 int *usedlen, /* characters after src that are used */
26908 char_u **fnamep, /* file name so far */
26909 char_u **bufp, /* buffer for allocated file name or NULL */
26910 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026911{
26912 int valid = 0;
26913 char_u *tail;
26914 char_u *s, *p, *pbuf;
26915 char_u dirname[MAXPATHL];
26916 int c;
26917 int has_fullname = 0;
26918#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026919 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026920 int has_shortname = 0;
26921#endif
26922
26923repeat:
26924 /* ":p" - full path/file_name */
26925 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26926 {
26927 has_fullname = 1;
26928
26929 valid |= VALID_PATH;
26930 *usedlen += 2;
26931
26932 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26933 if ((*fnamep)[0] == '~'
26934#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26935 && ((*fnamep)[1] == '/'
26936# ifdef BACKSLASH_IN_FILENAME
26937 || (*fnamep)[1] == '\\'
26938# endif
26939 || (*fnamep)[1] == NUL)
26940
26941#endif
26942 )
26943 {
26944 *fnamep = expand_env_save(*fnamep);
26945 vim_free(*bufp); /* free any allocated file name */
26946 *bufp = *fnamep;
26947 if (*fnamep == NULL)
26948 return -1;
26949 }
26950
26951 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026952 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026953 {
26954 if (vim_ispathsep(*p)
26955 && p[1] == '.'
26956 && (p[2] == NUL
26957 || vim_ispathsep(p[2])
26958 || (p[2] == '.'
26959 && (p[3] == NUL || vim_ispathsep(p[3])))))
26960 break;
26961 }
26962
26963 /* FullName_save() is slow, don't use it when not needed. */
26964 if (*p != NUL || !vim_isAbsName(*fnamep))
26965 {
26966 *fnamep = FullName_save(*fnamep, *p != NUL);
26967 vim_free(*bufp); /* free any allocated file name */
26968 *bufp = *fnamep;
26969 if (*fnamep == NULL)
26970 return -1;
26971 }
26972
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026973#ifdef WIN3264
26974# if _WIN32_WINNT >= 0x0500
26975 if (vim_strchr(*fnamep, '~') != NULL)
26976 {
26977 /* Expand 8.3 filename to full path. Needed to make sure the same
26978 * file does not have two different names.
26979 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26980 p = alloc(_MAX_PATH + 1);
26981 if (p != NULL)
26982 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026983 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026984 {
26985 vim_free(*bufp);
26986 *bufp = *fnamep = p;
26987 }
26988 else
26989 vim_free(p);
26990 }
26991 }
26992# endif
26993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026994 /* Append a path separator to a directory. */
26995 if (mch_isdir(*fnamep))
26996 {
26997 /* Make room for one or two extra characters. */
26998 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26999 vim_free(*bufp); /* free any allocated file name */
27000 *bufp = *fnamep;
27001 if (*fnamep == NULL)
27002 return -1;
27003 add_pathsep(*fnamep);
27004 }
27005 }
27006
27007 /* ":." - path relative to the current directory */
27008 /* ":~" - path relative to the home directory */
27009 /* ":8" - shortname path - postponed till after */
27010 while (src[*usedlen] == ':'
27011 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
27012 {
27013 *usedlen += 2;
27014 if (c == '8')
27015 {
27016#ifdef WIN3264
27017 has_shortname = 1; /* Postpone this. */
27018#endif
27019 continue;
27020 }
27021 pbuf = NULL;
27022 /* Need full path first (use expand_env() to remove a "~/") */
27023 if (!has_fullname)
27024 {
27025 if (c == '.' && **fnamep == '~')
27026 p = pbuf = expand_env_save(*fnamep);
27027 else
27028 p = pbuf = FullName_save(*fnamep, FALSE);
27029 }
27030 else
27031 p = *fnamep;
27032
27033 has_fullname = 0;
27034
27035 if (p != NULL)
27036 {
27037 if (c == '.')
27038 {
27039 mch_dirname(dirname, MAXPATHL);
27040 s = shorten_fname(p, dirname);
27041 if (s != NULL)
27042 {
27043 *fnamep = s;
27044 if (pbuf != NULL)
27045 {
27046 vim_free(*bufp); /* free any allocated file name */
27047 *bufp = pbuf;
27048 pbuf = NULL;
27049 }
27050 }
27051 }
27052 else
27053 {
27054 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
27055 /* Only replace it when it starts with '~' */
27056 if (*dirname == '~')
27057 {
27058 s = vim_strsave(dirname);
27059 if (s != NULL)
27060 {
27061 *fnamep = s;
27062 vim_free(*bufp);
27063 *bufp = s;
27064 }
27065 }
27066 }
27067 vim_free(pbuf);
27068 }
27069 }
27070
27071 tail = gettail(*fnamep);
27072 *fnamelen = (int)STRLEN(*fnamep);
27073
27074 /* ":h" - head, remove "/file_name", can be repeated */
27075 /* Don't remove the first "/" or "c:\" */
27076 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
27077 {
27078 valid |= VALID_HEAD;
27079 *usedlen += 2;
27080 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000027081 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000027082 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027083 *fnamelen = (int)(tail - *fnamep);
27084#ifdef VMS
27085 if (*fnamelen > 0)
27086 *fnamelen += 1; /* the path separator is part of the path */
27087#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000027088 if (*fnamelen == 0)
27089 {
27090 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
27091 p = vim_strsave((char_u *)".");
27092 if (p == NULL)
27093 return -1;
27094 vim_free(*bufp);
27095 *bufp = *fnamep = tail = p;
27096 *fnamelen = 1;
27097 }
27098 else
27099 {
27100 while (tail > s && !after_pathsep(s, tail))
27101 mb_ptr_back(*fnamep, tail);
27102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000027103 }
27104
27105 /* ":8" - shortname */
27106 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
27107 {
27108 *usedlen += 2;
27109#ifdef WIN3264
27110 has_shortname = 1;
27111#endif
27112 }
27113
27114#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020027115 /*
27116 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027117 */
27118 if (has_shortname)
27119 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027120 /* Copy the string if it is shortened by :h and when it wasn't copied
27121 * yet, because we are going to change it in place. Avoids changing
27122 * the buffer name for "%:8". */
27123 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027124 {
27125 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020027126 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027127 return -1;
27128 vim_free(*bufp);
27129 *bufp = *fnamep = p;
27130 }
27131
27132 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020027133 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000027134 if (!has_fullname && !vim_isAbsName(*fnamep))
27135 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027136 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027137 return -1;
27138 }
27139 else
27140 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027141 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027142
Bram Moolenaardc935552011-08-17 15:23:23 +020027143 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027144 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027145 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027146 return -1;
27147
27148 if (l == 0)
27149 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027150 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000027151 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027152 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027153 return -1;
27154 }
27155 *fnamelen = l;
27156 }
27157 }
27158#endif /* WIN3264 */
27159
27160 /* ":t" - tail, just the basename */
27161 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
27162 {
27163 *usedlen += 2;
27164 *fnamelen -= (int)(tail - *fnamep);
27165 *fnamep = tail;
27166 }
27167
27168 /* ":e" - extension, can be repeated */
27169 /* ":r" - root, without extension, can be repeated */
27170 while (src[*usedlen] == ':'
27171 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
27172 {
27173 /* find a '.' in the tail:
27174 * - for second :e: before the current fname
27175 * - otherwise: The last '.'
27176 */
27177 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
27178 s = *fnamep - 2;
27179 else
27180 s = *fnamep + *fnamelen - 1;
27181 for ( ; s > tail; --s)
27182 if (s[0] == '.')
27183 break;
27184 if (src[*usedlen + 1] == 'e') /* :e */
27185 {
27186 if (s > tail)
27187 {
27188 *fnamelen += (int)(*fnamep - (s + 1));
27189 *fnamep = s + 1;
27190#ifdef VMS
27191 /* cut version from the extension */
27192 s = *fnamep + *fnamelen - 1;
27193 for ( ; s > *fnamep; --s)
27194 if (s[0] == ';')
27195 break;
27196 if (s > *fnamep)
27197 *fnamelen = s - *fnamep;
27198#endif
27199 }
27200 else if (*fnamep <= tail)
27201 *fnamelen = 0;
27202 }
27203 else /* :r */
27204 {
27205 if (s > tail) /* remove one extension */
27206 *fnamelen = (int)(s - *fnamep);
27207 }
27208 *usedlen += 2;
27209 }
27210
27211 /* ":s?pat?foo?" - substitute */
27212 /* ":gs?pat?foo?" - global substitute */
27213 if (src[*usedlen] == ':'
27214 && (src[*usedlen + 1] == 's'
27215 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
27216 {
27217 char_u *str;
27218 char_u *pat;
27219 char_u *sub;
27220 int sep;
27221 char_u *flags;
27222 int didit = FALSE;
27223
27224 flags = (char_u *)"";
27225 s = src + *usedlen + 2;
27226 if (src[*usedlen + 1] == 'g')
27227 {
27228 flags = (char_u *)"g";
27229 ++s;
27230 }
27231
27232 sep = *s++;
27233 if (sep)
27234 {
27235 /* find end of pattern */
27236 p = vim_strchr(s, sep);
27237 if (p != NULL)
27238 {
27239 pat = vim_strnsave(s, (int)(p - s));
27240 if (pat != NULL)
27241 {
27242 s = p + 1;
27243 /* find end of substitution */
27244 p = vim_strchr(s, sep);
27245 if (p != NULL)
27246 {
27247 sub = vim_strnsave(s, (int)(p - s));
27248 str = vim_strnsave(*fnamep, *fnamelen);
27249 if (sub != NULL && str != NULL)
27250 {
27251 *usedlen = (int)(p + 1 - src);
27252 s = do_string_sub(str, pat, sub, flags);
27253 if (s != NULL)
27254 {
27255 *fnamep = s;
27256 *fnamelen = (int)STRLEN(s);
27257 vim_free(*bufp);
27258 *bufp = s;
27259 didit = TRUE;
27260 }
27261 }
27262 vim_free(sub);
27263 vim_free(str);
27264 }
27265 vim_free(pat);
27266 }
27267 }
27268 /* after using ":s", repeat all the modifiers */
27269 if (didit)
27270 goto repeat;
27271 }
27272 }
27273
Bram Moolenaar26df0922014-02-23 23:39:13 +010027274 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
27275 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010027276 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010027277 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027278 if (c != NUL)
27279 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027280 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027281 if (c != NUL)
27282 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027283 if (p == NULL)
27284 return -1;
27285 vim_free(*bufp);
27286 *bufp = *fnamep = p;
27287 *fnamelen = (int)STRLEN(p);
27288 *usedlen += 2;
27289 }
27290
Bram Moolenaar071d4272004-06-13 20:20:40 +000027291 return valid;
27292}
27293
27294/*
27295 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
27296 * "flags" can be "g" to do a global substitute.
27297 * Returns an allocated string, NULL for error.
27298 */
27299 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010027300do_string_sub(
27301 char_u *str,
27302 char_u *pat,
27303 char_u *sub,
27304 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027305{
27306 int sublen;
27307 regmatch_T regmatch;
27308 int i;
27309 int do_all;
27310 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027311 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027312 garray_T ga;
27313 char_u *ret;
27314 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027315 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027316
27317 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
27318 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027319 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027320
27321 ga_init2(&ga, 1, 200);
27322
27323 do_all = (flags[0] == 'g');
27324
27325 regmatch.rm_ic = p_ic;
27326 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
27327 if (regmatch.regprog != NULL)
27328 {
27329 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027330 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027331 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
27332 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010027333 /* Skip empty match except for first match. */
27334 if (regmatch.startp[0] == regmatch.endp[0])
27335 {
27336 if (zero_width == regmatch.startp[0])
27337 {
27338 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020027339 i = MB_PTR2LEN(tail);
27340 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
27341 (size_t)i);
27342 ga.ga_len += i;
27343 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027344 continue;
27345 }
27346 zero_width = regmatch.startp[0];
27347 }
27348
Bram Moolenaar071d4272004-06-13 20:20:40 +000027349 /*
27350 * Get some space for a temporary buffer to do the substitution
27351 * into. It will contain:
27352 * - The text up to where the match is.
27353 * - The substituted text.
27354 * - The text after the match.
27355 */
27356 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010027357 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000027358 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
27359 {
27360 ga_clear(&ga);
27361 break;
27362 }
27363
27364 /* copy the text up to where the match is */
27365 i = (int)(regmatch.startp[0] - tail);
27366 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
27367 /* add the substituted text */
27368 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
27369 + ga.ga_len + i, TRUE, TRUE, FALSE);
27370 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020027371 tail = regmatch.endp[0];
27372 if (*tail == NUL)
27373 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027374 if (!do_all)
27375 break;
27376 }
27377
27378 if (ga.ga_data != NULL)
27379 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
27380
Bram Moolenaar473de612013-06-08 18:19:48 +020027381 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027382 }
27383
27384 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
27385 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027386 if (p_cpo == empty_option)
27387 p_cpo = save_cpo;
27388 else
27389 /* Darn, evaluating {sub} expression changed the value. */
27390 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027391
27392 return ret;
27393}
27394
27395#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */